-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.c
783 lines (702 loc) · 20.1 KB
/
main.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/* Copyright (c) 2017-2018 Howard Chu @ Symas Corp. */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "dbb.h"
// Comma-separated list of operations to run in the specified order
// Actual benchmarks:
//
// fillseq -- write N values in sequential key order in async mode
// fillrandom -- write N values in random key order in async mode
// overwrite -- overwrite N values in random key order in async mode
// fillseqsync -- write N/100 values in sequential key order in sync mode
// fillseqbatch -- batch write N values in sequential key order in async mode
// fillrandsync -- write N/100 values in random key order in sync mode
// fillrandbatch -- batch write N values in random key order in async mode
// readseq -- read N times sequentially
// readreverse -- read N times in reverse order
// readrandom -- read N times in random order
static char* FLAGS_benchmarks =
"fillrandsync,"
"fillrandom,"
"fillrandbatch,"
"fillseqsync,"
"fillseq,"
"fillseqbatch,"
"overwrite,"
#if 0
"overwritebatch,"
#endif
"readrandom,"
"readseq,"
"readreverse,"
;
// Number of key/values to place in database
int64_t FLAGS_num = 1000000;
// Number of read operations to do. If negative, do FLAGS_num reads.
int64_t FLAGS_reads = -1;
// Number of concurrent threads to run.
int FLAGS_threads = 1;
// Maximum number of threads.
int FLAGS_max_threads = 0;
// Time in seconds for the random-ops tests to run.
int FLAGS_duration = 0;
// Per-thread rate limit on writes per second.
// Only for the readwhilewriting test.
int FLAGS_writes_per_second;
// Stats are reported every N seconds when this is
// greater than zero.
int FLAGS_stats_period = 0;
// Size of each value
int FLAGS_value_size = 100;
// Size of each key
int FLAGS_key_size = 16;
// Number of key/values to place in database
int FLAGS_batch = 1000;
// Arrange to generate values that shrink to this fraction of
// their original size after compression
float FLAGS_compression_ratio = 0.5;
// Compression disabled by default
int FLAGS_compression = 0;
// Print histogram of operation timings
int FLAGS_histogram = 0;
// If true, do not destroy the existing database. If you set this
// flag and also specify a benchmark that wants a fresh database, that
// benchmark will fail.
int FLAGS_use_existing_db = 0;
// If true, running on a raw device
int FLAGS_rawdev = 0;
// Use the db with the following name.
const char* FLAGS_db = NULL;
static rndctx **seeds;
static Hstctx **hists;
extern DBB_backend *dbb_backend;
static char *CompressibleString(rndctx *ctx, float ratio, int len, char *buf)
{
char *ptr, *end;
int i, raw = (int)len * ratio;
if (raw < 1) raw = 1;
ptr = buf;
end = buf + len;
for (i=0; i<raw; i++)
*ptr++ = (DBB_random(ctx) % 96) + ' ';
i = 0;
while (ptr < end) {
*ptr++ = buf[i++];
if (i >= raw)
i = 0;
}
return end;
}
#define RAND_STRSIZE 10485600
static void GenerateString(DBB_local *dl)
{
int i;
char *ptr, *end;
dl->dl_randstr = malloc(RAND_STRSIZE);
dl->dl_randstrpos = 0;
ptr = dl->dl_randstr;
end = ptr + RAND_STRSIZE;
while (ptr < end)
ptr = CompressibleString(dl->dl_rndctx, FLAGS_compression_ratio, 100, ptr);
}
/* Helper for quickly generating random data */
void DBB_randstring(DBB_local *dl, DBB_val *val)
{
if (dl->dl_randstrpos + val->dv_size > RAND_STRSIZE) {
dl->dl_randstrpos = 0;
assert(val->dv_size < RAND_STRSIZE);
}
val->dv_data = dl->dl_randstr + dl->dl_randstrpos;
dl->dl_randstrpos += val->dv_size;
}
static int timecmp(struct timeval *a, struct timeval *b)
{
int ret;
ret = a->tv_sec - b->tv_sec;
if (ret)
return ret;
return a->tv_usec - b->tv_usec;
}
static void timesub(struct timeval *end, struct timeval *beg)
{
end->tv_sec -= beg->tv_sec;
end->tv_usec -= beg->tv_usec;
if (end->tv_usec < 0) {
end->tv_usec += 1000000;
end->tv_sec--;
}
}
static void Merge(DBB_local *dst, DBB_local *src) {
DBB_hstmerge(dst->dl_hstctx, src->dl_hstctx);
dst->dl_done += src->dl_done;
dst->dl_bytes += src->dl_bytes;
dst->dl_seconds += src->dl_seconds;
if (timecmp(&dst->dl_start, &src->dl_start) > 0)
dst->dl_start = src->dl_start;
if (timecmp(&dst->dl_finish, &src->dl_finish) < 0)
dst->dl_finish = src->dl_finish;
/* Just keep the messages from one thread */
if (!dst->dl_message.dv_data && src->dl_message.dv_data)
dst->dl_message = src->dl_message;
}
void DBB_message(DBB_local *dl, char *msg) {
int len = strlen(msg) + 1; /* add a space */
char *ptr;
if (dl->dl_message.dv_size) {
ptr = realloc(dl->dl_message.dv_data, dl->dl_message.dv_size + len);
if (!ptr)
return;
dl->dl_message.dv_data = ptr;
ptr += dl->dl_message.dv_size - 1;
} else {
ptr = malloc(len + 1);
if (!ptr)
return;
dl->dl_message.dv_data = ptr;
dl->dl_message.dv_size = 1;
}
*ptr++ = ' ';
strcpy(ptr, msg);
dl->dl_message.dv_size += len;
}
static void TimeStr(time_t seconds, char *buf) {
struct tm tm;
localtime_r(&seconds, &tm);
sprintf(buf, "%04d/%02d/%02d-%02d:%02d:%02d",
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
}
void DBB_opdone(DBB_local *dl) {
if (FLAGS_histogram) {
struct timeval now;
struct timeval last;
gettimeofday(&now, NULL);
last = dl->dl_last_op_finish;
dl->dl_last_op_finish = now;
timesub(&now, &last);
DBB_hstadd(dl->dl_hstctx, &now);
if (now.tv_sec > 0 || now.tv_usec > 20000) {
fprintf(stderr, "long op: %d.%06d micros%30s\r", (int)now.tv_sec, (int)now.tv_usec, "");
fflush(stderr);
}
}
dl->dl_done++;
}
static void Report(DBB_local *dl, const char *name) {
char rate[100];
// Pretend at least one op was done in case we are running a benchmark
// that does not call FinishedSingleOp().
if (dl->dl_done < 1) dl->dl_done = 1;
timesub(&dl->dl_finish, &dl->dl_start);
double elapsed = dl->dl_finish.tv_sec + dl->dl_finish.tv_usec * 1e-6;
if (dl->dl_bytes > 0) {
// Rate is computed on actual elapsed time, not the sum of per-thread
// elapsed times.
snprintf(rate, sizeof(rate), "%6.1f MB/s",
(dl->dl_bytes / 1048576.0) / elapsed);
} else {
rate[0] = '\0';
}
double throughput = (double)dl->dl_done/elapsed;
fprintf(stdout, "%-13s : %11.3f micros/op %ld ops/sec;%s%s\n",
name,
elapsed * 1e6 / dl->dl_done,
(long)throughput, rate, dl->dl_message.dv_data ? (char *)dl->dl_message.dv_data : "");
if (FLAGS_histogram) {
DBB_hstprint(dl->dl_hstctx);
}
fflush(stdout);
}
int DBB_done(DBB_local *dl) {
/* If rate limiting, check every 100ms */
if (dl->dl_wps_by_10) {
struct timeval now;
++dl->dl_writes;
if (dl->dl_writes >= dl->dl_wps_by_10) {
struct timeval tv;
int delta;
gettimeofday(&now, NULL);
tv = now;
timesub(&tv, &dl->dl_lastwrite);
tv.tv_usec += tv.tv_sec * 1000000;
dl->dl_writes = 0;
dl->dl_lastwrite = now;
if (tv.tv_usec < 100000) {
usleep(100000 - tv.tv_usec);
gettimeofday(&dl->dl_lastwrite, NULL);
}
}
}
if (dl->dl_max_seconds) {
if (!(dl->dl_done % 1000)) {
// Recheck every appx 1000 ops (exact iff increment is factor of 1000)
time_t now = time(NULL);
return (now - dl->dl_start.tv_sec >= dl->dl_max_seconds);
} else {
return 0;
}
} else {
return dl->dl_done >= dl->dl_max_ops;
}
}
static void PrintWarnings() {
#if defined(__GNUC__) && !defined(__OPTIMIZE__)
fprintf(stdout,
"WARNING: Optimization is disabled: benchmarks unnecessarily slow\n"
);
#endif
#ifndef NDEBUG
fprintf(stdout,
"WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
#endif
}
static void PrintEnvironment() {
fprintf(stdout, "%s: version %s\n", dbb_backend->db_longname, dbb_backend->db_verstr());
#if defined(__linux)
time_t now = time(NULL);
fprintf(stdout, "Date: %s", ctime(&now)); // ctime() adds newline
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
if (cpuinfo != NULL) {
char line[1000];
int num_cpus = 0;
char cpu_type[1000];
char cache_size[1000];
cpu_type[0] = '\0';
cache_size[0] = '\0';
while (fgets(line, sizeof(line), cpuinfo) != NULL) {
char *val, *ptr;
char *sep = strchr(line, ':');
if (sep == NULL) {
continue;
}
*sep++ = '\0';
ptr = strchr(line, '\t');
if (ptr)
*ptr = '\0';
val = sep+1;
if (!strcmp(line,"model name")) {
strcpy(cpu_type, val);
} else if (!strcmp(line, "processor")) {
++num_cpus;
} else if (!strcmp(line, "cache size")) {
strcpy(cache_size, val);
}
}
fclose(cpuinfo);
if (!cpu_type[0]) {
char family[250], manuf[250], version[250], freq[200], *ptr;
int cachei = 0;
cpuinfo = popen("dmidecode -t processor", "r");
if (cpuinfo) {
while(fgets(line, sizeof(line), cpuinfo)) {
char *sep = strchr(line, ':');
if (!sep) continue;
*sep = '\0';
sep += 2;
ptr = strrchr(sep, '\n');
if (ptr) *ptr = '\0';
ptr = line+1;
if (!strcmp(ptr, "Manufacturer")) {
strcpy(manuf, sep);
} else if (!strcmp(ptr, "Family")) {
strcpy(family, sep);
} else if (!strcmp(ptr, "Version")) {
strcpy(version, sep);
} else if (!strcmp(ptr, "Max Speed")) {
strcpy(freq, sep);
}
}
pclose(cpuinfo);
snprintf(cpu_type, sizeof(cpu_type), "%s %s %s %s\n", manuf, family, version, freq);
cpuinfo = popen("dmidecode -t cache", "r");
while(fgets(line, sizeof(line), cpuinfo)) {
char *sep = strchr(line, ':');
if (!sep) continue;
*sep = '\0';
sep += 2;
ptr = line+1;
if (!strcmp(ptr, "Installed Size")) {
int i = atoi(sep);
if (i > cachei)
strcpy(cache_size, sep);
}
}
pclose(cpuinfo);
}
}
fprintf(stdout, "CPU: %d * %s", num_cpus, cpu_type);
fprintf(stdout, "CPUCache: %s", cache_size);
}
#endif
}
static void PrintHeader() {
PrintEnvironment();
fprintf(stdout, "Keys: %d bytes each\n", FLAGS_key_size);
fprintf(stdout, "Values: %d bytes each (%d bytes after compression)\n",
FLAGS_value_size,
(int)(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
fprintf(stdout, "Entries: %ld\n", FLAGS_num);
fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
(((int64_t)(FLAGS_key_size + FLAGS_value_size) * FLAGS_num)
/ 1048576.0));
fprintf(stdout, "FileSize: %.1f MB (estimated)\n",
(((FLAGS_key_size + FLAGS_value_size * FLAGS_compression_ratio) * FLAGS_num)
/ 1048576.0));
PrintWarnings();
fprintf(stdout, "------------------------------------------------\n");
fflush(stdout);
}
static void *RunThread(void *v) {
DBB_local *dl = v;
pthread_mutex_lock(&dl->dl_global->dg_mu);
dl->dl_global->dg_initialized++;
if (dl->dl_global->dg_initialized >= dl->dl_global->dg_threads)
pthread_cond_broadcast(&dl->dl_global->dg_cv);
while(!dl->dl_global->dg_start)
pthread_cond_wait(&dl->dl_global->dg_cv, &dl->dl_global->dg_mu);
pthread_mutex_unlock(&dl->dl_global->dg_mu);
dl->dl_done = 0;
dl->dl_last_report_done = 0;
dl->dl_bytes = 0;
dl->dl_seconds = 0;
dl->dl_max_ops = dl->dl_op == DO_READ ? dl->dl_global->dg_reads : dl->dl_global->dg_num;
dl->dl_max_seconds = dl->dl_order == DO_RANDOM ? FLAGS_duration : 0;
dl->dl_message.dv_size = 0;
dl->dl_message.dv_data = NULL;
gettimeofday(&dl->dl_start, NULL);
dl->dl_finish = dl->dl_start;
dl->dl_last_op_finish = dl->dl_start;
dl->dl_last_report_finish = dl->dl_start;
if (dl->dl_wps_by_10)
dl->dl_lastwrite = dl->dl_start;
if (dl->dl_op == DO_READ)
dbb_backend->db_read(dl);
else
dbb_backend->db_write(dl);
gettimeofday(&dl->dl_finish, NULL);
dl->dl_seconds = dl->dl_finish.tv_sec - dl->dl_start.tv_sec;
pthread_mutex_lock(&dl->dl_global->dg_mu);
dl->dl_global->dg_done++;
if (dl->dl_global->dg_done >= dl->dl_global->dg_threads)
pthread_cond_broadcast(&dl->dl_global->dg_cv);
pthread_mutex_unlock(&dl->dl_global->dg_mu);
return NULL;
}
static void *StatsThread(void *v) {
DBB_local *args = v;
DBB_global *dg = args->dl_global;
int i;
int banner = 0;
pthread_mutex_lock(&dg->dg_mu);
while(!dg->dg_start)
pthread_cond_wait(&dg->dg_cv, &dg->dg_mu);
pthread_mutex_unlock(&dg->dg_mu);
while(1) {
struct timeval now, end1, end2;
sleep(FLAGS_stats_period);
if (dg->dg_done >= dg->dg_threads)
break;
if (!banner) {
banner = 1;
fprintf(stderr,
" Timestamp Thread Cur Ops Tot Ops Cur Rate Avg Rate Cur Sec Tot Sec\n");
}
gettimeofday(&now, NULL);
char buf[20];
TimeStr(now.tv_sec, buf);
for (i=0; i<dg->dg_threads; i++) {
DBB_local *dl = &args[i];
int64_t done = dl->dl_done;
end1 = now; end2 = now;
timesub(&end1, &dl->dl_last_report_finish);
timesub(&end2, &dl->dl_start);
fprintf(stderr,
"%s\t%d\t%zd\t%zd\t%.1f\t%.1f\t%d.%06d\t%d.%06d\n",
buf, dl->dl_id,
done - dl->dl_last_report_done, done,
(float)(done - dl->dl_last_report_done) / end1.tv_sec,
(float)done / end2.tv_sec,
(int)end1.tv_sec, (int)end1.tv_usec, (int)end2.tv_sec, (int)end2.tv_usec);
dl->dl_last_report_done = dl->dl_done;
dl->dl_last_report_finish = now;
}
fflush(stderr);
}
}
static void RunBenchmark(int n, const char *name, DBB_global *dg) {
DBB_local *args;
int i;
pthread_t stats_thread;
dg->dg_threads = n;
dg->dg_initialized = 0;
dg->dg_done = 0;
dg->dg_start = 0;
args = malloc(n * sizeof(DBB_local));
for (i = 0; i < n; i++) {
args[i].dl_global = dg;
args[i].dl_rndctx = seeds[i];
args[i].dl_hstctx = hists[i];
args[i].dl_order = dg->dg_order;
args[i].dl_id = i;
args[i].dl_randstrpos = 0;
if (dg->dg_op == DO_READWRITE) {
if (i)
args[i].dl_op = DO_READ;
else {
args[i].dl_op = DO_WRITE;
args[i].dl_writes_per_sec = FLAGS_writes_per_second;
args[i].dl_wps_by_10 = FLAGS_writes_per_second / 10;
args[i].dl_writes = 0;
args[i].dl_lastwrite.tv_sec = 0;
args[i].dl_lastwrite.tv_usec = 0;
}
} else {
args[i].dl_op = dg->dg_op;
args[i].dl_writes_per_sec = 0;
}
if (args[i].dl_op == DO_WRITE)
GenerateString(&args[i]);
pthread_create(&args[i].dl_tid, NULL, RunThread, &args[i]);
}
if (FLAGS_stats_period)
pthread_create(&stats_thread, NULL, StatsThread, args);
pthread_mutex_lock(&dg->dg_mu);
while (dg->dg_initialized < n) {
pthread_cond_wait(&dg->dg_cv, &dg->dg_mu);
}
dg->dg_start = 1;
pthread_cond_broadcast(&dg->dg_cv);
while (dg->dg_done < n) {
pthread_cond_wait(&dg->dg_cv, &dg->dg_mu);
}
pthread_mutex_unlock(&dg->dg_mu);
if (dg->dg_op == DO_READWRITE) {
Report(&args[0], "writer");
for (i = 2; i < n; i++) {
Merge(&args[1], &args[i]);
}
Report(&args[1], name);
} else {
for (i = 1; i < n; i++) {
Merge(&args[0], &args[i]);
}
Report(&args[0], name);
}
if (FLAGS_stats_period)
pthread_join(stats_thread, NULL);
for (i = 0; i < n; i++) {
DBB_srandom(seeds[i], DBB_random(seeds[i]));
pthread_join(args[i].dl_tid, NULL);
}
free(args);
}
static void killdb() {
#define VM_ALIGN 0x200000
if (FLAGS_rawdev) {
int fd;
char *ptr;
fd = open(FLAGS_db, O_RDWR);
ptr = mmap(NULL, VM_ALIGN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
memset(ptr, 0, 16384);
msync(ptr, 16384, MS_SYNC);
munmap(ptr, VM_ALIGN);
close(fd);
} else {
char cmd[200];
sprintf(cmd, "rm -rf %s", FLAGS_db);
if (system(cmd)) exit(1);
}
}
static int db_open;
static void Benchmark() {
PrintHeader();
char *benchmarks = (char *)FLAGS_benchmarks;
DBB_global dg;
pthread_mutex_init(&dg.dg_mu, NULL);
pthread_cond_init(&dg.dg_cv, NULL);
if (!FLAGS_use_existing_db) {
dbb_backend->db_close();
killdb();
db_open = 0;
}
while (benchmarks != NULL) {
char *sep = strchr(benchmarks, ',');
char *name, namebuf[64];
int fresh_db = 0;
int num_threads = FLAGS_threads;
if (sep == NULL) {
name = benchmarks;
benchmarks = NULL;
} else {
int len = sep-benchmarks;
strncpy(namebuf, benchmarks, len);
benchmarks = sep+1;
name = namebuf;
namebuf[len] = '\0';
}
dg.dg_num = FLAGS_num;
dg.dg_reads = (FLAGS_reads < 0) ? FLAGS_num : FLAGS_reads;
dg.dg_batchsize = 1;
dg.dg_dbflags = 0;
if (!strcmp(name, "fillseq")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_FORWARD;
fresh_db = 1;
} else if (!strcmp(name, "fillseqbatch")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_FORWARD;
fresh_db = 1;
dg.dg_batchsize = FLAGS_batch;
} else if (!strcmp(name, "fillrandom")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_RANDOM;
fresh_db = 1;
} else if (!strcmp(name, "fillrandbatch")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_RANDOM;
fresh_db = 1;
dg.dg_batchsize = FLAGS_batch;
} else if (!strcmp(name, "overwrite")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_RANDOM;
} else if (!strcmp(name, "overwritebatch")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_RANDOM;
dg.dg_batchsize = FLAGS_batch;
} else if (!strcmp(name, "fillrandsync")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_RANDOM;
fresh_db = 1;
dg.dg_num /= 1000;
if (dg.dg_num<10) dg.dg_num=10;
dg.dg_dbflags = DBB_SYNC;
} else if (!strcmp(name, "fillseqsync")) {
dg.dg_op = DO_WRITE;
dg.dg_order = DO_FORWARD;
fresh_db = 1;
dg.dg_num /= 1000;
if (dg.dg_num<10) dg.dg_num=10;
dg.dg_dbflags = DBB_SYNC;
} else if (!strcmp(name, "readseq")) {
dg.dg_op = DO_READ;
dg.dg_order = DO_FORWARD;
} else if (!strcmp(name, "readreverse")) {
dg.dg_op = DO_READ;
dg.dg_order = DO_REVERSE;
} else if (!strcmp(name, "readrandom")) {
dg.dg_op = DO_READ;
dg.dg_order = DO_RANDOM;
} else if (!strcmp(name, "readwhilewriting")) {
dg.dg_op = DO_READWRITE;
dg.dg_order = DO_RANDOM;
num_threads++;
} else {
if (*name) { // No error message for empty name
fprintf(stderr, "unknown benchmark '%s'\n", name);
}
continue;
}
if (fresh_db) {
if (FLAGS_use_existing_db) {
fprintf(stdout, "%-12s : skipped (--use_existing_db is true)\n", name);
continue;
}
if (db_open) {
dbb_backend->db_close();
killdb();
db_open = 0;
}
}
if (!db_open) {
dbb_backend->db_open(dg.dg_dbflags);
db_open = 1;
}
RunBenchmark(num_threads, name, &dg);
if (dg.dg_op == DO_WRITE || dg.dg_op == DO_READWRITE) {
char cmd[200];
sprintf(cmd, "du %s", FLAGS_db);
if (system(cmd)) exit(1);
}
}
pthread_cond_destroy(&dg.dg_cv);
pthread_mutex_destroy(&dg.dg_mu);
}
static int benchflag(char *arg) {
FLAGS_benchmarks = arg;
return 1;
}
static arg_desc main_args[] = {
{ "benchmarks", arg_magic, benchflag },
{ "compression", arg_onoff, &FLAGS_compression },
{ "compression_ratio", arg_float, &FLAGS_compression_ratio },
{ "histogram", arg_onoff, &FLAGS_histogram },
{ "use_existing_db", arg_onoff, &FLAGS_use_existing_db },
{ "num", arg_long, &FLAGS_num },
{ "batch", arg_int, &FLAGS_batch },
{ "reads", arg_long, &FLAGS_reads },
{ "threads", arg_int, &FLAGS_threads },
{ "max_threads", arg_int, &FLAGS_max_threads },
{ "duration", arg_int, &FLAGS_duration },
{ "stats_period", arg_int, &FLAGS_stats_period },
{ "writes_per_second", arg_int, &FLAGS_writes_per_second },
{ "value_size", arg_int, &FLAGS_value_size },
{ "key_size", arg_int, &FLAGS_key_size },
{ "db", arg_string, &FLAGS_db },
{ NULL }
};
static char dirbuf[1024];
int main2(int argc, char *argv[]) {
int i, aret;
arg_setup(main_args, dbb_backend->db_args);
while ((aret = arg_process(argc, argv)) >= 0) {
/* Choose a location for the test database if none given with --db=<path> */
if (FLAGS_db == NULL) {
char *dir = getenv("TEST_TMPDIR");
if (!dir) {
dir = dirbuf;
sprintf(dirbuf, "/tmp/dbbench-%d", geteuid());
}
mkdir(dir, 0775);
strcat(dirbuf, "/");
strcat(dirbuf, dbb_backend->db_name);
FLAGS_db = dir;
}
{
struct stat st;
int rc;
rc = stat(FLAGS_db, &st);
if (rc == 0 && (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
FLAGS_rawdev = 1;
}
if (!FLAGS_max_threads)
FLAGS_max_threads = FLAGS_threads;
if (!seeds)
/* Set up an extra randctx */
seeds = calloc(FLAGS_max_threads+1, sizeof(rndctx *));
for (i=0; i<FLAGS_max_threads+1; i++)
if (!seeds[i]) seeds[i] = DBB_randctx();
DBB_srandom(seeds[0], 0);
for (i=1; i<FLAGS_threads+1; i++)
DBB_randjump(seeds[i-1], seeds[i]);
if (!hists)
hists = calloc(FLAGS_max_threads+1, sizeof(Hstctx *));
for (i=0; i<FLAGS_max_threads+1; i++)
if (!hists[i]) hists[i] = DBB_hstctx();
for (i=0; i<FLAGS_threads+1; i++)
DBB_hstinit(hists[i]);
Benchmark();
if (!aret)
break;
}
if (db_open)
dbb_backend->db_close();
return 0;
}
int main() __attribute__((weak, alias ("main2")));