forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtclient.cc
1724 lines (1548 loc) · 46.9 KB
/
mtclient.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-2014 President and Fellows of Harvard College
* Copyright (c) 2012-2014 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <assert.h>
#include <string.h>
#include <pthread.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <math.h>
#include <fcntl.h>
#include "kvstats.hh"
#include "kvio.hh"
#include "json.hh"
#include "kvtest.hh"
#include "mtclient.hh"
#include "kvrandom.hh"
#include "clp.h"
const char *serverip = "127.0.0.1";
static Json test_param;
typedef void (*get_async_cb)(struct child *c, struct async *a,
bool has_val, const Str &val);
typedef void (*put_async_cb)(struct child *c, struct async *a,
int status);
typedef void (*remove_async_cb)(struct child *c, struct async *a,
int status);
struct async {
int cmd; // Cmd_ constant
unsigned seq;
union {
get_async_cb get_fn;
put_async_cb put_fn;
remove_async_cb remove_fn;
};
char key[16]; // just first 16 bytes
char wanted[16]; // just first 16 bytes
int wantedlen;
int acked;
};
#define MAXWINDOW 512
unsigned window = MAXWINDOW;
struct child {
int s;
int udp; // 1 -> udp, 0 -> tcp
KVConn *conn;
struct async a[MAXWINDOW];
unsigned seq0_;
unsigned seq1_;
unsigned long long nsent_;
int childno;
inline void check_flush();
};
void checkasync(struct child *c, int force);
inline void child::check_flush() {
if ((seq1_ & ((window - 1) >> 1)) == 0)
conn->flush();
while (seq1_ - seq0_ >= window)
checkasync(this, 1);
}
void aget(struct child *, const Str &key, const Str &wanted, get_async_cb fn);
void aget(struct child *c, long ikey, long iwanted, get_async_cb fn);
void aget_col(struct child *c, const Str& key, int col, const Str& wanted,
get_async_cb fn);
int get(struct child *c, const Str &key, char *val, int max);
void asyncgetcb(struct child *, struct async *a, bool, const Str &val);
void asyncgetcb_int(struct child *, struct async *a, bool, const Str &val);
void aput(struct child *c, const Str &key, const Str &val,
put_async_cb fn = 0, const Str &wanted = Str());
void aput_col(struct child *c, const Str &key, int col, const Str &val,
put_async_cb fn = 0, const Str &wanted = Str());
int put(struct child *c, const Str &key, const Str &val);
void asyncputcb(struct child *, struct async *a, int status);
void aremove(struct child *c, const Str &key, remove_async_cb fn);
bool remove(struct child *c, const Str &key);
void udp1(struct child *);
void w1b(struct child *);
void u1(struct child *);
void over1(struct child *);
void over2(struct child *);
void rec1(struct child *);
void rec2(struct child *);
void cpa(struct child *);
void cpb(struct child *);
void cpc(struct child *);
void cpd(struct child *);
void volt1a(struct child *);
void volt1b(struct child *);
void volt2a(struct child *);
void volt2b(struct child *);
void scantest(struct child *);
static int children = 1;
static uint64_t nkeys = 0;
static int prefixLen = 0;
static int keylen = 0;
static uint64_t limit = ~uint64_t(0);
double duration = 10;
double duration2 = 0;
int udpflag = 0;
int quiet = 0;
int first_server_port = 2117;
// Should all child processes connects to the same UDP PORT on server
bool share_server_port = false;
volatile bool timeout[2] = {false, false};
int first_local_port = 0;
const char *input = NULL;
static int rsinit_part = 0;
int kvtest_first_seed = 0;
static int rscale_partsz = 0;
static int getratio = -1;
static int minkeyletter = '0';
static int maxkeyletter = '9';
struct kvtest_client {
kvtest_client(struct child& c)
: c_(&c) {
}
struct child* child() const {
return c_;
}
int id() const {
return c_->childno;
}
int nthreads() const {
return ::children;
}
char minkeyletter() const {
return ::minkeyletter;
}
char maxkeyletter() const {
return ::maxkeyletter;
}
void register_timeouts(int n) {
(void) n;
}
bool timeout(int which) const {
return ::timeout[which];
}
uint64_t limit() const {
return ::limit;
}
int getratio() const {
assert(::getratio >= 0);
return ::getratio;
}
uint64_t nkeys() const {
return ::nkeys;
}
int keylen() const {
return ::keylen;
}
int prefixLen() const {
return ::prefixLen;
}
Json param(const String& name, Json default_value = Json()) {
return test_param.count(name) ? test_param.at(name) : default_value;
}
double now() const {
return ::now();
}
void get(long ikey, Str *value) {
quick_istr key(ikey);
aget(c_, key.string(),
Str(reinterpret_cast<const char *>(&value), sizeof(value)),
asyncgetcb);
}
void get(const Str &key, int *ivalue) {
aget(c_, key,
Str(reinterpret_cast<const char *>(&ivalue), sizeof(ivalue)),
asyncgetcb_int);
}
bool get_sync(long ikey) {
char got[512];
quick_istr key(ikey);
return ::get(c_, key.string(), got, sizeof(got)) >= 0;
}
void get_check(long ikey, long iexpected) {
aget(c_, ikey, iexpected, 0);
}
void get_check(const char *key, const char *val) {
aget(c_, Str(key), Str(val), 0);
}
void get_check(const Str &key, const Str &val) {
aget(c_, key, val, 0);
}
void get_check_key8(long ikey, long iexpected) {
quick_istr key(ikey, 8), expected(iexpected);
aget(c_, key.string(), expected.string(), 0);
}
void get_check_key10(long ikey, long iexpected) {
quick_istr key(ikey, 10), expected(iexpected);
aget(c_, key.string(), expected.string(), 0);
}
void many_get_check(int, long [], long []) {
assert(0);
}
void get_col_check(const Str &key, int col, const Str &value) {
aget_col(c_, key, col, value, 0);
}
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 get_check_sync(long ikey, long iexpected) {
char key[512], val[512], got[512];
sprintf(key, "%010ld", ikey);
sprintf(val, "%ld", iexpected);
memset(got, 0, sizeof(got));
::get(c_, Str(key), got, sizeof(got));
if (strcmp(val, got)) {
fprintf(stderr, "key %s, expected %s, got %s\n", key, val, got);
always_assert(0);
}
}
void put(const Str &key, const Str &value) {
aput(c_, key, value);
}
void put(const Str &key, const Str &value, int *status) {
aput(c_, key, value,
asyncputcb,
Str(reinterpret_cast<const char *>(&status), sizeof(status)));
}
void put(const char *key, const char *value) {
aput(c_, Str(key), Str(value));
}
void put(const Str &key, long ivalue) {
quick_istr value(ivalue);
aput(c_, key, value.string());
}
void put(long ikey, long ivalue) {
quick_istr key(ikey), value(ivalue);
aput(c_, key.string(), value.string());
}
void put_key8(long ikey, long ivalue) {
quick_istr key(ikey, 8), value(ivalue);
aput(c_, key.string(), value.string());
}
void put_key10(long ikey, long ivalue) {
quick_istr key(ikey, 10), value(ivalue);
aput(c_, key.string(), value.string());
}
void put_col(const Str &key, int col, const Str &value) {
aput_col(c_, key, col, 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 put_sync(long ikey, long ivalue) {
quick_istr key(ikey, 10), value(ivalue);
::put(c_, key.string(), value.string());
}
void remove(const Str &key) {
aremove(c_, key, 0);
}
void remove(long ikey) {
quick_istr key(ikey);
remove(key.string());
}
bool remove_sync(long ikey) {
quick_istr key(ikey);
return ::remove(c_, key.string());
}
int ruscale_partsz() const {
return ::rscale_partsz;
}
int ruscale_init_part_no() const {
return ::rsinit_part;
}
long nseqkeys() const {
return 16 * ::rscale_partsz;
}
void wait_all() {
checkasync(c_, 2);
}
void puts_done() {
}
void rcu_quiesce() {
}
void notice(String s) {
if (!quiet) {
if (!s.empty() && s.back() == '\n')
s = s.substr(0, -1);
if (s.empty() || isspace((unsigned char) s[0]))
fprintf(stderr, "%d%.*s\n", c_->childno, s.length(), s.data());
else
fprintf(stderr, "%d %.*s\n", c_->childno, s.length(), s.data());
}
}
void notice(const char *fmt, ...) {
if (!quiet) {
va_list val;
va_start(val, fmt);
String x;
if (!*fmt || isspace((unsigned char) *fmt))
x = String(c_->childno) + fmt;
else
x = String(c_->childno) + String(" ") + fmt;
vfprintf(stderr, x.c_str(), val);
va_end(val);
}
}
const Json& report(const Json& x) {
return report_.merge(x);
}
void finish() {
if (!quiet) {
lcdf::StringAccum sa;
double dv;
if (report_.count("puts"))
sa << " total " << report_.get("puts");
if (report_.get("puts_per_sec", dv))
sa.snprintf(100, " %.0f put/s", dv);
if (report_.get("gets_per_sec", dv))
sa.snprintf(100, " %.0f get/s", dv);
if (!sa.empty())
notice(sa.take_string());
}
printf("%s\n", report_.unparse().c_str());
}
kvrandom_random rand;
struct child *c_;
Json report_;
};
#define TESTRUNNER_CLIENT_TYPE kvtest_client&
#include "testrunner.hh"
MAKE_TESTRUNNER(rw1, kvtest_rw1(client));
MAKE_TESTRUNNER(rw2, kvtest_rw2(client));
MAKE_TESTRUNNER(rw3, kvtest_rw3(client));
MAKE_TESTRUNNER(rw4, kvtest_rw4(client));
MAKE_TESTRUNNER(rw1fixed, kvtest_rw1fixed(client));
MAKE_TESTRUNNER(rw16, kvtest_rw16(client));
MAKE_TESTRUNNER(sync_rw1, kvtest_sync_rw1(client));
MAKE_TESTRUNNER(r1, kvtest_r1_seed(client, kvtest_first_seed + client.id()));
MAKE_TESTRUNNER(w1, kvtest_w1_seed(client, kvtest_first_seed + client.id()));
MAKE_TESTRUNNER(w1b, w1b(client.child()));
MAKE_TESTRUNNER(u1, u1(client.child()));
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(wd1check, kvtest_wd1_check(10000000, 1, client));
MAKE_TESTRUNNER(wd1m1check, kvtest_wd1_check(100000000, 1, client));
MAKE_TESTRUNNER(wd1m2check, kvtest_wd1_check(1000000000, 4, client));
MAKE_TESTRUNNER(wd2, kvtest_wd2(client));
MAKE_TESTRUNNER(wd2check, kvtest_wd2_check(client));
MAKE_TESTRUNNER(tri1, kvtest_tri1(10000000, 1, client));
MAKE_TESTRUNNER(tri1check, kvtest_tri1_check(10000000, 1, client));
MAKE_TESTRUNNER(same, kvtest_same(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(over1, over1(client.child()));
MAKE_TESTRUNNER(over2, over2(client.child()));
MAKE_TESTRUNNER(rec1, rec1(client.child()));
MAKE_TESTRUNNER(rec2, rec2(client.child()));
MAKE_TESTRUNNER(cpa, cpa(client.child()));
MAKE_TESTRUNNER(cpb, cpb(client.child()));
MAKE_TESTRUNNER(cpc, cpc(client.child()));
MAKE_TESTRUNNER(cpd, cpd(client.child()));
MAKE_TESTRUNNER(volt1a, volt1a(client.child()));
MAKE_TESTRUNNER(volt1b, volt1b(client.child()));
MAKE_TESTRUNNER(volt2a, volt2a(client.child()));
MAKE_TESTRUNNER(volt2b, volt2b(client.child()));
MAKE_TESTRUNNER(scantest, scantest(client.child()));
MAKE_TESTRUNNER(wscale, kvtest_wscale(client));
MAKE_TESTRUNNER(ruscale_init, kvtest_ruscale_init(client));
MAKE_TESTRUNNER(rscale, kvtest_rscale(client));
MAKE_TESTRUNNER(uscale, kvtest_uscale(client));
MAKE_TESTRUNNER(long_init, kvtest_long_init(client));
MAKE_TESTRUNNER(long_go, kvtest_long_go(client));
MAKE_TESTRUNNER(udp1, kvtest_udp1(client));
void run_child(testrunner*, int childno);
void
usage()
{
fprintf(stderr, "Usage: mtclient [-s serverip] [-w window] [--udp] "\
"[-j nchildren] [-d duration] [--ssp] [--flp first_local_port] "\
"[--fsp first_server_port] [-i json_input]\nTests:\n");
testrunner::print_names(stderr, 5);
exit(1);
}
void
settimeout(int)
{
if (!timeout[0]) {
timeout[0] = true;
if (duration2)
alarm((int) ceil(duration2));
} else
timeout[1] = true;
}
enum { clp_val_suffixdouble = Clp_ValFirstUser };
enum { opt_threads = 1, opt_threads_deprecated, opt_duration, opt_duration2,
opt_window, opt_server, opt_first_server_port, opt_quiet, opt_udp,
opt_first_local_port, opt_share_server_port, opt_input,
opt_rsinit_part, opt_first_seed, opt_rscale_partsz, opt_keylen,
opt_limit, opt_prefix_len, opt_nkeys, opt_get_ratio, opt_minkeyletter,
opt_maxkeyletter, opt_nofork };
static const Clp_Option options[] = {
{ "threads", 'j', opt_threads, Clp_ValInt, 0 },
{ 0, 'n', opt_threads_deprecated, Clp_ValInt, 0 },
{ "duration", 'd', opt_duration, Clp_ValDouble, 0 },
{ "duration2", 0, opt_duration2, Clp_ValDouble, 0 },
{ "d2", 0, opt_duration2, Clp_ValDouble, 0 },
{ "window", 'w', opt_window, Clp_ValUnsigned, 0 },
{ "server-ip", 's', opt_server, Clp_ValString, 0 },
{ "first-server-port", 0, opt_first_server_port, Clp_ValInt, 0 },
{ "fsp", 0, opt_first_server_port, Clp_ValInt, 0 },
{ "quiet", 'q', opt_quiet, 0, Clp_Negate },
{ "udp", 'u', opt_udp, 0, Clp_Negate },
{ "first-local-port", 0, opt_first_local_port, Clp_ValInt, 0 },
{ "flp", 0, opt_first_local_port, Clp_ValInt, 0 },
{ "share-server-port", 0, opt_share_server_port, 0, Clp_Negate },
{ "ssp", 0, opt_share_server_port, 0, Clp_Negate },
{ "input", 'i', opt_input, Clp_ValString, 0 },
{ "rsinit_part", 0, opt_rsinit_part, Clp_ValInt, 0 },
{ "first_seed", 0, opt_first_seed, Clp_ValInt, 0 },
{ "rscale_partsz", 0, opt_rscale_partsz, Clp_ValInt, 0 },
{ "keylen", 0, opt_keylen, Clp_ValInt, 0 },
{ "limit", 'l', opt_limit, clp_val_suffixdouble, 0 },
{ "prefixLen", 0, opt_prefix_len, Clp_ValInt, 0 },
{ "nkeys", 0, opt_nkeys, Clp_ValInt, 0 },
{ "getratio", 0, opt_get_ratio, Clp_ValInt, 0 },
{ "minkeyletter", 0, opt_minkeyletter, Clp_ValString, 0 },
{ "maxkeyletter", 0, opt_maxkeyletter, Clp_ValString, 0 },
{ "no-fork", 0, opt_nofork, 0, 0 }
};
int
main(int argc, char *argv[])
{
int i, pid, status;
testrunner* test = 0;
int pipes[512];
int dofork = 1;
Clp_Parser *clp = Clp_NewParser(argc, argv, (int) arraysize(options), options);
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_threads:
children = clp->val.i;
break;
case opt_threads_deprecated:
Clp_OptionError(clp, "%<%O%> is deprecated, use %<-j%>");
children = clp->val.i;
break;
case opt_duration:
duration = clp->val.d;
break;
case opt_duration2:
duration2 = clp->val.d;
break;
case opt_window:
window = clp->val.u;
always_assert(window <= MAXWINDOW);
always_assert((window & (window - 1)) == 0); // power of 2
break;
case opt_server:
serverip = clp->vstr;
break;
case opt_first_server_port:
first_server_port = clp->val.i;
break;
case opt_quiet:
quiet = !clp->negated;
break;
case opt_udp:
udpflag = !clp->negated;
break;
case opt_first_local_port:
first_local_port = clp->val.i;
break;
case opt_share_server_port:
share_server_port = !clp->negated;
break;
case opt_input:
input = clp->vstr;
break;
case opt_rsinit_part:
rsinit_part = clp->val.i;
break;
case opt_first_seed:
kvtest_first_seed = clp->val.i;
break;
case opt_rscale_partsz:
rscale_partsz = clp->val.i;
break;
case opt_keylen:
keylen = clp->val.i;
break;
case opt_limit:
limit = (uint64_t) clp->val.d;
break;
case opt_prefix_len:
prefixLen = clp->val.i;
break;
case opt_nkeys:
nkeys = clp->val.i;
break;
case opt_get_ratio:
getratio = clp->val.i;
break;
case opt_minkeyletter:
assert(strlen(clp->vstr) == 1);
minkeyletter = clp->vstr[0];
break;
case opt_maxkeyletter:
assert(strlen(clp->vstr) == 1);
maxkeyletter = clp->vstr[0];
break;
case opt_nofork:
dofork = !clp->negated;
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 {
test = testrunner::find(clp->vstr);
if (!test) {
usage();
}
}
break;
}
case Clp_BadOption:
usage();
break;
}
}
if(children < 1 || (children != 1 && !dofork))
usage();
if (!test)
test = testrunner::first();
printf("%s, w %d, test %s, children %d\n",
udpflag ? "udp" : "tcp", window,
test->name().c_str(), children);
fflush(stdout);
if (dofork) {
for(i = 0; i < children; i++){
int ptmp[2];
int r = pipe(ptmp);
always_assert(r == 0);
pid = fork();
if(pid < 0){
perror("fork");
exit(1);
}
if(pid == 0){
close(ptmp[0]);
dup2(ptmp[1], 1);
close(ptmp[1]);
signal(SIGALRM, settimeout);
alarm((int) ceil(duration));
run_child(test, i);
exit(0);
}
pipes[i] = ptmp[0];
close(ptmp[1]);
}
for(i = 0; i < children; i++){
if(wait(&status) <= 0){
perror("wait");
exit(1);
}
if (WIFSIGNALED(status))
fprintf(stderr, "child %d died by signal %d\n", i, WTERMSIG(status));
}
} else {
int ptmp[2];
int r = pipe(ptmp);
always_assert(r == 0);
pipes[0] = ptmp[0];
int stdout_fd = dup(STDOUT_FILENO);
always_assert(stdout_fd > 0);
r = dup2(ptmp[1], STDOUT_FILENO);
always_assert(r >= 0);
close(ptmp[1]);
signal(SIGALRM, settimeout);
alarm((int) ceil(duration));
run_child(test, 0);
fflush(stdout);
r = dup2(stdout_fd, STDOUT_FILENO);
always_assert(r >= 0);
close(stdout_fd);
}
long long total = 0;
kvstats puts, gets, scans, puts_per_sec, gets_per_sec, scans_per_sec;
for(i = 0; i < children; i++){
char buf[2048];
int cc = read(pipes[i], buf, sizeof(buf)-1);
assert(cc > 0);
buf[cc] = 0;
printf("%s", buf);
Json bufj = Json::parse(buf, buf + cc);
long long iv;
double dv;
if (bufj.to_i(iv))
total += iv;
else if (bufj.is_object()) {
if (bufj.get("ops", iv)
|| bufj.get("total", iv)
|| bufj.get("count", iv))
total += iv;
if (bufj.get("puts", iv))
puts.add(iv);
if (bufj.get("gets", iv))
gets.add(iv);
if (bufj.get("scans", iv))
scans.add(iv);
if (bufj.get("puts_per_sec", dv))
puts_per_sec.add(dv);
if (bufj.get("gets_per_sec", dv))
gets_per_sec.add(dv);
if (bufj.get("scans_per_sec", dv))
scans_per_sec.add(dv);
}
}
printf("total %lld\n", total);
puts.print_report("puts");
gets.print_report("gets");
scans.print_report("scans");
puts_per_sec.print_report("puts/s");
gets_per_sec.print_report("gets/s");
scans_per_sec.print_report("scans/s");
exit(0);
}
void
run_child(testrunner* test, int childno)
{
struct sockaddr_in sin;
int ret, yes = 1;
struct child c;
bzero(&c, sizeof(c));
c.childno = childno;
if(udpflag){
c.udp = 1;
c.s = socket(AF_INET, SOCK_DGRAM, 0);
} else {
c.s = socket(AF_INET, SOCK_STREAM, 0);
}
if (first_local_port) {
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(first_local_port + (childno % 48));
ret = ::bind(c.s, (struct sockaddr *) &sin, sizeof(sin));
if (ret < 0) {
perror("bind");
exit(1);
}
}
assert(c.s >= 0);
setsockopt(c.s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
if (udpflag && !share_server_port)
sin.sin_port = htons(first_server_port + (childno % 48));
else
sin.sin_port = htons(first_server_port);
sin.sin_addr.s_addr = inet_addr(serverip);
ret = connect(c.s, (struct sockaddr *) &sin, sizeof(sin));
if(ret < 0){
perror("connect");
exit(1);
}
c.conn = new KVConn(c.s, !udpflag);
kvtest_client client(c);
test->run(client);
checkasync(&c, 2);
delete c.conn;
close(c.s);
}
void KVConn::hard_check(int tryhard) {
masstree_precondition(inbufpos_ == inbuflen_);
if (parser_.empty()) {
inbufpos_ = inbuflen_ = 0;
for (auto x : oldinbuf_)
delete[] x;
oldinbuf_.clear();
} else if (inbufpos_ == inbufsz) {
oldinbuf_.push_back(inbuf_);
inbuf_ = new char[inbufsz];
inbufpos_ = inbuflen_ = 0;
}
if (tryhard == 1) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(infd_, &rfds);
struct timeval tv = {0, 0};
if (select(infd_ + 1, &rfds, NULL, NULL, &tv) <= 0)
return;
} else
kvflush(out_);
ssize_t r = read(infd_, inbuf_ + inbufpos_, inbufsz - inbufpos_);
if (r != -1)
inbuflen_ += r;
}
int
get(struct child *c, const Str &key, char *val, int max)
{
assert(c->seq0_ == c->seq1_);
unsigned sseq = c->seq1_;
++c->seq1_;
++c->nsent_;
c->conn->sendgetwhole(key, sseq);
c->conn->flush();
const Json& result = c->conn->receive();
always_assert(result && result[0] == sseq);
++c->seq0_;
if (!result[2])
return -1;
always_assert(result.size() == 3 && result[2].is_s()
&& result[2].as_s().length() <= max);
memcpy(val, result[2].as_s().data(), result[2].as_s().length());
return result[2].as_s().length();
}
// builtin aget callback: no check
void
nocheck(struct child *, struct async *, bool, const Str &)
{
}
// builtin aget callback: store string
void
asyncgetcb(struct child *, struct async *a, bool, const Str &val)
{
Str *sptr;
assert(a->wantedlen == sizeof(Str *));
memcpy(&sptr, a->wanted, sizeof(Str *));
sptr->len = std::min(sptr->len, val.len);
memcpy(const_cast<char *>(sptr->s), val.s, sptr->len);
}
// builtin aget callback: store string
void
asyncgetcb_int(struct child *, struct async *a, bool, const Str &val)
{
int *vptr;
assert(a->wantedlen == sizeof(int *));
memcpy(&vptr, a->wanted, sizeof(int *));
long x = 0;
if (val.len <= 0)
x = -1;
else
for (int i = 0; i < val.len; ++i)
if (val.s[i] >= '0' && val.s[i] <= '9')
x = (x * 10) + (val.s[i] - '0');
else {
x = -1;
break;
}
*vptr = x;
}
// default aget callback: check val against wanted
void
defaultget(struct child *, struct async *a, bool have_val, const Str &val)
{
// check that we got the expected value
int wanted_avail = std::min(a->wantedlen, int(sizeof(a->wanted)));
if (!have_val
|| a->wantedlen != val.len
|| memcmp(val.s, a->wanted, wanted_avail) != 0)
fprintf(stderr, "oops wanted %.*s(%d) got %.*s(%d)\n",
wanted_avail, a->wanted, a->wantedlen, val.len, val.s, val.len);
else {
always_assert(a->wantedlen == val.len);
always_assert(memcmp(val.s, a->wanted, wanted_avail) == 0);
}
}
// builtin aput/aremove callback: store status
void
asyncputcb(struct child *, struct async *a, int status)
{
int *sptr;
assert(a->wantedlen == sizeof(int *));
memcpy(&sptr, a->wanted, sizeof(int *));
*sptr = status;
}
// process any waiting replies to aget() and aput().
// force=0 means non-blocking check if anything waiting on socket.
// force=1 means wait for at least one reply.
// force=2 means wait for all pending (nw-nr) replies.
void
checkasync(struct child *c, int force)
{
while (c->seq0_ != c->seq1_) {
if (force)
c->conn->flush();
if (c->conn->check(force ? 2 : 1) > 0) {
const Json& result = c->conn->receive();
always_assert(result);
// is rseq in the nr..nw window?
// replies might arrive out of order if UDP
unsigned rseq = result[0].as_i();
always_assert(rseq - c->seq0_ < c->seq1_ - c->seq0_);
struct async *a = &c->a[rseq & (window - 1)];
always_assert(a->seq == rseq);
// advance the nr..nw window
always_assert(a->acked == 0);
a->acked = 1;
while (c->seq0_ != c->seq1_ && c->a[c->seq0_ & (window - 1)].acked)
++c->seq0_;
// might have been the last free slot,
// don't want to re-use it underfoot.
struct async tmpa = *a;
if(tmpa.cmd == Cmd_Get){
// this is a reply to a get
String s = result.size() > 2 ? result[2].as_s() : String();
if (tmpa.get_fn)
(tmpa.get_fn)(c, &tmpa, result.size() > 2, s);
} else if (tmpa.cmd == Cmd_Put || tmpa.cmd == Cmd_Replace) {
// this is a reply to a put
if (tmpa.put_fn)
(tmpa.put_fn)(c, &tmpa, result[2].as_i());
} else if(tmpa.cmd == Cmd_Scan){
// this is a reply to a scan
always_assert((result.size() - 2) / 2 <= tmpa.wantedlen);
} else if (tmpa.cmd == Cmd_Remove) {
// this is a reply to a remove
if (tmpa.remove_fn)
(tmpa.remove_fn)(c, &tmpa, result[2].as_i());
} else {
always_assert(0);
}
if (force < 2)
force = 0;
} else if (!force)
break;
}
}
// async get, checkasync() will eventually check reply
// against wanted.
void
aget(struct child *c, const Str &key, const Str &wanted, get_async_cb fn)
{
c->check_flush();
c->conn->sendgetwhole(key, c->seq1_);
if (c->udp)
c->conn->flush();
struct async *a = &c->a[c->seq1_ & (window - 1)];
a->cmd = Cmd_Get;
a->seq = c->seq1_;
a->get_fn = (fn ? fn : defaultget);
assert(key.len < int(sizeof(a->key)));
memcpy(a->key, key.s, key.len);
a->key[key.len] = 0;
a->wantedlen = wanted.len;
int wantedavail = std::min(wanted.len, int(sizeof(a->wanted)));
memcpy(a->wanted, wanted.s, wantedavail);
a->acked = 0;
++c->seq1_;
++c->nsent_;
}
void aget_col(struct child *c, const Str& key, int col, const Str& wanted,
get_async_cb fn)
{
c->check_flush();
c->conn->sendgetcol(key, col, c->seq1_);
if (c->udp)
c->conn->flush();
struct async *a = &c->a[c->seq1_ & (window - 1)];
a->cmd = Cmd_Get;
a->seq = c->seq1_;
a->get_fn = (fn ? fn : defaultget);
assert(key.len < int(sizeof(a->key)));
memcpy(a->key, key.s, key.len);
a->key[key.len] = 0;
a->wantedlen = wanted.len;
int wantedavail = std::min(wanted.len, int(sizeof(a->wanted)));
memcpy(a->wanted, wanted.s, wantedavail);
a->acked = 0;
++c->seq1_;
++c->nsent_;
}
void
aget(struct child *c, long ikey, long iwanted, get_async_cb fn)
{
quick_istr key(ikey), wanted(iwanted);
aget(c, key.string(), wanted.string(), fn);
}
int
put(struct child *c, const Str &key, const Str &val)
{
always_assert(c->seq0_ == c->seq1_);
unsigned int sseq = c->seq1_;