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