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 /* For some odd reason the option struct on solaris defines the argument
29 * as char* and not const char*
31 #define OPTIONSTRING char*
33 #define OPTIONSTRING const char*
37 static struct option long_options
[]=
39 { (OPTIONSTRING
)"servers", required_argument
, NULL
,
41 { (OPTIONSTRING
)"threads", required_argument
, NULL
,
43 { (OPTIONSTRING
)"concurrency", required_argument
, NULL
,
45 { (OPTIONSTRING
)"conn_sock", required_argument
, NULL
,
47 { (OPTIONSTRING
)"execute_number", required_argument
, NULL
,
49 { (OPTIONSTRING
)"time", required_argument
, NULL
,
51 { (OPTIONSTRING
)"cfg_cmd", required_argument
, NULL
,
53 { (OPTIONSTRING
)"win_size", required_argument
, NULL
,
55 { (OPTIONSTRING
)"fixed_size", required_argument
, NULL
,
57 { (OPTIONSTRING
)"verify", required_argument
, NULL
,
59 { (OPTIONSTRING
)"division", required_argument
, NULL
,
61 { (OPTIONSTRING
)"stat_freq", required_argument
, NULL
,
63 { (OPTIONSTRING
)"exp_verify", required_argument
, NULL
,
65 { (OPTIONSTRING
)"overwrite", required_argument
, NULL
,
67 { (OPTIONSTRING
)"reconnect", no_argument
, NULL
,
69 { (OPTIONSTRING
)"udp", no_argument
, NULL
,
71 { (OPTIONSTRING
)"facebook", no_argument
, NULL
,
73 { (OPTIONSTRING
)"binary", no_argument
, NULL
,
74 OPT_BINARY_PROTOCOL
},
75 { (OPTIONSTRING
)"tps", required_argument
, NULL
,
77 { (OPTIONSTRING
)"rep_write", required_argument
, NULL
,
79 { (OPTIONSTRING
)"verbose", no_argument
, NULL
,
81 { (OPTIONSTRING
)"help", no_argument
, NULL
,
83 { (OPTIONSTRING
)"version", no_argument
, NULL
,
89 static void ms_sync_lock_init(void);
90 static void ms_sync_lock_destroy(void);
91 static void ms_global_struct_init(void);
92 static void ms_global_struct_destroy(void);
93 static void ms_version_command(const char *command_name
);
94 static const char *ms_lookup_help(ms_options_t option
);
95 static int64_t ms_parse_time(void);
96 static int64_t ms_parse_size(void);
97 static void ms_options_parse(int argc
, char *argv
[]);
98 static int ms_check_para(void);
99 static void ms_statistic_init(void);
100 static void ms_stats_init(void);
101 static void ms_print_statistics(int time
);
102 static void ms_print_memslap_stats(struct timeval
*start_time
,
103 struct timeval
*end_time
);
104 static void ms_monitor_slap_mode(void);
105 void ms_help_command(const char *command_name
, const char *description
);
108 /* initialize the global locks */
109 static void ms_sync_lock_init()
111 ms_global
.init_lock
.count
= 0;
112 pthread_mutex_init(&ms_global
.init_lock
.lock
, NULL
);
113 pthread_cond_init(&ms_global
.init_lock
.cond
, NULL
);
115 ms_global
.run_lock
.count
= 0;
116 pthread_mutex_init(&ms_global
.run_lock
.lock
, NULL
);
117 pthread_cond_init(&ms_global
.run_lock
.cond
, NULL
);
119 pthread_mutex_init(&ms_global
.quit_mutex
, NULL
);
120 pthread_mutex_init(&ms_global
.seq_mutex
, NULL
);
121 } /* ms_sync_lock_init */
124 /* destroy the global locks */
125 static void ms_sync_lock_destroy()
127 pthread_mutex_destroy(&ms_global
.init_lock
.lock
);
128 pthread_cond_destroy(&ms_global
.init_lock
.cond
);
130 pthread_mutex_destroy(&ms_global
.run_lock
.lock
);
131 pthread_cond_destroy(&ms_global
.run_lock
.cond
);
133 pthread_mutex_destroy(&ms_global
.quit_mutex
);
134 pthread_mutex_destroy(&ms_global
.seq_mutex
);
136 if (ms_setting
.stat_freq
> 0)
138 pthread_mutex_destroy(&ms_statistic
.stat_mutex
);
140 } /* ms_sync_lock_destroy */
143 /* initialize the global structure */
144 static void ms_global_struct_init()
147 ms_global
.finish_warmup
= false;
148 ms_global
.time_out
= false;
152 /* destroy the global structure */
153 static void ms_global_struct_destroy()
155 ms_sync_lock_destroy();
160 * output the version information
162 * @param command_name, the string of this process
164 static void ms_version_command(const char *command_name
)
166 printf("%s v%u.%u\n", command_name
, 1, 0);
172 * get the description of the option
174 * @param option, option of command line
176 * @return char*, description of the command option
178 static const char *ms_lookup_help(ms_options_t option
)
184 "List one or more servers to connect. Servers count must be less than\n"
185 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
188 return "Display the version of the application and then exit.";
191 return "Display this message and then exit.";
193 case OPT_EXECUTE_NUMBER
:
194 return "Number of operations(get and set) to execute for the\n"
195 " given test. Default 1000000.";
197 case OPT_THREAD_NUMBER
:
199 "Number of threads to startup, better equal to CPU numbers. Default 8.";
201 case OPT_CONCURRENCY
:
202 return "Number of concurrency to simulate with load. Default 128.";
205 return "Fixed length of value.";
208 return "The proportion of date verification, e.g.: --verify=0.01";
210 case OPT_GETS_DIVISION
:
211 return "Number of keys to multi-get once. Default 1, means single get.";
215 "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
216 " d-days e.g.: --time=2h.";
220 "Load the configure file to get command,key and value distribution list.";
222 case OPT_WINDOW_SIZE
:
224 "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
229 "UDP support, default memslap uses TCP, TCP port and UDP port of\n"
230 " server must be same.";
234 "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
235 " Default no object with expire time";
239 "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
240 " Default never overwrite object.";
244 "Frequency of dumping statistic information. suffix: s-seconds,\n"
245 " m-minutes, e.g.: --resp_freq=10s.";
247 case OPT_SOCK_PER_CONN
:
248 return "Number of TCP socks per concurrency. Default 1.";
252 "Reconnect support, when connection is closed it will be reconnected.";
256 "Whether it outputs detailed information when verification fails.";
258 case OPT_FACEBOOK_TEST
:
260 "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
262 case OPT_BINARY_PROTOCOL
:
264 "Whether it enables binary protocol. Default with ASCII protocol.";
267 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
269 case OPT_REP_WRITE_SRV
:
270 return "The first nth servers can write data, e.g.: --rep_write=2.";
273 return "Forgot to document this option :)";
275 } /* ms_lookup_help */
279 * output the help information
281 * @param command_name, the string of this process
282 * @param description, description of this process
283 * @param long_options, global options array
285 void ms_help_command(const char *command_name
, const char *description
)
287 char *help_message
= NULL
;
289 printf("%s v%u.%u\n", command_name
, 1, 0);
290 printf(" %s\n\n", description
);
293 " memslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
296 for (int x
= 0; long_options
[x
].name
; x
++)
298 printf(" -%c, --%s%c\n", long_options
[x
].val
, long_options
[x
].name
,
299 long_options
[x
].has_arg
? '=' : ' ');
300 if ((help_message
= (char *)ms_lookup_help(long_options
[x
].val
)) != NULL
)
302 printf(" %s\n", help_message
);
308 " memslap -s 127.0.0.1:11211 -S 5s\n"
309 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
310 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
311 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
312 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
313 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
314 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
317 } /* ms_help_command */
320 /* used to parse the time string */
321 static int64_t ms_parse_time()
324 char unit
= optarg
[strlen(optarg
) - 1];
326 optarg
[strlen(optarg
) - 1]= '\0';
353 } /* ms_parse_time */
356 /* used to parse the size string */
357 static int64_t ms_parse_size()
360 char unit
= optarg
[strlen(optarg
) - 1];
362 optarg
[strlen(optarg
) - 1]= '\0';
363 ret
= strtoll(optarg
, (char **)NULL
, 10);
379 ret
*= 1024 * 1024 * 1024;
388 } /* ms_parse_size */
391 /* used to parse the options of command line */
392 static void ms_options_parse(int argc
, char *argv
[])
397 while ((option_rv
= getopt_long(argc
, argv
, "VhURbaBs:x:T:c:X:v:d:"
398 "t:S:F:w:e:o:n:P:p:",
399 long_options
, &option_index
)) != -1)
406 case OPT_VERSION
: /* --version or -V */
407 ms_version_command(PROGRAM_NAME
);
410 case OPT_HELP
: /* --help or -h */
411 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
414 case OPT_SERVERS
: /* --servers or -s */
415 ms_setting
.srv_str
= strdup(optarg
);
418 case OPT_CONCURRENCY
: /* --concurrency or -c */
419 ms_setting
.nconns
= atoi(optarg
);
420 if (ms_setting
.nconns
<= 0)
422 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
427 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
428 ms_setting
.exec_num
= atoll(optarg
);
429 if (ms_setting
.exec_num
<= 0)
431 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
436 case OPT_THREAD_NUMBER
: /* --threads or -T */
437 ms_setting
.nthreads
= atoi(optarg
);
438 if (ms_setting
.nthreads
<= 0)
440 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
445 case OPT_FIXED_LTH
: /* --fixed_size or -X */
446 ms_setting
.fixed_value_size
= (size_t)atoi(optarg
);
447 if ((ms_setting
.fixed_value_size
<= 0)
448 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
450 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
455 case OPT_VERIFY
: /* --verify or -v */
456 ms_setting
.verify_percent
= atof(optarg
);
457 if ((ms_setting
.verify_percent
<= 0)
458 || (ms_setting
.verify_percent
> 1.0))
460 fprintf(stderr
, "Data verification rate must be "
461 "greater than 0 and less than 1.0. :-)\n");
466 case OPT_GETS_DIVISION
: /* --division or -d */
467 ms_setting
.mult_key_num
= atoi(optarg
);
468 if (ms_setting
.mult_key_num
<= 0)
470 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
475 case OPT_TIME
: /* --time or -t */
476 ms_setting
.run_time
= (int)ms_parse_time();
477 if (ms_setting
.run_time
== -1)
479 fprintf(stderr
, "Please specify the run time. :-)\n"
480 "'s' for second, 'm' for minute, 'h' for hour, "
481 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
485 if (ms_setting
.run_time
== 0)
487 fprintf(stderr
, "Running time can not be 0. :-)\n");
492 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */
493 ms_setting
.cfg_file
= strdup(optarg
);
496 case OPT_WINDOW_SIZE
: /* --win_size or -w */
497 ms_setting
.win_size
= (size_t)ms_parse_size();
498 if (ms_setting
.win_size
== (size_t)-1)
502 "Please specify the item window size. :-)\n"
503 "e.g.: --win_size=10k (means 10k task window size).\n");
508 case OPT_UDP
: /* --udp or -U*/
509 ms_setting
.udp
= true;
512 case OPT_EXPIRE
: /* --exp_verify or -e */
513 ms_setting
.exp_ver_per
= atof(optarg
);
514 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0))
516 fprintf(stderr
, "Expire time verification rate must be "
517 "greater than 0 and less than 1.0. :-)\n");
522 case OPT_OVERWRITE
: /* --overwrite or -o */
523 ms_setting
.overwrite_percent
= atof(optarg
);
524 if ((ms_setting
.overwrite_percent
<= 0)
525 || (ms_setting
.overwrite_percent
> 1.0))
527 fprintf(stderr
, "Objects overwrite rate must be "
528 "greater than 0 and less than 1.0. :-)\n");
533 case OPT_STAT_FREQ
: /* --stat_freq or -S */
534 ms_setting
.stat_freq
= (int)ms_parse_time();
535 if (ms_setting
.stat_freq
== -1)
537 fprintf(stderr
, "Please specify the frequency of dumping "
538 "statistic information. :-)\n"
539 "'s' for second, 'm' for minute, 'h' for hour, "
540 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
544 if (ms_setting
.stat_freq
== 0)
546 fprintf(stderr
, "The frequency of dumping statistic information "
547 "can not be 0. :-)\n");
552 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
553 ms_setting
.sock_per_conn
= atoi(optarg
);
554 if (ms_setting
.sock_per_conn
<= 0)
556 fprintf(stderr
, "Number of socks of each concurrency "
557 "must be greater than 0.:-)\n");
562 case OPT_RECONNECT
: /* --reconnect or -R */
563 ms_setting
.reconnect
= true;
566 case OPT_VERBOSE
: /* --verbose or -b */
567 ms_setting
.verbose
= true;
570 case OPT_FACEBOOK_TEST
: /* --facebook or -a */
571 ms_setting
.facebook_test
= true;
574 case OPT_BINARY_PROTOCOL
: /* --binary or -B */
575 ms_setting
.binary_prot
= true;
578 case OPT_TPS
: /* --tps or -P */
579 ms_setting
.expected_tps
= (int)ms_parse_size();
580 if (ms_setting
.expected_tps
== -1)
583 "Please specify the item expected throughput. :-)\n"
584 "e.g.: --tps=10k (means 10k throughput).\n");
589 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
590 ms_setting
.rep_write_srv
= atoi(optarg
);
591 if (ms_setting
.rep_write_srv
<= 0)
594 "Number of replication writing server must be greater "
601 /* getopt_long already printed an error message. */
608 } /* ms_options_parse */
611 static int ms_check_para()
613 if (ms_setting
.srv_str
== NULL
)
615 fprintf(stderr
, "No Servers provided.\n\n");
619 if (ms_setting
.nconns
% ms_setting
.nthreads
!= 0)
621 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
625 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0)
627 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
632 } /* ms_check_para */
635 /* initialize the statistic structure */
636 static void ms_statistic_init()
638 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
639 ms_init_stats(&ms_statistic
.get_stat
, "Get");
640 ms_init_stats(&ms_statistic
.set_stat
, "Set");
641 ms_init_stats(&ms_statistic
.total_stat
, "Total");
642 } /* ms_statistic_init */
645 /* initialize the global state structure */
646 static void ms_stats_init()
648 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
649 if (ms_setting
.stat_freq
> 0)
653 } /* ms_stats_init */
656 /* use to output the statistic */
657 static void ms_print_statistics(int in_time
)
659 int obj_size
= (int)(ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
661 printf("\033[1;1H\033[2J\n");
662 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
,
663 ms_setting
.stat_freq
, obj_size
);
664 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
,
665 ms_setting
.stat_freq
, obj_size
);
666 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
,
667 ms_setting
.stat_freq
, obj_size
);
668 } /* ms_print_statistics */
671 /* used to print the states of memslap */
672 static void ms_print_memslap_stats(struct timeval
*start_time
,
673 struct timeval
*end_time
)
680 (unsigned long long)ms_stats
.cmd_get
);
683 (unsigned long long)ms_stats
.cmd_set
);
685 "get_misses: %llu\n",
686 (unsigned long long)ms_stats
.get_misses
);
688 if (ms_setting
.verify_percent
> 0)
690 pos
+= sprintf(pos
, "verify_misses: %llu\n",
691 (unsigned long long)ms_stats
.vef_miss
);
692 pos
+= sprintf(pos
, "verify_failed: %llu\n",
693 (unsigned long long)ms_stats
.vef_failed
);
696 if (ms_setting
.exp_ver_per
> 0)
698 pos
+= sprintf(pos
, "expired_get: %llu\n",
699 (unsigned long long)ms_stats
.exp_get
);
700 pos
+= sprintf(pos
, "unexpired_unget: %llu\n",
701 (unsigned long long)ms_stats
.unexp_unget
);
705 "written_bytes: %llu\n",
706 (unsigned long long)ms_stats
.bytes_written
);
708 "read_bytes: %llu\n",
709 (unsigned long long)ms_stats
.bytes_read
);
711 "object_bytes: %llu\n",
712 (unsigned long long)ms_stats
.obj_bytes
);
714 if (ms_setting
.udp
|| ms_setting
.facebook_test
)
717 "packet_disorder: %llu\n",
718 (unsigned long long)ms_stats
.pkt_disorder
);
720 "packet_drop: %llu\n",
721 (unsigned long long)ms_stats
.pkt_drop
);
723 "udp_timeout: %llu\n",
724 (unsigned long long)ms_stats
.udp_timeout
);
727 if (ms_setting
.stat_freq
> 0)
729 ms_dump_stats(&ms_statistic
.get_stat
);
730 ms_dump_stats(&ms_statistic
.set_stat
);
731 ms_dump_stats(&ms_statistic
.total_stat
);
734 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
737 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
738 (double)time_diff
/ 1000000,
739 (unsigned long long)(ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
741 + ms_stats
.cmd_set
) / ((long double)time_diff
/ 1000000),
743 ms_stats
.bytes_written
744 + ms_stats
.bytes_read
) / 1024 / 1024
745 / ((double)time_diff
/ 1000000));
747 fprintf(stdout
, "%s", buf
);
749 } /* ms_print_memslap_stats */
752 /* the loop of the main thread, wait the work threads to complete */
753 static void ms_monitor_slap_mode()
756 struct timeval start_time
, end_time
;
758 /* only when there is no set operation it need warm up */
759 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
)
761 /* Wait all the connects complete warm up. */
762 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
763 while (ms_global
.init_lock
.count
< ms_setting
.nconns
)
765 pthread_cond_wait(&ms_global
.init_lock
.cond
,
766 &ms_global
.init_lock
.lock
);
768 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
771 ms_global
.finish_warmup
= true;
773 /* running in "run time" mode, user specify run time */
774 if (ms_setting
.run_time
> 0)
776 gettimeofday(&start_time
, NULL
);
782 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
783 && ((int32_t)ms_stats
.active_conns
>= ms_setting
.nconns
)
784 && (ms_stats
.active_conns
<= (uint32_t)INT_MAX
))
786 ms_print_statistics(second
);
789 if (ms_setting
.run_time
<= second
)
791 ms_global
.time_out
= true;
795 /* all connections disconnect */
796 if ((second
> 5) && (ms_stats
.active_conns
== 0))
801 gettimeofday(&end_time
, NULL
);
802 sleep(1); /* wait all threads clean up */
806 /* running in "execute number" mode, user specify execute number */
807 gettimeofday(&start_time
, NULL
);
810 * We loop until we know that all connects have cleaned up.
812 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
813 while (ms_global
.run_lock
.count
< ms_setting
.nconns
)
815 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
817 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
819 gettimeofday(&end_time
, NULL
);
822 ms_print_memslap_stats(&start_time
, &end_time
);
823 } /* ms_monitor_slap_mode */
826 /* the main function */
827 int main(int argc
, char *argv
[])
829 srandom((unsigned int)time(NULL
));
830 ms_global_struct_init();
833 ms_setting_init_pre();
834 ms_options_parse(argc
, argv
);
837 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
840 ms_setting_init_post();
844 /* waiting work thread complete its task */
845 ms_monitor_slap_mode();
849 ms_global_struct_destroy();
850 ms_setting_cleanup();