WIP
[m6w6/libmemcached] / contrib / bin / memaslap / ms_main.c
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <getopt.h>
22 #include <limits.h>
23
24 #if defined(HAVE_SYS_TIME_H)
25 # include <sys/time.h>
26 #endif
27 #include <time.h>
28
29 #include "ms_atomic.h"
30 #include "ms_sigsegv.h"
31 #include "ms_setting.h"
32 #include "ms_thread.h"
33
34 /* global structure */
35 ms_global_t ms_global;
36
37 /* global stats information structure */
38 ms_stats_t ms_stats;
39
40 /* global statistic structure */
41 ms_statistic_t ms_statistic;
42
43 #define PROGRAM_NAME "memaslap"
44 #define PROGRAM_DESCRIPTION "Generates workload against memcached servers."
45
46 #ifdef __sun
47 /* For some odd reason the option struct on solaris defines the argument
48 * as char* and not const char*
49 */
50 # define OPTIONSTRING char *
51 #else
52 # define OPTIONSTRING const char *
53 #endif
54
55 /* options */
56 static struct option long_options[] = {
57 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
58 {(OPTIONSTRING) "threads", required_argument, NULL, OPT_THREAD_NUMBER},
59 {(OPTIONSTRING) "concurrency", required_argument, NULL, OPT_CONCURRENCY},
60 {(OPTIONSTRING) "conn_sock", required_argument, NULL, OPT_SOCK_PER_CONN},
61 {(OPTIONSTRING) "execute_number", required_argument, NULL, OPT_EXECUTE_NUMBER},
62 {(OPTIONSTRING) "time", required_argument, NULL, OPT_TIME},
63 {(OPTIONSTRING) "cfg_cmd", required_argument, NULL, OPT_CONFIG_CMD},
64 {(OPTIONSTRING) "win_size", required_argument, NULL, OPT_WINDOW_SIZE},
65 {(OPTIONSTRING) "fixed_size", required_argument, NULL, OPT_FIXED_LTH},
66 {(OPTIONSTRING) "verify", required_argument, NULL, OPT_VERIFY},
67 {(OPTIONSTRING) "division", required_argument, NULL, OPT_GETS_DIVISION},
68 {(OPTIONSTRING) "stat_freq", required_argument, NULL, OPT_STAT_FREQ},
69 {(OPTIONSTRING) "exp_verify", required_argument, NULL, OPT_EXPIRE},
70 {(OPTIONSTRING) "overwrite", required_argument, NULL, OPT_OVERWRITE},
71 {(OPTIONSTRING) "reconnect", no_argument, NULL, OPT_RECONNECT},
72 {(OPTIONSTRING) "udp", no_argument, NULL, OPT_UDP},
73 {(OPTIONSTRING) "facebook", no_argument, NULL, OPT_FACEBOOK_TEST},
74 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY_PROTOCOL},
75 {(OPTIONSTRING) "tps", required_argument, NULL, OPT_TPS},
76 {(OPTIONSTRING) "rep_write", required_argument, NULL, OPT_REP_WRITE_SRV},
77 {(OPTIONSTRING) "verbose", no_argument, NULL, OPT_VERBOSE},
78 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
79 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
80 {0, 0, 0, 0},
81 };
82
83 /* Prototypes */
84 static void ms_sync_lock_init(void);
85 static void ms_sync_lock_destroy(void);
86 static void ms_global_struct_init(void);
87 static void ms_global_struct_destroy(void);
88 static void ms_version_command(const char *command_name);
89 static const char *ms_lookup_help(ms_options_t option);
90 static int64_t ms_parse_time(void);
91 static int64_t ms_parse_size(void);
92 static void ms_options_parse(int argc, char *argv[]);
93 static int ms_check_para(void);
94 static void ms_statistic_init(void);
95 static void ms_stats_init(void);
96 static void ms_print_statistics(int in_time);
97 static void ms_print_memaslap_stats(struct timeval *start_time, struct timeval *end_time);
98 static void ms_monitor_slap_mode(void);
99
100 /**
101 * output the help information
102 *
103 * @param command_name, the string of this process
104 * @param description, description of this process
105 * @param long_options, global options array
106 */
107 static void ms_help_command(const char *command_name, const char *description) {
108 char *help_message = NULL;
109
110 printf("%s v%u.%u\n", command_name, 1U, 0U);
111 printf(" %s\n\n", description);
112 printf("Usage:\n"
113 " memaslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
114 "Options:\n");
115
116 for (int x = 0; long_options[x].name; x++) {
117 printf(" -%c, --%s%c\n", long_options[x].val, long_options[x].name,
118 long_options[x].has_arg ? '=' : ' ');
119
120 if ((help_message = (char *) ms_lookup_help(long_options[x].val))) {
121 printf(" %s\n", help_message);
122 }
123 }
124
125 printf("\nExamples:\n"
126 " memaslap -s 127.0.0.1:11211 -S 5s\n"
127 " memaslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
128 " memaslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
129 " memaslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
130 " memaslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
131 " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
132 " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
133 } /* ms_help_command */
134
135 /* initialize the global locks */
136 static void ms_sync_lock_init() {
137 ms_global.init_lock.count = 0;
138 pthread_mutex_init(&ms_global.init_lock.lock, NULL);
139 pthread_cond_init(&ms_global.init_lock.cond, NULL);
140
141 ms_global.warmup_lock.count = 0;
142 pthread_mutex_init(&ms_global.warmup_lock.lock, NULL);
143 pthread_cond_init(&ms_global.warmup_lock.cond, NULL);
144
145 ms_global.run_lock.count = 0;
146 pthread_mutex_init(&ms_global.run_lock.lock, NULL);
147 pthread_cond_init(&ms_global.run_lock.cond, NULL);
148
149 pthread_mutex_init(&ms_global.quit_mutex, NULL);
150 pthread_mutex_init(&ms_global.seq_mutex, NULL);
151 } /* ms_sync_lock_init */
152
153 /* destroy the global locks */
154 static void ms_sync_lock_destroy() {
155 pthread_mutex_destroy(&ms_global.init_lock.lock);
156 pthread_cond_destroy(&ms_global.init_lock.cond);
157
158 pthread_mutex_destroy(&ms_global.warmup_lock.lock);
159 pthread_cond_destroy(&ms_global.warmup_lock.cond);
160
161 pthread_mutex_destroy(&ms_global.run_lock.lock);
162 pthread_cond_destroy(&ms_global.run_lock.cond);
163
164 pthread_mutex_destroy(&ms_global.quit_mutex);
165 pthread_mutex_destroy(&ms_global.seq_mutex);
166
167 if (ms_setting.stat_freq > 0) {
168 pthread_mutex_destroy(&ms_statistic.stat_mutex);
169 }
170 } /* ms_sync_lock_destroy */
171
172 /* initialize the global structure */
173 static void ms_global_struct_init() {
174 ms_sync_lock_init();
175 ms_global.finish_warmup = false;
176 ms_global.time_out = false;
177 }
178
179 /* destroy the global structure */
180 static void ms_global_struct_destroy() {
181 ms_sync_lock_destroy();
182 }
183
184 /**
185 * output the version information
186 *
187 * @param command_name, the string of this process
188 */
189 static void ms_version_command(const char *command_name) {
190 printf("%s v%u.%u\n", command_name, 1U, 0U);
191 exit(0);
192 }
193
194 /**
195 * get the description of the option
196 *
197 * @param option, option of command line
198 *
199 * @return char*, description of the command option
200 */
201 static const char *ms_lookup_help(ms_options_t option) {
202 switch (option) {
203 case OPT_SERVERS:
204 return "List one or more servers to connect. Servers count must be less than\n"
205 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
206
207 case OPT_VERSION:
208 return "Display the version of the application and then exit.";
209
210 case OPT_HELP:
211 return "Display this message and then exit.";
212
213 case OPT_EXECUTE_NUMBER:
214 return "Number of operations(get and set) to execute for the\n"
215 " given test. Default 1000000.";
216
217 case OPT_THREAD_NUMBER:
218 return "Number of threads to startup, better equal to CPU numbers. Default 8.";
219
220 case OPT_CONCURRENCY:
221 return "Number of concurrency to simulate with load. Default 128.";
222
223 case OPT_FIXED_LTH:
224 return "Fixed length of value.";
225
226 case OPT_VERIFY:
227 return "The proportion of date verification, e.g.: --verify=0.01";
228
229 case OPT_GETS_DIVISION:
230 return "Number of keys to multi-get once. Default 1, means single get.";
231
232 case OPT_TIME:
233 return "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
234 " d-days e.g.: --time=2h.";
235
236 case OPT_CONFIG_CMD:
237 return "Load the configure file to get command,key and value distribution list.";
238
239 case OPT_WINDOW_SIZE:
240 return "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
241 " Default 10k.";
242
243 case OPT_UDP:
244 return "UDP support, by default memaslap uses TCP; TCP port and UDP port of\n"
245 " server must be same.";
246
247 case OPT_EXPIRE:
248 return "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
249 " Default no object with expire time";
250
251 case OPT_OVERWRITE:
252 return "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
253 " Default never overwrite object.";
254
255 case OPT_STAT_FREQ:
256 return "Frequency of dumping statistic information. suffix: s-seconds,\n"
257 " m-minutes, e.g.: --resp_freq=10s.";
258
259 case OPT_SOCK_PER_CONN:
260 return "Number of TCP socks per concurrency. Default 1.";
261
262 case OPT_RECONNECT:
263 return "Reconnect support, when connection is closed it will be reconnected.";
264
265 case OPT_VERBOSE:
266 return "Whether it outputs detailed information when verification fails.";
267
268 case OPT_FACEBOOK_TEST:
269 return "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
270
271 case OPT_BINARY_PROTOCOL:
272 return "Whether it enables binary protocol. Default with ASCII protocol.";
273
274 case OPT_TPS:
275 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
276
277 case OPT_REP_WRITE_SRV:
278 return "The first nth servers can write data, e.g.: --rep_write=2.";
279
280 default:
281 return "Forgot to document this option :)";
282 } /* switch */
283 } /* ms_lookup_help */
284
285 /* used to parse the time string */
286 static int64_t ms_parse_time() {
287 int64_t ret = 0;
288 char unit = optarg[strlen(optarg) - 1];
289
290 optarg[strlen(optarg) - 1] = '\0';
291 ret = atoi(optarg);
292
293 switch (unit) {
294 case 'd':
295 case 'D':
296 ret *= 24;
297 /* fall through */
298 case 'h':
299 case 'H':
300 ret *= 60;
301 /* fall through */
302 case 'm':
303 case 'M':
304 ret *= 60;
305 /* fall through */
306 case 's':
307 case 'S':
308 break;
309
310 default:
311 ret = -1;
312 break;
313 } /* switch */
314
315 return ret;
316 } /* ms_parse_time */
317
318 /* used to parse the size string */
319 static int64_t ms_parse_size() {
320 int64_t ret = -1;
321 char unit = optarg[strlen(optarg) - 1];
322
323 optarg[strlen(optarg) - 1] = '\0';
324 errno = 0;
325 ret = strtoll(optarg, (char **) NULL, 10);
326 if (errno) {
327 fprintf(stderr, "strtoll(optarg,..): %s\n", strerror(errno));
328 exit(1);
329 }
330
331 switch (unit) {
332 case 'k':
333 case 'K':
334 ret *= 1024;
335 break;
336
337 case 'm':
338 case 'M':
339 ret *= 1024 * 1024;
340 break;
341
342 case 'g':
343 case 'G':
344 ret *= 1024 * 1024 * 1024;
345 break;
346
347 default:
348 ret = -1;
349 break;
350 } /* switch */
351
352 return ret;
353 } /* ms_parse_size */
354
355 /* used to parse the options of command line */
356 static void ms_options_parse(int argc, char *argv[]) {
357 int option_index = 0;
358 int option_rv;
359
360 while ((option_rv = getopt_long(argc, argv,
361 "VhURbaBs:x:T:c:X:v:d:"
362 "t:S:F:w:e:o:n:P:p:",
363 long_options, &option_index))
364 != -1)
365 {
366 switch (option_rv) {
367 case 0:
368 break;
369
370 case OPT_VERSION: /* --version or -V */
371 ms_version_command(PROGRAM_NAME);
372 break;
373
374 case OPT_HELP: /* --help or -h */
375 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
376 exit(0);
377 break;
378
379 case OPT_SERVERS: /* --servers or -s */
380 ms_setting.srv_str = strdup(optarg);
381 break;
382
383 case OPT_CONCURRENCY: /* --concurrency or -c */
384 errno = 0;
385 ms_setting.nconns = (uint32_t) strtoul(optarg, (char **) NULL, 10);
386 if (ms_setting.nconns <= 0 || errno) {
387 fprintf(stderr, "Concurrency must be greater than 0.:-)\n");
388 exit(1);
389 }
390 break;
391
392 case OPT_EXECUTE_NUMBER: /* --execute_number or -x */
393 errno = 0;
394 ms_setting.exec_num = (int) strtol(optarg, (char **) NULL, 10);
395 if (ms_setting.exec_num <= 0 || errno) {
396 fprintf(stderr, "Execute number must be greater than 0.:-)\n");
397 exit(1);
398 }
399 break;
400
401 case OPT_THREAD_NUMBER: /* --threads or -T */
402 errno = 0;
403 ms_setting.nthreads = (uint32_t) strtoul(optarg, (char **) NULL, 10);
404 if (ms_setting.nthreads <= 0 || errno) {
405 fprintf(stderr, "Threads number must be greater than 0.:-)\n");
406 exit(1);
407 }
408 break;
409
410 case OPT_FIXED_LTH: /* --fixed_size or -X */
411 errno = 0;
412 ms_setting.fixed_value_size = (size_t) strtoull(optarg, (char **) NULL, 10);
413 if ((ms_setting.fixed_value_size <= 0 || errno)
414 || (ms_setting.fixed_value_size > MAX_VALUE_SIZE))
415 {
416 fprintf(stderr, "Value size must be between 0 and 1M.:-)\n");
417 exit(1);
418 }
419 break;
420
421 case OPT_VERIFY: /* --verify or -v */
422 ms_setting.verify_percent = atof(optarg);
423 if ((ms_setting.verify_percent <= 0) || (ms_setting.verify_percent > 1.0)) {
424 fprintf(stderr,
425 "Data verification rate must be "
426 "greater than 0 and less than 1.0. :-)\n");
427 exit(1);
428 }
429 break;
430
431 case OPT_GETS_DIVISION: /* --division or -d */
432 errno = 0;
433 ms_setting.mult_key_num = (int) strtol(optarg, (char **) NULL, 10);
434 if (ms_setting.mult_key_num <= 0 || errno) {
435 fprintf(stderr, "Multi-get key number must be greater than 0.:-)\n");
436 exit(1);
437 }
438 break;
439
440 case OPT_TIME: /* --time or -t */
441 ms_setting.run_time = (int) ms_parse_time();
442 if (ms_setting.run_time == -1) {
443 fprintf(stderr,
444 "Please specify the run time. :-)\n"
445 "'s' for second, 'm' for minute, 'h' for hour, "
446 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
447 exit(1);
448 }
449
450 if (ms_setting.run_time == 0) {
451 fprintf(stderr, "Running time can not be 0. :-)\n");
452 exit(1);
453 }
454 break;
455
456 case OPT_CONFIG_CMD: /* --cfg_cmd or -F */
457 ms_setting.cfg_file = strdup(optarg);
458 break;
459
460 case OPT_WINDOW_SIZE: /* --win_size or -w */
461 ms_setting.win_size = (size_t) ms_parse_size();
462 if (ms_setting.win_size == (size_t) -1) {
463 fprintf(stderr,
464 "Please specify the item window size. :-)\n"
465 "e.g.: --win_size=10k (means 10k task window size).\n");
466 exit(1);
467 }
468 break;
469
470 case OPT_UDP: /* --udp or -U*/
471 ms_setting.udp = true;
472 break;
473
474 case OPT_EXPIRE: /* --exp_verify or -e */
475 ms_setting.exp_ver_per = atof(optarg);
476 if ((ms_setting.exp_ver_per <= 0) || (ms_setting.exp_ver_per > 1.0)) {
477 fprintf(stderr,
478 "Expire time verification rate must be "
479 "greater than 0 and less than 1.0. :-)\n");
480 exit(1);
481 }
482 break;
483
484 case OPT_OVERWRITE: /* --overwrite or -o */
485 ms_setting.overwrite_percent = atof(optarg);
486 if ((ms_setting.overwrite_percent <= 0) || (ms_setting.overwrite_percent > 1.0)) {
487 fprintf(stderr,
488 "Objects overwrite rate must be "
489 "greater than 0 and less than 1.0. :-)\n");
490 exit(1);
491 }
492 break;
493
494 case OPT_STAT_FREQ: /* --stat_freq or -S */
495 ms_setting.stat_freq = (int) ms_parse_time();
496 if (ms_setting.stat_freq == -1) {
497 fprintf(stderr,
498 "Please specify the frequency of dumping "
499 "statistic information. :-)\n"
500 "'s' for second, 'm' for minute, 'h' for hour, "
501 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
502 exit(1);
503 }
504
505 if (ms_setting.stat_freq == 0) {
506 fprintf(stderr,
507 "The frequency of dumping statistic information "
508 "can not be 0. :-)\n");
509 exit(1);
510 }
511 break;
512
513 case OPT_SOCK_PER_CONN: /* --conn_sock or -n */
514 errno = 0;
515 ms_setting.sock_per_conn = (uint32_t) strtoul(optarg, (char **) NULL, 10);
516 if (ms_setting.sock_per_conn <= 0 || errno) {
517 fprintf(stderr,
518 "Number of socks of each concurrency "
519 "must be greater than 0.:-)\n");
520 exit(1);
521 }
522 break;
523
524 case OPT_RECONNECT: /* --reconnect or -R */
525 ms_setting.reconnect = true;
526 break;
527
528 case OPT_VERBOSE: /* --verbose or -b */
529 ms_setting.verbose = true;
530 break;
531
532 case OPT_FACEBOOK_TEST: /* --facebook or -a */
533 ms_setting.facebook_test = true;
534 break;
535
536 case OPT_BINARY_PROTOCOL: /* --binary or -B */
537 ms_setting.binary_prot_ = true;
538 break;
539
540 case OPT_TPS: /* --tps or -P */
541 ms_setting.expected_tps = (int) ms_parse_size();
542 if (ms_setting.expected_tps == -1) {
543 fprintf(stderr,
544 "Please specify the item expected throughput. :-)\n"
545 "e.g.: --tps=10k (means 10k throughput).\n");
546 exit(1);
547 }
548 break;
549
550 case OPT_REP_WRITE_SRV: /* --rep_write or -p */
551 errno = 0;
552 ms_setting.rep_write_srv = (uint32_t) strtoul(optarg, (char **) NULL, 10);
553 if (ms_setting.rep_write_srv <= 0 || errno) {
554 fprintf(stderr,
555 "Number of replication writing server must be greater "
556 "than 0.:-)\n");
557 exit(1);
558 }
559 break;
560
561 case '?':
562 /* getopt_long already printed an error message. */
563 exit(1);
564
565 default:
566 abort();
567 } /* switch */
568 }
569 } /* ms_options_parse */
570
571 static int ms_check_para() {
572 if (ms_setting.srv_str == NULL) {
573 char *temp;
574
575 if ((temp = getenv("MEMCACHED_SERVERS"))) {
576 ms_setting.srv_str = strdup(temp);
577 } else {
578 fprintf(stderr, "No servers provided\n\n");
579 return -1;
580 }
581 }
582
583 if (ms_setting.nconns % (uint32_t) ms_setting.nthreads) {
584 fprintf(stderr, "Concurrency must be the multiples of threads count.\n");
585 return -1;
586 }
587
588 if (ms_setting.win_size % UNIT_ITEMS_COUNT) {
589 fprintf(stderr, "Window size must be the multiples of 1024.\n\n");
590 return -1;
591 }
592
593 return EXIT_SUCCESS;
594 } /* ms_check_para */
595
596 /* initialize the statistic structure */
597 static void ms_statistic_init() {
598 pthread_mutex_init(&ms_statistic.stat_mutex, NULL);
599 ms_init_stats(&ms_statistic.get_stat, "Get");
600 ms_init_stats(&ms_statistic.set_stat, "Set");
601 ms_init_stats(&ms_statistic.total_stat, "Total");
602 } /* ms_statistic_init */
603
604 /* initialize the global state structure */
605 static void ms_stats_init() {
606 memset(&ms_stats, 0, sizeof(ms_stats_t));
607 if (ms_setting.stat_freq > 0) {
608 ms_statistic_init();
609 }
610 } /* ms_stats_init */
611
612 /* use to output the statistic */
613 static void ms_print_statistics(int in_time) {
614 int obj_size = (int) (ms_setting.avg_key_size + ms_setting.avg_val_size);
615
616 printf("\033[1;1H\033[2J\n");
617 ms_dump_format_stats(&ms_statistic.get_stat, in_time, ms_setting.stat_freq, obj_size);
618 ms_dump_format_stats(&ms_statistic.set_stat, in_time, ms_setting.stat_freq, obj_size);
619 ms_dump_format_stats(&ms_statistic.total_stat, in_time, ms_setting.stat_freq, obj_size);
620 } /* ms_print_statistics */
621
622 /* used to print the states of memaslap */
623 static void ms_print_memaslap_stats(struct timeval *start_time, struct timeval *end_time) {
624 char buf[0x2000];
625 char *pos = buf;
626
627 pos += snprintf(pos, sizeof(buf), "cmd_get: %lu\n", (unsigned long) ms_stats.cmd_get);
628 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "cmd_set: %lu\n",
629 (unsigned long) ms_stats.cmd_set);
630 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "get_misses: %lu\n",
631 (unsigned long) ms_stats.get_misses);
632
633 if (ms_setting.verify_percent > 0) {
634 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "verify_misses: %lu\n",
635 (unsigned long) ms_stats.vef_miss);
636 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "verify_failed: %lu\n",
637 (unsigned long) ms_stats.vef_failed);
638 }
639
640 if (ms_setting.exp_ver_per > 0) {
641 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "expired_get: %lu\n",
642 (unsigned long) ms_stats.exp_get);
643 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "unexpired_unget: %lu\n",
644 (unsigned long) ms_stats.unexp_unget);
645 }
646
647 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "written_bytes: %lu\n",
648 (unsigned long) ms_stats.bytes_written);
649 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "read_bytes: %lu\n",
650 (unsigned long) ms_stats.bytes_read);
651 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "object_bytes: %lu\n",
652 (unsigned long) ms_stats.obj_bytes);
653
654 if (ms_setting.udp || ms_setting.facebook_test) {
655 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "packet_disorder: %lu\n",
656 (unsigned long) ms_stats.pkt_disorder);
657 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "packet_drop: %lu\n",
658 (unsigned long) ms_stats.pkt_drop);
659 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "udp_timeout: %lu\n",
660 (unsigned long) ms_stats.udp_timeout);
661 }
662
663 if (ms_setting.stat_freq > 0) {
664 ms_dump_stats(&ms_statistic.get_stat);
665 ms_dump_stats(&ms_statistic.set_stat);
666 ms_dump_stats(&ms_statistic.total_stat);
667 }
668
669 int64_t time_diff = ms_time_diff(start_time, end_time);
670 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf),
671 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
672 (double) time_diff / 1000000,
673 (unsigned long long) (ms_stats.cmd_get + ms_stats.cmd_set),
674 (ms_stats.cmd_get + ms_stats.cmd_set) / ((long double) time_diff / 1000000),
675 (double) (ms_stats.bytes_written + ms_stats.bytes_read) / 1024 / 1024
676 / ((double) time_diff / 1000000));
677 assert(pos <= (buf + sizeof(buf)));
678
679 fwrite(buf, 1, pos - buf, stdout);
680 fflush(stdout);
681 } /* ms_print_memaslap_stats */
682
683 /* the loop of the main thread, wait the work threads to complete */
684 static void ms_monitor_slap_mode() {
685 struct timeval start_time, end_time;
686
687 /* Wait all the threads complete initialization. */
688 pthread_mutex_lock(&ms_global.init_lock.lock);
689 while (ms_global.init_lock.count < ms_setting.nthreads) {
690 pthread_cond_wait(&ms_global.init_lock.cond, &ms_global.init_lock.lock);
691 }
692 pthread_mutex_unlock(&ms_global.init_lock.lock);
693
694 /* only when there is no set operation it need warm up */
695 if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR) {
696 /* Wait all the connects complete warm up. */
697 pthread_mutex_lock(&ms_global.warmup_lock.lock);
698 while (ms_global.warmup_lock.count < ms_setting.nconns) {
699 pthread_cond_wait(&ms_global.warmup_lock.cond, &ms_global.warmup_lock.lock);
700 }
701 pthread_mutex_unlock(&ms_global.warmup_lock.lock);
702 }
703 ms_global.finish_warmup = true;
704
705 /* running in "run time" mode, user specify run time */
706 if (ms_setting.run_time > 0) {
707 int second = 0;
708 gettimeofday(&start_time, NULL);
709 while (1) {
710 sleep(1);
711 second++;
712
713 if ((ms_setting.stat_freq > 0) && (second % ms_setting.stat_freq == 0)
714 && (ms_stats.active_conns >= ms_setting.nconns) && (ms_stats.active_conns <= INT_MAX))
715 {
716 ms_print_statistics(second);
717 }
718
719 if (ms_setting.run_time <= second) {
720 ms_global.time_out = true;
721 break;
722 }
723
724 /* all connections disconnect */
725 if ((second > 5) && (ms_stats.active_conns == 0)) {
726 break;
727 }
728 }
729 gettimeofday(&end_time, NULL);
730 sleep(1); /* wait all threads clean up */
731 } else {
732 /* running in "execute number" mode, user specify execute number */
733 gettimeofday(&start_time, NULL);
734
735 /*
736 * We loop until we know that all connects have cleaned up.
737 */
738 pthread_mutex_lock(&ms_global.run_lock.lock);
739 while (ms_global.run_lock.count < ms_setting.nconns) {
740 pthread_cond_wait(&ms_global.run_lock.cond, &ms_global.run_lock.lock);
741 }
742 pthread_mutex_unlock(&ms_global.run_lock.lock);
743
744 gettimeofday(&end_time, NULL);
745 }
746
747 ms_print_memaslap_stats(&start_time, &end_time);
748 } /* ms_monitor_slap_mode */
749
750 /* the main function */
751 int main(int argc, char *argv[]) {
752 srandom((unsigned int) time(NULL));
753 ms_global_struct_init();
754
755 /* initialization */
756 ms_setting_init_pre();
757 ms_options_parse(argc, argv);
758 if (ms_check_para()) {
759 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
760 exit(1);
761 }
762 ms_setting_init_post();
763 ms_stats_init();
764 ms_thread_init();
765
766 /* waiting work thread complete its task */
767 ms_monitor_slap_mode();
768
769 /* clean up */
770 ms_thread_cleanup();
771 ms_global_struct_destroy();
772 ms_setting_cleanup();
773
774 return EXIT_SUCCESS;
775 } /* main */