This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
c0cp.c
460 lines (424 loc) · 11.3 KB
/
c0cp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/* -*- C -*- */
/*
* Copyright (c) 2017-2020 Seagate Technology LLC and/or its Affiliates
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For any questions about this software or licensing,
* please email opensource@seagate.com or cortx-questions@seagate.com.
*
* Original author: Ganesan Umanesan <ganesan.umanesan@seagate.com>
* Original creation date: 24-Jan-2017
*/
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include <inttypes.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include "c0appz.h"
#include "c0appz_internal.h"
#include "dir.h"
struct Block {
uint64_t idh; /* object id high */
uint64_t idl; /* object is low */
uint64_t bsz; /* block size */
uint64_t cnt; /* count */
uint64_t ocnt; /* OP count */
uint64_t m0bs; /* m0 block size */
uint64_t pos; /* starting position */
char *fbuf; /* file buffer */
};
/* threads */
pthread_t wthrd[512]; /* max 512 threads */
struct Block wthrd_blk[512]; /* max 512 threads */
static void *tfunc_c0appz_mw(void *block)
{
struct Block *b = (struct Block *)block;
// printf("pos = %" PRIu64 "\n", b->pos);
c0appz_mw(b->fbuf, b->idh, b->idl, b->pos, b->bsz, b->cnt, b->m0bs);
return NULL;
}
static void *tfunc_c0appz_mw_async(void *block)
{
struct Block *b = (struct Block *)block;
c0appz_mw_async(b->fbuf, b->idh, b->idl, b->pos, b->bsz, b->cnt, b->ocnt,b->m0bs);
return NULL;
}
void pack(int idx, char *fbuf, uint64_t idh, uint64_t idl, uint64_t pos,
uint64_t bsz, uint64_t cnt, uint32_t ocnt, uint64_t m0bs)
{
wthrd_blk[idx].idh = idh;
wthrd_blk[idx].idl = idl;
wthrd_blk[idx].pos = pos;
wthrd_blk[idx].bsz = bsz;
wthrd_blk[idx].cnt = cnt;
wthrd_blk[idx].ocnt = ocnt;
wthrd_blk[idx].m0bs = m0bs;
wthrd_blk[idx].fbuf = (char *)fbuf;
/*
printf("\n#####\n");
printf("idh = %" PRIu64 "\n", wthrd_blk[idx].idh);
printf("idl = %" PRIu64 "\n", wthrd_blk[idx].idl);
printf("pos = %" PRIu64 "\n", wthrd_blk[idx].pos);
printf("bsz = %" PRIu64 "\n", wthrd_blk[idx].bsz);
printf("cnt = %" PRIu64 "\n", wthrd_blk[idx].cnt);
*/
return;
}
/*
******************************************************************************
* EXTERN VARIABLES
******************************************************************************
*/
extern int perf; /* performance */
int force=0; /* overwrite */
extern uint64_t qos_whgt_served;
extern uint64_t qos_whgt_remain;
extern uint64_t qos_laps_served;
extern uint64_t qos_laps_remain;
extern pthread_mutex_t qos_lock; /* lock qos_total_weight */
char *prog;
const char *help_c0cp_txt = "\
Usage:\n\
c0cp [-fptv] [-x id] [-u sz] [-b [sz]] [-c n] [-a n] idh idl file bsz\n\
c0cp 1234 56789 file 64\n\
\n\
Copy file to the object store.\n\
\n\
idh - object id high number\n\
idl - object id low number\n\
bsz - block size (in KiBs)\n\
\n\
-a n async mode: do n object store operations in a batch\n\
-b [sz] block size for the object store i/o (m0bs);\n\
if sz is not specified, automatically figure out the optimal\n\
value based on the object size and the pool width (does not\n\
work for the composite objects atm); (by default, use bsz)\n\
-c n write n contiguous copies of the file into the object\n\
-m use multiple threads\n\
-f force: overwrite an existing object\n\
-p show performance stats\n\
-u sz unit size (in KiBs), must be power of 2, >= 4 and <= 4096;\n\
by default, determined automatically for the new objects\n\
based on the m0bs and parity configuration of the pool\n\
-x id id of the tier (pool) to create the object in (1,2,3,..)\n\
-t create m0trace.pid file\n\
-v be more verbose\n\
-i | n socket index, [0-3] currently\n\
-h print this help\n\
\n\
Note: in order to get the maximum performance, m0bs should be multiple\n\
of the data size in the parity group, i.e. multiple of (unit_size * n),\n\
where n is 2 in 2+1 parity group configuration, 8 in 8+2 and so on.\n";
int help()
{
fprintf(stderr, "%s\n%s\n", help_c0cp_txt, c0appz_help_txt);
exit(1);
}
int main(int argc, char **argv)
{
uint64_t idh; /* object id high */
uint64_t idl; /* object is low */
uint64_t bsz; /* block size */
uint64_t m0bs=0; /* m0 block size */
uint64_t cnt; /* count */
uint64_t pos=0; /* starting position */
char *fname; /* input filename */
struct stat64 fs; /* file statistics */
int opt; /* options */
int rc; /* return code */
char *fbuf; /* file buffer */
int cont=0; /* continuous mode */
int pool=0; /* default pool ID */
int op_cnt=0; /* number of parallel OPs */
int mthrd=0; /* multi-threaded */
int idx=0; /* socket index */
int dir=0; /* directory mode */
int i;
prog = basename(strdup(argv[0]));
/* getopt */
while ((opt = getopt(argc, argv, ":i:a:b:pfmc:x:u:tvh")) != -1) {
switch (opt) {
case 'p':
perf = 1;
break;
case 'f':
force = 1;
break;
case 'a':
op_cnt = atoi(optarg);
if (op_cnt < 1) {
ERR("invalid -a option argument: %s\n", optarg);
help();
}
break;
case 'b':
if (sscanf(optarg, "%li", &m0bs) != 1) {
/* optarg might contain some other options */
optind--; /* rewind */
m0bs = 1; /* get automatically */
} else
m0bs *= 1024;
break;
case 'c':
cont = atoi(optarg);
if (cont < 1) {
ERR("invalid -c option argument: %s\n", optarg);
help();
}
break;
case 'm':
mthrd = 1;
break;
case 'u':
if (sscanf(optarg, "%i", &unit_size) != 1) {
ERR("invalid unit size: %s\n", optarg);
help();
}
break;
case 'i':
idx = atoi(optarg);
if (idx < 0 || idx > 3) {
ERR("invalid socket index: %s (allowed \n"
"values are 0, 1, 2, or 3 atm)\n", optarg);
help();
}
break;
case 'x':
pool = atoi(optarg);
if (pool < 1 || pool > 6) {
ERR("invalid pool index: %s (allowed \n"
"values are 1, 2, 3, 4 or 5 atm)\n", optarg);
help();
}
break;
case 't':
m0trace_on = true;
break;
case 'v':
trace_level++;
break;
case 'h':
help();
break;
case ':':
switch (optopt) {
case 'b':
m0bs = 1; /* get automatically */
break;
default:
ERR("option -%c needs a value\n", optopt);
help();
}
break;
case '?':
fprintf(stderr, "unknown option: %c\n", optopt);
help();
break;
default:
fprintf(stderr, "unknown option: %c\n", optopt);
help();
break;
}
}
/* check input */
if (argc - optind != 4)
help();
c0appz_setrc(prog);
c0appz_putrc();
/* set input */
if (sscanf(argv[optind+0], "%li", &idh) != 1) {
ERR("invalid idhi - %s", argv[optind+0]);
help();
}
if (sscanf(argv[optind+1], "%li", &idl) != 1) {
ERR("invalid idlo - %s", argv[optind+1]);
help();
}
fname = argv[optind+2];
if (sscanf(argv[optind+3], "%li", &bsz) != 1) {
ERR("invalid block size - %s", argv[optind+3]);
help();
}
bsz *= 1024;
if (bsz == 0) {
fprintf(stderr,"%s: bsz must be > 0\n", prog);
help();
}
rc = stat64(fname, &fs);
if (rc != 0) {
ERRS("%s", fname);
exit(1);
}
cnt = (fs.st_size + bsz - 1) / bsz;
if(S_ISDIR(fs.st_mode)) dir = 1;
/* init */
c0appz_timein();
rc = c0appz_init(idx);
if (rc != 0) {
fprintf(stderr,"%s(): error: c0appz_init() failed: %d\n",
__func__, rc);
return 222;
}
ppf("%8s","init");
c0appz_timeout(0);
if (m0bs == 1) {
m0bs = c0appz_m0bs(idh, idl, bsz * cnt, pool);
if (!m0bs)
LOG("WARNING: failed to figure out the optimal m0bs,"
" will use bsz for m0bs\n");
}
if (!m0bs)
m0bs = bsz;
/* directory mode */
if(dir) {
printf("path: %s\n",fname);
qos_pthread_start();
if(!mthrd) {
c0appz_cp_dir_sthread(idh, idl, fname, bsz, pool, m0bs);
}
else {
c0appz_cp_dir_mthread(idh, idl, fname, bsz, pool, m0bs,cont);
c0appz_cp_dir_mthread_wait();
}
qos_pthread_wait();
printf("Done!\n");
return 0;
}
/* create object */
c0appz_timein();
rc = c0appz_cr(idh, idl, pool, m0bs);
if (rc < 0 || (rc != 0 && !force)) {
if (rc < 0)
fprintf(stderr,"%s(): object create failed: rc=%d\n",
__func__, rc);
rc = 333;
goto end;
}
ppf("%8s","create");
c0appz_timeout(0);
/* continuous write */
if (cont) {
fbuf = malloc(fs.st_size + bsz - 1);
if (!fbuf) {
fprintf(stderr,"%s(): error: not enough memory.\n",
__func__);
rc = 111;
goto end;
}
rc = c0appz_fr(fbuf, fname, bsz, cnt);
if (rc != 0) {
fprintf(stderr,"%s(): c0appz_fr failed: rc=%d\n",
__func__, rc);
rc = 555;
goto end;
}
qos_whgt_served = 0;
qos_whgt_remain = bsz * cnt * cont;
qos_laps_served = 0;
qos_laps_remain = cont;
qos_pthread_start();
c0appz_timein();
if (!mthrd) {
for (pos = 0; cont > 0; cont--, pos += cnt * bsz) {
rc = op_cnt ?
c0appz_mw_async(fbuf, idh, idl, pos, bsz, cnt,
op_cnt, m0bs) :
c0appz_mw(fbuf, idh, idl, pos, bsz, cnt, m0bs);
if (rc != 0) {
ERR("copying failed at pos %lu: %d\n", pos, rc);
break;
}
pthread_mutex_lock(&qos_lock);
qos_laps_served++;
qos_laps_remain--;
pthread_mutex_unlock(&qos_lock);
}
}
else {
pos = 0;
for(i=0; i<cont; i++) {
pack(i,fbuf, idh, idl, pos, bsz, cnt, op_cnt, m0bs);
op_cnt ? pthread_create(&(wthrd[i]),NULL,&tfunc_c0appz_mw_async,(void *)(wthrd_blk+i))
: pthread_create(&(wthrd[i]),NULL,&tfunc_c0appz_mw,(void *)(wthrd_blk+i));
pos += cnt * bsz;
}
}
for(i=0; i<cont; i++) {
pthread_join(wthrd[i],NULL);
}
ppf("%8s","write");
c0appz_timeout(pos);
qos_pthread_wait();
printf("%" PRIu64 " x %" PRIu64 " = %" PRIu64 "\n", cnt, bsz,
cnt * bsz);
free(fbuf);
goto end;
}
qos_whgt_served = 0;
qos_whgt_remain = bsz * cnt;
qos_laps_served = 0;
qos_laps_remain = 1;
qos_pthread_start();
c0appz_timein();
/* copy */
if (op_cnt)
rc = c0appz_cp_async(idh, idl, fname, bsz, cnt, op_cnt, m0bs);
else
rc = c0appz_cp(idh, idl, fname, bsz, cnt, m0bs);
if (rc != 0) {
ERR("copying failed: rc=%d\n", rc);
rc = 222;
goto end;
};
pthread_mutex_lock(&qos_lock);
qos_laps_served++;
qos_laps_remain--;
pthread_mutex_unlock(&qos_lock);
ppf("%8s","copy");
c0appz_timeout(bsz*cnt);
qos_pthread_wait();
end:
/* free */
c0appz_timein();
c0appz_free();
ppf("%8s","free");
c0appz_timeout(0);
/* failure */
if (rc != 0) {
printf("%s %" PRIu64 "\n",fname,fs.st_size);
fprintf(stderr,"%s failed!\n", prog);
return rc;
}
/* success */
c0appz_dump_perf();
printf("%s %" PRIu64 "\n",fname,fs.st_size);
fprintf(stderr,"%s success\n", prog);
return 0;
}
/*
* Local variables:
* c-indentation-style: "K&R"
* c-basic-offset: 8
* tab-width: 8
* fill-column: 80
* scroll-step: 1
* End:
*/