forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtclient.cc
1820 lines (1638 loc) · 42.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-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.
*/
//
// kvc: key/value client
//
// cc -O -o kvc kvc.c kvio.c
//
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <limits.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";
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 500
int window = MAXWINDOW;
#define SEQMOD 10000000
struct child {
int s;
int udp; // 1 -> udp, 0 -> tcp
KVConn *conn;
struct async a[MAXWINDOW];
long long nw; // # sent
long long nr; // # we're seen replies for
int childno;
};
void aget(struct child *, const Str &key, const Str &wanted, get_async_cb fn);
void ascan(struct child *c, int numpairs, const char *key);
void child(void (*fn)(struct child *), int childno);
void checkasync(struct child *c, int force);
void sync_rw1(struct child *);
void rw1(struct child *);
void udp1(struct child *);
void rw2(struct child *);
void rw3(struct child *);
void rw4(struct child *);
void rw5(struct child *);
void rw16(struct child *);
void r1(struct child *);
void w1(struct child *);
void w1b(struct child *);
void wd1(struct child *);
void wd1m1(struct child *);
void wd1m2(struct child *);
void wd1check(struct child *);
void wd1m1check(struct child *);
void wd1m2check(struct child *);
void wd2(struct child *);
void wd2check(struct child *);
void tri1(struct child *);
void tri1check(struct child *);
void u1(struct child *);
void same(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 *);
void wscale(struct child *);
void ruscale_init(struct child *);
void rscale(struct child *);
void uscale(struct child *);
void long_go(struct child *);
void long_init(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;
static int first_seed = 0;
static int rscale_partsz = 0;
static int getratio = -1;
static int minkeyletter = '0';
static int maxkeyletter = '9';
static struct {
const char *name;
void (*fn)(struct child *);
} tests[] = {
{ "sync_rw1", sync_rw1 },
{ "rw1", rw1 },
{ "udp1", udp1 },
{ "rw2", rw2 },
{ "rw3", rw3 },
{ "rw4", rw4 },
{ "rw5", rw5},
{ "rw16", rw16 },
{ "r1", r1 },
{ "w1", w1 },
{ "w1b", w1b },
{ "u1", u1 },
{ "wd1", wd1 },
{ "wd1m1", wd1m1 },
{ "wd1m2", wd1m2 },
{ "wd1check", wd1check },
{ "wd1m1check", wd1m1check },
{ "wd1m2check", wd1m2check },
{ "wd2", wd2 },
{ "wd2check", wd2check },
{ "tri1", tri1 },
{ "tri1check", tri1check },
{ "same", same },
{ "over1", over1 },
{ "over2", over2 },
{ "rec1", rec1 },
{ "rec2", rec2 },
{ "cpa", cpa },
{ "cpb", cpb },
{ "cpc", cpc },
{ "cpd", cpd },
{ "volt1a", volt1a },
{ "volt1b", volt1b },
{ "volt2a", volt2a },
{ "volt2b", volt2b },
{ "scantest", scantest },
{ "wscale", wscale},
{ "ruscale_init", ruscale_init},
{ "rscale", rscale},
{ "uscale", uscale},
{ "long_init", long_init},
{ "long_go", long_go},
{ 0, 0 }
};
void
usage()
{
fprintf(stderr, "Usage: kvc [-s serverip] [-w window] [--udp] "\
"[-j nchildren] [-d duration] [--ssp] [--flp first_local_port] "\
"[--fsp first_server_port] [-i json_input]");
int i;
for(i = 0; tests[i].name; i++)
fprintf(stderr, "%s|", tests[i].name);
fprintf(stderr, "\n");
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_ValInt, 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;
int 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)) >= 0) {
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.i;
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:
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:
minkeyletter = clp->vstr[0];
break;
case opt_maxkeyletter:
maxkeyletter = clp->vstr[0];
break;
case opt_nofork:
dofork = !clp->negated;
break;
case Clp_NotOption: {
int j;
for (j = 0; tests[j].name; j++)
if (strcmp(clp->vstr, tests[j].name) == 0)
break;
if (tests[j].name == 0)
usage();
test = j;
}
}
}
if(children < 1 || (children != 1 && !dofork))
usage();
printf("%s, w %d, test %s, children %d\n",
udpflag ? "udp" : "tcp", window,
tests[test].name, children);
if (dofork) {
for(i = 0; i < children; i++){
int ptmp[2];
int r = pipe(ptmp);
mandatory_assert(r == 0);
fflush(stdout);
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));
child(tests[test].fn, 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
child(tests[test].fn, 0);
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
child(void (*fn)(struct child *), 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);
(*fn)(&c);
checkasync(&c, 2);
delete c.conn;
close(c.s);
}
int
get(struct child *c, const Str &key, char *val, int max)
{
assert(c->nr == c->nw);
unsigned int sseq = c->nw % SEQMOD;
c->nw++;
c->nr++;
c->conn->sendgetwhole(key, sseq);
c->conn->flush();
unsigned int rseq = 0;
vector<string> row;
int r = c->conn->recvget(row, &rseq, false);
mandatory_assert(r == 0);
mandatory_assert(rseq == sseq);
if (row.size() == 0)
return -1;
mandatory_assert(row.size() == 1 && row[0].length() <= (unsigned)max);
memcpy(val, row[0].data(), row[0].length());
return row[0].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 {
mandatory_assert(a->wantedlen == val.len);
mandatory_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->nw > c->nr){
if(force)
c->conn->flush();
if(kvcheck(c->conn->in, force ? 2 : 1) > 0){
unsigned int rseq = 0;
c->conn->readseq(&rseq);
// is rseq in the nr..nw window?
// replies might arrive out of order if UDP
int nn;
for(nn = c->nr; nn < c->nw; nn++)
if((int) rseq == (nn % SEQMOD))
break;
if(nn >= c->nw)
fprintf(stderr, "rseq %d nr %qd nw %qd\n", rseq, c->nr, c->nw);
mandatory_assert(nn < c->nw);
struct async *a = &c->a[nn % window];
mandatory_assert(a->seq == rseq);
// advance the nr..nw window
mandatory_assert(a->acked == 0);
a->acked = 1;
while(c->nr < c->nw && c->a[c->nr % window].acked)
c->nr += 1;
// 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
vector<string> row;
int r = c->conn->recvget(row, NULL, true);
mandatory_assert(r == 0);
Str s = (row.size() ? Str(row[0].data(), row[0].length()) : Str());
if (tmpa.get_fn)
(tmpa.get_fn)(c, &tmpa, row.size(), s);
} else if(tmpa.cmd == Cmd_Put){
// this is a reply to a put
int r = c->conn->recvput(NULL, true);
mandatory_assert(r >= 0);
} else if(tmpa.cmd == Cmd_Put_Status){
// this is a reply to a put
int status;
int r = c->conn->recvputstatus(&status, NULL, true);
mandatory_assert(r >= 0);
if (tmpa.put_fn)
(tmpa.put_fn)(c, &tmpa, status);
} else if(tmpa.cmd == Cmd_Scan){
// this is a reply to a scan
vector<string> keys;
vector< vector<string> > rows;
int r = c->conn->recvscan(keys, rows, NULL, true);
mandatory_assert(r == 0);
mandatory_assert(keys.size() <= (unsigned)tmpa.wantedlen);
} else if (tmpa.cmd == Cmd_Remove) {
// this is a reply to a remove
int status;
int r = c->conn->recvremove(&status, NULL, true);
mandatory_assert(r == 0);
if (tmpa.remove_fn)
(tmpa.remove_fn)(c, &tmpa, status);
} else {
mandatory_assert(0);
}
if(force < 2)
force = 0;
} else if(force == 0){
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)
{
if((c->nw % (window/2)) == 0)
c->conn->flush();
while(c->nw - c->nr >= window)
checkasync(c, 1);
c->conn->sendgetwhole(key, c->nw % SEQMOD);
if (c->udp)
c->conn->flush();
struct async *a = &c->a[c->nw % window];
a->cmd = Cmd_Get;
a->seq = c->nw % SEQMOD;
a->get_fn = (fn ? fn : defaultget);
assert(key.len < int(sizeof(a->key)) - 1);
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->nw += 1;
}
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)
{
mandatory_assert(c->nw == c->nr);
unsigned int sseq = key.s[0]; // XXX
c->conn->sendputwhole(key, val, sseq);
c->conn->flush();
unsigned int rseq = 0;
int ret = c->conn->recvput(&rseq, false);
mandatory_assert(ret == 0);
mandatory_assert(rseq == sseq);
return 0;
}
void
aput(struct child *c, const Str &key, const Str &val,
put_async_cb fn = 0, const Str &wanted = Str())
{
if((c->nw % (window/2)) == 0)
c->conn->flush();
while(c->nw - c->nr >= window)
checkasync(c, 1);
c->conn->sendputwhole(key, val, c->nw % SEQMOD, fn != 0);
if (c->udp)
c->conn->flush();
struct async *a = &c->a[c->nw % window];
a->cmd = (fn != 0 ? Cmd_Put_Status : Cmd_Put);
a->seq = c->nw % SEQMOD;
assert(key.len < int(sizeof(a->key)) - 1);
memcpy(a->key, key.s, key.len);
a->key[key.len] = 0;
a->put_fn = fn;
if (fn) {
assert(wanted.len <= int(sizeof(a->wanted)));
a->wantedlen = wanted.len;
memcpy(a->wanted, wanted.s, wanted.len);
} else {
a->wantedlen = -1;
a->wanted[0] = 0;
}
a->acked = 0;
c->nw += 1;
}
bool
remove(struct child *c, const Str &key)
{
mandatory_assert(c->nw == c->nr);
unsigned int sseq = key.s[0]; // XXX
c->conn->sendremove(key, sseq);
c->conn->flush();
unsigned int rseq = 0;
int status = 0;
int ret = c->conn->recvremove(&status, &rseq, false);
mandatory_assert(ret == 0);
mandatory_assert(rseq == sseq);
return status;
}
void
aremove(struct child *c, const Str &key, remove_async_cb fn)
{
if((c->nw % (window/2)) == 0)
c->conn->flush();
while(c->nw - c->nr >= window)
checkasync(c, 1);
c->conn->sendremove(key, c->nw % SEQMOD);
if (c->udp)
c->conn->flush();
struct async *a = &c->a[c->nw % window];
a->cmd = Cmd_Remove;
a->seq = c->nw % SEQMOD;
assert(key.len < int(sizeof(a->key)) - 1);
memcpy(a->key, key.s, key.len);
a->key[key.len] = 0;
a->acked = 0;
a->remove_fn = fn;
c->nw += 1;
}
void
ascan(struct child *c, int numpairs, const char *key)
{
if((c->nw % (window/2)) == 0)
c->conn->flush();
while(c->nw - c->nr >= window)
checkasync(c, 1);
c->conn->sendscanwhole(numpairs, key, c->nw % SEQMOD);
if (c->udp)
c->conn->flush();
struct async *a = &c->a[c->nw % window];
a->cmd = Cmd_Scan;
a->seq = c->nw % SEQMOD;
strncpy(a->key, key, sizeof(a->key));
a->wantedlen = numpairs;
a->acked = 0;
c->nw += 1;
}
struct kvtest_client {
kvtest_client(struct child *c, int id)
: c_(c), id_(id) {
}
int id() const {
return id_;
}
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;
}
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_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);
mandatory_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_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.substring(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);
}
}
void report(const Json &result) {
if (!quiet) {
lcdf::StringAccum sa;
double dv;
if (result.count("puts"))
sa << " total " << result.get("puts");
if (result.get("puts_per_sec", dv))
sa.snprintf(100, " %.0f put/s", dv);