2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
22 #if defined(HAVE_SYS_TIME_H)
23 # include <sys/time.h>
26 #if defined(HAVE_TIME_H)
30 #include "ms_sigsegv.h"
31 #include "ms_setting.h"
32 #include "ms_thread.h"
34 /* global structure */
35 ms_global_t ms_global
;
37 /* global stats information structure */
40 /* global statistic structure */
41 ms_statistic_t ms_statistic
;
43 #define PROGRAM_NAME "memaslap"
44 #define PROGRAM_DESCRIPTION "Generates workload against memcached servers."
47 /* For some odd reason the option struct on solaris defines the argument
48 * as char* and not const char*
50 # define OPTIONSTRING char *
52 # define OPTIONSTRING const char *
56 static struct option long_options
[] = {
57 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
58 {(OPTIONSTRING
) "threads", required_argument
, NULL
, OPT_THREAD_NUMBER
},
59 {(OPTIONSTRING
) "concurrency", required_argument
, NULL
, OPT_CONCURRENCY
},
60 {(OPTIONSTRING
) "conn_sock", required_argument
, NULL
, OPT_SOCK_PER_CONN
},
61 {(OPTIONSTRING
) "execute_number", required_argument
, NULL
, OPT_EXECUTE_NUMBER
},
62 {(OPTIONSTRING
) "time", required_argument
, NULL
, OPT_TIME
},
63 {(OPTIONSTRING
) "cfg_cmd", required_argument
, NULL
, OPT_CONFIG_CMD
},
64 {(OPTIONSTRING
) "win_size", required_argument
, NULL
, OPT_WINDOW_SIZE
},
65 {(OPTIONSTRING
) "fixed_size", required_argument
, NULL
, OPT_FIXED_LTH
},
66 {(OPTIONSTRING
) "verify", required_argument
, NULL
, OPT_VERIFY
},
67 {(OPTIONSTRING
) "division", required_argument
, NULL
, OPT_GETS_DIVISION
},
68 {(OPTIONSTRING
) "stat_freq", required_argument
, NULL
, OPT_STAT_FREQ
},
69 {(OPTIONSTRING
) "exp_verify", required_argument
, NULL
, OPT_EXPIRE
},
70 {(OPTIONSTRING
) "overwrite", required_argument
, NULL
, OPT_OVERWRITE
},
71 {(OPTIONSTRING
) "reconnect", no_argument
, NULL
, OPT_RECONNECT
},
72 {(OPTIONSTRING
) "udp", no_argument
, NULL
, OPT_UDP
},
73 {(OPTIONSTRING
) "facebook", no_argument
, NULL
, OPT_FACEBOOK_TEST
},
74 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY_PROTOCOL
},
75 {(OPTIONSTRING
) "tps", required_argument
, NULL
, OPT_TPS
},
76 {(OPTIONSTRING
) "rep_write", required_argument
, NULL
, OPT_REP_WRITE_SRV
},
77 {(OPTIONSTRING
) "verbose", no_argument
, NULL
, OPT_VERBOSE
},
78 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
79 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
84 static void ms_sync_lock_init(void);
85 static void ms_sync_lock_destroy(void);
86 static void ms_global_struct_init(void);
87 static void ms_global_struct_destroy(void);
88 static void ms_version_command(const char *command_name
);
89 static const char *ms_lookup_help(ms_options_t option
);
90 static int64_t ms_parse_time(void);
91 static int64_t ms_parse_size(void);
92 static void ms_options_parse(int argc
, char *argv
[]);
93 static int ms_check_para(void);
94 static void ms_statistic_init(void);
95 static void ms_stats_init(void);
96 static void ms_print_statistics(int in_time
);
97 static void ms_print_memaslap_stats(struct timeval
*start_time
, struct timeval
*end_time
);
98 static void ms_monitor_slap_mode(void);
101 * output the help information
103 * @param command_name, the string of this process
104 * @param description, description of this process
105 * @param long_options, global options array
107 static void ms_help_command(const char *command_name
, const char *description
) {
108 char *help_message
= NULL
;
110 printf("%s v%u.%u\n", command_name
, 1U, 0U);
111 printf(" %s\n\n", description
);
113 " memaslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
116 for (int x
= 0; long_options
[x
].name
; x
++) {
117 printf(" -%c, --%s%c\n", long_options
[x
].val
, long_options
[x
].name
,
118 long_options
[x
].has_arg
? '=' : ' ');
120 if ((help_message
= (char *) ms_lookup_help(long_options
[x
].val
)) != NULL
) {
121 printf(" %s\n", help_message
);
125 printf("\nExamples:\n"
126 " memaslap -s 127.0.0.1:11211 -S 5s\n"
127 " memaslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
128 " memaslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
129 " memaslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
130 " memaslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
131 " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
132 " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
133 } /* ms_help_command */
135 /* initialize the global locks */
136 static void ms_sync_lock_init() {
137 ms_global
.init_lock
.count
= 0;
138 pthread_mutex_init(&ms_global
.init_lock
.lock
, NULL
);
139 pthread_cond_init(&ms_global
.init_lock
.cond
, NULL
);
141 ms_global
.warmup_lock
.count
= 0;
142 pthread_mutex_init(&ms_global
.warmup_lock
.lock
, NULL
);
143 pthread_cond_init(&ms_global
.warmup_lock
.cond
, NULL
);
145 ms_global
.run_lock
.count
= 0;
146 pthread_mutex_init(&ms_global
.run_lock
.lock
, NULL
);
147 pthread_cond_init(&ms_global
.run_lock
.cond
, NULL
);
149 pthread_mutex_init(&ms_global
.quit_mutex
, NULL
);
150 pthread_mutex_init(&ms_global
.seq_mutex
, NULL
);
151 } /* ms_sync_lock_init */
153 /* destroy the global locks */
154 static void ms_sync_lock_destroy() {
155 pthread_mutex_destroy(&ms_global
.init_lock
.lock
);
156 pthread_cond_destroy(&ms_global
.init_lock
.cond
);
158 pthread_mutex_destroy(&ms_global
.warmup_lock
.lock
);
159 pthread_cond_destroy(&ms_global
.warmup_lock
.cond
);
161 pthread_mutex_destroy(&ms_global
.run_lock
.lock
);
162 pthread_cond_destroy(&ms_global
.run_lock
.cond
);
164 pthread_mutex_destroy(&ms_global
.quit_mutex
);
165 pthread_mutex_destroy(&ms_global
.seq_mutex
);
167 if (ms_setting
.stat_freq
> 0) {
168 pthread_mutex_destroy(&ms_statistic
.stat_mutex
);
170 } /* ms_sync_lock_destroy */
172 /* initialize the global structure */
173 static void ms_global_struct_init() {
175 ms_global
.finish_warmup
= false;
176 ms_global
.time_out
= false;
179 /* destroy the global structure */
180 static void ms_global_struct_destroy() {
181 ms_sync_lock_destroy();
185 * output the version information
187 * @param command_name, the string of this process
189 static void ms_version_command(const char *command_name
) {
190 printf("%s v%u.%u\n", command_name
, 1U, 0U);
195 * get the description of the option
197 * @param option, option of command line
199 * @return char*, description of the command option
201 static const char *ms_lookup_help(ms_options_t option
) {
204 return "List one or more servers to connect. Servers count must be less than\n"
205 " threads count. e.g.: --servers=localhost:1234,localhost:11211";
207 case OPT_VERSION
: return "Display the version of the application and then exit.";
209 case OPT_HELP
: return "Display this message and then exit.";
211 case OPT_EXECUTE_NUMBER
:
212 return "Number of operations(get and set) to execute for the\n"
213 " given test. Default 1000000.";
215 case OPT_THREAD_NUMBER
:
216 return "Number of threads to startup, better equal to CPU numbers. Default 8.";
218 case OPT_CONCURRENCY
: return "Number of concurrency to simulate with load. Default 128.";
220 case OPT_FIXED_LTH
: return "Fixed length of value.";
222 case OPT_VERIFY
: return "The proportion of date verification, e.g.: --verify=0.01";
224 case OPT_GETS_DIVISION
: return "Number of keys to multi-get once. Default 1, means single get.";
227 return "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
228 " d-days e.g.: --time=2h.";
231 return "Load the configure file to get command,key and value distribution list.";
233 case OPT_WINDOW_SIZE
:
234 return "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
238 return "UDP support, by default memaslap uses TCP; TCP port and UDP port of\n"
239 " server must be same.";
242 return "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
243 " Default no object with expire time";
246 return "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
247 " Default never overwrite object.";
250 return "Frequency of dumping statistic information. suffix: s-seconds,\n"
251 " m-minutes, e.g.: --resp_freq=10s.";
253 case OPT_SOCK_PER_CONN
: return "Number of TCP socks per concurrency. Default 1.";
255 case OPT_RECONNECT
: return "Reconnect support, when connection is closed it will be reconnected.";
257 case OPT_VERBOSE
: return "Whether it outputs detailed information when verification fails.";
259 case OPT_FACEBOOK_TEST
:
260 return "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
262 case OPT_BINARY_PROTOCOL
:
263 return "Whether it enables binary protocol. Default with ASCII protocol.";
265 case OPT_TPS
: return "Expected throughput, suffix: K, e.g.: --tps=10k.";
267 case OPT_REP_WRITE_SRV
: return "The first nth servers can write data, e.g.: --rep_write=2.";
269 default: return "Forgot to document this option :)";
271 } /* ms_lookup_help */
273 /* used to parse the time string */
274 static int64_t ms_parse_time() {
276 char unit
= optarg
[strlen(optarg
) - 1];
278 optarg
[strlen(optarg
) - 1] = '\0';
297 default: ret
= -1; break;
301 } /* ms_parse_time */
303 /* used to parse the size string */
304 static int64_t ms_parse_size() {
306 char unit
= optarg
[strlen(optarg
) - 1];
308 optarg
[strlen(optarg
) - 1] = '\0';
310 ret
= strtoll(optarg
, (char **) NULL
, 10);
312 fprintf(stderr
, "strtoll(optarg,..): %s\n", strerror(errno
));
318 case 'K': ret
*= 1024; break;
321 case 'M': ret
*= 1024 * 1024; break;
324 case 'G': ret
*= 1024 * 1024 * 1024; break;
326 default: ret
= -1; break;
330 } /* ms_parse_size */
332 /* used to parse the options of command line */
333 static void ms_options_parse(int argc
, char *argv
[]) {
334 int option_index
= 0;
337 while ((option_rv
= getopt_long(argc
, argv
,
338 "VhURbaBs:x:T:c:X:v:d:"
339 "t:S:F:w:e:o:n:P:p:",
340 long_options
, &option_index
))
346 case OPT_VERSION
: /* --version or -V */ ms_version_command(PROGRAM_NAME
); break;
348 case OPT_HELP
: /* --help or -h */
349 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
353 case OPT_SERVERS
: /* --servers or -s */ ms_setting
.srv_str
= strdup(optarg
); break;
355 case OPT_CONCURRENCY
: /* --concurrency or -c */
357 ms_setting
.nconns
= (uint32_t) strtoul(optarg
, (char **) NULL
, 10);
358 if (ms_setting
.nconns
<= 0 || errno
!= 0) {
359 fprintf(stderr
, "Concurrency must be greater than 0.:-)\n");
364 case OPT_EXECUTE_NUMBER
: /* --execute_number or -x */
366 ms_setting
.exec_num
= (int) strtol(optarg
, (char **) NULL
, 10);
367 if (ms_setting
.exec_num
<= 0 || errno
!= 0) {
368 fprintf(stderr
, "Execute number must be greater than 0.:-)\n");
373 case OPT_THREAD_NUMBER
: /* --threads or -T */
375 ms_setting
.nthreads
= (uint32_t) strtoul(optarg
, (char **) NULL
, 10);
376 if (ms_setting
.nthreads
<= 0 || errno
!= 0) {
377 fprintf(stderr
, "Threads number must be greater than 0.:-)\n");
382 case OPT_FIXED_LTH
: /* --fixed_size or -X */
384 ms_setting
.fixed_value_size
= (size_t) strtoull(optarg
, (char **) NULL
, 10);
385 if ((ms_setting
.fixed_value_size
<= 0 || errno
!= 0)
386 || (ms_setting
.fixed_value_size
> MAX_VALUE_SIZE
))
388 fprintf(stderr
, "Value size must be between 0 and 1M.:-)\n");
393 case OPT_VERIFY
: /* --verify or -v */
394 ms_setting
.verify_percent
= atof(optarg
);
395 if ((ms_setting
.verify_percent
<= 0) || (ms_setting
.verify_percent
> 1.0)) {
397 "Data verification rate must be "
398 "greater than 0 and less than 1.0. :-)\n");
403 case OPT_GETS_DIVISION
: /* --division or -d */
405 ms_setting
.mult_key_num
= (int) strtol(optarg
, (char **) NULL
, 10);
406 if (ms_setting
.mult_key_num
<= 0 || errno
!= 0) {
407 fprintf(stderr
, "Multi-get key number must be greater than 0.:-)\n");
412 case OPT_TIME
: /* --time or -t */
413 ms_setting
.run_time
= (int) ms_parse_time();
414 if (ms_setting
.run_time
== -1) {
416 "Please specify the run time. :-)\n"
417 "'s' for second, 'm' for minute, 'h' for hour, "
418 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
422 if (ms_setting
.run_time
== 0) {
423 fprintf(stderr
, "Running time can not be 0. :-)\n");
428 case OPT_CONFIG_CMD
: /* --cfg_cmd or -F */ ms_setting
.cfg_file
= strdup(optarg
); break;
430 case OPT_WINDOW_SIZE
: /* --win_size or -w */
431 ms_setting
.win_size
= (size_t) ms_parse_size();
432 if (ms_setting
.win_size
== (size_t) -1) {
434 "Please specify the item window size. :-)\n"
435 "e.g.: --win_size=10k (means 10k task window size).\n");
440 case OPT_UDP
: /* --udp or -U*/ ms_setting
.udp
= true; break;
442 case OPT_EXPIRE
: /* --exp_verify or -e */
443 ms_setting
.exp_ver_per
= atof(optarg
);
444 if ((ms_setting
.exp_ver_per
<= 0) || (ms_setting
.exp_ver_per
> 1.0)) {
446 "Expire time verification rate must be "
447 "greater than 0 and less than 1.0. :-)\n");
452 case OPT_OVERWRITE
: /* --overwrite or -o */
453 ms_setting
.overwrite_percent
= atof(optarg
);
454 if ((ms_setting
.overwrite_percent
<= 0) || (ms_setting
.overwrite_percent
> 1.0)) {
456 "Objects overwrite rate must be "
457 "greater than 0 and less than 1.0. :-)\n");
462 case OPT_STAT_FREQ
: /* --stat_freq or -S */
463 ms_setting
.stat_freq
= (int) ms_parse_time();
464 if (ms_setting
.stat_freq
== -1) {
466 "Please specify the frequency of dumping "
467 "statistic information. :-)\n"
468 "'s' for second, 'm' for minute, 'h' for hour, "
469 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
473 if (ms_setting
.stat_freq
== 0) {
475 "The frequency of dumping statistic information "
476 "can not be 0. :-)\n");
481 case OPT_SOCK_PER_CONN
: /* --conn_sock or -n */
483 ms_setting
.sock_per_conn
= (uint32_t) strtoul(optarg
, (char **) NULL
, 10);
484 if (ms_setting
.sock_per_conn
<= 0 || errno
!= 0) {
486 "Number of socks of each concurrency "
487 "must be greater than 0.:-)\n");
492 case OPT_RECONNECT
: /* --reconnect or -R */ ms_setting
.reconnect
= true; break;
494 case OPT_VERBOSE
: /* --verbose or -b */ ms_setting
.verbose
= true; break;
496 case OPT_FACEBOOK_TEST
: /* --facebook or -a */ ms_setting
.facebook_test
= true; break;
498 case OPT_BINARY_PROTOCOL
: /* --binary or -B */ ms_setting
.binary_prot_
= true; break;
500 case OPT_TPS
: /* --tps or -P */
501 ms_setting
.expected_tps
= (int) ms_parse_size();
502 if (ms_setting
.expected_tps
== -1) {
504 "Please specify the item expected throughput. :-)\n"
505 "e.g.: --tps=10k (means 10k throughput).\n");
510 case OPT_REP_WRITE_SRV
: /* --rep_write or -p */
512 ms_setting
.rep_write_srv
= (uint32_t) strtoul(optarg
, (char **) NULL
, 10);
513 if (ms_setting
.rep_write_srv
<= 0 || errno
!= 0) {
515 "Number of replication writing server must be greater "
522 /* getopt_long already printed an error message. */
528 } /* ms_options_parse */
530 static int ms_check_para() {
531 if (ms_setting
.srv_str
== NULL
) {
534 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
535 ms_setting
.srv_str
= strdup(temp
);
537 fprintf(stderr
, "No servers provided\n\n");
542 if (ms_setting
.nconns
% (uint32_t) ms_setting
.nthreads
!= 0) {
543 fprintf(stderr
, "Concurrency must be the multiples of threads count.\n");
547 if (ms_setting
.win_size
% UNIT_ITEMS_COUNT
!= 0) {
548 fprintf(stderr
, "Window size must be the multiples of 1024.\n\n");
553 } /* ms_check_para */
555 /* initialize the statistic structure */
556 static void ms_statistic_init() {
557 pthread_mutex_init(&ms_statistic
.stat_mutex
, NULL
);
558 ms_init_stats(&ms_statistic
.get_stat
, "Get");
559 ms_init_stats(&ms_statistic
.set_stat
, "Set");
560 ms_init_stats(&ms_statistic
.total_stat
, "Total");
561 } /* ms_statistic_init */
563 /* initialize the global state structure */
564 static void ms_stats_init() {
565 memset(&ms_stats
, 0, sizeof(ms_stats_t
));
566 if (ms_setting
.stat_freq
> 0) {
569 } /* ms_stats_init */
571 /* use to output the statistic */
572 static void ms_print_statistics(int in_time
) {
573 int obj_size
= (int) (ms_setting
.avg_key_size
+ ms_setting
.avg_val_size
);
575 printf("\033[1;1H\033[2J\n");
576 ms_dump_format_stats(&ms_statistic
.get_stat
, in_time
, ms_setting
.stat_freq
, obj_size
);
577 ms_dump_format_stats(&ms_statistic
.set_stat
, in_time
, ms_setting
.stat_freq
, obj_size
);
578 ms_dump_format_stats(&ms_statistic
.total_stat
, in_time
, ms_setting
.stat_freq
, obj_size
);
579 } /* ms_print_statistics */
581 /* used to print the states of memaslap */
582 static void ms_print_memaslap_stats(struct timeval
*start_time
, struct timeval
*end_time
) {
586 pos
+= snprintf(pos
, sizeof(buf
), "cmd_get: %lu\n", (unsigned long) ms_stats
.cmd_get
);
587 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "cmd_set: %lu\n",
588 (unsigned long) ms_stats
.cmd_set
);
589 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "get_misses: %lu\n",
590 (unsigned long) ms_stats
.get_misses
);
592 if (ms_setting
.verify_percent
> 0) {
593 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "verify_misses: %lu\n",
594 (unsigned long) ms_stats
.vef_miss
);
595 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "verify_failed: %lu\n",
596 (unsigned long) ms_stats
.vef_failed
);
599 if (ms_setting
.exp_ver_per
> 0) {
600 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "expired_get: %lu\n",
601 (unsigned long) ms_stats
.exp_get
);
602 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "unexpired_unget: %lu\n",
603 (unsigned long) ms_stats
.unexp_unget
);
606 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "written_bytes: %lu\n",
607 (unsigned long) ms_stats
.bytes_written
);
608 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "read_bytes: %lu\n",
609 (unsigned long) ms_stats
.bytes_read
);
610 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "object_bytes: %lu\n",
611 (unsigned long) ms_stats
.obj_bytes
);
613 if (ms_setting
.udp
|| ms_setting
.facebook_test
) {
614 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "packet_disorder: %lu\n",
615 (unsigned long) ms_stats
.pkt_disorder
);
616 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "packet_drop: %lu\n",
617 (unsigned long) ms_stats
.pkt_drop
);
618 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
), "udp_timeout: %lu\n",
619 (unsigned long) ms_stats
.udp_timeout
);
622 if (ms_setting
.stat_freq
> 0) {
623 ms_dump_stats(&ms_statistic
.get_stat
);
624 ms_dump_stats(&ms_statistic
.set_stat
);
625 ms_dump_stats(&ms_statistic
.total_stat
);
628 int64_t time_diff
= ms_time_diff(start_time
, end_time
);
629 pos
+= snprintf(pos
, sizeof(buf
) - (size_t)(pos
- buf
),
630 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
631 (double) time_diff
/ 1000000,
632 (unsigned long long) (ms_stats
.cmd_get
+ ms_stats
.cmd_set
),
633 (ms_stats
.cmd_get
+ ms_stats
.cmd_set
) / ((long double) time_diff
/ 1000000),
634 (double) (ms_stats
.bytes_written
+ ms_stats
.bytes_read
) / 1024 / 1024
635 / ((double) time_diff
/ 1000000));
636 assert(pos
<= (buf
+ sizeof(buf
)));
638 fwrite(buf
, 1, pos
- buf
, stdout
);
640 } /* ms_print_memaslap_stats */
642 /* the loop of the main thread, wait the work threads to complete */
643 static void ms_monitor_slap_mode() {
644 struct timeval start_time
, end_time
;
646 /* Wait all the threads complete initialization. */
647 pthread_mutex_lock(&ms_global
.init_lock
.lock
);
648 while (ms_global
.init_lock
.count
< ms_setting
.nthreads
) {
649 pthread_cond_wait(&ms_global
.init_lock
.cond
, &ms_global
.init_lock
.lock
);
651 pthread_mutex_unlock(&ms_global
.init_lock
.lock
);
653 /* only when there is no set operation it need warm up */
654 if (ms_setting
.cmd_distr
[CMD_SET
].cmd_prop
< PROP_ERROR
) {
655 /* Wait all the connects complete warm up. */
656 pthread_mutex_lock(&ms_global
.warmup_lock
.lock
);
657 while (ms_global
.warmup_lock
.count
< ms_setting
.nconns
) {
658 pthread_cond_wait(&ms_global
.warmup_lock
.cond
, &ms_global
.warmup_lock
.lock
);
660 pthread_mutex_unlock(&ms_global
.warmup_lock
.lock
);
662 ms_global
.finish_warmup
= true;
664 /* running in "run time" mode, user specify run time */
665 if (ms_setting
.run_time
> 0) {
667 gettimeofday(&start_time
, NULL
);
672 if ((ms_setting
.stat_freq
> 0) && (second
% ms_setting
.stat_freq
== 0)
673 && (ms_stats
.active_conns
>= ms_setting
.nconns
) && (ms_stats
.active_conns
<= INT_MAX
))
675 ms_print_statistics(second
);
678 if (ms_setting
.run_time
<= second
) {
679 ms_global
.time_out
= true;
683 /* all connections disconnect */
684 if ((second
> 5) && (ms_stats
.active_conns
== 0)) {
688 gettimeofday(&end_time
, NULL
);
689 sleep(1); /* wait all threads clean up */
691 /* running in "execute number" mode, user specify execute number */
692 gettimeofday(&start_time
, NULL
);
695 * We loop until we know that all connects have cleaned up.
697 pthread_mutex_lock(&ms_global
.run_lock
.lock
);
698 while (ms_global
.run_lock
.count
< ms_setting
.nconns
) {
699 pthread_cond_wait(&ms_global
.run_lock
.cond
, &ms_global
.run_lock
.lock
);
701 pthread_mutex_unlock(&ms_global
.run_lock
.lock
);
703 gettimeofday(&end_time
, NULL
);
706 ms_print_memaslap_stats(&start_time
, &end_time
);
707 } /* ms_monitor_slap_mode */
709 /* the main function */
710 int main(int argc
, char *argv
[]) {
711 srandom((unsigned int) time(NULL
));
712 ms_global_struct_init();
715 ms_setting_init_pre();
716 ms_options_parse(argc
, argv
);
717 if (ms_check_para()) {
718 ms_help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
);
721 ms_setting_init_post();
725 /* waiting work thread complete its task */
726 ms_monitor_slap_mode();
730 ms_global_struct_destroy();
731 ms_setting_cleanup();