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