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