3ed6a2b5a664d15feeca54e015ff9b2eb958a422
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>
18 #include "ms_sigsegv.h"
19 #include "ms_setting.h"
20 #include "ms_thread.h"
22 #define PROGRAM_NAME "memslap"
23 #define PROGRAM_DESCRIPTION \
24 "Generates workload against memcached servers."
27 static struct option long_options
[]=
29 { "servers", required_argument
, NULL
,
31 { "threads", required_argument
, NULL
,
33 { "concurrency", required_argument
, NULL
,
35 { "conn_sock", required_argument
, NULL
,
37 { "execute_number", required_argument
, NULL
,
39 { "time", required_argument
, NULL
,
41 { "cfg_cmd", required_argument
, NULL
,
43 { "win_size", required_argument
, NULL
,
45 { "fixed_size", required_argument
, NULL
,
47 { "verify", required_argument
, NULL
,
49 { "division", required_argument
, NULL
,
51 { "stat_freq", required_argument
, NULL
,
53 { "exp_verify", required_argument
, NULL
,
55 { "overwrite", required_argument
, NULL
,
57 { "reconnect", no_argument
, NULL
,
59 { "udp", no_argument
, NULL
,
61 { "facebook", no_argument
, NULL
,
63 { "binary", no_argument
, NULL
,
64 OPT_BINARY_PROTOCOL
},
65 { "tps", required_argument
, NULL
,
67 { "rep_write", required_argument
, NULL
,
69 { "verbose", no_argument
, NULL
,
71 { "help", no_argument
, NULL
,
73 { "version", no_argument
, NULL
,
79 static void ms_sync_lock_init(void);
80 static void ms_sync_lock_destroy(void);
81 static void ms_global_struct_init(void);
82 static void ms_global_struct_destroy(void);
83 static void ms_version_command(const char *command_name
);
84 static const char *ms_lookup_help(ms_options_t option
);
85 static int64_t ms_parse_time(void);
86 static int64_t ms_parse_size(void);
87 static void ms_options_parse(int argc
, char *argv
[]);
88 static int ms_check_para(void);
89 static void ms_statistic_init(void);
90 static void ms_stats_init(void);
91 static void ms_print_statistics(int time
);
92 static void ms_print_memslap_stats(struct timeval
*start_time
,
93 struct timeval
*end_time
);
94 static void ms_monitor_slap_mode(void);
95 void ms_help_command(const char *command_name
, const char *description
);
98 /* initialize the global locks */
99 static void ms_sync_lock_init()
101 ms_global
.init_lock
.count
= 0;
102 pthread_mutex_init(&ms_global
.init_lock
.lock
, NULL
);
103 pthread_cond_init(&ms_global
.init_lock
.cond
, NULL
);
105 ms_global
.run_lock
.count
= 0;
106 pthread_mutex_init(&ms_global
.run_lock
.lock
, NULL
);
107 pthread_cond_init(&ms_global
.run_lock
.cond
, NULL
);
109 pthread_mutex_init(&ms_global
.quit_mutex
, NULL
);
110 pthread_mutex_init(&ms_global
.seq_mutex
, NULL
);
111 } /* ms_sync_lock_init */
114 /* destroy the global locks */
115 static void ms_sync_lock_destroy()
117 pthread_mutex_destroy(&ms_global
.init_lock
.lock
);
118 pthread_cond_destroy(&ms_global
.init_lock
.cond
);
120 pthread_mutex_destroy(&ms_global
.run_lock
.lock
);
121 pthread_cond_destroy(&ms_global
.run_lock
.cond
);
123 pthread_mutex_destroy(&ms_global
.quit_mutex
);
124 pthread_mutex_destroy(&ms_global
.seq_mutex
);
126 if (ms_setting
.stat_freq
> 0)
128 pthread_mutex_destroy(&ms_statistic
.stat_mutex
);
130 } /* ms_sync_lock_destroy */
133 /* initialize the global structure */
134 static void ms_global_struct_init()
137 ms_global
.finish_warmup
= false;
138 ms_global
.time_out
= false;
142 /* destroy the global structure */
143 static void ms_global_struct_destroy()
145 ms_sync_lock_destroy();
150 * output the version information
152 * @param command_name, the string of this process
154 static void ms_version_command(const char *command_name
)
156 printf("%s v%u.%u\n", command_name
, 1, 0);
162 * get the description of the option
164 * @param option, option of command line
166 * @return char*, description of the command option
168 static const char *ms_lookup_help(ms_options_t option
)
174 "List one or more servers to connect. Servers count must be less than\n"
175 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
178 return "Display the version of the application and then exit.";
181 return "Display this message and then exit.";
183 case OPT_EXECUTE_NUMBER
:
184 return "Number of operations(get and set) to execute for the\n"
185 " given test. Default 1000000.";
187 case OPT_THREAD_NUMBER
:
189 "Number of threads to startup, better equal to CPU numbers. Default 8.";
191 case OPT_CONCURRENCY
:
192 return "Number of concurrency to simulate with load. Default 128.";
195 return "Fixed length of value.";
198 return "The proportion of date verification, e.g.: --verify=0.01";
200 case OPT_GETS_DIVISION
:
201 return "Number of keys to multi-get once. Default 1, means single get.";
205 "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
206 " d-days e.g.: --time=2h.";
210 "Load the configure file to get command,key and value distribution list.";
212 case OPT_WINDOW_SIZE
:
214 "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
219 "UDP support, default memslap uses TCP, TCP port and UDP port of\n"
220 " server must be same.";
224 "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
225 " Default no object with expire time";
229 "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
230 " Default never overwrite object.";
234 "Frequency of dumping statistic information. suffix: s-seconds,\n"
235 " m-minutes, e.g.: --resp_freq=10s.";
237 case OPT_SOCK_PER_CONN
:
238 return "Number of TCP socks per concurrency. Default 1.";
242 "Reconnect support, when connection is closed it will be reconnected.";
246 "Whether it outputs detailed information when verification fails.";
248 case OPT_FACEBOOK_TEST
:
250 "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
252 case OPT_BINARY_PROTOCOL
:
254 "Whether it enables binary protocol. Default with ASCII protocol.";
257 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
259 case OPT_REP_WRITE_SRV
:
260 return "The first nth servers can write data, e.g.: --rep_write=2.";
263 return "Forgot to document this option :)";
265 } /* ms_lookup_help */
269 * output the help information
271 * @param command_name, the string of this process
272 * @param description, description of this process
273 * @param long_options, global options array
275 void ms_help_command(const char *command_name
, const char *description
)
277 char *help_message
= NULL
;
279 printf("%s v%u.%u\n", command_name
, 1, 0);
280 printf(" %s\n\n", description
);
283 " memslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
286 for (int x
= 0; long_options
[x
].name
; x
++)
288 printf(" -%c, --%s%c\n", long_options
[x
].val
, long_options
[x
].name
,
289 long_options
[x
].has_arg
? '=' : ' ');
290 if ((help_message
= (char *)ms_lookup_help(long_options
[x
].val
)) != NULL
)
292 printf(" %s\n", help_message
);
298 " memslap -s 127.0.0.1:11211 -S 5s\n"
299 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
300 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
301 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
302 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
303 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
304 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
307 } /* ms_help_command */
310 /* used to parse the time string */
311 static int64_t ms_parse_time()
314 char unit
= optarg
[strlen(optarg
) - 1];
316 optarg
[strlen(optarg
) - 1]= '\0';
343 } /* ms_parse_time */
346 /* used to parse the size string */
347 static int64_t ms_parse_size()
350 char unit
= optarg
[strlen(optarg
) - 1];
352 optarg
[strlen(optarg
) - 1]= '\0';
353 ret
= strtoll(optarg
, (char **)NULL
, 10);
369 ret
*= 1024 * 1024 * 1024;
378 } /* ms_parse_size */
381 /* used to parse the options of command line */
382 static void ms_options_parse(int argc
, char *argv
[])
387 while ((option_rv
= getopt_long(argc
, argv
, "VhURbaBs:x:T:c:X:v:d:"
388 "t:S:F:w:e:o:n:P:p:",
389 long_options
, &option_index
)) != -1)
396 case OPT_VERSION
: /* --version or -V */
397 ms_version_command(PROGRAM_NAME
);
400 case OPT_HELP
: /* --help or -h */
401 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
404 case OPT_SERVERS
: /* --servers or -s */
405 ms_setting
.srv_str
= strdup(optarg
);
408 case OPT_CONCURRENCY
: /* --concurrency or -c */
409 ms_setting
.nconns
= atoi(optarg
);
410 if (ms_setting
.nconns
<= 0)
412 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
417 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
418 ms_setting
.exec_num
= atoll(optarg
);
419 if (ms_setting
.exec_num
<= 0)
421 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
426 case OPT_THREAD_NUMBER
: /* --threads or -T */
427 ms_setting
.nthreads
= atoi(optarg
);
428 if (ms_setting
.nthreads
<= 0)
430 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
435 case OPT_FIXED_LTH
: /* --fixed_size or -X */
436 ms_setting
.fixed_value_size
= (size_t)atoi(optarg
);
437 if ((ms_setting
.fixed_value_size
<= 0)
438 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
440 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
445 case OPT_VERIFY
: /* --verify or -v */
446 ms_setting
.verify_percent
= atof(optarg
);
447 if ((ms_setting
.verify_percent
<= 0)
448 || (ms_setting
.verify_percent
> 1.0))
450 fprintf(stderr
, "Data verification rate must be "
451 "greater than 0 and less than 1.0. :-)\n");
456 case OPT_GETS_DIVISION
: /* --division or -d */
457 ms_setting
.mult_key_num
= atoi(optarg
);
458 if (ms_setting
.mult_key_num
<= 0)
460 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
465 case OPT_TIME
: /* --time or -t */
466 ms_setting
.run_time
= (int)ms_parse_time();
467 if (ms_setting
.run_time
== -1)
469 fprintf(stderr
, "Please specify the run time. :-)\n"
470 "'s' for second, 'm' for minute, 'h' for hour, "
471 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
475 if (ms_setting
.run_time
== 0)
477 fprintf(stderr
, "Running time can not be 0. :-)\n");
482 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */
483 ms_setting
.cfg_file
= strdup(optarg
);
486 case OPT_WINDOW_SIZE
: /* --win_size or -w */
487 ms_setting
.win_size
= (size_t)ms_parse_size();
488 if (ms_setting
.win_size
== (size_t)-1)
492 "Please specify the item window size. :-)\n"
493 "e.g.: --win_size=10k (means 10k task window size).\n");
498 case OPT_UDP
: /* --udp or -U*/
499 ms_setting
.udp
= true;
502 case OPT_EXPIRE
: /* --exp_verify or -e */
503 ms_setting
.exp_ver_per
= atof(optarg
);
504 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0))
506 fprintf(stderr
, "Expire time verification rate must be "
507 "greater than 0 and less than 1.0. :-)\n");
512 case OPT_OVERWRITE
: /* --overwrite or -o */
513 ms_setting
.overwrite_percent
= atof(optarg
);
514 if ((ms_setting
.overwrite_percent
<= 0)
515 || (ms_setting
.overwrite_percent
> 1.0))
517 fprintf(stderr
, "Objects overwrite rate must be "
518 "greater than 0 and less than 1.0. :-)\n");
523 case OPT_STAT_FREQ
: /* --stat_freq or -S */
524 ms_setting
.stat_freq
= (int)ms_parse_time();
525 if (ms_setting
.stat_freq
== -1)
527 fprintf(stderr
, "Please specify the frequency of dumping "
528 "statistic information. :-)\n"
529 "'s' for second, 'm' for minute, 'h' for hour, "
530 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
534 if (ms_setting
.stat_freq
== 0)
536 fprintf(stderr
, "The frequency of dumping statistic information "
537 "can not be 0. :-)\n");
542 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
543 ms_setting
.sock_per_conn
= atoi(optarg
);
544 if (ms_setting
.sock_per_conn
<= 0)
546 fprintf(stderr
, "Number of socks of each concurrency "
547 "must be greater than 0.:-)\n");
552 case OPT_RECONNECT
: /* --reconnect or -R */
553 ms_setting
.reconnect
= true;
556 case OPT_VERBOSE
: /* --verbose or -b */
557 ms_setting
.verbose
= true;
560 case OPT_FACEBOOK_TEST
: /* --facebook or -a */
561 ms_setting
.facebook_test
= true;
564 case OPT_BINARY_PROTOCOL
: /* --binary or -B */
565 ms_setting
.binary_prot
= true;
568 case OPT_TPS
: /* --tps or -P */
569 ms_setting
.expected_tps
= (int)ms_parse_size();
570 if (ms_setting
.expected_tps
== -1)
573 "Please specify the item expected throughput. :-)\n"
574 "e.g.: --tps=10k (means 10k throughput).\n");
579 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
580 ms_setting
.rep_write_srv
= atoi(optarg
);
581 if (ms_setting
.rep_write_srv
<= 0)
584 "Number of replication writing server must be greater "
591 /* getopt_long already printed an error message. */
598 } /* ms_options_parse */
601 static int ms_check_para()
603 if (ms_setting
.srv_str
== NULL
)
605 fprintf(stderr
, "No Servers provided.\n\n");
609 if (ms_setting
.nconns
% ms_setting
.nthreads
!= 0)
611 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
615 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0)
617 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
622 } /* ms_check_para */
625 /* initialize the statistic structure */
626 static void ms_statistic_init()
628 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
629 ms_init_stats(&ms_statistic
.get_stat
, "Get");
630 ms_init_stats(&ms_statistic
.set_stat
, "Set");
631 ms_init_stats(&ms_statistic
.total_stat
, "Total");
632 } /* ms_statistic_init */
635 /* initialize the global state structure */
636 static void ms_stats_init()
638 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
639 if (ms_setting
.stat_freq
> 0)
643 } /* ms_stats_init */
646 /* use to output the statistic */
647 static void ms_print_statistics(int in_time
)
649 int obj_size
= (int)(ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
651 printf("\033[1;1H\033[2J\n");
652 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
,
653 ms_setting
.stat_freq
, obj_size
);
654 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
,
655 ms_setting
.stat_freq
, obj_size
);
656 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
,
657 ms_setting
.stat_freq
, obj_size
);
658 } /* ms_print_statistics */
661 /* used to print the states of memslap */
662 static void ms_print_memslap_stats(struct timeval
*start_time
,
663 struct timeval
*end_time
)
670 (unsigned long long)ms_stats
.cmd_get
);
673 (unsigned long long)ms_stats
.cmd_set
);
675 "get_misses: %llu\n",
676 (unsigned long long)ms_stats
.get_misses
);
678 if (ms_setting
.verify_percent
> 0)
680 pos
+= sprintf(pos
, "verify_misses: %llu\n",
681 (unsigned long long)ms_stats
.vef_miss
);
682 pos
+= sprintf(pos
, "verify_failed: %llu\n",
683 (unsigned long long)ms_stats
.vef_failed
);
686 if (ms_setting
.exp_ver_per
> 0)
688 pos
+= sprintf(pos
, "expired_get: %llu\n",
689 (unsigned long long)ms_stats
.exp_get
);
690 pos
+= sprintf(pos
, "unexpired_unget: %llu\n",
691 (unsigned long long)ms_stats
.unexp_unget
);
695 "written_bytes: %llu\n",
696 (unsigned long long)ms_stats
.bytes_written
);
698 "read_bytes: %llu\n",
699 (unsigned long long)ms_stats
.bytes_read
);
701 "object_bytes: %llu\n",
702 (unsigned long long)ms_stats
.obj_bytes
);
704 if (ms_setting
.udp
|| ms_setting
.facebook_test
)
707 "packet_disorder: %llu\n",
708 (unsigned long long)ms_stats
.pkt_disorder
);
710 "packet_drop: %llu\n",
711 (unsigned long long)ms_stats
.pkt_drop
);
713 "udp_timeout: %llu\n",
714 (unsigned long long)ms_stats
.udp_timeout
);
717 if (ms_setting
.stat_freq
> 0)
719 ms_dump_stats(&ms_statistic
.get_stat
);
720 ms_dump_stats(&ms_statistic
.set_stat
);
721 ms_dump_stats(&ms_statistic
.total_stat
);
724 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
727 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
728 (double)time_diff
/ 1000000,
729 (unsigned long long)(ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
731 + ms_stats
.cmd_set
) / ((long double)time_diff
/ 1000000),
733 ms_stats
.bytes_written
734 + ms_stats
.bytes_read
) / 1024 / 1024
735 / ((double)time_diff
/ 1000000));
737 fprintf(stdout
, "%s", buf
);
739 } /* ms_print_memslap_stats */
742 /* the loop of the main thread, wait the work threads to complete */
743 static void ms_monitor_slap_mode()
746 struct timeval start_time
, end_time
;
748 /* only when there is no set operation it need warm up */
749 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
)
751 /* Wait all the connects complete warm up. */
752 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
753 while (ms_global
.init_lock
.count
< ms_setting
.nconns
)
755 pthread_cond_wait(&ms_global
.init_lock
.cond
,
756 &ms_global
.init_lock
.lock
);
758 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
761 ms_global
.finish_warmup
= true;
763 /* running in "run time" mode, user specify run time */
764 if (ms_setting
.run_time
> 0)
766 gettimeofday(&start_time
, NULL
);
772 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
773 && (ms_stats
.active_conns
>= ms_setting
.nconns
))
775 ms_print_statistics(second
);
778 if (ms_setting
.run_time
<= second
)
780 ms_global
.time_out
= true;
784 /* all connections disconnect */
785 if ((second
> 5) && (ms_stats
.active_conns
== 0))
790 gettimeofday(&end_time
, NULL
);
791 sleep(1); /* wait all threads clean up */
795 /* running in "execute number" mode, user specify execute number */
796 gettimeofday(&start_time
, NULL
);
799 * We loop until we know that all connects have cleaned up.
801 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
802 while (ms_global
.run_lock
.count
< ms_setting
.nconns
)
804 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
806 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
808 gettimeofday(&end_time
, NULL
);
811 ms_print_memslap_stats(&start_time
, &end_time
);
812 } /* ms_monitor_slap_mode */
815 /* the main function */
816 int main(int argc
, char *argv
[])
818 srandom((unsigned int)time(NULL
));
819 ms_global_struct_init();
822 ms_setting_init_pre();
823 ms_options_parse(argc
, argv
);
826 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
829 ms_setting_init_post();
833 /* waiting work thread complete its task */
834 ms_monitor_slap_mode();
838 ms_global_struct_destroy();
839 ms_setting_cleanup();