-
Notifications
You must be signed in to change notification settings - Fork 32
/
mc-crusher.c
1204 lines (1087 loc) · 36.2 KB
/
mc-crusher.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
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
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* mc-crusher - make the rabbit fear you
*
* https://github.com/dormando/mc-crusher
*
* Copyright 2011 Dormando. All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* Authors:
* dormando <[email protected]>
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <event.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include <stdbool.h>
#include <getopt.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <limits.h>
#include <sysexits.h>
#include <stddef.h>
#include <sys/stat.h>
#include <signal.h>
#ifdef USE_TLS
#include <openssl/ssl.h>
#include <poll.h>
#endif
// for pow() for zipf calculations.
#include <math.h>
#include "protocol_binary.h"
#include "pcg-basic.h"
#include "itoa_ljust.h"
#define SHARED_RBUF_SIZE 1024 * 64
#define SHARED_VALUE_SIZE 1024 * 1024
// avoiding some hacks for finding member size.
#define SOCK_MAX 100
char host_default[NI_MAXHOST] = "127.0.0.1";
char port_num_default[NI_MAXSERV] = "11211";
char sock_path_default[SOCK_MAX];
int alarm_fired = 0;
#ifdef USE_TLS
SSL_CTX *global_ctx;
#endif
enum conn_states {
conn_connecting = 0,
conn_sending,
conn_reading,
conn_sleeping,
};
enum conn_rand {
conn_rand_off = 0,
conn_rand_uniform,
conn_rand_zipf,
};
typedef struct _mc_thread {
pthread_t thread_id;
struct event_base *base;
unsigned char *shared_value;
unsigned char *shared_rbuf;
} mc_thread;
// data shared within a single template.
// a thread can have multiple templates in it, so key_prefix/value can't be
// part of mc_thread.
// TODO: Move more of the static values in here. counts/etc.
typedef struct _mc_tshared {
size_t key_prefix_len;
size_t cmd_postfix_len;
unsigned char key_prefix[284];
unsigned char cmd_postfix[1024];
unsigned char value[2048]; /* manually specified seed value */
} mc_tshared;
struct connection {
/* Owner thread */
mc_thread *t;
/* some same-template shared data */
mc_tshared *s;
/* host */
char host[NI_MAXHOST];
char port_num[NI_MAXSERV];
/* Event stuff */
int fd;
struct event ev;
enum conn_states state;
enum conn_states next_state;
short ev_flags;
#ifdef USE_TLS
SSL *ssl;
#endif
/* Counters, bits, flags for individual senders/getters. */
int mget_count; /* # of ascii mget keys to send at once */
int value_size;
int use_shared_value;
int wbuf_written;
int wbuf_towrite;
uint32_t expire;
uint32_t flags;
uint64_t pipelines; /* number of repeated commands per write */
int usleep; /* us to sleep between write runs */
uint64_t stop_after; /* run this many write events then stop */
/* Buffers */
uint64_t *cur_key;
uint64_t *write_count;
uint32_t key_count;
unsigned char *wbuf_pos;
/* random number handling */
pcg32_random_t rng; // every connection can have its own rng.
enum conn_rand rand; // randomized options for run_counter().
double zipf_skew;
double zipf_t; // precalc against the skew
/* time pacing */
struct timeval next_sleep;
struct timeval tosleep;
/* reader/writer function pointers */
void (*writer)(void *arg);
void (*reader)(void *arg);
/* helper function specific to the generic ascii writer */
int (*ascii_format)(struct connection *c);
int (*bin_format)(struct connection *c);
int (*bin_prep_cmd)(struct connection *c);
unsigned char wbuf[65536]; // putting this at the end to get more of the above into fewer cachelines.
};
static void client_handler(const int fd, const short which, void *arg);
static void sleep_handler(const int fd, const short which, void *arg);
static void setup_thread(mc_thread *t);
static void create_thread(mc_thread *t);
static void start_template(struct connection *template, int conns_tomake, bool use_sock);
static inline void run_write(struct connection *c);
static uint32_t zipf_sample(pcg32_random_t *rng, double t, double skew);
static double zipf_calc_t(uint32_t n, double skew);
static int update_conn_event(struct connection *c, const int new_flags)
{
if (c->ev_flags == new_flags) return 2;
if (event_del(&c->ev) == -1) return 0;
c->ev_flags = new_flags;
event_set(&c->ev, c->fd, new_flags, client_handler, (void *)c);
event_base_set(c->t->base, &c->ev);
if (event_add(&c->ev, 0) == -1) return 0;
return 1;
}
static int update_conn_event_sleep(struct connection *c)
{
struct timeval t = {.tv_sec = 0, .tv_usec = 0};
struct timeval now;
if (event_del(&c->ev) == -1) return 0;
c->ev_flags = 0; // clear event flags in case we ping-pong to other modes
evtimer_set(&c->ev, sleep_handler, (void *)c);
event_base_set(c->t->base, &c->ev);
gettimeofday(&now, NULL);
// every time we come into this loop, we've run once. which means we
// always have to advance the next_sleep timer.
if (c->next_sleep.tv_sec == 0) {
// initialize next_sleep as late as possible to avoid spamming.
gettimeofday(&c->next_sleep, NULL);
}
memcpy(&t, &c->next_sleep, sizeof(struct timeval));
timeradd(&t, &c->tosleep, &c->next_sleep);
timersub(&c->next_sleep, &now, &t);
// so far as I can tell, it treats times in the past as "Wake up
// immediately".
evtimer_add(&c->ev, &t);
return 1;
}
static void write_flat(struct connection *c, enum conn_states next_state) {
int written = 0;
#ifdef USE_TLS
written = SSL_write(c->ssl, c->wbuf + c->wbuf_written, c->wbuf_towrite);
#else
written = write(c->fd, c->wbuf + c->wbuf_written, c->wbuf_towrite);
#endif
if (written == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
update_conn_event(c, EV_READ | EV_WRITE | EV_PERSIST);
// the sender always checks for reads. not necessary to change?
c->state = conn_sending;
return;
} else {
perror("Write error to client");
exit(1);
return;
}
}
c->wbuf_towrite -= written;
if (c->wbuf_towrite > 0) {
update_conn_event(c, EV_READ | EV_WRITE | EV_PERSIST);
//fprintf(stderr, "Draining flat buffer %d by (%d)\n", c->wbuf_towrite, written);
c->wbuf_written += written;
c->state = conn_sending;
} else {
c->state = next_state;
if (c->state == conn_reading) {
update_conn_event(c, EV_READ | EV_PERSIST);
} else if (c->state == conn_sending) {
update_conn_event(c, EV_READ | EV_WRITE | EV_PERSIST);
} else if (c->state == conn_sleeping) {
update_conn_event_sleep(c);
}
}
}
static inline void run_counter(struct connection *c) {
++*c->write_count; // for limiting requests in a particular test
switch (c->rand) {
case conn_rand_off:
if (++*c->cur_key >= c->key_count) {
//fprintf(stdout, "Did %llu writes\n", (unsigned long long)c->key_count);
*c->cur_key = 0;
}
break;
case conn_rand_uniform:
*c->cur_key = pcg32_boundedrand_r(&c->rng, c->key_count);
break;
case conn_rand_zipf:
*c->cur_key = zipf_sample(&c->rng, c->zipf_t, c->zipf_skew);
break;
}
}
// adapted from: https://medium.com/@jasoncrease/rejection-sampling-the-zipf-distribution-6b359792cffa
static uint32_t zipf_sample(pcg32_random_t *rng, double t, double skew) {
double inv_B;
double X, R;
double inv_skew = 1.0 / (1.0 - skew);
for (;;) {
// always need two random samples, so exploit some cache coherency and
// grab them at the same time.
double rand_b = pcg32_double_r(rng);
double rand_y = pcg32_double_r(rng);
double t_b = rand_b * t;
// inv cdf for b.
if (t_b <= 1) {
inv_B = t_b;
} else {
inv_B = pow(t_b * (1.0 - skew) + skew, inv_skew);
}
X = floor(inv_B - 1.0);
R = pow(X, -skew) /
((X <= 1.0 ? 1.0 / t : pow(inv_B, -skew) / t) * t);
if (rand_y < R) {
return (uint32_t)X;
}
}
}
static double zipf_calc_t(uint32_t n, double skew) {
return (pow((double)n, 1.0 - skew) - skew) / (1 - skew);
}
/* === BINARY PROTOCOL === */
static int bin_key_format(struct connection *c) {
char *p = c->wbuf_pos;
memcpy(p, c->s->key_prefix, c->s->key_prefix_len);
p = itoa_u64(*c->cur_key, p + c->s->key_prefix_len);
return (p - (char *)c->wbuf_pos);
}
// can generalize this a bit further.
static int bin_prep_getq(struct connection *c) {
protocol_binary_request_get *pkt = (protocol_binary_request_get *)c->wbuf_pos;
pkt->message.header.request.opcode = PROTOCOL_BINARY_CMD_GETQ;
int l = sizeof(protocol_binary_request_get);
c->wbuf_pos += l;
return l;
}
static int bin_prep_get(struct connection *c) {
protocol_binary_request_get *pkt = (protocol_binary_request_get *)c->wbuf_pos;
pkt->message.header.request.opcode = PROTOCOL_BINARY_CMD_GET;
int l = sizeof(protocol_binary_request_get);
c->wbuf_pos += l;
return l;
}
static int bin_prep_set(struct connection *c) {
protocol_binary_request_set *pkt = (protocol_binary_request_set *)c->wbuf_pos;
pkt->message.header.request.opcode = PROTOCOL_BINARY_CMD_SET;
pkt->message.header.request.extlen = 8; /* flags + exptime */
pkt->message.body.flags = htonl(c->flags);
pkt->message.body.expiration = htonl(c->expire);
int l = sizeof(protocol_binary_request_header) + 8;
c->wbuf_pos += l;
return l;
}
/* slightly crazy; since bin_prep_set changes wbuf_pos create the packet
* pointer first, run original prep, then switch the command out.
*/
static int bin_prep_setq(struct connection *c) {
protocol_binary_request_set *pkt = (protocol_binary_request_set *)c->wbuf_pos;
int l = bin_prep_set(c);
pkt->message.header.request.opcode = PROTOCOL_BINARY_CMD_SETQ;
// Continue to send since we don't expect to read anything.
c->next_state = conn_sending;
return l;
}
static int bin_prep_touch(struct connection *c) {
protocol_binary_request_touch *pkt = (protocol_binary_request_touch *)c->wbuf_pos;
pkt->message.header.request.opcode = PROTOCOL_BINARY_CMD_TOUCH;
pkt->message.header.request.extlen = 4; /* exptime */
pkt->message.body.expiration = htonl(c->expire);
int l = sizeof(protocol_binary_request_header) + 4;
c->wbuf_pos += l;
return l;
}
static void bin_write_flat_to_client(void *arg) {
struct connection *c = arg;
protocol_binary_request_header *pkt = (protocol_binary_request_header *)c->wbuf_pos;
memset(pkt, 0, sizeof(protocol_binary_request_header));
pkt->request.magic = PROTOCOL_BINARY_REQ;
c->bin_prep_cmd(c);
// FIXME: move wbuf_pos here instead of in the func.
int keylen = c->bin_format(c);
c->wbuf_pos += keylen;
int bodylen = keylen + pkt->request.extlen;
pkt->request.keylen = htons(keylen);
if (c->value_size) {
bodylen += c->value_size;
if (c->use_shared_value) {
memcpy(c->wbuf_pos, c->t->shared_value, c->value_size);
} else {
memcpy(c->wbuf_pos, c->s->value, c->value_size);
}
c->wbuf_pos += c->value_size;
}
pkt->request.bodylen = htonl(bodylen);
run_counter(c);
}
/* === ASCII PROTOCOL TESTS === */
static int ascii_mget_format(struct connection *c) {
char *p = c->wbuf_pos;
memcpy(p, c->s->key_prefix, c->s->key_prefix_len);
p = itoa_u64(*c->cur_key, p + c->s->key_prefix_len);
*p = ' ';
return (p - (char *)c->wbuf_pos) + 1;
}
static void ascii_write_flat_mget_to_client(void *arg) {
struct connection *c = arg;
int i;
memcpy(c->wbuf_pos, "get ", 4);
c->wbuf_pos += 4;
for (i = 0; i < c->mget_count; i++) {
c->wbuf_pos += ascii_mget_format(c);
run_counter(c);
}
c->wbuf_pos[0] = '\r';
c->wbuf_pos[1] = '\n';
c->wbuf_pos += 2;
}
static int ascii_set_format(struct connection *c) {
char *p = c->wbuf_pos;
memcpy(p, c->s->key_prefix, c->s->key_prefix_len);
p = itoa_u64(*c->cur_key, p + c->s->key_prefix_len);
*p = ' ';
p = itoa_u32(c->flags, p+1);
*p = ' ';
p = itoa_u32(c->expire, p+1);
*p = ' ';
p = itoa_u32(c->value_size, p+1);
*p = '\r';
*(p+1) = '\n';
return (p - (char *)c->wbuf_pos) + 2;
}
static int ascii_incrdecr_format(struct connection *c) {
char *p = c->wbuf_pos;
memcpy(p, c->s->key_prefix, c->s->key_prefix_len);
p = itoa_u64(*c->cur_key, p + c->s->key_prefix_len);
*p = ' ';
*(p+1) = '1';
*(p+2) = '\r';
*(p+3) = '\n';
return (p - (char *)c->wbuf_pos) + 4;
}
static int ascii_touch_format(struct connection *c) {
char *p = c->wbuf_pos;
memcpy(p, c->s->key_prefix, c->s->key_prefix_len);
p = itoa_u64(*c->cur_key, p + c->s->key_prefix_len);
*p = ' ';
p = itoa_u32(c->expire, p+1);
*p = '\r';
*(p+1) = '\n';
return (p - (char *)c->wbuf_pos) + 2;
}
// get, delete, and so on.
static int ascii_single_format(struct connection *c) {
char *p = c->wbuf_pos;
memcpy(p, c->s->key_prefix, c->s->key_prefix_len);
p = itoa_u64(*c->cur_key, p + c->s->key_prefix_len);
*p = ' ';
*(p+1) = '\r';
*(p+2) = '\n';
return (p - (char *)c->wbuf_pos) + 3;
}
// meta commands: cmd key flags tokens
static int ascii_metacmd_format(struct connection *c) {
char *p = c->wbuf_pos;
memcpy(p, c->s->key_prefix, c->s->key_prefix_len);
p = itoa_u64(*c->cur_key, p + c->s->key_prefix_len);
// now the flags/tokens
if (c->s->cmd_postfix_len) {
*p = ' ';
p++;
memcpy(p, c->s->cmd_postfix, c->s->cmd_postfix_len);
p += c->s->cmd_postfix_len;
}
*(p) = '\r';
*(p+1) = '\n';
return (p - (char *)c->wbuf_pos) + 2;
}
// for fast-writing to wbuf
// WARNING: sets can easily blow this up :(
static void ascii_write_flat_to_client(void *arg) {
struct connection *c = arg;
c->wbuf_pos += c->ascii_format(c);
if (c->value_size) {
if (c->use_shared_value) {
memcpy(c->wbuf_pos, c->t->shared_value, c->value_size);
} else {
memcpy(c->wbuf_pos, c->s->value, c->value_size);
}
c->wbuf_pos += c->value_size;
c->wbuf_pos[0] = '\r';
c->wbuf_pos[1] = '\n';
c->wbuf_pos += 2;
}
run_counter(c);
}
/* === READERS === */
// Seems like leaving some data in SSL_read causes wobbles in performance.
// SSL_read() returns after each individual TLS frame read, not with a full read
// buffer.
#ifdef USE_TLS
#define READ_MIN 1
#else
#define READ_MIN 2096
#endif
static void read_from_client(void *arg) {
struct connection *c = arg;
int rbytes = 0;
for (;;) {
#ifdef USE_TLS
rbytes = SSL_read(c->ssl, c->t->shared_rbuf, SHARED_RBUF_SIZE);
#else
rbytes = read(c->fd, c->t->shared_rbuf, SHARED_RBUF_SIZE);
#endif
if (rbytes == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
} else {
perror("Read error from client");
}
}
if (rbytes < READ_MIN)
break; /* don't call read() again unless we may get data */
}
}
/* === HANDLERS === */
static void sleep_handler(const int fd, const short which, void *arg) {
struct connection *c = (struct connection *)arg;
c->next_state = conn_sleeping;
c->reader(c);
run_write(c);
}
static inline void run_write(struct connection *c) {
int i;
c->wbuf_pos = c->wbuf;
for (i = 0; i < c->pipelines; i++) {
c->writer(c);
}
{
// not using iovecs, save some libc/kernel looping.
c->wbuf_towrite = c->wbuf_pos - (unsigned char *)&c->wbuf;
c->wbuf_written = 0;
//fprintf(stderr, "WBUF towrite: %d\n", c->wbuf_towrite);
write_flat(c, c->next_state);
}
if (c->stop_after && *c->write_count >= c->stop_after) {
event_del(&c->ev);
}
}
static void client_handler(const int fd, const short which, void *arg) {
struct connection *c = (struct connection *)arg;
int err = 0;
socklen_t errsize = sizeof(err);
int written = 0;
switch (c->state) {
case conn_connecting:
if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &errsize) < 0) {
return;
}
if (err != 0) {
return;
}
c->state = conn_sending;
update_conn_event(c, EV_READ | EV_PERSIST);
case conn_sending:
if (which & EV_READ) {
c->reader(c);
}
if (which & EV_WRITE) {
if (c->wbuf_towrite) {
write_flat(c, c->next_state);
}
if (c->wbuf_towrite == 0) {
run_write(c);
}
}
break;
case conn_reading:
c->reader(c);
c->state = conn_sending;
if (c->wbuf_towrite <= 0)
run_write(c);
break;
}
}
/* === TLS code === */
#ifdef USE_TLS
// Can add/ignore flags/settings explicitly here.
static int ssl_init(void) {
OPENSSL_init_ssl(0, NULL);
global_ctx = SSL_CTX_new(TLS_client_method());
if (global_ctx == NULL) {
fprintf(stderr, "failed to initialize OpenSSL\n");
exit(1);
}
int flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1;
SSL_CTX_set_options(global_ctx, flags);
fprintf(stderr, "Initialized OpenSSL\n");
}
#endif
#define U_PER_S 1000000
static void timeval_split(const uint64_t in, long int *outs, long int *outu) {
if (in >= U_PER_S) {
*outs = in / U_PER_S;
*outu = in - (*outs * U_PER_S);
} else {
*outs = 0;
*outu = in;
}
}
static int new_connection(struct connection *t, char *sock_path)
{
int sock;
struct addrinfo *ai;
struct addrinfo *ai_next;
struct addrinfo hints = { .ai_flags = AI_PASSIVE,
.ai_family = AF_UNSPEC };
struct sockaddr_un un_addr;
int flags = 1;
int error;
struct connection *c = (struct connection *)malloc(sizeof(struct connection));
memcpy(c, t, sizeof(struct connection));
// no reason to avoid initializing an rng. this gives us a sequence unique
// to the memory address of this particular connection.
// could also mix or adjust time, but according to the PCG documentation
// this shouldn't matter and we're not after secure randomization.
// TODO: Should find some way to make this deterministic.
pcg32_srandom_r(&c->rng, time(NULL), (intptr_t)c);
if (t->rand == conn_rand_zipf) {
c->zipf_t = zipf_calc_t(c->key_count, c->zipf_skew);
}
if (sock_path == NULL) {
error = getaddrinfo(c->host, c->port_num, &hints, &ai);
if (error != 0) {
if (error != EAI_SYSTEM) {
fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error));
} else {
perror("getaddrinfo()");
}
freeaddrinfo(ai);
return -1;
}
for (ai_next = ai; ai_next; ai_next = ai_next->ai_next) {
sock = socket(ai_next->ai_family, ai_next->ai_socktype, ai_next->ai_protocol);
if (sock == -1) {
perror("socket");
continue;
} else {
break;
}
}
if (sock < 0) {
fprintf(stderr, "getaddrinfo failed to provide any valid addresses: %s[%s]\n",
c->host, c->port_num);
freeaddrinfo(ai);
return -1;
}
if ( (flags = fcntl(sock, F_GETFL, 0)) < 0 ||
fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
close(sock);
freeaddrinfo(ai);
return -1;
}
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
if (connect(sock, ai_next->ai_addr, ai_next->ai_addrlen) == -1) {
if (errno != EINPROGRESS) {
close(sock);
freeaddrinfo(ai);
return -1;
}
}
freeaddrinfo(ai);
} else {
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
return -1;
}
if ( (flags = fcntl(sock, F_GETFL, 0)) < 0 ||
fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
close(sock);
return -1;
}
memset(&un_addr, 0, sizeof(un_addr));
un_addr.sun_family = AF_UNIX;
strncpy(un_addr.sun_path, sock_path, SOCK_MAX-1);
if (connect(sock, (struct sockaddr *)&un_addr, sizeof(un_addr)) == -1) {
if (errno != EINPROGRESS) {
close(sock);
perror("Failed to connect to unix socket");
return -1;
}
}
}
c->fd = sock;
c->state = conn_connecting;
c->ev_flags = EV_WRITE;
#ifdef USE_TLS
c->ssl = SSL_new(global_ctx);
SSL_set_fd(c->ssl, sock);
// we want the benchmark to generally start all at once. I don't want to
// spread the handshake stuff through the code either.
for (;;) {
struct pollfd p = { .fd = sock };
int ret = SSL_connect(c->ssl);
if (ret == 1) {
break;
}
switch (SSL_get_error(c->ssl, ret)) {
case SSL_ERROR_WANT_READ:
p.events = POLLIN;
break;
case SSL_ERROR_WANT_WRITE:
p.events = POLLOUT;
break;
default:
perror("Unhandled OpenSSL error while connecting");
exit(1);
}
poll(&p, 1, 5000 * 1000);
if ((p.revents & (POLLIN|POLLOUT)) == 0) {
fprintf(stderr, "Socket hangup while waiting for SSL connection\n");
exit(1);
}
}
#endif
if (c->usleep) {
// spread out the initial wakeup times
long int initsleep = pcg32_boundedrand_r(&c->rng, c->usleep);
long int initsleep_s = 0;
timeval_split(c->usleep, &c->tosleep.tv_sec, &c->tosleep.tv_usec);
timeval_split(initsleep, &initsleep_s, &initsleep);
struct timeval t = {.tv_sec = initsleep_s, .tv_usec = initsleep};
evtimer_set(&c->ev, sleep_handler, (void *)c);
event_base_set(c->t->base, &c->ev);
evtimer_add(&c->ev, &t);
} else {
event_set(&c->ev, sock, c->ev_flags, client_handler, (void *)c);
event_base_set(c->t->base, &c->ev);
event_add(&c->ev, NULL);
}
return sock;
}
/* Get a little verbose to avoid a big if/else if tree */
static void parse_config_line(mc_thread *main_thread, char *line, bool use_sock) {
char *in_progress, *token;
struct connection template;
int conns_tomake = 1;
int newsock;
int i, x;
char *tmp;
char *sender = NULL;
int add_space = 0;
int new_thread = 0;
char key_prefix_tmp[270];
char cmd_postfix_tmp[1024];
enum {
SEND = 0,
RECV,
TIME,
USLEEP,
COUNT,
CONNS,
EXPIRE,
FLAGS,
KEY_PREFIX,
CMD_POSTFIX,
KEY_LEN,
KEY_GENERATE,
VALUE_SIZE,
VALUE_RANGE,
VALUE_RANGE_STEP,
MGET_COUNT,
VALUE,
LIVE_RAND,
LIVE_RAND_ZIPF,
STOP_AFTER,
KEY_COUNT,
HOST,
PORT,
THREAD,
PIPELINES,
ZIPF_SKEW
};
char *const key_options[] = {
[SEND] = "send",
[RECV] = "recv",
[TIME] = "time",
[USLEEP] = "usleep",
[COUNT] = "count",
[CONNS] = "conns",
[EXPIRE] = "expire",
[FLAGS] = "flags",
[KEY_PREFIX] = "key_prefix",
[CMD_POSTFIX] = "cmd_postfix",
[KEY_LEN] = "key_len",
[KEY_GENERATE] = "key_generate",
[VALUE_SIZE] = "value_size",
[VALUE_RANGE] = "value_range",
[VALUE_RANGE_STEP] = "value_range_step",
[MGET_COUNT] = "mget_count",
[VALUE] = "value",
[LIVE_RAND] = "live_rand",
[LIVE_RAND_ZIPF] = "live_rand_zipf",
[STOP_AFTER] = "stop_after",
[KEY_COUNT] = "key_count",
[HOST] = "host",
[PORT] = "port",
[THREAD] = "thread",
[PIPELINES] = "pipelines",
[ZIPF_SKEW] = "zipf_skew",
NULL
};
memset(&template, 0, sizeof(struct connection));
/* Set defaults into template */
strcpy(key_prefix_tmp, "foo");
cmd_postfix_tmp[0] = 0;
template.t = main_thread;
template.mget_count = 2;
template.value_size = 0;
template.use_shared_value = 1;
template.wbuf_written = 0;
template.wbuf_towrite = 0;
template.key_count = 200000;
template.rand = conn_rand_off;
template.zipf_skew = 0.25; // default to a relatively gentle curve.
template.pipelines = 1;
template.expire = 0;
template.flags = 0;
strcpy(template.host, host_default);
strcpy(template.port_num, port_num_default);
template.next_state = conn_reading;
template.s = calloc(1, sizeof(mc_tshared));
template.s->value[0] = '\0';
/* Chomp the ending newline */
tmp = rindex(line, '\n');
if (tmp != NULL)
*tmp = '\0';
while ((token = strtok_r(line, ",", &in_progress)) != NULL) {
int key = 0;
char *value = NULL;
value = index(token, '=');
*value = '\0';
value++;
line = NULL; /* lazy */
while (key_options[key] != NULL) {
if (strcmp(token, key_options[key]) == 0)
break;
key++;
}
fprintf(stderr, "id %d for key %s value %s\n", key, token, value);
switch (key) {
case SEND:
sender = value;
break;
case RECV:
template.reader = read_from_client;
break;
case CONNS:
conns_tomake = atoi(value);
break;
case COUNT:
break;
case EXPIRE:
// TODO: import strtoul wrappers
template.expire = atoi(value);
break;
case FLAGS:
template.flags = atoi(value);
break;
case KEY_PREFIX:
strcpy(key_prefix_tmp, value);
break;
case CMD_POSTFIX:
strcpy(cmd_postfix_tmp, value);
break;
case MGET_COUNT:
template.mget_count = atoi(value);
break;
case VALUE_SIZE:
template.value_size = atoi(value);
break;
case VALUE:
strcpy(template.s->value, value);
template.value_size = strlen(value);
template.use_shared_value = 0;
break;
case LIVE_RAND:
template.rand = conn_rand_uniform;
break;
case LIVE_RAND_ZIPF:
template.rand = conn_rand_zipf;
break;
case STOP_AFTER:
template.stop_after = atoi(value);
break;
case KEY_COUNT:
template.key_count = atoi(value);
break;
case HOST:
strcpy(template.host, value);
break;
case PORT:
strcpy(template.port_num, value);
break;
case USLEEP:
template.usleep = atoi(value);
break;
case THREAD:
new_thread = atoi(value);
break;
case PIPELINES:
template.pipelines = atoi(value);
break;
case ZIPF_SKEW:
template.zipf_skew = strtod(value, NULL);
break;
}
}
if (strcmp(sender, "ascii_get") == 0) {
template.writer = ascii_write_flat_to_client;
template.ascii_format = ascii_single_format;
sprintf(template.s->key_prefix, "get %s", key_prefix_tmp);
} else if (strcmp(sender, "ascii_set") == 0) {
template.writer = ascii_write_flat_to_client;
template.ascii_format = ascii_set_format;
sprintf(template.s->key_prefix, "set %s", key_prefix_tmp);
} else if (strcmp(sender, "ascii_mget") == 0) {
template.writer = ascii_write_flat_mget_to_client;
sprintf(template.s->key_prefix, "%s", key_prefix_tmp);
} else if (strcmp(sender, "ascii_incr") == 0) {
template.writer = ascii_write_flat_to_client;
template.ascii_format = ascii_incrdecr_format;
sprintf(template.s->key_prefix, "incr %s", key_prefix_tmp);
} else if (strcmp(sender, "ascii_delete") == 0) {
template.writer = ascii_write_flat_to_client;
template.ascii_format = ascii_single_format;
sprintf(template.s->key_prefix, "delete %s", key_prefix_tmp);
} else if (strcmp(sender, "ascii_decr") == 0) {
template.writer = ascii_write_flat_to_client;
template.ascii_format = ascii_incrdecr_format;
sprintf(template.s->key_prefix, "decr %s", key_prefix_tmp);
} else if (strcmp(sender, "ascii_touch") == 0) {
template.writer = ascii_write_flat_to_client;
template.ascii_format = ascii_touch_format;
sprintf(template.s->key_prefix, "touch %s", key_prefix_tmp);
} else if (strcmp(sender, "ascii_mg") == 0) {
template.writer = ascii_write_flat_to_client;
template.ascii_format = ascii_metacmd_format;
sprintf(template.s->key_prefix, "mg %s", key_prefix_tmp);
sprintf(template.s->cmd_postfix, "%s", cmd_postfix_tmp);
} else if (strcmp(sender, "bin_get") == 0) {
template.writer = bin_write_flat_to_client;
template.bin_prep_cmd = bin_prep_get;
template.bin_format = bin_key_format;
strcpy(template.s->key_prefix, key_prefix_tmp);
} else if (strcmp(sender, "bin_getq") == 0) {
template.writer = bin_write_flat_to_client;
template.bin_prep_cmd = bin_prep_getq;
template.bin_format = bin_key_format;
strcpy(template.s->key_prefix, key_prefix_tmp);
} else if (strcmp(sender, "bin_set") == 0) {
template.writer = bin_write_flat_to_client;