src/bin: apply clang-format
[m6w6/libmemcached] / src / bin / 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 <stdlib.h>
19 #include <getopt.h>
20 #include <limits.h>
21
22 #if defined(HAVE_SYS_TIME_H)
23 # include <sys/time.h>
24 #endif
25
26 #if defined(HAVE_TIME_H)
27 # include <time.h>
28 #endif
29
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)) != NULL) {
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: return "Display the version of the application and then exit.";
208
209 case OPT_HELP: return "Display this message and then exit.";
210
211 case OPT_EXECUTE_NUMBER:
212 return "Number of operations(get and set) to execute for the\n"
213 " given test. Default 1000000.";
214
215 case OPT_THREAD_NUMBER:
216 return "Number of threads to startup, better equal to CPU numbers. Default 8.";
217
218 case OPT_CONCURRENCY: return "Number of concurrency to simulate with load. Default 128.";
219
220 case OPT_FIXED_LTH: return "Fixed length of value.";
221
222 case OPT_VERIFY: return "The proportion of date verification, e.g.: --verify=0.01";
223
224 case OPT_GETS_DIVISION: return "Number of keys to multi-get once. Default 1, means single get.";
225
226 case OPT_TIME:
227 return "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
228 " d-days e.g.: --time=2h.";
229
230 case OPT_CONFIG_CMD:
231 return "Load the configure file to get command,key and value distribution list.";
232
233 case OPT_WINDOW_SIZE:
234 return "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
235 " Default 10k.";
236
237 case OPT_UDP:
238 return "UDP support, by default memaslap uses TCP; TCP port and UDP port of\n"
239 " server must be same.";
240
241 case OPT_EXPIRE:
242 return "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
243 " Default no object with expire time";
244
245 case OPT_OVERWRITE:
246 return "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
247 " Default never overwrite object.";
248
249 case OPT_STAT_FREQ:
250 return "Frequency of dumping statistic information. suffix: s-seconds,\n"
251 " m-minutes, e.g.: --resp_freq=10s.";
252
253 case OPT_SOCK_PER_CONN: return "Number of TCP socks per concurrency. Default 1.";
254
255 case OPT_RECONNECT: return "Reconnect support, when connection is closed it will be reconnected.";
256
257 case OPT_VERBOSE: return "Whether it outputs detailed information when verification fails.";
258
259 case OPT_FACEBOOK_TEST:
260 return "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
261
262 case OPT_BINARY_PROTOCOL:
263 return "Whether it enables binary protocol. Default with ASCII protocol.";
264
265 case OPT_TPS: return "Expected throughput, suffix: K, e.g.: --tps=10k.";
266
267 case OPT_REP_WRITE_SRV: return "The first nth servers can write data, e.g.: --rep_write=2.";
268
269 default: return "Forgot to document this option :)";
270 } /* switch */
271 } /* ms_lookup_help */
272
273 /* used to parse the time string */
274 static int64_t ms_parse_time() {
275 int64_t ret = 0;
276 char unit = optarg[strlen(optarg) - 1];
277
278 optarg[strlen(optarg) - 1] = '\0';
279 ret = atoi(optarg);
280
281 switch (unit) {
282 case 'd':
283 case 'D':
284 ret *= 24;
285 /* fall through */
286 case 'h':
287 case 'H':
288 ret *= 60;
289 /* fall through */
290 case 'm':
291 case 'M':
292 ret *= 60;
293 /* fall through */
294 case 's':
295 case 'S': break;
296
297 default: ret = -1; break;
298 } /* switch */
299
300 return ret;
301 } /* ms_parse_time */
302
303 /* used to parse the size string */
304 static int64_t ms_parse_size() {
305 int64_t ret = -1;
306 char unit = optarg[strlen(optarg) - 1];
307
308 optarg[strlen(optarg) - 1] = '\0';
309 errno = 0;
310 ret = strtoll(optarg, (char **) NULL, 10);
311 if (errno != 0) {
312 fprintf(stderr, "strtoll(optarg,..): %s\n", strerror(errno));
313 exit(1);
314 }
315
316 switch (unit) {
317 case 'k':
318 case 'K': ret *= 1024; break;
319
320 case 'm':
321 case 'M': ret *= 1024 * 1024; break;
322
323 case 'g':
324 case 'G': ret *= 1024 * 1024 * 1024; break;
325
326 default: ret = -1; break;
327 } /* switch */
328
329 return ret;
330 } /* ms_parse_size */
331
332 /* used to parse the options of command line */
333 static void ms_options_parse(int argc, char *argv[]) {
334 int option_index = 0;
335 int option_rv;
336
337 while ((option_rv = getopt_long(argc, argv,
338 "VhURbaBs:x:T:c:X:v:d:"
339 "t:S:F:w:e:o:n:P:p:",
340 long_options, &option_index))
341 != -1)
342 {
343 switch (option_rv) {
344 case 0: break;
345
346 case OPT_VERSION: /* --version or -V */ ms_version_command(PROGRAM_NAME); break;
347
348 case OPT_HELP: /* --help or -h */
349 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
350 exit(0);
351 break;
352
353 case OPT_SERVERS: /* --servers or -s */ ms_setting.srv_str = strdup(optarg); break;
354
355 case OPT_CONCURRENCY: /* --concurrency or -c */
356 errno = 0;
357 ms_setting.nconns = (uint32_t) strtoul(optarg, (char **) NULL, 10);
358 if (ms_setting.nconns <= 0 || errno != 0) {
359 fprintf(stderr, "Concurrency must be greater than 0.:-)\n");
360 exit(1);
361 }
362 break;
363
364 case OPT_EXECUTE_NUMBER: /* --execute_number or -x */
365 errno = 0;
366 ms_setting.exec_num = (int) strtol(optarg, (char **) NULL, 10);
367 if (ms_setting.exec_num <= 0 || errno != 0) {
368 fprintf(stderr, "Execute number must be greater than 0.:-)\n");
369 exit(1);
370 }
371 break;
372
373 case OPT_THREAD_NUMBER: /* --threads or -T */
374 errno = 0;
375 ms_setting.nthreads = (uint32_t) strtoul(optarg, (char **) NULL, 10);
376 if (ms_setting.nthreads <= 0 || errno != 0) {
377 fprintf(stderr, "Threads number must be greater than 0.:-)\n");
378 exit(1);
379 }
380 break;
381
382 case OPT_FIXED_LTH: /* --fixed_size or -X */
383 errno = 0;
384 ms_setting.fixed_value_size = (size_t) strtoull(optarg, (char **) NULL, 10);
385 if ((ms_setting.fixed_value_size <= 0 || errno != 0)
386 || (ms_setting.fixed_value_size > MAX_VALUE_SIZE))
387 {
388 fprintf(stderr, "Value size must be between 0 and 1M.:-)\n");
389 exit(1);
390 }
391 break;
392
393 case OPT_VERIFY: /* --verify or -v */
394 ms_setting.verify_percent = atof(optarg);
395 if ((ms_setting.verify_percent <= 0) || (ms_setting.verify_percent > 1.0)) {
396 fprintf(stderr,
397 "Data verification rate must be "
398 "greater than 0 and less than 1.0. :-)\n");
399 exit(1);
400 }
401 break;
402
403 case OPT_GETS_DIVISION: /* --division or -d */
404 errno = 0;
405 ms_setting.mult_key_num = (int) strtol(optarg, (char **) NULL, 10);
406 if (ms_setting.mult_key_num <= 0 || errno != 0) {
407 fprintf(stderr, "Multi-get key number must be greater than 0.:-)\n");
408 exit(1);
409 }
410 break;
411
412 case OPT_TIME: /* --time or -t */
413 ms_setting.run_time = (int) ms_parse_time();
414 if (ms_setting.run_time == -1) {
415 fprintf(stderr,
416 "Please specify the run time. :-)\n"
417 "'s' for second, 'm' for minute, 'h' for hour, "
418 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
419 exit(1);
420 }
421
422 if (ms_setting.run_time == 0) {
423 fprintf(stderr, "Running time can not be 0. :-)\n");
424 exit(1);
425 }
426 break;
427
428 case OPT_CONFIG_CMD: /* --cfg_cmd or -F */ ms_setting.cfg_file = strdup(optarg); break;
429
430 case OPT_WINDOW_SIZE: /* --win_size or -w */
431 ms_setting.win_size = (size_t) ms_parse_size();
432 if (ms_setting.win_size == (size_t) -1) {
433 fprintf(stderr,
434 "Please specify the item window size. :-)\n"
435 "e.g.: --win_size=10k (means 10k task window size).\n");
436 exit(1);
437 }
438 break;
439
440 case OPT_UDP: /* --udp or -U*/ ms_setting.udp = true; break;
441
442 case OPT_EXPIRE: /* --exp_verify or -e */
443 ms_setting.exp_ver_per = atof(optarg);
444 if ((ms_setting.exp_ver_per <= 0) || (ms_setting.exp_ver_per > 1.0)) {
445 fprintf(stderr,
446 "Expire time verification rate must be "
447 "greater than 0 and less than 1.0. :-)\n");
448 exit(1);
449 }
450 break;
451
452 case OPT_OVERWRITE: /* --overwrite or -o */
453 ms_setting.overwrite_percent = atof(optarg);
454 if ((ms_setting.overwrite_percent <= 0) || (ms_setting.overwrite_percent > 1.0)) {
455 fprintf(stderr,
456 "Objects overwrite rate must be "
457 "greater than 0 and less than 1.0. :-)\n");
458 exit(1);
459 }
460 break;
461
462 case OPT_STAT_FREQ: /* --stat_freq or -S */
463 ms_setting.stat_freq = (int) ms_parse_time();
464 if (ms_setting.stat_freq == -1) {
465 fprintf(stderr,
466 "Please specify the frequency of dumping "
467 "statistic information. :-)\n"
468 "'s' for second, 'm' for minute, 'h' for hour, "
469 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
470 exit(1);
471 }
472
473 if (ms_setting.stat_freq == 0) {
474 fprintf(stderr,
475 "The frequency of dumping statistic information "
476 "can not be 0. :-)\n");
477 exit(1);
478 }
479 break;
480
481 case OPT_SOCK_PER_CONN: /* --conn_sock or -n */
482 errno = 0;
483 ms_setting.sock_per_conn = (uint32_t) strtoul(optarg, (char **) NULL, 10);
484 if (ms_setting.sock_per_conn <= 0 || errno != 0) {
485 fprintf(stderr,
486 "Number of socks of each concurrency "
487 "must be greater than 0.:-)\n");
488 exit(1);
489 }
490 break;
491
492 case OPT_RECONNECT: /* --reconnect or -R */ ms_setting.reconnect = true; break;
493
494 case OPT_VERBOSE: /* --verbose or -b */ ms_setting.verbose = true; break;
495
496 case OPT_FACEBOOK_TEST: /* --facebook or -a */ ms_setting.facebook_test = true; break;
497
498 case OPT_BINARY_PROTOCOL: /* --binary or -B */ ms_setting.binary_prot_ = true; break;
499
500 case OPT_TPS: /* --tps or -P */
501 ms_setting.expected_tps = (int) ms_parse_size();
502 if (ms_setting.expected_tps == -1) {
503 fprintf(stderr,
504 "Please specify the item expected throughput. :-)\n"
505 "e.g.: --tps=10k (means 10k throughput).\n");
506 exit(1);
507 }
508 break;
509
510 case OPT_REP_WRITE_SRV: /* --rep_write or -p */
511 errno = 0;
512 ms_setting.rep_write_srv = (uint32_t) strtoul(optarg, (char **) NULL, 10);
513 if (ms_setting.rep_write_srv <= 0 || errno != 0) {
514 fprintf(stderr,
515 "Number of replication writing server must be greater "
516 "than 0.:-)\n");
517 exit(1);
518 }
519 break;
520
521 case '?':
522 /* getopt_long already printed an error message. */
523 exit(1);
524
525 default: abort();
526 } /* switch */
527 }
528 } /* ms_options_parse */
529
530 static int ms_check_para() {
531 if (ms_setting.srv_str == NULL) {
532 char *temp;
533
534 if ((temp = getenv("MEMCACHED_SERVERS"))) {
535 ms_setting.srv_str = strdup(temp);
536 } else {
537 fprintf(stderr, "No servers provided\n\n");
538 return -1;
539 }
540 }
541
542 if (ms_setting.nconns % (uint32_t) ms_setting.nthreads != 0) {
543 fprintf(stderr, "Concurrency must be the multiples of threads count.\n");
544 return -1;
545 }
546
547 if (ms_setting.win_size % UNIT_ITEMS_COUNT != 0) {
548 fprintf(stderr, "Window size must be the multiples of 1024.\n\n");
549 return -1;
550 }
551
552 return EXIT_SUCCESS;
553 } /* ms_check_para */
554
555 /* initialize the statistic structure */
556 static void ms_statistic_init() {
557 pthread_mutex_init(&ms_statistic.stat_mutex, NULL);
558 ms_init_stats(&ms_statistic.get_stat, "Get");
559 ms_init_stats(&ms_statistic.set_stat, "Set");
560 ms_init_stats(&ms_statistic.total_stat, "Total");
561 } /* ms_statistic_init */
562
563 /* initialize the global state structure */
564 static void ms_stats_init() {
565 memset(&ms_stats, 0, sizeof(ms_stats_t));
566 if (ms_setting.stat_freq > 0) {
567 ms_statistic_init();
568 }
569 } /* ms_stats_init */
570
571 /* use to output the statistic */
572 static void ms_print_statistics(int in_time) {
573 int obj_size = (int) (ms_setting.avg_key_size + ms_setting.avg_val_size);
574
575 printf("\033[1;1H\033[2J\n");
576 ms_dump_format_stats(&ms_statistic.get_stat, in_time, ms_setting.stat_freq, obj_size);
577 ms_dump_format_stats(&ms_statistic.set_stat, in_time, ms_setting.stat_freq, obj_size);
578 ms_dump_format_stats(&ms_statistic.total_stat, in_time, ms_setting.stat_freq, obj_size);
579 } /* ms_print_statistics */
580
581 /* used to print the states of memaslap */
582 static void ms_print_memaslap_stats(struct timeval *start_time, struct timeval *end_time) {
583 char buf[0x2000];
584 char *pos = buf;
585
586 pos += snprintf(pos, sizeof(buf), "cmd_get: %lu\n", (unsigned long) ms_stats.cmd_get);
587 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "cmd_set: %lu\n",
588 (unsigned long) ms_stats.cmd_set);
589 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "get_misses: %lu\n",
590 (unsigned long) ms_stats.get_misses);
591
592 if (ms_setting.verify_percent > 0) {
593 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "verify_misses: %lu\n",
594 (unsigned long) ms_stats.vef_miss);
595 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "verify_failed: %lu\n",
596 (unsigned long) ms_stats.vef_failed);
597 }
598
599 if (ms_setting.exp_ver_per > 0) {
600 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "expired_get: %lu\n",
601 (unsigned long) ms_stats.exp_get);
602 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "unexpired_unget: %lu\n",
603 (unsigned long) ms_stats.unexp_unget);
604 }
605
606 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "written_bytes: %lu\n",
607 (unsigned long) ms_stats.bytes_written);
608 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "read_bytes: %lu\n",
609 (unsigned long) ms_stats.bytes_read);
610 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "object_bytes: %lu\n",
611 (unsigned long) ms_stats.obj_bytes);
612
613 if (ms_setting.udp || ms_setting.facebook_test) {
614 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "packet_disorder: %lu\n",
615 (unsigned long) ms_stats.pkt_disorder);
616 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "packet_drop: %lu\n",
617 (unsigned long) ms_stats.pkt_drop);
618 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "udp_timeout: %lu\n",
619 (unsigned long) ms_stats.udp_timeout);
620 }
621
622 if (ms_setting.stat_freq > 0) {
623 ms_dump_stats(&ms_statistic.get_stat);
624 ms_dump_stats(&ms_statistic.set_stat);
625 ms_dump_stats(&ms_statistic.total_stat);
626 }
627
628 int64_t time_diff = ms_time_diff(start_time, end_time);
629 pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf),
630 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
631 (double) time_diff / 1000000,
632 (unsigned long long) (ms_stats.cmd_get + ms_stats.cmd_set),
633 (ms_stats.cmd_get + ms_stats.cmd_set) / ((long double) time_diff / 1000000),
634 (double) (ms_stats.bytes_written + ms_stats.bytes_read) / 1024 / 1024
635 / ((double) time_diff / 1000000));
636 assert(pos <= (buf + sizeof(buf)));
637
638 fwrite(buf, 1, pos - buf, stdout);
639 fflush(stdout);
640 } /* ms_print_memaslap_stats */
641
642 /* the loop of the main thread, wait the work threads to complete */
643 static void ms_monitor_slap_mode() {
644 struct timeval start_time, end_time;
645
646 /* Wait all the threads complete initialization. */
647 pthread_mutex_lock(&ms_global.init_lock.lock);
648 while (ms_global.init_lock.count < ms_setting.nthreads) {
649 pthread_cond_wait(&ms_global.init_lock.cond, &ms_global.init_lock.lock);
650 }
651 pthread_mutex_unlock(&ms_global.init_lock.lock);
652
653 /* only when there is no set operation it need warm up */
654 if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR) {
655 /* Wait all the connects complete warm up. */
656 pthread_mutex_lock(&ms_global.warmup_lock.lock);
657 while (ms_global.warmup_lock.count < ms_setting.nconns) {
658 pthread_cond_wait(&ms_global.warmup_lock.cond, &ms_global.warmup_lock.lock);
659 }
660 pthread_mutex_unlock(&ms_global.warmup_lock.lock);
661 }
662 ms_global.finish_warmup = true;
663
664 /* running in "run time" mode, user specify run time */
665 if (ms_setting.run_time > 0) {
666 int second = 0;
667 gettimeofday(&start_time, NULL);
668 while (1) {
669 sleep(1);
670 second++;
671
672 if ((ms_setting.stat_freq > 0) && (second % ms_setting.stat_freq == 0)
673 && (ms_stats.active_conns >= ms_setting.nconns) && (ms_stats.active_conns <= INT_MAX))
674 {
675 ms_print_statistics(second);
676 }
677
678 if (ms_setting.run_time <= second) {
679 ms_global.time_out = true;
680 break;
681 }
682
683 /* all connections disconnect */
684 if ((second > 5) && (ms_stats.active_conns == 0)) {
685 break;
686 }
687 }
688 gettimeofday(&end_time, NULL);
689 sleep(1); /* wait all threads clean up */
690 } else {
691 /* running in "execute number" mode, user specify execute number */
692 gettimeofday(&start_time, NULL);
693
694 /*
695 * We loop until we know that all connects have cleaned up.
696 */
697 pthread_mutex_lock(&ms_global.run_lock.lock);
698 while (ms_global.run_lock.count < ms_setting.nconns) {
699 pthread_cond_wait(&ms_global.run_lock.cond, &ms_global.run_lock.lock);
700 }
701 pthread_mutex_unlock(&ms_global.run_lock.lock);
702
703 gettimeofday(&end_time, NULL);
704 }
705
706 ms_print_memaslap_stats(&start_time, &end_time);
707 } /* ms_monitor_slap_mode */
708
709 /* the main function */
710 int main(int argc, char *argv[]) {
711 srandom((unsigned int) time(NULL));
712 ms_global_struct_init();
713
714 /* initialization */
715 ms_setting_init_pre();
716 ms_options_parse(argc, argv);
717 if (ms_check_para()) {
718 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
719 exit(1);
720 }
721 ms_setting_init_post();
722 ms_stats_init();
723 ms_thread_init();
724
725 /* waiting work thread complete its task */
726 ms_monitor_slap_mode();
727
728 /* clean up */
729 ms_thread_cleanup();
730 ms_global_struct_destroy();
731 ms_setting_cleanup();
732
733 return EXIT_SUCCESS;
734 } /* main */