4 * (c) Copyright 2009, Schooner Information Technology, Inc.
6 * http://www.schoonerinfotech.com/
8 * Use and distribution licensed under the BSD license. See
9 * the COPYING file for full text.
13 * Mingqiang Zhuang <mingqiangzhuang@hengtiansoft.com>
19 #include "ms_sigsegv.h"
20 #include "ms_setting.h"
21 #include "ms_thread.h"
23 #define PROGRAM_NAME "memslap"
24 #define PROGRAM_DESCRIPTION \
25 "Generates workload against memcached servers."
28 static struct option long_options
[]=
30 { "servers", required_argument
, NULL
,
32 { "threads", required_argument
, NULL
,
34 { "concurrency", required_argument
, NULL
,
36 { "conn_sock", required_argument
, NULL
,
38 { "execute_number", required_argument
, NULL
,
40 { "time", required_argument
, NULL
,
42 { "cfg_cmd", required_argument
, NULL
,
44 { "win_size", required_argument
, NULL
,
46 { "fixed_size", required_argument
, NULL
,
48 { "verify", required_argument
, NULL
,
50 { "division", required_argument
, NULL
,
52 { "stat_freq", required_argument
, NULL
,
54 { "exp_verify", required_argument
, NULL
,
56 { "overwrite", required_argument
, NULL
,
58 { "reconnect", no_argument
, NULL
,
60 { "udp", no_argument
, NULL
,
62 { "facebook", no_argument
, NULL
,
64 { "binary", no_argument
, NULL
,
65 OPT_BINARY_PROTOCOL
},
66 { "tps", required_argument
, NULL
,
68 { "rep_write", required_argument
, NULL
,
70 { "verbose", no_argument
, NULL
,
72 { "help", no_argument
, NULL
,
74 { "version", no_argument
, NULL
,
80 static void ms_sync_lock_init(void);
81 static void ms_sync_lock_destroy(void);
82 static void ms_global_struct_init(void);
83 static void ms_global_struct_destroy(void);
84 static void ms_version_command(const char *command_name
);
85 static const char *ms_lookup_help(ms_options_t option
);
86 static int64_t ms_parse_time(void);
87 static int64_t ms_parse_size(void);
88 static void ms_options_parse(int argc
, char *argv
[]);
89 static int ms_check_para(void);
90 static void ms_statistic_init(void);
91 static void ms_stats_init(void);
92 static void ms_print_statistics(int time
);
93 static void ms_print_memslap_stats(struct timeval
*start_time
,
94 struct timeval
*end_time
);
95 static void ms_monitor_slap_mode(void);
96 void ms_help_command(const char *command_name
, const char *description
);
99 /* initialize the global locks */
100 static void ms_sync_lock_init()
102 ms_global
.init_lock
.count
= 0;
103 pthread_mutex_init(&ms_global
.init_lock
.lock
, NULL
);
104 pthread_cond_init(&ms_global
.init_lock
.cond
, NULL
);
106 ms_global
.run_lock
.count
= 0;
107 pthread_mutex_init(&ms_global
.run_lock
.lock
, NULL
);
108 pthread_cond_init(&ms_global
.run_lock
.cond
, NULL
);
110 pthread_mutex_init(&ms_global
.quit_mutex
, NULL
);
111 pthread_mutex_init(&ms_global
.seq_mutex
, NULL
);
112 } /* ms_sync_lock_init */
115 /* destroy the global locks */
116 static void ms_sync_lock_destroy()
118 pthread_mutex_destroy(&ms_global
.init_lock
.lock
);
119 pthread_cond_destroy(&ms_global
.init_lock
.cond
);
121 pthread_mutex_destroy(&ms_global
.run_lock
.lock
);
122 pthread_cond_destroy(&ms_global
.run_lock
.cond
);
124 pthread_mutex_destroy(&ms_global
.quit_mutex
);
125 pthread_mutex_destroy(&ms_global
.seq_mutex
);
127 if (ms_setting
.stat_freq
> 0)
129 pthread_mutex_destroy(&ms_statistic
.stat_mutex
);
131 } /* ms_sync_lock_destroy */
134 /* initialize the global structure */
135 static void ms_global_struct_init()
138 ms_global
.finish_warmup
= false;
139 ms_global
.time_out
= false;
143 /* destroy the global structure */
144 static void ms_global_struct_destroy()
146 ms_sync_lock_destroy();
151 * output the version information
153 * @param command_name, the string of this process
155 static void ms_version_command(const char *command_name
)
157 printf("%s v%u.%u\n", command_name
, 1, 0);
163 * get the description of the option
165 * @param option, option of command line
167 * @return char*, description of the command option
169 static const char *ms_lookup_help(ms_options_t option
)
175 "List one or more servers to connect. Servers count must be less than\n"
176 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
179 return "Display the version of the application and then exit.";
182 return "Display this message and then exit.";
184 case OPT_EXECUTE_NUMBER
:
185 return "Number of operations(get and set) to execute for the\n"
186 " given test. Default 1000000.";
188 case OPT_THREAD_NUMBER
:
190 "Number of threads to startup, better equal to CPU numbers. Default 8.";
192 case OPT_CONCURRENCY
:
193 return "Number of concurrency to simulate with load. Default 128.";
196 return "Fixed length of value.";
199 return "The proportion of date verification, e.g.: --verify=0.01";
201 case OPT_GETS_DIVISION
:
202 return "Number of keys to multi-get once. Default 1, means single get.";
206 "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
207 " d-days e.g.: --time=2h.";
211 "Load the configure file to get command,key and value distribution list.";
213 case OPT_WINDOW_SIZE
:
215 "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
220 "UDP support, default memslap uses TCP, TCP port and UDP port of\n"
221 " server must be same.";
225 "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
226 " Default no object with expire time";
230 "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
231 " Default never overwrite object.";
235 "Frequency of dumping statistic information. suffix: s-seconds,\n"
236 " m-minutes, e.g.: --resp_freq=10s.";
238 case OPT_SOCK_PER_CONN
:
239 return "Number of TCP socks per concurrency. Default 1.";
243 "Reconnect support, when connection is closed it will be reconnected.";
247 "Whether it outputs detailed information when verification fails.";
249 case OPT_FACEBOOK_TEST
:
251 "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
253 case OPT_BINARY_PROTOCOL
:
255 "Whether it enables binary protocol. Default with ASCII protocol.";
258 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
260 case OPT_REP_WRITE_SRV
:
261 return "The first nth servers can write data, e.g.: --rep_write=2.";
264 return "Forgot to document this option :)";
266 } /* ms_lookup_help */
270 * output the help information
272 * @param command_name, the string of this process
273 * @param description, description of this process
274 * @param long_options, global options array
276 void ms_help_command(const char *command_name
, const char *description
)
278 char *help_message
= NULL
;
280 printf("%s v%u.%u\n", command_name
, 1, 0);
281 printf(" %s\n\n", description
);
284 " memslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
287 for (int x
= 0; long_options
[x
].name
; x
++)
289 printf(" -%c, --%s%c\n", long_options
[x
].val
, long_options
[x
].name
,
290 long_options
[x
].has_arg
? '=' : ' ');
291 if ((help_message
= (char *)ms_lookup_help(long_options
[x
].val
)) != NULL
)
293 printf(" %s\n", help_message
);
299 " memslap -s 127.0.0.1:11211 -S 5s\n"
300 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
301 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
302 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
303 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
304 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
305 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
308 } /* ms_help_command */
311 /* used to parse the time string */
312 static int64_t ms_parse_time()
315 char unit
= optarg
[strlen(optarg
) - 1];
317 optarg
[strlen(optarg
) - 1]= '\0';
344 } /* ms_parse_time */
347 /* used to parse the size string */
348 static int64_t ms_parse_size()
351 char unit
= optarg
[strlen(optarg
) - 1];
353 optarg
[strlen(optarg
) - 1]= '\0';
354 ret
= strtoll(optarg
, (char **)NULL
, 10);
370 ret
*= 1024 * 1024 * 1024;
379 } /* ms_parse_size */
382 /* used to parse the options of command line */
383 static void ms_options_parse(int argc
, char *argv
[])
388 while ((option_rv
= getopt_long(argc
, argv
, "VhURbaBs:x:T:c:X:v:d:"
389 "t:S:F:w:e:o:n:P:p:",
390 long_options
, &option_index
)) != -1)
397 case OPT_VERSION
: /* --version or -V */
398 ms_version_command(PROGRAM_NAME
);
401 case OPT_HELP
: /* --help or -h */
402 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
405 case OPT_SERVERS
: /* --servers or -s */
406 ms_setting
.srv_str
= strdup(optarg
);
409 case OPT_CONCURRENCY
: /* --concurrency or -c */
410 ms_setting
.nconns
= atoi(optarg
);
411 if (ms_setting
.nconns
<= 0)
413 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
418 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
419 ms_setting
.exec_num
= atoll(optarg
);
420 if (ms_setting
.exec_num
<= 0)
422 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
427 case OPT_THREAD_NUMBER
: /* --threads or -T */
428 ms_setting
.nthreads
= atoi(optarg
);
429 if (ms_setting
.nthreads
<= 0)
431 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
436 case OPT_FIXED_LTH
: /* --fixed_size or -X */
437 ms_setting
.fixed_value_size
= (size_t)atoi(optarg
);
438 if ((ms_setting
.fixed_value_size
<= 0)
439 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
441 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
446 case OPT_VERIFY
: /* --verify or -v */
447 ms_setting
.verify_percent
= atof(optarg
);
448 if ((ms_setting
.verify_percent
<= 0)
449 || (ms_setting
.verify_percent
> 1.0))
451 fprintf(stderr
, "Data verification rate must be "
452 "greater than 0 and less than 1.0. :-)\n");
457 case OPT_GETS_DIVISION
: /* --division or -d */
458 ms_setting
.mult_key_num
= atoi(optarg
);
459 if (ms_setting
.mult_key_num
<= 0)
461 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
466 case OPT_TIME
: /* --time or -t */
467 ms_setting
.run_time
= (int)ms_parse_time();
468 if (ms_setting
.run_time
== -1)
470 fprintf(stderr
, "Please specify the run time. :-)\n"
471 "'s' for second, 'm' for minute, 'h' for hour, "
472 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
476 if (ms_setting
.run_time
== 0)
478 fprintf(stderr
, "Running time can not be 0. :-)\n");
483 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */
484 ms_setting
.cfg_file
= strdup(optarg
);
487 case OPT_WINDOW_SIZE
: /* --win_size or -w */
488 ms_setting
.win_size
= (size_t)ms_parse_size();
489 if (ms_setting
.win_size
== (size_t)-1)
493 "Please specify the item window size. :-)\n"
494 "e.g.: --win_size=10k (means 10k task window size).\n");
499 case OPT_UDP
: /* --udp or -U*/
500 ms_setting
.udp
= true;
503 case OPT_EXPIRE
: /* --exp_verify or -e */
504 ms_setting
.exp_ver_per
= atof(optarg
);
505 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0))
507 fprintf(stderr
, "Expire time verification rate must be "
508 "greater than 0 and less than 1.0. :-)\n");
513 case OPT_OVERWRITE
: /* --overwrite or -o */
514 ms_setting
.overwrite_percent
= atof(optarg
);
515 if ((ms_setting
.overwrite_percent
<= 0)
516 || (ms_setting
.overwrite_percent
> 1.0))
518 fprintf(stderr
, "Objects overwrite rate must be "
519 "greater than 0 and less than 1.0. :-)\n");
524 case OPT_STAT_FREQ
: /* --stat_freq or -S */
525 ms_setting
.stat_freq
= (int)ms_parse_time();
526 if (ms_setting
.stat_freq
== -1)
528 fprintf(stderr
, "Please specify the frequency of dumping "
529 "statistic information. :-)\n"
530 "'s' for second, 'm' for minute, 'h' for hour, "
531 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
535 if (ms_setting
.stat_freq
== 0)
537 fprintf(stderr
, "The frequency of dumping statistic information "
538 "can not be 0. :-)\n");
543 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
544 ms_setting
.sock_per_conn
= atoi(optarg
);
545 if (ms_setting
.sock_per_conn
<= 0)
547 fprintf(stderr
, "Number of socks of each concurrency "
548 "must be greater than 0.:-)\n");
553 case OPT_RECONNECT
: /* --reconnect or -R */
554 ms_setting
.reconnect
= true;
557 case OPT_VERBOSE
: /* --verbose or -b */
558 ms_setting
.verbose
= true;
561 case OPT_FACEBOOK_TEST
: /* --facebook or -a */
562 ms_setting
.facebook_test
= true;
565 case OPT_BINARY_PROTOCOL
: /* --binary or -B */
566 ms_setting
.binary_prot
= true;
569 case OPT_TPS
: /* --tps or -P */
570 ms_setting
.expected_tps
= (int)ms_parse_size();
571 if (ms_setting
.expected_tps
== -1)
574 "Please specify the item expected throughput. :-)\n"
575 "e.g.: --tps=10k (means 10k throughput).\n");
580 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
581 ms_setting
.rep_write_srv
= atoi(optarg
);
582 if (ms_setting
.rep_write_srv
<= 0)
585 "Number of replication writing server must be greater "
592 /* getopt_long already printed an error message. */
599 } /* ms_options_parse */
602 static int ms_check_para()
604 if (ms_setting
.srv_str
== NULL
)
606 fprintf(stderr
, "No Servers provided.\n\n");
610 if (ms_setting
.nconns
% ms_setting
.nthreads
!= 0)
612 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
616 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0)
618 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
623 } /* ms_check_para */
626 /* initialize the statistic structure */
627 static void ms_statistic_init()
629 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
630 ms_init_stats(&ms_statistic
.get_stat
, "Get");
631 ms_init_stats(&ms_statistic
.set_stat
, "Set");
632 ms_init_stats(&ms_statistic
.total_stat
, "Total");
633 } /* ms_statistic_init */
636 /* initialize the global state structure */
637 static void ms_stats_init()
639 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
640 if (ms_setting
.stat_freq
> 0)
644 } /* ms_stats_init */
647 /* use to output the statistic */
648 static void ms_print_statistics(int in_time
)
650 int obj_size
= (int)(ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
652 printf("\033[1;1H\033[2J\n");
653 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
,
654 ms_setting
.stat_freq
, obj_size
);
655 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
,
656 ms_setting
.stat_freq
, obj_size
);
657 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
,
658 ms_setting
.stat_freq
, obj_size
);
659 } /* ms_print_statistics */
662 /* used to print the states of memslap */
663 static void ms_print_memslap_stats(struct timeval
*start_time
,
664 struct timeval
*end_time
)
671 (unsigned long long)ms_stats
.cmd_get
);
674 (unsigned long long)ms_stats
.cmd_set
);
676 "get_misses: %llu\n",
677 (unsigned long long)ms_stats
.get_misses
);
679 if (ms_setting
.verify_percent
> 0)
681 pos
+= sprintf(pos
, "verify_misses: %llu\n",
682 (unsigned long long)ms_stats
.vef_miss
);
683 pos
+= sprintf(pos
, "verify_failed: %llu\n",
684 (unsigned long long)ms_stats
.vef_failed
);
687 if (ms_setting
.exp_ver_per
> 0)
689 pos
+= sprintf(pos
, "expired_get: %llu\n",
690 (unsigned long long)ms_stats
.exp_get
);
691 pos
+= sprintf(pos
, "unexpired_unget: %llu\n",
692 (unsigned long long)ms_stats
.unexp_unget
);
696 "written_bytes: %llu\n",
697 (unsigned long long)ms_stats
.bytes_written
);
699 "read_bytes: %llu\n",
700 (unsigned long long)ms_stats
.bytes_read
);
702 "object_bytes: %llu\n",
703 (unsigned long long)ms_stats
.obj_bytes
);
705 if (ms_setting
.udp
|| ms_setting
.facebook_test
)
708 "packet_disorder: %llu\n",
709 (unsigned long long)ms_stats
.pkt_disorder
);
711 "packet_drop: %llu\n",
712 (unsigned long long)ms_stats
.pkt_drop
);
714 "udp_timeout: %llu\n",
715 (unsigned long long)ms_stats
.udp_timeout
);
718 if (ms_setting
.stat_freq
> 0)
720 ms_dump_stats(&ms_statistic
.get_stat
);
721 ms_dump_stats(&ms_statistic
.set_stat
);
722 ms_dump_stats(&ms_statistic
.total_stat
);
725 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
728 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
729 (double)time_diff
/ 1000000,
730 (unsigned long long)(ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
732 + ms_stats
.cmd_set
) / ((long double)time_diff
/ 1000000),
734 ms_stats
.bytes_written
735 + ms_stats
.bytes_read
) / 1024 / 1024
736 / ((double)time_diff
/ 1000000));
738 fprintf(stdout
, "%s", buf
);
740 } /* ms_print_memslap_stats */
743 /* the loop of the main thread, wait the work threads to complete */
744 static void ms_monitor_slap_mode()
747 struct timeval start_time
, end_time
;
749 /* only when there is no set operation it need warm up */
750 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
)
752 /* Wait all the connects complete warm up. */
753 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
754 while (ms_global
.init_lock
.count
< ms_setting
.nconns
)
756 pthread_cond_wait(&ms_global
.init_lock
.cond
,
757 &ms_global
.init_lock
.lock
);
759 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
762 ms_global
.finish_warmup
= true;
764 /* running in "run time" mode, user specify run time */
765 if (ms_setting
.run_time
> 0)
767 gettimeofday(&start_time
, NULL
);
773 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
774 && ((int32_t)ms_stats
.active_conns
>= ms_setting
.nconns
)
775 && (ms_stats
.active_conns
<= (uint32_t)INT_MAX
))
777 ms_print_statistics(second
);
780 if (ms_setting
.run_time
<= second
)
782 ms_global
.time_out
= true;
786 /* all connections disconnect */
787 if ((second
> 5) && (ms_stats
.active_conns
== 0))
792 gettimeofday(&end_time
, NULL
);
793 sleep(1); /* wait all threads clean up */
797 /* running in "execute number" mode, user specify execute number */
798 gettimeofday(&start_time
, NULL
);
801 * We loop until we know that all connects have cleaned up.
803 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
804 while (ms_global
.run_lock
.count
< ms_setting
.nconns
)
806 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
808 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
810 gettimeofday(&end_time
, NULL
);
813 ms_print_memslap_stats(&start_time
, &end_time
);
814 } /* ms_monitor_slap_mode */
817 /* the main function */
818 int main(int argc
, char *argv
[])
820 srandom((unsigned int)time(NULL
));
821 ms_global_struct_init();
824 ms_setting_init_pre();
825 ms_options_parse(argc
, argv
);
828 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
831 ms_setting_init_post();
835 /* waiting work thread complete its task */
836 ms_monitor_slap_mode();
840 ms_global_struct_destroy();
841 ms_setting_cleanup();