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>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
33 #include "ms_sigsegv.h"
34 #include "ms_setting.h"
35 #include "ms_thread.h"
37 #define PROGRAM_NAME "memslap"
38 #define PROGRAM_DESCRIPTION \
39 "Generates workload against memcached servers."
42 /* For some odd reason the option struct on solaris defines the argument
43 * as char* and not const char*
45 #define OPTIONSTRING char*
47 #define OPTIONSTRING const char*
51 static struct option long_options
[]=
53 { (OPTIONSTRING
)"servers", required_argument
, NULL
,
55 { (OPTIONSTRING
)"threads", required_argument
, NULL
,
57 { (OPTIONSTRING
)"concurrency", required_argument
, NULL
,
59 { (OPTIONSTRING
)"conn_sock", required_argument
, NULL
,
61 { (OPTIONSTRING
)"execute_number", required_argument
, NULL
,
63 { (OPTIONSTRING
)"time", required_argument
, NULL
,
65 { (OPTIONSTRING
)"cfg_cmd", required_argument
, NULL
,
67 { (OPTIONSTRING
)"win_size", required_argument
, NULL
,
69 { (OPTIONSTRING
)"fixed_size", required_argument
, NULL
,
71 { (OPTIONSTRING
)"verify", required_argument
, NULL
,
73 { (OPTIONSTRING
)"division", required_argument
, NULL
,
75 { (OPTIONSTRING
)"stat_freq", required_argument
, NULL
,
77 { (OPTIONSTRING
)"exp_verify", required_argument
, NULL
,
79 { (OPTIONSTRING
)"overwrite", required_argument
, NULL
,
81 { (OPTIONSTRING
)"reconnect", no_argument
, NULL
,
83 { (OPTIONSTRING
)"udp", no_argument
, NULL
,
85 { (OPTIONSTRING
)"facebook", no_argument
, NULL
,
87 { (OPTIONSTRING
)"binary", no_argument
, NULL
,
88 OPT_BINARY_PROTOCOL
},
89 { (OPTIONSTRING
)"tps", required_argument
, NULL
,
91 { (OPTIONSTRING
)"rep_write", required_argument
, NULL
,
93 { (OPTIONSTRING
)"verbose", no_argument
, NULL
,
95 { (OPTIONSTRING
)"help", no_argument
, NULL
,
97 { (OPTIONSTRING
)"version", no_argument
, NULL
,
103 static void ms_sync_lock_init(void);
104 static void ms_sync_lock_destroy(void);
105 static void ms_global_struct_init(void);
106 static void ms_global_struct_destroy(void);
107 static void ms_version_command(const char *command_name
);
108 static const char *ms_lookup_help(ms_options_t option
);
109 static int64_t ms_parse_time(void);
110 static int64_t ms_parse_size(void);
111 static void ms_options_parse(int argc
, char *argv
[]);
112 static int ms_check_para(void);
113 static void ms_statistic_init(void);
114 static void ms_stats_init(void);
115 static void ms_print_statistics(int in_time
);
116 static void ms_print_memslap_stats(struct timeval
*start_time
,
117 struct timeval
*end_time
);
118 static void ms_monitor_slap_mode(void);
119 void ms_help_command(const char *command_name
, const char *description
);
122 /* initialize the global locks */
123 static void ms_sync_lock_init()
125 ms_global
.init_lock
.count
= 0;
126 pthread_mutex_init(&ms_global
.init_lock
.lock
, NULL
);
127 pthread_cond_init(&ms_global
.init_lock
.cond
, NULL
);
129 ms_global
.warmup_lock
.count
= 0;
130 pthread_mutex_init(&ms_global
.warmup_lock
.lock
, NULL
);
131 pthread_cond_init(&ms_global
.warmup_lock
.cond
, NULL
);
133 ms_global
.run_lock
.count
= 0;
134 pthread_mutex_init(&ms_global
.run_lock
.lock
, NULL
);
135 pthread_cond_init(&ms_global
.run_lock
.cond
, NULL
);
137 pthread_mutex_init(&ms_global
.quit_mutex
, NULL
);
138 pthread_mutex_init(&ms_global
.seq_mutex
, NULL
);
139 } /* ms_sync_lock_init */
142 /* destroy the global locks */
143 static void ms_sync_lock_destroy()
145 pthread_mutex_destroy(&ms_global
.init_lock
.lock
);
146 pthread_cond_destroy(&ms_global
.init_lock
.cond
);
148 pthread_mutex_destroy(&ms_global
.warmup_lock
.lock
);
149 pthread_cond_destroy(&ms_global
.warmup_lock
.cond
);
151 pthread_mutex_destroy(&ms_global
.run_lock
.lock
);
152 pthread_cond_destroy(&ms_global
.run_lock
.cond
);
154 pthread_mutex_destroy(&ms_global
.quit_mutex
);
155 pthread_mutex_destroy(&ms_global
.seq_mutex
);
157 if (ms_setting
.stat_freq
> 0)
159 pthread_mutex_destroy(&ms_statistic
.stat_mutex
);
161 } /* ms_sync_lock_destroy */
164 /* initialize the global structure */
165 static void ms_global_struct_init()
168 ms_global
.finish_warmup
= false;
169 ms_global
.time_out
= false;
173 /* destroy the global structure */
174 static void ms_global_struct_destroy()
176 ms_sync_lock_destroy();
181 * output the version information
183 * @param command_name, the string of this process
185 static void ms_version_command(const char *command_name
)
187 printf("%s v%u.%u\n", command_name
, 1U, 0U);
193 * get the description of the option
195 * @param option, option of command line
197 * @return char*, description of the command option
199 static const char *ms_lookup_help(ms_options_t option
)
205 "List one or more servers to connect. Servers count must be less than\n"
206 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
209 return "Display the version of the application and then exit.";
212 return "Display this message and then exit.";
214 case OPT_EXECUTE_NUMBER
:
215 return "Number of operations(get and set) to execute for the\n"
216 " given test. Default 1000000.";
218 case OPT_THREAD_NUMBER
:
220 "Number of threads to startup, better equal to CPU numbers. Default 8.";
222 case OPT_CONCURRENCY
:
223 return "Number of concurrency to simulate with load. Default 128.";
226 return "Fixed length of value.";
229 return "The proportion of date verification, e.g.: --verify=0.01";
231 case OPT_GETS_DIVISION
:
232 return "Number of keys to multi-get once. Default 1, means single get.";
236 "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
237 " d-days e.g.: --time=2h.";
241 "Load the configure file to get command,key and value distribution list.";
243 case OPT_WINDOW_SIZE
:
245 "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
250 "UDP support, default memslap uses TCP, TCP port and UDP port of\n"
251 " server must be same.";
255 "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
256 " Default no object with expire time";
260 "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
261 " Default never overwrite object.";
265 "Frequency of dumping statistic information. suffix: s-seconds,\n"
266 " m-minutes, e.g.: --resp_freq=10s.";
268 case OPT_SOCK_PER_CONN
:
269 return "Number of TCP socks per concurrency. Default 1.";
273 "Reconnect support, when connection is closed it will be reconnected.";
277 "Whether it outputs detailed information when verification fails.";
279 case OPT_FACEBOOK_TEST
:
281 "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
283 case OPT_BINARY_PROTOCOL
:
285 "Whether it enables binary protocol. Default with ASCII protocol.";
288 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
290 case OPT_REP_WRITE_SRV
:
291 return "The first nth servers can write data, e.g.: --rep_write=2.";
294 return "Forgot to document this option :)";
296 } /* ms_lookup_help */
300 * output the help information
302 * @param command_name, the string of this process
303 * @param description, description of this process
304 * @param long_options, global options array
306 void ms_help_command(const char *command_name
, const char *description
)
308 char *help_message
= NULL
;
310 printf("%s v%u.%u\n", command_name
, 1U, 0U);
311 printf(" %s\n\n", description
);
314 " memslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
317 for (int x
= 0; long_options
[x
].name
; x
++)
319 printf(" -%c, --%s%c\n", long_options
[x
].val
, long_options
[x
].name
,
320 long_options
[x
].has_arg
? '=' : ' ');
322 if ((help_message
= (char *)ms_lookup_help(long_options
[x
].val
)) != NULL
)
324 printf(" %s\n", help_message
);
330 " memslap -s 127.0.0.1:11211 -S 5s\n"
331 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
332 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
333 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
334 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
335 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
336 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
339 } /* ms_help_command */
342 /* used to parse the time string */
343 static int64_t ms_parse_time()
346 char unit
= optarg
[strlen(optarg
) - 1];
348 optarg
[strlen(optarg
) - 1]= '\0';
375 } /* ms_parse_time */
378 /* used to parse the size string */
379 static int64_t ms_parse_size()
382 char unit
= optarg
[strlen(optarg
) - 1];
384 optarg
[strlen(optarg
) - 1]= '\0';
385 ret
= strtoll(optarg
, (char **)NULL
, 10);
401 ret
*= 1024 * 1024 * 1024;
410 } /* ms_parse_size */
413 /* used to parse the options of command line */
414 static void ms_options_parse(int argc
, char *argv
[])
419 while ((option_rv
= getopt_long(argc
, argv
, "VhURbaBs:x:T:c:X:v:d:"
420 "t:S:F:w:e:o:n:P:p:",
421 long_options
, &option_index
)) != -1)
428 case OPT_VERSION
: /* --version or -V */
429 ms_version_command(PROGRAM_NAME
);
432 case OPT_HELP
: /* --help or -h */
433 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
436 case OPT_SERVERS
: /* --servers or -s */
437 ms_setting
.srv_str
= strdup(optarg
);
440 case OPT_CONCURRENCY
: /* --concurrency or -c */
441 ms_setting
.nconns
= (uint32_t)strtoul(optarg
, (char **) NULL
, 10);
442 if (ms_setting
.nconns
<= 0)
444 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
449 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
450 ms_setting
.exec_num
= (int)strtol(optarg
, (char **) NULL
, 10);
451 if (ms_setting
.exec_num
<= 0)
453 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
458 case OPT_THREAD_NUMBER
: /* --threads or -T */
459 ms_setting
.nthreads
= (uint32_t)strtoul(optarg
, (char **) NULL
, 10);
460 if (ms_setting
.nthreads
<= 0)
462 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
467 case OPT_FIXED_LTH
: /* --fixed_size or -X */
468 ms_setting
.fixed_value_size
= (size_t)strtoull(optarg
, (char **) NULL
, 10);
469 if ((ms_setting
.fixed_value_size
<= 0)
470 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
472 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
477 case OPT_VERIFY
: /* --verify or -v */
478 ms_setting
.verify_percent
= atof(optarg
);
479 if ((ms_setting
.verify_percent
<= 0)
480 || (ms_setting
.verify_percent
> 1.0))
482 fprintf(stderr
, "Data verification rate must be "
483 "greater than 0 and less than 1.0. :-)\n");
488 case OPT_GETS_DIVISION
: /* --division or -d */
489 ms_setting
.mult_key_num
= (int)strtol(optarg
, (char **) NULL
, 10);
490 if (ms_setting
.mult_key_num
<= 0)
492 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
497 case OPT_TIME
: /* --time or -t */
498 ms_setting
.run_time
= (int)ms_parse_time();
499 if (ms_setting
.run_time
== -1)
501 fprintf(stderr
, "Please specify the run time. :-)\n"
502 "'s' for second, 'm' for minute, 'h' for hour, "
503 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
507 if (ms_setting
.run_time
== 0)
509 fprintf(stderr
, "Running time can not be 0. :-)\n");
514 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */
515 ms_setting
.cfg_file
= strdup(optarg
);
518 case OPT_WINDOW_SIZE
: /* --win_size or -w */
519 ms_setting
.win_size
= (size_t)ms_parse_size();
520 if (ms_setting
.win_size
== (size_t)-1)
524 "Please specify the item window size. :-)\n"
525 "e.g.: --win_size=10k (means 10k task window size).\n");
530 case OPT_UDP
: /* --udp or -U*/
531 ms_setting
.udp
= true;
534 case OPT_EXPIRE
: /* --exp_verify or -e */
535 ms_setting
.exp_ver_per
= atof(optarg
);
536 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0))
538 fprintf(stderr
, "Expire time verification rate must be "
539 "greater than 0 and less than 1.0. :-)\n");
544 case OPT_OVERWRITE
: /* --overwrite or -o */
545 ms_setting
.overwrite_percent
= atof(optarg
);
546 if ((ms_setting
.overwrite_percent
<= 0)
547 || (ms_setting
.overwrite_percent
> 1.0))
549 fprintf(stderr
, "Objects overwrite rate must be "
550 "greater than 0 and less than 1.0. :-)\n");
555 case OPT_STAT_FREQ
: /* --stat_freq or -S */
556 ms_setting
.stat_freq
= (int)ms_parse_time();
557 if (ms_setting
.stat_freq
== -1)
559 fprintf(stderr
, "Please specify the frequency of dumping "
560 "statistic information. :-)\n"
561 "'s' for second, 'm' for minute, 'h' for hour, "
562 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
566 if (ms_setting
.stat_freq
== 0)
568 fprintf(stderr
, "The frequency of dumping statistic information "
569 "can not be 0. :-)\n");
574 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
575 ms_setting
.sock_per_conn
= (uint32_t)strtoul(optarg
, (char **) NULL
, 10);
576 if (ms_setting
.sock_per_conn
<= 0)
578 fprintf(stderr
, "Number of socks of each concurrency "
579 "must be greater than 0.:-)\n");
584 case OPT_RECONNECT
: /* --reconnect or -R */
585 ms_setting
.reconnect
= true;
588 case OPT_VERBOSE
: /* --verbose or -b */
589 ms_setting
.verbose
= true;
592 case OPT_FACEBOOK_TEST
: /* --facebook or -a */
593 ms_setting
.facebook_test
= true;
596 case OPT_BINARY_PROTOCOL
: /* --binary or -B */
597 ms_setting
.binary_prot
= true;
600 case OPT_TPS
: /* --tps or -P */
601 ms_setting
.expected_tps
= (int)ms_parse_size();
602 if (ms_setting
.expected_tps
== -1)
605 "Please specify the item expected throughput. :-)\n"
606 "e.g.: --tps=10k (means 10k throughput).\n");
611 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
612 ms_setting
.rep_write_srv
= (uint32_t)strtoul(optarg
, (char **) NULL
, 10);
613 if (ms_setting
.rep_write_srv
<= 0)
616 "Number of replication writing server must be greater "
623 /* getopt_long already printed an error message. */
630 } /* ms_options_parse */
633 static int ms_check_para()
635 if (ms_setting
.srv_str
== NULL
)
639 if ((temp
= getenv("MEMCACHED_SERVERS")))
641 ms_setting
.srv_str
= strdup(temp
);
645 fprintf(stderr
, "No Servers provided\n\n");
650 if (ms_setting
.nconns
% (uint32_t)ms_setting
.nthreads
!= 0)
652 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
656 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0)
658 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
663 } /* ms_check_para */
666 /* initialize the statistic structure */
667 static void ms_statistic_init()
669 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
670 ms_init_stats(&ms_statistic
.get_stat
, "Get");
671 ms_init_stats(&ms_statistic
.set_stat
, "Set");
672 ms_init_stats(&ms_statistic
.total_stat
, "Total");
673 } /* ms_statistic_init */
676 /* initialize the global state structure */
677 static void ms_stats_init()
679 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
680 if (ms_setting
.stat_freq
> 0)
684 } /* ms_stats_init */
687 /* use to output the statistic */
688 static void ms_print_statistics(int in_time
)
690 int obj_size
= (int)(ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
692 printf("\033[1;1H\033[2J\n");
693 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
,
694 ms_setting
.stat_freq
, obj_size
);
695 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
,
696 ms_setting
.stat_freq
, obj_size
);
697 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
,
698 ms_setting
.stat_freq
, obj_size
);
699 } /* ms_print_statistics */
702 /* used to print the states of memslap */
703 static void ms_print_memslap_stats(struct timeval
*start_time
,
704 struct timeval
*end_time
)
709 pos
+= sprintf(pos
, "cmd_get: %zu\n",
711 pos
+= sprintf(pos
, "cmd_set: %zu\n",
713 pos
+= sprintf(pos
, "get_misses: %zu\n",
714 ms_stats
.get_misses
);
716 if (ms_setting
.verify_percent
> 0)
718 pos
+= sprintf(pos
, "verify_misses: %zu\n",
720 pos
+= sprintf(pos
, "verify_failed: %zu\n",
721 ms_stats
.vef_failed
);
724 if (ms_setting
.exp_ver_per
> 0)
726 pos
+= sprintf(pos
, "expired_get: %zu\n",
728 pos
+= sprintf(pos
, "unexpired_unget: %zu\n",
729 ms_stats
.unexp_unget
);
733 "written_bytes: %zu\n",
734 ms_stats
.bytes_written
);
735 pos
+= sprintf(pos
, "read_bytes: %zu\n",
736 ms_stats
.bytes_read
);
737 pos
+= sprintf(pos
, "object_bytes: %zu\n",
740 if (ms_setting
.udp
|| ms_setting
.facebook_test
)
742 pos
+= sprintf(pos
, "packet_disorder: %zu\n",
743 ms_stats
.pkt_disorder
);
744 pos
+= sprintf(pos
, "packet_drop: %zu\n",
746 pos
+= sprintf(pos
, "udp_timeout: %zu\n",
747 ms_stats
.udp_timeout
);
750 if (ms_setting
.stat_freq
> 0)
752 ms_dump_stats(&ms_statistic
.get_stat
);
753 ms_dump_stats(&ms_statistic
.set_stat
);
754 ms_dump_stats(&ms_statistic
.total_stat
);
757 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
760 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
761 (double)time_diff
/ 1000000,
762 (unsigned long long)(ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
764 + ms_stats
.cmd_set
) / ((long double)time_diff
/ 1000000),
766 ms_stats
.bytes_written
767 + ms_stats
.bytes_read
) / 1024 / 1024
768 / ((double)time_diff
/ 1000000));
770 fprintf(stdout
, "%s", buf
);
772 } /* ms_print_memslap_stats */
775 /* the loop of the main thread, wait the work threads to complete */
776 static void ms_monitor_slap_mode()
779 struct timeval start_time
, end_time
;
781 /* Wait all the threads complete initialization. */
782 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
783 while (ms_global
.init_lock
.count
< ms_setting
.nthreads
)
785 pthread_cond_wait(&ms_global
.init_lock
.cond
,
786 &ms_global
.init_lock
.lock
);
788 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
790 /* only when there is no set operation it need warm up */
791 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
)
793 /* Wait all the connects complete warm up. */
794 pthread_mutex_lock(&ms_global
.warmup_lock
.lock
);
795 while (ms_global
.warmup_lock
.count
< ms_setting
.nconns
)
797 pthread_cond_wait(&ms_global
.warmup_lock
.cond
, &ms_global
.warmup_lock
.lock
);
799 pthread_mutex_unlock(&ms_global
.warmup_lock
.lock
);
801 ms_global
.finish_warmup
= true;
803 /* running in "run time" mode, user specify run time */
804 if (ms_setting
.run_time
> 0)
806 gettimeofday(&start_time
, NULL
);
812 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
813 && (ms_stats
.active_conns
>= ms_setting
.nconns
)
814 && (ms_stats
.active_conns
<= INT_MAX
))
816 ms_print_statistics(second
);
819 if (ms_setting
.run_time
<= second
)
821 ms_global
.time_out
= true;
825 /* all connections disconnect */
826 if ((second
> 5) && (ms_stats
.active_conns
== 0))
831 gettimeofday(&end_time
, NULL
);
832 sleep(1); /* wait all threads clean up */
836 /* running in "execute number" mode, user specify execute number */
837 gettimeofday(&start_time
, NULL
);
840 * We loop until we know that all connects have cleaned up.
842 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
843 while (ms_global
.run_lock
.count
< ms_setting
.nconns
)
845 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
847 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
849 gettimeofday(&end_time
, NULL
);
852 ms_print_memslap_stats(&start_time
, &end_time
);
853 } /* ms_monitor_slap_mode */
856 /* the main function */
857 int main(int argc
, char *argv
[])
859 srandom((unsigned int)time(NULL
));
860 ms_global_struct_init();
863 ms_setting_init_pre();
864 ms_options_parse(argc
, argv
);
867 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
870 ms_setting_init_post();
874 /* waiting work thread complete its task */
875 ms_monitor_slap_mode();
879 ms_global_struct_destroy();
880 ms_setting_cleanup();