804d8b7b8981031954be4aaaaba2be2158e64252
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
? '=' : ' ');
302 if ((help_message
= (char *)ms_lookup_help(long_options
[x
].val
)) != NULL
)
304 printf(" %s\n", help_message
);
310 " memslap -s 127.0.0.1:11211 -S 5s\n"
311 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
312 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
313 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
314 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
315 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
316 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
319 } /* ms_help_command */
322 /* used to parse the time string */
323 static int64_t ms_parse_time()
326 char unit
= optarg
[strlen(optarg
) - 1];
328 optarg
[strlen(optarg
) - 1]= '\0';
355 } /* ms_parse_time */
358 /* used to parse the size string */
359 static int64_t ms_parse_size()
362 char unit
= optarg
[strlen(optarg
) - 1];
364 optarg
[strlen(optarg
) - 1]= '\0';
365 ret
= strtoll(optarg
, (char **)NULL
, 10);
381 ret
*= 1024 * 1024 * 1024;
390 } /* ms_parse_size */
393 /* used to parse the options of command line */
394 static void ms_options_parse(int argc
, char *argv
[])
399 while ((option_rv
= getopt_long(argc
, argv
, "VhURbaBs:x:T:c:X:v:d:"
400 "t:S:F:w:e:o:n:P:p:",
401 long_options
, &option_index
)) != -1)
408 case OPT_VERSION
: /* --version or -V */
409 ms_version_command(PROGRAM_NAME
);
412 case OPT_HELP
: /* --help or -h */
413 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
416 case OPT_SERVERS
: /* --servers or -s */
417 ms_setting
.srv_str
= strdup(optarg
);
420 case OPT_CONCURRENCY
: /* --concurrency or -c */
421 ms_setting
.nconns
= (int)strtol(optarg
, (char **) NULL
, 10);
422 if (ms_setting
.nconns
<= 0)
424 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
429 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
430 ms_setting
.exec_num
= (int)strtol(optarg
, (char **) NULL
, 10);
431 if (ms_setting
.exec_num
<= 0)
433 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
438 case OPT_THREAD_NUMBER
: /* --threads or -T */
439 ms_setting
.nthreads
= (int)strtol(optarg
, (char **) NULL
, 10);
440 if (ms_setting
.nthreads
<= 0)
442 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
447 case OPT_FIXED_LTH
: /* --fixed_size or -X */
448 ms_setting
.fixed_value_size
= (size_t)strtoull(optarg
, (char **) NULL
, 10);
449 if ((ms_setting
.fixed_value_size
<= 0)
450 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
452 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
457 case OPT_VERIFY
: /* --verify or -v */
458 ms_setting
.verify_percent
= atof(optarg
);
459 if ((ms_setting
.verify_percent
<= 0)
460 || (ms_setting
.verify_percent
> 1.0))
462 fprintf(stderr
, "Data verification rate must be "
463 "greater than 0 and less than 1.0. :-)\n");
468 case OPT_GETS_DIVISION
: /* --division or -d */
469 ms_setting
.mult_key_num
= (int)strtol(optarg
, (char **) NULL
, 10);
470 if (ms_setting
.mult_key_num
<= 0)
472 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
477 case OPT_TIME
: /* --time or -t */
478 ms_setting
.run_time
= (int)ms_parse_time();
479 if (ms_setting
.run_time
== -1)
481 fprintf(stderr
, "Please specify the run time. :-)\n"
482 "'s' for second, 'm' for minute, 'h' for hour, "
483 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
487 if (ms_setting
.run_time
== 0)
489 fprintf(stderr
, "Running time can not be 0. :-)\n");
494 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */
495 ms_setting
.cfg_file
= strdup(optarg
);
498 case OPT_WINDOW_SIZE
: /* --win_size or -w */
499 ms_setting
.win_size
= (size_t)ms_parse_size();
500 if (ms_setting
.win_size
== (size_t)-1)
504 "Please specify the item window size. :-)\n"
505 "e.g.: --win_size=10k (means 10k task window size).\n");
510 case OPT_UDP
: /* --udp or -U*/
511 ms_setting
.udp
= true;
514 case OPT_EXPIRE
: /* --exp_verify or -e */
515 ms_setting
.exp_ver_per
= atof(optarg
);
516 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0))
518 fprintf(stderr
, "Expire time verification rate must be "
519 "greater than 0 and less than 1.0. :-)\n");
524 case OPT_OVERWRITE
: /* --overwrite or -o */
525 ms_setting
.overwrite_percent
= atof(optarg
);
526 if ((ms_setting
.overwrite_percent
<= 0)
527 || (ms_setting
.overwrite_percent
> 1.0))
529 fprintf(stderr
, "Objects overwrite rate must be "
530 "greater than 0 and less than 1.0. :-)\n");
535 case OPT_STAT_FREQ
: /* --stat_freq or -S */
536 ms_setting
.stat_freq
= (int)ms_parse_time();
537 if (ms_setting
.stat_freq
== -1)
539 fprintf(stderr
, "Please specify the frequency of dumping "
540 "statistic information. :-)\n"
541 "'s' for second, 'm' for minute, 'h' for hour, "
542 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
546 if (ms_setting
.stat_freq
== 0)
548 fprintf(stderr
, "The frequency of dumping statistic information "
549 "can not be 0. :-)\n");
554 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
555 ms_setting
.sock_per_conn
= (int)strtol(optarg
, (char **) NULL
, 10);
556 if (ms_setting
.sock_per_conn
<= 0)
558 fprintf(stderr
, "Number of socks of each concurrency "
559 "must be greater than 0.:-)\n");
564 case OPT_RECONNECT
: /* --reconnect or -R */
565 ms_setting
.reconnect
= true;
568 case OPT_VERBOSE
: /* --verbose or -b */
569 ms_setting
.verbose
= true;
572 case OPT_FACEBOOK_TEST
: /* --facebook or -a */
573 ms_setting
.facebook_test
= true;
576 case OPT_BINARY_PROTOCOL
: /* --binary or -B */
577 ms_setting
.binary_prot
= true;
580 case OPT_TPS
: /* --tps or -P */
581 ms_setting
.expected_tps
= (int)ms_parse_size();
582 if (ms_setting
.expected_tps
== -1)
585 "Please specify the item expected throughput. :-)\n"
586 "e.g.: --tps=10k (means 10k throughput).\n");
591 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
592 ms_setting
.rep_write_srv
= (int)strtol(optarg
, (char **) NULL
, 10);
593 if (ms_setting
.rep_write_srv
<= 0)
596 "Number of replication writing server must be greater "
603 /* getopt_long already printed an error message. */
610 } /* ms_options_parse */
613 static int ms_check_para()
615 if (ms_setting
.srv_str
== NULL
)
619 if ((temp
= getenv("MEMCACHED_SERVERS")))
621 ms_setting
.srv_str
= strdup(temp
);
625 fprintf(stderr
, "No Servers provided\n\n");
630 if (ms_setting
.nconns
% ms_setting
.nthreads
!= 0)
632 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
636 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0)
638 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
643 } /* ms_check_para */
646 /* initialize the statistic structure */
647 static void ms_statistic_init()
649 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
650 ms_init_stats(&ms_statistic
.get_stat
, "Get");
651 ms_init_stats(&ms_statistic
.set_stat
, "Set");
652 ms_init_stats(&ms_statistic
.total_stat
, "Total");
653 } /* ms_statistic_init */
656 /* initialize the global state structure */
657 static void ms_stats_init()
659 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
660 if (ms_setting
.stat_freq
> 0)
664 } /* ms_stats_init */
667 /* use to output the statistic */
668 static void ms_print_statistics(int in_time
)
670 int obj_size
= (int)(ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
672 printf("\033[1;1H\033[2J\n");
673 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
,
674 ms_setting
.stat_freq
, obj_size
);
675 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
,
676 ms_setting
.stat_freq
, obj_size
);
677 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
,
678 ms_setting
.stat_freq
, obj_size
);
679 } /* ms_print_statistics */
682 /* used to print the states of memslap */
683 static void ms_print_memslap_stats(struct timeval
*start_time
,
684 struct timeval
*end_time
)
691 (unsigned long long)ms_stats
.cmd_get
);
694 (unsigned long long)ms_stats
.cmd_set
);
696 "get_misses: %llu\n",
697 (unsigned long long)ms_stats
.get_misses
);
699 if (ms_setting
.verify_percent
> 0)
701 pos
+= sprintf(pos
, "verify_misses: %llu\n",
702 (unsigned long long)ms_stats
.vef_miss
);
703 pos
+= sprintf(pos
, "verify_failed: %llu\n",
704 (unsigned long long)ms_stats
.vef_failed
);
707 if (ms_setting
.exp_ver_per
> 0)
709 pos
+= sprintf(pos
, "expired_get: %llu\n",
710 (unsigned long long)ms_stats
.exp_get
);
711 pos
+= sprintf(pos
, "unexpired_unget: %llu\n",
712 (unsigned long long)ms_stats
.unexp_unget
);
716 "written_bytes: %llu\n",
717 (unsigned long long)ms_stats
.bytes_written
);
719 "read_bytes: %llu\n",
720 (unsigned long long)ms_stats
.bytes_read
);
722 "object_bytes: %llu\n",
723 (unsigned long long)ms_stats
.obj_bytes
);
725 if (ms_setting
.udp
|| ms_setting
.facebook_test
)
728 "packet_disorder: %llu\n",
729 (unsigned long long)ms_stats
.pkt_disorder
);
731 "packet_drop: %llu\n",
732 (unsigned long long)ms_stats
.pkt_drop
);
734 "udp_timeout: %llu\n",
735 (unsigned long long)ms_stats
.udp_timeout
);
738 if (ms_setting
.stat_freq
> 0)
740 ms_dump_stats(&ms_statistic
.get_stat
);
741 ms_dump_stats(&ms_statistic
.set_stat
);
742 ms_dump_stats(&ms_statistic
.total_stat
);
745 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
748 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
749 (double)time_diff
/ 1000000,
750 (unsigned long long)(ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
752 + ms_stats
.cmd_set
) / ((long double)time_diff
/ 1000000),
754 ms_stats
.bytes_written
755 + ms_stats
.bytes_read
) / 1024 / 1024
756 / ((double)time_diff
/ 1000000));
758 fprintf(stdout
, "%s", buf
);
760 } /* ms_print_memslap_stats */
763 /* the loop of the main thread, wait the work threads to complete */
764 static void ms_monitor_slap_mode()
767 struct timeval start_time
, end_time
;
769 /* only when there is no set operation it need warm up */
770 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
)
772 /* Wait all the connects complete warm up. */
773 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
774 while (ms_global
.init_lock
.count
< ms_setting
.nconns
)
776 pthread_cond_wait(&ms_global
.init_lock
.cond
,
777 &ms_global
.init_lock
.lock
);
779 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
782 ms_global
.finish_warmup
= true;
784 /* running in "run time" mode, user specify run time */
785 if (ms_setting
.run_time
> 0)
787 gettimeofday(&start_time
, NULL
);
793 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
794 && (ms_stats
.active_conns
>= ms_setting
.nconns
)
795 && (ms_stats
.active_conns
<= INT_MAX
))
797 ms_print_statistics(second
);
800 if (ms_setting
.run_time
<= second
)
802 ms_global
.time_out
= true;
806 /* all connections disconnect */
807 if ((second
> 5) && (ms_stats
.active_conns
== 0))
812 gettimeofday(&end_time
, NULL
);
813 sleep(1); /* wait all threads clean up */
817 /* running in "execute number" mode, user specify execute number */
818 gettimeofday(&start_time
, NULL
);
821 * We loop until we know that all connects have cleaned up.
823 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
824 while (ms_global
.run_lock
.count
< ms_setting
.nconns
)
826 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
828 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
830 gettimeofday(&end_time
, NULL
);
833 ms_print_memslap_stats(&start_time
, &end_time
);
834 } /* ms_monitor_slap_mode */
837 /* the main function */
838 int main(int argc
, char *argv
[])
840 srandom((unsigned int)time(NULL
));
841 ms_global_struct_init();
844 ms_setting_init_pre();
845 ms_options_parse(argc
, argv
);
848 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
851 ms_setting_init_post();
855 /* waiting work thread complete its task */
856 ms_monitor_slap_mode();
860 ms_global_struct_destroy();
861 ms_setting_cleanup();