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