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>
20 #include "ms_sigsegv.h"
21 #include "ms_setting.h"
22 #include "ms_thread.h"
24 #define PROGRAM_NAME "memslap"
25 #define PROGRAM_DESCRIPTION \
26 "Generates workload against memcached servers."
29 /* For some odd reason the option struct on solaris defines the argument
30 * as char* and not const char*
32 #define OPTIONSTRING char*
34 #define OPTIONSTRING const char*
38 static struct option long_options
[]=
40 { (OPTIONSTRING
)"servers", required_argument
, NULL
,
42 { (OPTIONSTRING
)"threads", required_argument
, NULL
,
44 { (OPTIONSTRING
)"concurrency", required_argument
, NULL
,
46 { (OPTIONSTRING
)"conn_sock", required_argument
, NULL
,
48 { (OPTIONSTRING
)"execute_number", required_argument
, NULL
,
50 { (OPTIONSTRING
)"time", required_argument
, NULL
,
52 { (OPTIONSTRING
)"cfg_cmd", required_argument
, NULL
,
54 { (OPTIONSTRING
)"win_size", required_argument
, NULL
,
56 { (OPTIONSTRING
)"fixed_size", required_argument
, NULL
,
58 { (OPTIONSTRING
)"verify", required_argument
, NULL
,
60 { (OPTIONSTRING
)"division", required_argument
, NULL
,
62 { (OPTIONSTRING
)"stat_freq", required_argument
, NULL
,
64 { (OPTIONSTRING
)"exp_verify", required_argument
, NULL
,
66 { (OPTIONSTRING
)"overwrite", required_argument
, NULL
,
68 { (OPTIONSTRING
)"reconnect", no_argument
, NULL
,
70 { (OPTIONSTRING
)"udp", no_argument
, NULL
,
72 { (OPTIONSTRING
)"facebook", no_argument
, NULL
,
74 { (OPTIONSTRING
)"binary", no_argument
, NULL
,
75 OPT_BINARY_PROTOCOL
},
76 { (OPTIONSTRING
)"tps", required_argument
, NULL
,
78 { (OPTIONSTRING
)"rep_write", required_argument
, NULL
,
80 { (OPTIONSTRING
)"verbose", no_argument
, NULL
,
82 { (OPTIONSTRING
)"help", no_argument
, NULL
,
84 { (OPTIONSTRING
)"version", no_argument
, NULL
,
90 static void ms_sync_lock_init(void);
91 static void ms_sync_lock_destroy(void);
92 static void ms_global_struct_init(void);
93 static void ms_global_struct_destroy(void);
94 static void ms_version_command(const char *command_name
);
95 static const char *ms_lookup_help(ms_options_t option
);
96 static int64_t ms_parse_time(void);
97 static int64_t ms_parse_size(void);
98 static void ms_options_parse(int argc
, char *argv
[]);
99 static int ms_check_para(void);
100 static void ms_statistic_init(void);
101 static void ms_stats_init(void);
102 static void ms_print_statistics(int time
);
103 static void ms_print_memslap_stats(struct timeval
*start_time
,
104 struct timeval
*end_time
);
105 static void ms_monitor_slap_mode(void);
106 void ms_help_command(const char *command_name
, const char *description
);
109 /* initialize the global locks */
110 static void ms_sync_lock_init()
112 ms_global
.init_lock
.count
= 0;
113 pthread_mutex_init(&ms_global
.init_lock
.lock
, NULL
);
114 pthread_cond_init(&ms_global
.init_lock
.cond
, NULL
);
116 ms_global
.run_lock
.count
= 0;
117 pthread_mutex_init(&ms_global
.run_lock
.lock
, NULL
);
118 pthread_cond_init(&ms_global
.run_lock
.cond
, NULL
);
120 pthread_mutex_init(&ms_global
.quit_mutex
, NULL
);
121 pthread_mutex_init(&ms_global
.seq_mutex
, NULL
);
122 } /* ms_sync_lock_init */
125 /* destroy the global locks */
126 static void ms_sync_lock_destroy()
128 pthread_mutex_destroy(&ms_global
.init_lock
.lock
);
129 pthread_cond_destroy(&ms_global
.init_lock
.cond
);
131 pthread_mutex_destroy(&ms_global
.run_lock
.lock
);
132 pthread_cond_destroy(&ms_global
.run_lock
.cond
);
134 pthread_mutex_destroy(&ms_global
.quit_mutex
);
135 pthread_mutex_destroy(&ms_global
.seq_mutex
);
137 if (ms_setting
.stat_freq
> 0)
139 pthread_mutex_destroy(&ms_statistic
.stat_mutex
);
141 } /* ms_sync_lock_destroy */
144 /* initialize the global structure */
145 static void ms_global_struct_init()
148 ms_global
.finish_warmup
= false;
149 ms_global
.time_out
= false;
153 /* destroy the global structure */
154 static void ms_global_struct_destroy()
156 ms_sync_lock_destroy();
161 * output the version information
163 * @param command_name, the string of this process
165 static void ms_version_command(const char *command_name
)
167 printf("%s v%u.%u\n", command_name
, 1U, 0U);
173 * get the description of the option
175 * @param option, option of command line
177 * @return char*, description of the command option
179 static const char *ms_lookup_help(ms_options_t option
)
185 "List one or more servers to connect. Servers count must be less than\n"
186 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
189 return "Display the version of the application and then exit.";
192 return "Display this message and then exit.";
194 case OPT_EXECUTE_NUMBER
:
195 return "Number of operations(get and set) to execute for the\n"
196 " given test. Default 1000000.";
198 case OPT_THREAD_NUMBER
:
200 "Number of threads to startup, better equal to CPU numbers. Default 8.";
202 case OPT_CONCURRENCY
:
203 return "Number of concurrency to simulate with load. Default 128.";
206 return "Fixed length of value.";
209 return "The proportion of date verification, e.g.: --verify=0.01";
211 case OPT_GETS_DIVISION
:
212 return "Number of keys to multi-get once. Default 1, means single get.";
216 "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
217 " d-days e.g.: --time=2h.";
221 "Load the configure file to get command,key and value distribution list.";
223 case OPT_WINDOW_SIZE
:
225 "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
230 "UDP support, default memslap uses TCP, TCP port and UDP port of\n"
231 " server must be same.";
235 "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
236 " Default no object with expire time";
240 "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
241 " Default never overwrite object.";
245 "Frequency of dumping statistic information. suffix: s-seconds,\n"
246 " m-minutes, e.g.: --resp_freq=10s.";
248 case OPT_SOCK_PER_CONN
:
249 return "Number of TCP socks per concurrency. Default 1.";
253 "Reconnect support, when connection is closed it will be reconnected.";
257 "Whether it outputs detailed information when verification fails.";
259 case OPT_FACEBOOK_TEST
:
261 "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
263 case OPT_BINARY_PROTOCOL
:
265 "Whether it enables binary protocol. Default with ASCII protocol.";
268 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
270 case OPT_REP_WRITE_SRV
:
271 return "The first nth servers can write data, e.g.: --rep_write=2.";
274 return "Forgot to document this option :)";
276 } /* ms_lookup_help */
280 * output the help information
282 * @param command_name, the string of this process
283 * @param description, description of this process
284 * @param long_options, global options array
286 void ms_help_command(const char *command_name
, const char *description
)
288 char *help_message
= NULL
;
290 printf("%s v%u.%u\n", command_name
, 1U, 0U);
291 printf(" %s\n\n", description
);
294 " memslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
297 for (int x
= 0; long_options
[x
].name
; x
++)
299 printf(" -%c, --%s%c\n", long_options
[x
].val
, long_options
[x
].name
,
300 long_options
[x
].has_arg
? '=' : ' ');
301 if ((help_message
= (char *)ms_lookup_help(long_options
[x
].val
)) != NULL
)
303 printf(" %s\n", help_message
);
309 " memslap -s 127.0.0.1:11211 -S 5s\n"
310 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
311 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
312 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
313 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
314 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
315 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
318 } /* ms_help_command */
321 /* used to parse the time string */
322 static int64_t ms_parse_time()
325 char unit
= optarg
[strlen(optarg
) - 1];
327 optarg
[strlen(optarg
) - 1]= '\0';
354 } /* ms_parse_time */
357 /* used to parse the size string */
358 static int64_t ms_parse_size()
361 char unit
= optarg
[strlen(optarg
) - 1];
363 optarg
[strlen(optarg
) - 1]= '\0';
364 ret
= strtoll(optarg
, (char **)NULL
, 10);
380 ret
*= 1024 * 1024 * 1024;
389 } /* ms_parse_size */
392 /* used to parse the options of command line */
393 static void ms_options_parse(int argc
, char *argv
[])
398 while ((option_rv
= getopt_long(argc
, argv
, "VhURbaBs:x:T:c:X:v:d:"
399 "t:S:F:w:e:o:n:P:p:",
400 long_options
, &option_index
)) != -1)
407 case OPT_VERSION
: /* --version or -V */
408 ms_version_command(PROGRAM_NAME
);
411 case OPT_HELP
: /* --help or -h */
412 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
415 case OPT_SERVERS
: /* --servers or -s */
416 ms_setting
.srv_str
= strdup(optarg
);
419 case OPT_CONCURRENCY
: /* --concurrency or -c */
420 ms_setting
.nconns
= atoi(optarg
);
421 if (ms_setting
.nconns
<= 0)
423 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
428 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
429 ms_setting
.exec_num
= atoll(optarg
);
430 if (ms_setting
.exec_num
<= 0)
432 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
437 case OPT_THREAD_NUMBER
: /* --threads or -T */
438 ms_setting
.nthreads
= atoi(optarg
);
439 if (ms_setting
.nthreads
<= 0)
441 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
446 case OPT_FIXED_LTH
: /* --fixed_size or -X */
447 ms_setting
.fixed_value_size
= (size_t)atoi(optarg
);
448 if ((ms_setting
.fixed_value_size
<= 0)
449 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
451 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
456 case OPT_VERIFY
: /* --verify or -v */
457 ms_setting
.verify_percent
= atof(optarg
);
458 if ((ms_setting
.verify_percent
<= 0)
459 || (ms_setting
.verify_percent
> 1.0))
461 fprintf(stderr
, "Data verification rate must be "
462 "greater than 0 and less than 1.0. :-)\n");
467 case OPT_GETS_DIVISION
: /* --division or -d */
468 ms_setting
.mult_key_num
= atoi(optarg
);
469 if (ms_setting
.mult_key_num
<= 0)
471 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
476 case OPT_TIME
: /* --time or -t */
477 ms_setting
.run_time
= (int)ms_parse_time();
478 if (ms_setting
.run_time
== -1)
480 fprintf(stderr
, "Please specify the run time. :-)\n"
481 "'s' for second, 'm' for minute, 'h' for hour, "
482 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
486 if (ms_setting
.run_time
== 0)
488 fprintf(stderr
, "Running time can not be 0. :-)\n");
493 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */
494 ms_setting
.cfg_file
= strdup(optarg
);
497 case OPT_WINDOW_SIZE
: /* --win_size or -w */
498 ms_setting
.win_size
= (size_t)ms_parse_size();
499 if (ms_setting
.win_size
== (size_t)-1)
503 "Please specify the item window size. :-)\n"
504 "e.g.: --win_size=10k (means 10k task window size).\n");
509 case OPT_UDP
: /* --udp or -U*/
510 ms_setting
.udp
= true;
513 case OPT_EXPIRE
: /* --exp_verify or -e */
514 ms_setting
.exp_ver_per
= atof(optarg
);
515 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0))
517 fprintf(stderr
, "Expire time verification rate must be "
518 "greater than 0 and less than 1.0. :-)\n");
523 case OPT_OVERWRITE
: /* --overwrite or -o */
524 ms_setting
.overwrite_percent
= atof(optarg
);
525 if ((ms_setting
.overwrite_percent
<= 0)
526 || (ms_setting
.overwrite_percent
> 1.0))
528 fprintf(stderr
, "Objects overwrite rate must be "
529 "greater than 0 and less than 1.0. :-)\n");
534 case OPT_STAT_FREQ
: /* --stat_freq or -S */
535 ms_setting
.stat_freq
= (int)ms_parse_time();
536 if (ms_setting
.stat_freq
== -1)
538 fprintf(stderr
, "Please specify the frequency of dumping "
539 "statistic information. :-)\n"
540 "'s' for second, 'm' for minute, 'h' for hour, "
541 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
545 if (ms_setting
.stat_freq
== 0)
547 fprintf(stderr
, "The frequency of dumping statistic information "
548 "can not be 0. :-)\n");
553 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
554 ms_setting
.sock_per_conn
= atoi(optarg
);
555 if (ms_setting
.sock_per_conn
<= 0)
557 fprintf(stderr
, "Number of socks of each concurrency "
558 "must be greater than 0.:-)\n");
563 case OPT_RECONNECT
: /* --reconnect or -R */
564 ms_setting
.reconnect
= true;
567 case OPT_VERBOSE
: /* --verbose or -b */
568 ms_setting
.verbose
= true;
571 case OPT_FACEBOOK_TEST
: /* --facebook or -a */
572 ms_setting
.facebook_test
= true;
575 case OPT_BINARY_PROTOCOL
: /* --binary or -B */
576 ms_setting
.binary_prot
= true;
579 case OPT_TPS
: /* --tps or -P */
580 ms_setting
.expected_tps
= (int)ms_parse_size();
581 if (ms_setting
.expected_tps
== -1)
584 "Please specify the item expected throughput. :-)\n"
585 "e.g.: --tps=10k (means 10k throughput).\n");
590 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
591 ms_setting
.rep_write_srv
= atoi(optarg
);
592 if (ms_setting
.rep_write_srv
<= 0)
595 "Number of replication writing server must be greater "
602 /* getopt_long already printed an error message. */
609 } /* ms_options_parse */
612 static int ms_check_para()
614 if (ms_setting
.srv_str
== NULL
)
616 fprintf(stderr
, "No Servers provided.\n\n");
620 if (ms_setting
.nconns
% ms_setting
.nthreads
!= 0)
622 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
626 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0)
628 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
633 } /* ms_check_para */
636 /* initialize the statistic structure */
637 static void ms_statistic_init()
639 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
640 ms_init_stats(&ms_statistic
.get_stat
, "Get");
641 ms_init_stats(&ms_statistic
.set_stat
, "Set");
642 ms_init_stats(&ms_statistic
.total_stat
, "Total");
643 } /* ms_statistic_init */
646 /* initialize the global state structure */
647 static void ms_stats_init()
649 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
650 if (ms_setting
.stat_freq
> 0)
654 } /* ms_stats_init */
657 /* use to output the statistic */
658 static void ms_print_statistics(int in_time
)
660 int obj_size
= (int)(ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
662 printf("\033[1;1H\033[2J\n");
663 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
,
664 ms_setting
.stat_freq
, obj_size
);
665 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
,
666 ms_setting
.stat_freq
, obj_size
);
667 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
,
668 ms_setting
.stat_freq
, obj_size
);
669 } /* ms_print_statistics */
672 /* used to print the states of memslap */
673 static void ms_print_memslap_stats(struct timeval
*start_time
,
674 struct timeval
*end_time
)
681 (unsigned long long)ms_stats
.cmd_get
);
684 (unsigned long long)ms_stats
.cmd_set
);
686 "get_misses: %llu\n",
687 (unsigned long long)ms_stats
.get_misses
);
689 if (ms_setting
.verify_percent
> 0)
691 pos
+= sprintf(pos
, "verify_misses: %llu\n",
692 (unsigned long long)ms_stats
.vef_miss
);
693 pos
+= sprintf(pos
, "verify_failed: %llu\n",
694 (unsigned long long)ms_stats
.vef_failed
);
697 if (ms_setting
.exp_ver_per
> 0)
699 pos
+= sprintf(pos
, "expired_get: %llu\n",
700 (unsigned long long)ms_stats
.exp_get
);
701 pos
+= sprintf(pos
, "unexpired_unget: %llu\n",
702 (unsigned long long)ms_stats
.unexp_unget
);
706 "written_bytes: %llu\n",
707 (unsigned long long)ms_stats
.bytes_written
);
709 "read_bytes: %llu\n",
710 (unsigned long long)ms_stats
.bytes_read
);
712 "object_bytes: %llu\n",
713 (unsigned long long)ms_stats
.obj_bytes
);
715 if (ms_setting
.udp
|| ms_setting
.facebook_test
)
718 "packet_disorder: %llu\n",
719 (unsigned long long)ms_stats
.pkt_disorder
);
721 "packet_drop: %llu\n",
722 (unsigned long long)ms_stats
.pkt_drop
);
724 "udp_timeout: %llu\n",
725 (unsigned long long)ms_stats
.udp_timeout
);
728 if (ms_setting
.stat_freq
> 0)
730 ms_dump_stats(&ms_statistic
.get_stat
);
731 ms_dump_stats(&ms_statistic
.set_stat
);
732 ms_dump_stats(&ms_statistic
.total_stat
);
735 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
738 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
739 (double)time_diff
/ 1000000,
740 (unsigned long long)(ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
742 + ms_stats
.cmd_set
) / ((long double)time_diff
/ 1000000),
744 ms_stats
.bytes_written
745 + ms_stats
.bytes_read
) / 1024 / 1024
746 / ((double)time_diff
/ 1000000));
748 fprintf(stdout
, "%s", buf
);
750 } /* ms_print_memslap_stats */
753 /* the loop of the main thread, wait the work threads to complete */
754 static void ms_monitor_slap_mode()
757 struct timeval start_time
, end_time
;
759 /* only when there is no set operation it need warm up */
760 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
)
762 /* Wait all the connects complete warm up. */
763 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
764 while (ms_global
.init_lock
.count
< ms_setting
.nconns
)
766 pthread_cond_wait(&ms_global
.init_lock
.cond
,
767 &ms_global
.init_lock
.lock
);
769 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
772 ms_global
.finish_warmup
= true;
774 /* running in "run time" mode, user specify run time */
775 if (ms_setting
.run_time
> 0)
777 gettimeofday(&start_time
, NULL
);
783 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
784 && ((int32_t)ms_stats
.active_conns
>= ms_setting
.nconns
)
785 && (ms_stats
.active_conns
<= (uint32_t)INT_MAX
))
787 ms_print_statistics(second
);
790 if (ms_setting
.run_time
<= second
)
792 ms_global
.time_out
= true;
796 /* all connections disconnect */
797 if ((second
> 5) && (ms_stats
.active_conns
== 0))
802 gettimeofday(&end_time
, NULL
);
803 sleep(1); /* wait all threads clean up */
807 /* running in "execute number" mode, user specify execute number */
808 gettimeofday(&start_time
, NULL
);
811 * We loop until we know that all connects have cleaned up.
813 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
814 while (ms_global
.run_lock
.count
< ms_setting
.nconns
)
816 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
818 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
820 gettimeofday(&end_time
, NULL
);
823 ms_print_memslap_stats(&start_time
, &end_time
);
824 } /* ms_monitor_slap_mode */
827 /* the main function */
828 int main(int argc
, char *argv
[])
830 srandom((unsigned int)time(NULL
));
831 ms_global_struct_init();
834 ms_setting_init_pre();
835 ms_options_parse(argc
, argv
);
838 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
841 ms_setting_init_post();
845 /* waiting work thread complete its task */
846 ms_monitor_slap_mode();
850 ms_global_struct_destroy();
851 ms_setting_cleanup();