forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mttest.cc
1377 lines (1257 loc) · 46.9 KB
/
mttest.cc
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
// -*- mode: c++ -*-
// mttest: key/value tester
//
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <limits.h>
#if HAVE_NUMA_H
#include <numa.h>
#endif
#if HAVE_SYS_EPOLL_H
#include <sys/epoll.h>
#endif
#if HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#if __linux__
#include <asm-generic/mman.h>
#endif
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <pthread.h>
#include <math.h>
#include <signal.h>
#include <errno.h>
#ifdef __linux__
#include <malloc.h>
#endif
#include "nodeversion.hh"
#include "kvstats.hh"
#include "query_masstree.hh"
#include "masstree_tcursor.hh"
#include "masstree_insert.hh"
#include "masstree_remove.hh"
#include "masstree_scan.hh"
#include "timestamp.hh"
#include "json.hh"
#include "kvtest.hh"
#include "kvrandom.hh"
#include "kvrow.hh"
#include "kvio.hh"
#include "clp.h"
#include <algorithm>
#include <numeric>
static std::vector<int> cores;
volatile bool timeout[2] = {false, false};
double duration[2] = {10, 0};
// Do not start timer until asked
static bool lazy_timer = false;
int kvtest_first_seed = 31949;
uint64_t test_limit = ~uint64_t(0);
static Json test_param;
bool quiet = false;
bool print_table = false;
static const char *gid = NULL;
// all default to the number of cores
static int udpthreads = 0;
static int tcpthreads = 0;
static bool tree_stats = false;
static bool json_stats = false;
static String gnuplot_yrange;
static bool pinthreads = false;
static nodeversion32 global_epoch_lock(false);
volatile mrcu_epoch_type globalepoch = 1; // global epoch, updated by main thread regularly
volatile mrcu_epoch_type active_epoch = 1;
kvepoch_t global_log_epoch = 0;
static int port = 2117;
static int rscale_ncores = 0;
#if MEMSTATS && HAVE_NUMA_H && HAVE_LIBNUMA
struct mttest_numainfo {
long long free;
long long size;
};
std::vector<mttest_numainfo> numa;
#endif
volatile bool recovering = false; // so don't add log entries, and free old value immediately
kvtimestamp_t initial_timestamp;
static const char *threadcounter_names[(int) tc_max];
/* running local tests */
void test_timeout(int) {
size_t n;
for (n = 0; n < arraysize(timeout) && timeout[n]; ++n)
/* do nothing */;
if (n < arraysize(timeout)) {
timeout[n] = true;
if (n + 1 < arraysize(timeout) && duration[n + 1])
xalarm(duration[n + 1]);
}
}
void set_global_epoch(mrcu_epoch_type e) {
global_epoch_lock.lock();
if (mrcu_signed_epoch_type(e - globalepoch) > 0) {
globalepoch = e;
active_epoch = threadinfo::min_active_epoch();
}
global_epoch_lock.unlock();
}
template <typename T>
struct kvtest_client {
kvtest_client()
: limit_(test_limit), ncores_(udpthreads), kvo_() {
}
~kvtest_client() {
if (kvo_)
free_kvout(kvo_);
}
int nthreads() const {
return udpthreads;
}
int id() const {
return ti_->index();
}
void set_table(T *table, threadinfo *ti) {
table_ = table;
ti_ = ti;
}
void reset(const String &test, int trial) {
report_ = Json().set("table", T().name())
.set("test", test).set("trial", trial)
.set("thread", ti_->index());
}
static void start_timer() {
always_assert(lazy_timer && "Cannot start timer without lazy_timer option");
always_assert(duration[0] && "Must specify timeout[0]");
xalarm(duration[0]);
}
bool timeout(int which) const {
return ::timeout[which];
}
uint64_t limit() const {
return limit_;
}
Json param(const String& name) const {
return test_param[name];
}
int ncores() const {
return ncores_;
}
double now() const {
return ::now();
}
int ruscale_partsz() const {
return (140 * 1000000) / 16;
}
int ruscale_init_part_no() const {
return ti_->index();
}
long nseqkeys() const {
return 16 * ruscale_partsz();
}
void get(long ikey);
bool get_sync(const Str &key);
bool get_sync(const Str &key, Str &value);
bool get_sync(long ikey) {
quick_istr key(ikey);
return get_sync(key.string());
}
bool get_sync_key16(long ikey) {
quick_istr key(ikey, 16);
return get_sync(key.string());
}
void get_check(const Str &key, const Str &expected);
void get_check(const char *key, const char *expected) {
get_check(Str(key), Str(expected));
}
void get_check(long ikey, long iexpected) {
quick_istr key(ikey), expected(iexpected);
get_check(key.string(), expected.string());
}
void get_check(const Str &key, long iexpected) {
quick_istr expected(iexpected);
get_check(key, expected.string());
}
void get_check_key8(long ikey, long iexpected) {
quick_istr key(ikey, 8), expected(iexpected);
get_check(key.string(), expected.string());
}
void get_col_check(const Str &key, int col, const Str &value);
void get_col_check(long ikey, int col, long ivalue) {
quick_istr key(ikey), value(ivalue);
get_col_check(key.string(), col, value.string());
}
void get_col_check_key10(long ikey, int col, long ivalue) {
quick_istr key(ikey, 10), value(ivalue);
get_col_check(key.string(), col, value.string());
}
//void many_get_check(int nk, long ikey[], long iexpected[]);
void scan_sync(const Str &firstkey, int n,
std::vector<Str> &keys, std::vector<Str> &values);
void rscan_sync(const Str &firstkey, int n,
std::vector<Str> &keys, std::vector<Str> &values);
void put(const Str &key, const Str &value);
void put(const char *key, const char *value) {
put(Str(key), Str(value));
}
void put(long ikey, long ivalue) {
quick_istr key(ikey), value(ivalue);
put(key.string(), value.string());
}
void put(const Str &key, long ivalue) {
quick_istr value(ivalue);
put(key, value.string());
}
void put_key8(long ikey, long ivalue) {
quick_istr key(ikey, 8), value(ivalue);
put(key.string(), value.string());
}
void put_key16(long ikey, long ivalue) {
quick_istr key(ikey, 16), value(ivalue);
put(key.string(), value.string());
}
void put_col(const Str &key, int col, const Str &value);
void put_col(long ikey, int col, long ivalue) {
quick_istr key(ikey), value(ivalue);
put_col(key.string(), col, value.string());
}
void put_col_key10(long ikey, int col, long ivalue) {
quick_istr key(ikey, 10), value(ivalue);
put_col(key.string(), col, value.string());
}
void remove(const Str &key);
void remove(long ikey) {
quick_istr key(ikey);
remove(key.string());
}
void remove_key8(long ikey) {
quick_istr key(ikey, 8);
remove(key.string());
}
void remove_key16(long ikey) {
quick_istr key(ikey, 16);
remove(key.string());
}
bool remove_sync(const Str &key);
bool remove_sync(long ikey) {
quick_istr key(ikey);
return remove_sync(key.string());
}
void puts_done() {
}
void wait_all() {
}
void rcu_quiesce() {
mrcu_epoch_type e = timestamp() >> 16;
if (e != globalepoch)
set_global_epoch(e);
ti_->rcu_quiesce();
}
String make_message(lcdf::StringAccum &sa) const;
void notice(const char *fmt, ...);
void fail(const char *fmt, ...);
const Json& report(const Json& x) {
return report_.merge(x);
}
void finish() {
Json counters;
for (int i = 0; i < tc_max; ++i)
if (uint64_t c = ti_->counter(threadcounter(i)))
counters.set(threadcounter_names[i], c);
if (counters)
report_.set("counters", counters);
if (!quiet)
fprintf(stderr, "%d: %s\n", ti_->index(), report_.unparse().c_str());
}
T *table_;
threadinfo *ti_;
query<row_type> q_[1];
kvrandom_lcg_nr rand;
uint64_t limit_;
Json report_;
Json req_;
int ncores_;
kvout *kvo_;
private:
void output_scan(const Json& req, std::vector<Str>& keys, std::vector<Str>& values) const;
};
static volatile int kvtest_printing;
template <typename T> inline void kvtest_print(const T &table, FILE* f, threadinfo *ti) {
// only print out the tree from the first failure
while (!bool_cmpxchg((int *) &kvtest_printing, 0, ti->index() + 1))
/* spin */;
table.print(f);
}
template <typename T> inline void kvtest_json_stats(T& table, Json& j, threadinfo& ti) {
table.json_stats(j, ti);
}
template <typename T>
void kvtest_client<T>::get(long ikey) {
quick_istr key(ikey);
Str val;
(void) q_[0].run_get1(table_->table(), key.string(), 0, val, *ti_);
}
template <typename T>
bool kvtest_client<T>::get_sync(const Str& key) {
Str val;
return q_[0].run_get1(table_->table(), key, 0, val, *ti_);
}
template <typename T>
bool kvtest_client<T>::get_sync(const Str &key, Str &value) {
return q_[0].run_get1(table_->table(), key, 0, value, *ti_);
}
template <typename T>
void kvtest_client<T>::get_check(const Str &key, const Str &expected) {
Str val;
if (!q_[0].run_get1(table_->table(), key, 0, val, *ti_))
fail("get(%.*s) failed (expected %.*s)\n", key.len, key.s,
expected.len, expected.s);
else if (expected != val)
fail("get(%.*s) returned unexpected value %.*s (expected %.*s)\n",
key.len, key.s, std::min(val.len, 40), val.s,
expected.len, expected.s);
}
template <typename T>
void kvtest_client<T>::get_col_check(const Str &key, int col,
const Str &expected) {
Str val;
if (!q_[0].run_get1(table_->table(), key, col, val, *ti_))
fail("get.%d(%.*s) failed (expected %.*s)\n",
col, key.len, key.s, expected.len, expected.s);
else if (expected != val)
fail("get.%d(%.*s) returned unexpected value %.*s (expected %.*s)\n",
col, key.len, key.s, std::min(val.len, 40), val.s,
expected.len, expected.s);
}
/*template <typename T>
void kvtest_client<T>::many_get_check(int nk, long ikey[], long iexpected[]) {
std::vector<quick_istr> ka(2*nk, quick_istr());
for(int i = 0; i < nk; i++){
ka[i].set(ikey[i]);
ka[i+nk].set(iexpected[i]);
q_[i].begin_get1(ka[i].string());
}
table_->many_get(q_, nk, *ti_);
for(int i = 0; i < nk; i++){
Str val = q_[i].get1_value();
if (ka[i+nk] != val){
printf("get(%ld) returned unexpected value %.*s (expected %ld)\n",
ikey[i], std::min(val.len, 40), val.s, iexpected[i]);
exit(1);
}
}
}*/
template <typename T>
void kvtest_client<T>::scan_sync(const Str &firstkey, int n,
std::vector<Str> &keys,
std::vector<Str> &values) {
req_ = Json::array(0, 0, firstkey, n);
q_[0].run_scan(table_->table(), req_, *ti_);
output_scan(req_, keys, values);
}
template <typename T>
void kvtest_client<T>::rscan_sync(const Str &firstkey, int n,
std::vector<Str> &keys,
std::vector<Str> &values) {
req_ = Json::array(0, 0, firstkey, n);
q_[0].run_rscan(table_->table(), req_, *ti_);
output_scan(req_, keys, values);
}
template <typename T>
void kvtest_client<T>::output_scan(const Json& req, std::vector<Str>& keys,
std::vector<Str>& values) const {
keys.clear();
values.clear();
for (int i = 2; i != req.size(); i += 2) {
keys.push_back(req[i].as_s());
values.push_back(req[i + 1].as_s());
}
}
template <typename T>
void kvtest_client<T>::put(const Str &key, const Str &value) {
q_[0].run_replace(table_->table(), key, value, *ti_);
}
template <typename T>
void kvtest_client<T>::put_col(const Str &key, int col, const Str &value) {
#if !MASSTREE_ROW_TYPE_STR
if (!kvo_)
kvo_ = new_kvout(-1, 2048);
Json x[2] = {Json(col), Json(String::make_stable(value))};
q_[0].run_put(table_->table(), key, &x[0], &x[2], *ti_);
#else
(void) key, (void) col, (void) value;
assert(0);
#endif
}
template <typename T> inline bool kvtest_remove(kvtest_client<T> &client, const Str &key) {
return client.q_[0].run_remove(client.table_->table(), key, *client.ti_);
}
template <typename T>
void kvtest_client<T>::remove(const Str &key) {
(void) kvtest_remove(*this, key);
}
template <typename T>
bool kvtest_client<T>::remove_sync(const Str &key) {
return kvtest_remove(*this, key);
}
template <typename T>
String kvtest_client<T>::make_message(lcdf::StringAccum &sa) const {
const char *begin = sa.begin();
while (begin != sa.end() && isspace((unsigned char) *begin))
++begin;
String s = String(begin, sa.end());
if (!s.empty() && s.back() != '\n')
s += '\n';
return s;
}
template <typename T>
void kvtest_client<T>::notice(const char *fmt, ...) {
va_list val;
va_start(val, fmt);
String m = make_message(lcdf::StringAccum().vsnprintf(500, fmt, val));
va_end(val);
if (m && !quiet)
fprintf(stderr, "%d: %s", ti_->index(), m.c_str());
}
template <typename T>
void kvtest_client<T>::fail(const char *fmt, ...) {
static nodeversion32 failing_lock(false);
static nodeversion32 fail_message_lock(false);
static String fail_message;
va_list val;
va_start(val, fmt);
String m = make_message(lcdf::StringAccum().vsnprintf(500, fmt, val));
va_end(val);
if (!m)
m = "unknown failure";
fail_message_lock.lock();
if (fail_message != m) {
fail_message = m;
fprintf(stderr, "%d: %s", ti_->index(), m.c_str());
}
fail_message_lock.unlock();
failing_lock.lock();
fprintf(stdout, "%d: %s", ti_->index(), m.c_str());
kvtest_print(*table_, stdout, ti_);
always_assert(0);
}
static const char *current_test_name;
static int current_trial;
static FILE *test_output_file;
static pthread_mutex_t subtest_mutex;
static pthread_cond_t subtest_cond;
#define TESTRUNNER_CLIENT_TYPE kvtest_client<Masstree::default_table>&
#include "testrunner.hh"
MAKE_TESTRUNNER(rw1, kvtest_rw1(client));
// MAKE_TESTRUNNER(palma, kvtest_palma(client));
// MAKE_TESTRUNNER(palmb, kvtest_palmb(client));
MAKE_TESTRUNNER(rw1fixed, kvtest_rw1fixed(client));
MAKE_TESTRUNNER(rw1long, kvtest_rw1long(client));
MAKE_TESTRUNNER(rw1puts, kvtest_rw1puts(client));
MAKE_TESTRUNNER(rw2, kvtest_rw2(client));
MAKE_TESTRUNNER(rw2fixed, kvtest_rw2fixed(client));
MAKE_TESTRUNNER(rw2g90, kvtest_rw2g90(client));
MAKE_TESTRUNNER(rw2fixedg90, kvtest_rw2fixedg90(client));
MAKE_TESTRUNNER(rw2g98, kvtest_rw2g98(client));
MAKE_TESTRUNNER(rw2fixedg98, kvtest_rw2fixedg98(client));
MAKE_TESTRUNNER(rw3, kvtest_rw3(client));
MAKE_TESTRUNNER(rw4, kvtest_rw4(client));
MAKE_TESTRUNNER(rw4fixed, kvtest_rw4fixed(client));
MAKE_TESTRUNNER(wd1, kvtest_wd1(10000000, 1, client));
MAKE_TESTRUNNER(wd1m1, kvtest_wd1(100000000, 1, client));
MAKE_TESTRUNNER(wd1m2, kvtest_wd1(1000000000, 4, client));
MAKE_TESTRUNNER(same, kvtest_same(client));
MAKE_TESTRUNNER(rwsmall24, kvtest_rwsmall24(client));
MAKE_TESTRUNNER(rwsep24, kvtest_rwsep24(client));
MAKE_TESTRUNNER(wscale, kvtest_wscale(client));
MAKE_TESTRUNNER(ruscale_init, kvtest_ruscale_init(client));
MAKE_TESTRUNNER(rscale, if (client.ti_->index() < ::rscale_ncores) kvtest_rscale(client));
MAKE_TESTRUNNER(uscale, kvtest_uscale(client));
MAKE_TESTRUNNER(bdb, kvtest_bdb(client));
MAKE_TESTRUNNER(wcol1, kvtest_wcol1at(client, client.id() % 24, kvtest_first_seed + client.id() % 48, 5000000));
MAKE_TESTRUNNER(rcol1, kvtest_rcol1at(client, client.id() % 24, kvtest_first_seed + client.id() % 48, 5000000));
MAKE_TESTRUNNER(wcol1o1, kvtest_wcol1at(client, (client.id() + 1) % 24, kvtest_first_seed + client.id() % 48, 5000000));
MAKE_TESTRUNNER(rcol1o1, kvtest_rcol1at(client, (client.id() + 1) % 24, kvtest_first_seed + client.id() % 48, 5000000));
MAKE_TESTRUNNER(wcol1o2, kvtest_wcol1at(client, (client.id() + 2) % 24, kvtest_first_seed + client.id() % 48, 5000000));
MAKE_TESTRUNNER(rcol1o2, kvtest_rcol1at(client, (client.id() + 2) % 24, kvtest_first_seed + client.id() % 48, 5000000));
MAKE_TESTRUNNER(scan1, kvtest_scan1(client, 0));
MAKE_TESTRUNNER(scan1q80, kvtest_scan1(client, 0.8));
MAKE_TESTRUNNER(rscan1, kvtest_rscan1(client, 0));
MAKE_TESTRUNNER(rscan1q80, kvtest_rscan1(client, 0.8));
MAKE_TESTRUNNER(splitremove1, kvtest_splitremove1(client));
MAKE_TESTRUNNER(url, kvtest_url(client));
enum {
test_thread_initialize = 1,
test_thread_destroy = 2,
test_thread_stats = 3
};
template <typename T>
struct test_thread {
test_thread(threadinfo* ti) {
client_.set_table(table_, ti);
client_.ti_->rcu_start();
}
~test_thread() {
client_.ti_->rcu_stop();
}
static void setup(threadinfo* ti, int action) {
if (action == test_thread_initialize) {
assert(!table_);
table_ = new T;
table_->initialize(*ti);
} else if (action == test_thread_destroy) {
assert(table_);
delete table_;
table_ = 0;
} else if (action == test_thread_stats) {
assert(table_);
table_->stats(test_output_file);
}
}
static void* go(void* x) {
threadinfo* ti = reinterpret_cast<threadinfo*>(x);
ti->pthread() = pthread_self();
assert(table_);
#if __linux__
if (pinthreads) {
cpu_set_t cs;
CPU_ZERO(&cs);
CPU_SET(cores[ti->index()], &cs);
int r = sched_setaffinity(0, sizeof(cs), &cs);
always_assert(r == 0);
}
#else
always_assert(!pinthreads && "pinthreads not supported\n");
#endif
test_thread<T> tt(ti);
if (fetch_and_add(&active_threads_, 1) == 0)
tt.ready_timeouts();
String test = ::current_test_name;
int subtestno = 0;
for (int pos = 0; pos < test.length(); ) {
int comma = test.find_left(',', pos);
comma = (comma < 0 ? test.length() : comma);
String subtest = test.substr(pos, comma - pos), tname;
testrunner* tr = testrunner::find(subtest);
tname = (subtest == test ? subtest : test + String("@") + String(subtestno));
tt.client_.reset(tname, ::current_trial);
if (tr)
tr->run(tt.client_);
else
tt.client_.fail("unknown test %s", subtest.c_str());
if (comma == test.length())
break;
pthread_mutex_lock(&subtest_mutex);
if (fetch_and_add(&active_threads_, -1) == 1) {
pthread_cond_broadcast(&subtest_cond);
tt.ready_timeouts();
} else
pthread_cond_wait(&subtest_cond, &subtest_mutex);
fprintf(test_output_file, "%s\n", tt.client_.report_.unparse().c_str());
pthread_mutex_unlock(&subtest_mutex);
fetch_and_add(&active_threads_, 1);
pos = comma + 1;
++subtestno;
}
int at = fetch_and_add(&active_threads_, -1);
if (at == 1 && print_table)
kvtest_print(*table_, stdout, tt.client_.ti_);
if (at == 1 && json_stats) {
Json j;
kvtest_json_stats(*table_, j, *tt.client_.ti_);
if (j) {
fprintf(stderr, "%s\n", j.unparse(Json::indent_depth(1).tab_width(2).newline_terminator(true)).c_str());
tt.client_.report_.merge(j);
}
}
fprintf(test_output_file, "%s\n", tt.client_.report_.unparse().c_str());
return 0;
}
void ready_timeouts() {
for (size_t i = 0; i < arraysize(timeout); ++i)
timeout[i] = false;
if (!lazy_timer && duration[0])
xalarm(duration[0]);
}
static T *table_;
static unsigned active_threads_;
kvtest_client<T> client_;
};
template <typename T> T *test_thread<T>::table_;
template <typename T> unsigned test_thread<T>::active_threads_;
typedef test_thread<Masstree::default_table> masstree_test_thread;
static struct {
const char *treetype;
void* (*go_func)(void*);
void (*setup_func)(threadinfo*, int);
} test_thread_map[] = {
{ "masstree", masstree_test_thread::go, masstree_test_thread::setup },
{ "mass", masstree_test_thread::go, masstree_test_thread::setup },
{ "mbtree", masstree_test_thread::go, masstree_test_thread::setup },
{ "mb", masstree_test_thread::go, masstree_test_thread::setup },
{ "m", masstree_test_thread::go, masstree_test_thread::setup }
};
void runtest(int nthreads, void* (*func)(void*)) {
std::vector<threadinfo*> tis;
for (int i = 0; i < nthreads; ++i)
tis.push_back(threadinfo::make(threadinfo::TI_PROCESS, i));
signal(SIGALRM, test_timeout);
for (int i = 0; i < nthreads; ++i) {
int r = pthread_create(&tis[i]->pthread(), 0, func, tis[i]);
always_assert(r == 0);
}
for (int i = 0; i < nthreads; ++i)
pthread_join(tis[i]->pthread(), 0);
}
static const char * const kvstats_name[] = {
"ops_per_sec", "puts_per_sec", "gets_per_sec", "scans_per_sec"
};
static Json experiment_stats;
void *stat_collector(void *arg) {
int p = (int) (intptr_t) arg;
FILE *f = fdopen(p, "r");
char buf[8192];
while (fgets(buf, sizeof(buf), f)) {
Json result = Json::parse(buf);
if (result && result["table"] && result["test"]) {
String key = result["test"].to_s() + "/" + result["table"].to_s()
+ "/" + result["trial"].to_s();
Json &thisex = experiment_stats.get_insert(key);
thisex[result["thread"].to_i()] = result;
} else
fprintf(stderr, "%s\n", buf);
}
fclose(f);
return 0;
}
/* main loop */
enum { clp_val_normalize = Clp_ValFirstUser, clp_val_suffixdouble };
enum { opt_pin = 1, opt_port, opt_duration,
opt_test, opt_test_name, opt_threads, opt_trials, opt_quiet, opt_print,
opt_normalize, opt_limit, opt_notebook, opt_compare, opt_no_run,
opt_lazy_timer, opt_gid, opt_tree_stats, opt_rscale_ncores, opt_cores,
opt_stats, opt_help, opt_yrange };
static const Clp_Option options[] = {
{ "pin", 'p', opt_pin, 0, Clp_Negate },
{ "port", 0, opt_port, Clp_ValInt, 0 },
{ "duration", 'd', opt_duration, Clp_ValDouble, 0 },
{ "lazy-timer", 0, opt_lazy_timer, 0, 0 },
{ "limit", 'l', opt_limit, clp_val_suffixdouble, 0 },
{ "normalize", 0, opt_normalize, clp_val_normalize, Clp_Negate },
{ "test", 0, opt_test, Clp_ValString, 0 },
{ "rscale_ncores", 'r', opt_rscale_ncores, Clp_ValInt, 0 },
{ "test-rw1", 0, opt_test_name, 0, 0 },
{ "test-rw2", 0, opt_test_name, 0, 0 },
{ "test-rw3", 0, opt_test_name, 0, 0 },
{ "test-rw4", 0, opt_test_name, 0, 0 },
{ "test-rd1", 0, opt_test_name, 0, 0 },
{ "threads", 'j', opt_threads, Clp_ValInt, 0 },
{ "trials", 'T', opt_trials, Clp_ValInt, 0 },
{ "quiet", 'q', opt_quiet, 0, Clp_Negate },
{ "print", 0, opt_print, 0, Clp_Negate },
{ "notebook", 'b', opt_notebook, Clp_ValString, Clp_Negate },
{ "gid", 'g', opt_gid, Clp_ValString, 0 },
{ "tree-stats", 0, opt_tree_stats, 0, 0 },
{ "stats", 0, opt_stats, 0, 0 },
{ "compare", 'c', opt_compare, Clp_ValString, 0 },
{ "cores", 0, opt_cores, Clp_ValString, 0 },
{ "yrange", 0, opt_yrange, Clp_ValString, 0 },
{ "no-run", 'n', opt_no_run, 0, 0 },
{ "help", 0, opt_help, 0, 0 }
};
static void help() {
printf("Masstree-beta mttest\n\
Usage: mttest [-jTHREADS] [OPTIONS] [PARAM=VALUE...] TEST...\n\
mttest -n -c TESTNAME...\n\
\n\
Options:\n\
-j, --threads=THREADS Run with THREADS threads (default %d).\n\
-p, --pin Pin each thread to its own core.\n\
-T, --trials=TRIALS Run each test TRIALS times.\n\
-q, --quiet Do not generate verbose and Gnuplot output.\n\
-l, --limit=LIMIT Limit relevant tests to LIMIT operations.\n\
-d, --duration=TIME Limit relevant tests to TIME seconds.\n\
-b, --notebook=FILE Record JSON results in FILE (notebook-mttest.json).\n\
--no-notebook Do not record JSON results.\n\
\n\
-n, --no-run Do not run new tests.\n\
-c, --compare=EXPERIMENT Generated plot compares to EXPERIMENT.\n\
--yrange=YRANGE Set Y range for plot.\n\
\n\
Known TESTs:\n",
(int) sysconf(_SC_NPROCESSORS_ONLN));
testrunner_base::print_names(stdout, 5);
printf("Or say TEST1,TEST2,... to run several tests in sequence\n\
on the same tree.\n");
exit(0);
}
static void run_one_test(int trial, const char *treetype, const char *test,
const int *collectorpipe, int nruns);
enum { normtype_none, normtype_pertest, normtype_firsttest };
static void print_gnuplot(FILE *f, const char * const *types_begin, const char * const *types_end, std::vector<String> &comparisons, int normalizetype);
static void update_labnotebook(String notebook);
#if HAVE_EXECINFO_H
static const int abortable_signals[] = {
SIGSEGV, SIGBUS, SIGILL, SIGABRT, SIGFPE
};
static void abortable_signal_handler(int) {
// reset signals so if a signal recurs, we exit
for (const int* it = abortable_signals;
it != abortable_signals + arraysize(abortable_signals); ++it)
signal(*it, SIG_DFL);
// dump backtrace to standard error
void* return_addrs[50];
int n = backtrace(return_addrs, arraysize(return_addrs));
backtrace_symbols_fd(return_addrs, n, STDERR_FILENO);
// re-abort
abort();
}
#endif
int
main(int argc, char *argv[])
{
threadcounter_names[(int) tc_root_retry] = "root_retry";
threadcounter_names[(int) tc_internode_retry] = "internode_retry";
threadcounter_names[(int) tc_leaf_retry] = "leaf_retry";
threadcounter_names[(int) tc_leaf_walk] = "leaf_walk";
threadcounter_names[(int) tc_stable_internode_insert] = "stable_internode_insert";
threadcounter_names[(int) tc_stable_internode_split] = "stable_internode_split";
threadcounter_names[(int) tc_stable_leaf_insert] = "stable_leaf_insert";
threadcounter_names[(int) tc_stable_leaf_split] = "stable_leaf_split";
threadcounter_names[(int) tc_internode_lock] = "internode_lock_retry";
threadcounter_names[(int) tc_leaf_lock] = "leaf_lock_retry";
int ret, ntrials = 1, normtype = normtype_pertest, firstcore = -1, corestride = 1;
std::vector<const char *> tests, treetypes;
std::vector<String> comparisons;
const char *notebook = "notebook-mttest.json";
tcpthreads = udpthreads = sysconf(_SC_NPROCESSORS_ONLN);
Clp_Parser *clp = Clp_NewParser(argc, argv, (int) arraysize(options), options);
Clp_AddStringListType(clp, clp_val_normalize, 0,
"none", (int) normtype_none,
"pertest", (int) normtype_pertest,
"test", (int) normtype_pertest,
"firsttest", (int) normtype_firsttest,
(const char *) 0);
Clp_AddType(clp, clp_val_suffixdouble, Clp_DisallowOptions, clp_parse_suffixdouble, 0);
int opt;
while ((opt = Clp_Next(clp)) != Clp_Done) {
switch (opt) {
case opt_pin:
pinthreads = !clp->negated;
break;
case opt_threads:
tcpthreads = udpthreads = clp->val.i;
break;
case opt_trials:
ntrials = clp->val.i;
break;
case opt_quiet:
quiet = !clp->negated;
break;
case opt_print:
print_table = !clp->negated;
break;
case opt_rscale_ncores:
rscale_ncores = clp->val.i;
break;
case opt_port:
port = clp->val.i;
break;
case opt_duration:
duration[0] = clp->val.d;
break;
case opt_lazy_timer:
lazy_timer = true;
break;
case opt_limit:
test_limit = uint64_t(clp->val.d);
break;
case opt_test:
tests.push_back(clp->vstr);
break;
case opt_test_name:
tests.push_back(clp->option->long_name + 5);
break;
case opt_normalize:
normtype = clp->negated ? normtype_none : clp->val.i;
break;
case opt_gid:
gid = clp->vstr;
break;
case opt_tree_stats:
tree_stats = true;
break;
case opt_stats:
json_stats = true;
break;
case opt_yrange:
gnuplot_yrange = clp->vstr;
break;
case opt_notebook:
if (clp->negated)
notebook = 0;
else if (clp->have_val)
notebook = clp->vstr;
else
notebook = "notebook-mttest.json";
break;
case opt_compare:
comparisons.push_back(clp->vstr);
break;
case opt_no_run:
ntrials = 0;
break;
case opt_cores:
if (firstcore >= 0 || cores.size() > 0) {
Clp_OptionError(clp, "%<%O%> already given");
exit(EXIT_FAILURE);
} else {
const char *plus = strchr(clp->vstr, '+');
Json ij = Json::parse(clp->vstr),
aj = Json::parse(String("[") + String(clp->vstr) + String("]")),
pj1 = Json::parse(plus ? String(clp->vstr, plus) : "x"),
pj2 = Json::parse(plus ? String(plus + 1) : "x");
for (int i = 0; aj && i < aj.size(); ++i)
if (!aj[i].is_int() || aj[i].to_i() < 0)
aj = Json();
if (ij && ij.is_int() && ij.to_i() >= 0)
firstcore = ij.to_i(), corestride = 1;
else if (pj1 && pj2 && pj1.is_int() && pj1.to_i() >= 0 && pj2.is_int())
firstcore = pj1.to_i(), corestride = pj2.to_i();
else if (aj) {
for (int i = 0; i < aj.size(); ++i)
cores.push_back(aj[i].to_i());
} else {
Clp_OptionError(clp, "bad %<%O%>, expected %<CORE1%>, %<CORE1+STRIDE%>, or %<CORE1,CORE2,...%>");
exit(EXIT_FAILURE);
}
}
break;
case opt_help:
help();
break;
case Clp_NotOption:
// check for parameter setting
if (const char* eqchr = strchr(clp->vstr, '=')) {
Json& param = test_param[String(clp->vstr, eqchr)];
const char* end_vstr = clp->vstr + strlen(clp->vstr);
if (param.assign_parse(eqchr + 1, end_vstr))
/* OK, param was valid JSON */;
else if (eqchr[1] != 0)
param = String(eqchr + 1, end_vstr);
else
param = Json();
} else {
// otherwise, tree or test
bool is_treetype = false;
for (int i = 0; i < (int) arraysize(test_thread_map) && !is_treetype; ++i)
is_treetype = (strcmp(test_thread_map[i].treetype, clp->vstr) == 0);
(is_treetype ? treetypes.push_back(clp->vstr) : tests.push_back(clp->vstr));
}
break;
default:
fprintf(stderr, "Usage: mttest [-jN] TESTS...\n\
Try 'mttest --help' for options.\n");
exit(EXIT_FAILURE);
}
}
Clp_DeleteParser(clp);
if (firstcore < 0)
firstcore = cores.size() ? cores.back() + 1 : 0;
for (; (int) cores.size() < udpthreads; firstcore += corestride)
cores.push_back(firstcore);
#if PMC_ENABLED
always_assert(pinthreads && "Using performance counter requires pinning threads to cores!");
#endif
#if MEMSTATS && HAVE_NUMA_H && HAVE_LIBNUMA
if (numa_available() != -1)
for (int i = 0; i <= numa_max_node(); i++) {
numa.push_back(mttest_numainfo());
numa.back().size = numa_node_size64(i, &numa.back().free);
}
#endif
#if HAVE_EXECINFO_H
for (const int* it = abortable_signals;
it != abortable_signals + arraysize(abortable_signals); ++it)
signal(*it, abortable_signal_handler);
#endif
if (treetypes.empty())
treetypes.push_back("m");
if (tests.empty())
tests.push_back("rw1");
pthread_mutex_init(&subtest_mutex, 0);
pthread_cond_init(&subtest_cond, 0);
// pipe for them to write back to us
int p[2];
ret = pipe(p);
always_assert(ret == 0);
test_output_file = fdopen(p[1], "w");
pthread_t collector;
ret = pthread_create(&collector, 0, stat_collector, (void *) (intptr_t) p[0]);
always_assert(ret == 0);
initial_timestamp = timestamp();
// run tests
int nruns = ntrials * (int) tests.size() * (int) treetypes.size();
std::vector<int> runlist(nruns, 0);
for (int i = 0; i < nruns; ++i)
runlist[i] = i;