efda8affae122722bb2f5aae61db84ad7d31488d
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 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
.run_lock
.count
= 0;
130 pthread_mutex_init(&ms_global
.run_lock
.lock
, NULL
);
131 pthread_cond_init(&ms_global
.run_lock
.cond
, NULL
);
133 pthread_mutex_init(&ms_global
.quit_mutex
, NULL
);
134 pthread_mutex_init(&ms_global
.seq_mutex
, NULL
);
135 } /* ms_sync_lock_init */
138 /* destroy the global locks */
139 static void ms_sync_lock_destroy()
141 pthread_mutex_destroy(&ms_global
.init_lock
.lock
);
142 pthread_cond_destroy(&ms_global
.init_lock
.cond
);
144 pthread_mutex_destroy(&ms_global
.run_lock
.lock
);
145 pthread_cond_destroy(&ms_global
.run_lock
.cond
);
147 pthread_mutex_destroy(&ms_global
.quit_mutex
);
148 pthread_mutex_destroy(&ms_global
.seq_mutex
);
150 if (ms_setting
.stat_freq
> 0)
152 pthread_mutex_destroy(&ms_statistic
.stat_mutex
);
154 } /* ms_sync_lock_destroy */
157 /* initialize the global structure */
158 static void ms_global_struct_init()
161 ms_global
.finish_warmup
= false;
162 ms_global
.time_out
= false;
166 /* destroy the global structure */
167 static void ms_global_struct_destroy()
169 ms_sync_lock_destroy();
174 * output the version information
176 * @param command_name, the string of this process
178 static void ms_version_command(const char *command_name
)
180 printf("%s v%u.%u\n", command_name
, 1U, 0U);
186 * get the description of the option
188 * @param option, option of command line
190 * @return char*, description of the command option
192 static const char *ms_lookup_help(ms_options_t option
)
198 "List one or more servers to connect. Servers count must be less than\n"
199 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
202 return "Display the version of the application and then exit.";
205 return "Display this message and then exit.";
207 case OPT_EXECUTE_NUMBER
:
208 return "Number of operations(get and set) to execute for the\n"
209 " given test. Default 1000000.";
211 case OPT_THREAD_NUMBER
:
213 "Number of threads to startup, better equal to CPU numbers. Default 8.";
215 case OPT_CONCURRENCY
:
216 return "Number of concurrency to simulate with load. Default 128.";
219 return "Fixed length of value.";
222 return "The proportion of date verification, e.g.: --verify=0.01";
224 case OPT_GETS_DIVISION
:
225 return "Number of keys to multi-get once. Default 1, means single get.";
229 "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
230 " d-days e.g.: --time=2h.";
234 "Load the configure file to get command,key and value distribution list.";
236 case OPT_WINDOW_SIZE
:
238 "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
243 "UDP support, default memslap uses TCP, TCP port and UDP port of\n"
244 " server must be same.";
248 "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
249 " Default no object with expire time";
253 "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
254 " Default never overwrite object.";
258 "Frequency of dumping statistic information. suffix: s-seconds,\n"
259 " m-minutes, e.g.: --resp_freq=10s.";
261 case OPT_SOCK_PER_CONN
:
262 return "Number of TCP socks per concurrency. Default 1.";
266 "Reconnect support, when connection is closed it will be reconnected.";
270 "Whether it outputs detailed information when verification fails.";
272 case OPT_FACEBOOK_TEST
:
274 "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
276 case OPT_BINARY_PROTOCOL
:
278 "Whether it enables binary protocol. Default with ASCII protocol.";
281 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
283 case OPT_REP_WRITE_SRV
:
284 return "The first nth servers can write data, e.g.: --rep_write=2.";
287 return "Forgot to document this option :)";
289 } /* ms_lookup_help */
293 * output the help information
295 * @param command_name, the string of this process
296 * @param description, description of this process
297 * @param long_options, global options array
299 void ms_help_command(const char *command_name
, const char *description
)
301 char *help_message
= NULL
;
303 printf("%s v%u.%u\n", command_name
, 1U, 0U);
304 printf(" %s\n\n", description
);
307 " memslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
310 for (int x
= 0; long_options
[x
].name
; x
++)
312 printf(" -%c, --%s%c\n", long_options
[x
].val
, long_options
[x
].name
,
313 long_options
[x
].has_arg
? '=' : ' ');
315 if ((help_message
= (char *)ms_lookup_help(long_options
[x
].val
)) != NULL
)
317 printf(" %s\n", help_message
);
323 " memslap -s 127.0.0.1:11211 -S 5s\n"
324 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
325 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
326 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
327 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
328 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
329 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
332 } /* ms_help_command */
335 /* used to parse the time string */
336 static int64_t ms_parse_time()
339 char unit
= optarg
[strlen(optarg
) - 1];
341 optarg
[strlen(optarg
) - 1]= '\0';
368 } /* ms_parse_time */
371 /* used to parse the size string */
372 static int64_t ms_parse_size()
375 char unit
= optarg
[strlen(optarg
) - 1];
377 optarg
[strlen(optarg
) - 1]= '\0';
378 ret
= strtoll(optarg
, (char **)NULL
, 10);
394 ret
*= 1024 * 1024 * 1024;
403 } /* ms_parse_size */
406 /* used to parse the options of command line */
407 static void ms_options_parse(int argc
, char *argv
[])
412 while ((option_rv
= getopt_long(argc
, argv
, "VhURbaBs:x:T:c:X:v:d:"
413 "t:S:F:w:e:o:n:P:p:",
414 long_options
, &option_index
)) != -1)
421 case OPT_VERSION
: /* --version or -V */
422 ms_version_command(PROGRAM_NAME
);
425 case OPT_HELP
: /* --help or -h */
426 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
429 case OPT_SERVERS
: /* --servers or -s */
430 ms_setting
.srv_str
= strdup(optarg
);
433 case OPT_CONCURRENCY
: /* --concurrency or -c */
434 ms_setting
.nconns
= (int)strtol(optarg
, (char **) NULL
, 10);
435 if (ms_setting
.nconns
<= 0)
437 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
442 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
443 ms_setting
.exec_num
= (int)strtol(optarg
, (char **) NULL
, 10);
444 if (ms_setting
.exec_num
<= 0)
446 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
451 case OPT_THREAD_NUMBER
: /* --threads or -T */
452 ms_setting
.nthreads
= (int)strtol(optarg
, (char **) NULL
, 10);
453 if (ms_setting
.nthreads
<= 0)
455 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
460 case OPT_FIXED_LTH
: /* --fixed_size or -X */
461 ms_setting
.fixed_value_size
= (size_t)strtoull(optarg
, (char **) NULL
, 10);
462 if ((ms_setting
.fixed_value_size
<= 0)
463 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
465 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
470 case OPT_VERIFY
: /* --verify or -v */
471 ms_setting
.verify_percent
= atof(optarg
);
472 if ((ms_setting
.verify_percent
<= 0)
473 || (ms_setting
.verify_percent
> 1.0))
475 fprintf(stderr
, "Data verification rate must be "
476 "greater than 0 and less than 1.0. :-)\n");
481 case OPT_GETS_DIVISION
: /* --division or -d */
482 ms_setting
.mult_key_num
= (int)strtol(optarg
, (char **) NULL
, 10);
483 if (ms_setting
.mult_key_num
<= 0)
485 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
490 case OPT_TIME
: /* --time or -t */
491 ms_setting
.run_time
= (int)ms_parse_time();
492 if (ms_setting
.run_time
== -1)
494 fprintf(stderr
, "Please specify the run time. :-)\n"
495 "'s' for second, 'm' for minute, 'h' for hour, "
496 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
500 if (ms_setting
.run_time
== 0)
502 fprintf(stderr
, "Running time can not be 0. :-)\n");
507 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */
508 ms_setting
.cfg_file
= strdup(optarg
);
511 case OPT_WINDOW_SIZE
: /* --win_size or -w */
512 ms_setting
.win_size
= (size_t)ms_parse_size();
513 if (ms_setting
.win_size
== (size_t)-1)
517 "Please specify the item window size. :-)\n"
518 "e.g.: --win_size=10k (means 10k task window size).\n");
523 case OPT_UDP
: /* --udp or -U*/
524 ms_setting
.udp
= true;
527 case OPT_EXPIRE
: /* --exp_verify or -e */
528 ms_setting
.exp_ver_per
= atof(optarg
);
529 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0))
531 fprintf(stderr
, "Expire time verification rate must be "
532 "greater than 0 and less than 1.0. :-)\n");
537 case OPT_OVERWRITE
: /* --overwrite or -o */
538 ms_setting
.overwrite_percent
= atof(optarg
);
539 if ((ms_setting
.overwrite_percent
<= 0)
540 || (ms_setting
.overwrite_percent
> 1.0))
542 fprintf(stderr
, "Objects overwrite rate must be "
543 "greater than 0 and less than 1.0. :-)\n");
548 case OPT_STAT_FREQ
: /* --stat_freq or -S */
549 ms_setting
.stat_freq
= (int)ms_parse_time();
550 if (ms_setting
.stat_freq
== -1)
552 fprintf(stderr
, "Please specify the frequency of dumping "
553 "statistic information. :-)\n"
554 "'s' for second, 'm' for minute, 'h' for hour, "
555 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
559 if (ms_setting
.stat_freq
== 0)
561 fprintf(stderr
, "The frequency of dumping statistic information "
562 "can not be 0. :-)\n");
567 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
568 ms_setting
.sock_per_conn
= (int)strtol(optarg
, (char **) NULL
, 10);
569 if (ms_setting
.sock_per_conn
<= 0)
571 fprintf(stderr
, "Number of socks of each concurrency "
572 "must be greater than 0.:-)\n");
577 case OPT_RECONNECT
: /* --reconnect or -R */
578 ms_setting
.reconnect
= true;
581 case OPT_VERBOSE
: /* --verbose or -b */
582 ms_setting
.verbose
= true;
585 case OPT_FACEBOOK_TEST
: /* --facebook or -a */
586 ms_setting
.facebook_test
= true;
589 case OPT_BINARY_PROTOCOL
: /* --binary or -B */
590 ms_setting
.binary_prot
= true;
593 case OPT_TPS
: /* --tps or -P */
594 ms_setting
.expected_tps
= (int)ms_parse_size();
595 if (ms_setting
.expected_tps
== -1)
598 "Please specify the item expected throughput. :-)\n"
599 "e.g.: --tps=10k (means 10k throughput).\n");
604 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
605 ms_setting
.rep_write_srv
= (int)strtol(optarg
, (char **) NULL
, 10);
606 if (ms_setting
.rep_write_srv
<= 0)
609 "Number of replication writing server must be greater "
616 /* getopt_long already printed an error message. */
623 } /* ms_options_parse */
626 static int ms_check_para()
628 if (ms_setting
.srv_str
== NULL
)
632 if ((temp
= getenv("MEMCACHED_SERVERS")))
634 ms_setting
.srv_str
= strdup(temp
);
638 fprintf(stderr
, "No Servers provided\n\n");
643 if (ms_setting
.nconns
% ms_setting
.nthreads
!= 0)
645 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
649 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0)
651 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
656 } /* ms_check_para */
659 /* initialize the statistic structure */
660 static void ms_statistic_init()
662 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
663 ms_init_stats(&ms_statistic
.get_stat
, "Get");
664 ms_init_stats(&ms_statistic
.set_stat
, "Set");
665 ms_init_stats(&ms_statistic
.total_stat
, "Total");
666 } /* ms_statistic_init */
669 /* initialize the global state structure */
670 static void ms_stats_init()
672 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
673 if (ms_setting
.stat_freq
> 0)
677 } /* ms_stats_init */
680 /* use to output the statistic */
681 static void ms_print_statistics(int in_time
)
683 int obj_size
= (int)(ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
685 printf("\033[1;1H\033[2J\n");
686 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
,
687 ms_setting
.stat_freq
, obj_size
);
688 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
,
689 ms_setting
.stat_freq
, obj_size
);
690 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
,
691 ms_setting
.stat_freq
, obj_size
);
692 } /* ms_print_statistics */
695 /* used to print the states of memslap */
696 static void ms_print_memslap_stats(struct timeval
*start_time
,
697 struct timeval
*end_time
)
704 (unsigned long long)ms_stats
.cmd_get
);
707 (unsigned long long)ms_stats
.cmd_set
);
709 "get_misses: %llu\n",
710 (unsigned long long)ms_stats
.get_misses
);
712 if (ms_setting
.verify_percent
> 0)
714 pos
+= sprintf(pos
, "verify_misses: %llu\n",
715 (unsigned long long)ms_stats
.vef_miss
);
716 pos
+= sprintf(pos
, "verify_failed: %llu\n",
717 (unsigned long long)ms_stats
.vef_failed
);
720 if (ms_setting
.exp_ver_per
> 0)
722 pos
+= sprintf(pos
, "expired_get: %llu\n",
723 (unsigned long long)ms_stats
.exp_get
);
724 pos
+= sprintf(pos
, "unexpired_unget: %llu\n",
725 (unsigned long long)ms_stats
.unexp_unget
);
729 "written_bytes: %llu\n",
730 (unsigned long long)ms_stats
.bytes_written
);
732 "read_bytes: %llu\n",
733 (unsigned long long)ms_stats
.bytes_read
);
735 "object_bytes: %llu\n",
736 (unsigned long long)ms_stats
.obj_bytes
);
738 if (ms_setting
.udp
|| ms_setting
.facebook_test
)
741 "packet_disorder: %llu\n",
742 (unsigned long long)ms_stats
.pkt_disorder
);
744 "packet_drop: %llu\n",
745 (unsigned long long)ms_stats
.pkt_drop
);
747 "udp_timeout: %llu\n",
748 (unsigned long long)ms_stats
.udp_timeout
);
751 if (ms_setting
.stat_freq
> 0)
753 ms_dump_stats(&ms_statistic
.get_stat
);
754 ms_dump_stats(&ms_statistic
.set_stat
);
755 ms_dump_stats(&ms_statistic
.total_stat
);
758 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
761 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
762 (double)time_diff
/ 1000000,
763 (unsigned long long)(ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
765 + ms_stats
.cmd_set
) / ((long double)time_diff
/ 1000000),
767 ms_stats
.bytes_written
768 + ms_stats
.bytes_read
) / 1024 / 1024
769 / ((double)time_diff
/ 1000000));
771 fprintf(stdout
, "%s", buf
);
773 } /* ms_print_memslap_stats */
776 /* the loop of the main thread, wait the work threads to complete */
777 static void ms_monitor_slap_mode()
780 struct timeval start_time
, end_time
;
782 /* only when there is no set operation it need warm up */
783 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
)
785 /* Wait all the connects complete warm up. */
786 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
787 while (ms_global
.init_lock
.count
< ms_setting
.nconns
)
789 pthread_cond_wait(&ms_global
.init_lock
.cond
,
790 &ms_global
.init_lock
.lock
);
792 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
795 ms_global
.finish_warmup
= true;
797 /* running in "run time" mode, user specify run time */
798 if (ms_setting
.run_time
> 0)
800 gettimeofday(&start_time
, NULL
);
806 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
807 && (ms_stats
.active_conns
>= ms_setting
.nconns
)
808 && (ms_stats
.active_conns
<= INT_MAX
))
810 ms_print_statistics(second
);
813 if (ms_setting
.run_time
<= second
)
815 ms_global
.time_out
= true;
819 /* all connections disconnect */
820 if ((second
> 5) && (ms_stats
.active_conns
== 0))
825 gettimeofday(&end_time
, NULL
);
826 sleep(1); /* wait all threads clean up */
830 /* running in "execute number" mode, user specify execute number */
831 gettimeofday(&start_time
, NULL
);
834 * We loop until we know that all connects have cleaned up.
836 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
837 while (ms_global
.run_lock
.count
< ms_setting
.nconns
)
839 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
841 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
843 gettimeofday(&end_time
, NULL
);
846 ms_print_memslap_stats(&start_time
, &end_time
);
847 } /* ms_monitor_slap_mode */
850 /* the main function */
851 int main(int argc
, char *argv
[])
853 srandom((unsigned int)time(NULL
));
854 ms_global_struct_init();
857 ms_setting_init_pre();
858 ms_options_parse(argc
, argv
);
861 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
864 ms_setting_init_post();
868 /* waiting work thread complete its task */
869 ms_monitor_slap_mode();
873 ms_global_struct_destroy();
874 ms_setting_cleanup();