11b4df8735ffe0f50a49522552fbaf1f9c04efd1
[awesomized/libmemcached] / clients / memslap.c
1 /*
2 * memslap
3 *
4 * (c) Copyright 2009, Schooner Information Technology, Inc.
5 * All rights reserved.
6 * http://www.schoonerinfotech.com/
7 *
8 * Use and distribution licensed under the BSD license. See
9 * the COPYING file for full text.
10 *
11 * Authors:
12 * Brian Aker
13 * Mingqiang Zhuang <mingqiangzhuang@hengtiansoft.com>
14 *
15 */
16 #include "config.h"
17
18 #include <stdlib.h>
19 #include <getopt.h>
20 #include <limits.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 # include <sys/time.h>
27 # else
28 # include <time.h>
29 # endif
30 #endif
31
32
33 #include "ms_sigsegv.h"
34 #include "ms_setting.h"
35 #include "ms_thread.h"
36
37 #define PROGRAM_NAME "memslap"
38 #define PROGRAM_DESCRIPTION \
39 "Generates workload against memcached servers."
40
41 #ifdef __sun
42 /* For some odd reason the option struct on solaris defines the argument
43 * as char* and not const char*
44 */
45 #define OPTIONSTRING char*
46 #else
47 #define OPTIONSTRING const char*
48 #endif
49
50 /* options */
51 static struct option long_options[]=
52 {
53 { (OPTIONSTRING)"servers", required_argument, NULL,
54 OPT_SERVERS },
55 { (OPTIONSTRING)"threads", required_argument, NULL,
56 OPT_THREAD_NUMBER },
57 { (OPTIONSTRING)"concurrency", required_argument, NULL,
58 OPT_CONCURRENCY },
59 { (OPTIONSTRING)"conn_sock", required_argument, NULL,
60 OPT_SOCK_PER_CONN },
61 { (OPTIONSTRING)"execute_number", required_argument, NULL,
62 OPT_EXECUTE_NUMBER },
63 { (OPTIONSTRING)"time", required_argument, NULL,
64 OPT_TIME },
65 { (OPTIONSTRING)"cfg_cmd", required_argument, NULL,
66 OPT_CONFIG_CMD },
67 { (OPTIONSTRING)"win_size", required_argument, NULL,
68 OPT_WINDOW_SIZE },
69 { (OPTIONSTRING)"fixed_size", required_argument, NULL,
70 OPT_FIXED_LTH },
71 { (OPTIONSTRING)"verify", required_argument, NULL,
72 OPT_VERIFY },
73 { (OPTIONSTRING)"division", required_argument, NULL,
74 OPT_GETS_DIVISION },
75 { (OPTIONSTRING)"stat_freq", required_argument, NULL,
76 OPT_STAT_FREQ },
77 { (OPTIONSTRING)"exp_verify", required_argument, NULL,
78 OPT_EXPIRE },
79 { (OPTIONSTRING)"overwrite", required_argument, NULL,
80 OPT_OVERWRITE },
81 { (OPTIONSTRING)"reconnect", no_argument, NULL,
82 OPT_RECONNECT },
83 { (OPTIONSTRING)"udp", no_argument, NULL,
84 OPT_UDP },
85 { (OPTIONSTRING)"facebook", no_argument, NULL,
86 OPT_FACEBOOK_TEST },
87 { (OPTIONSTRING)"binary", no_argument, NULL,
88 OPT_BINARY_PROTOCOL },
89 { (OPTIONSTRING)"tps", required_argument, NULL,
90 OPT_TPS },
91 { (OPTIONSTRING)"rep_write", required_argument, NULL,
92 OPT_REP_WRITE_SRV },
93 { (OPTIONSTRING)"verbose", no_argument, NULL,
94 OPT_VERBOSE },
95 { (OPTIONSTRING)"help", no_argument, NULL,
96 OPT_HELP },
97 { (OPTIONSTRING)"version", no_argument, NULL,
98 OPT_VERSION },
99 { 0, 0, 0, 0 },
100 };
101
102 /* Prototypes */
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);
120
121
122 /* initialize the global locks */
123 static void ms_sync_lock_init()
124 {
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);
128
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);
132
133 pthread_mutex_init(&ms_global.quit_mutex, NULL);
134 pthread_mutex_init(&ms_global.seq_mutex, NULL);
135 } /* ms_sync_lock_init */
136
137
138 /* destroy the global locks */
139 static void ms_sync_lock_destroy()
140 {
141 pthread_mutex_destroy(&ms_global.init_lock.lock);
142 pthread_cond_destroy(&ms_global.init_lock.cond);
143
144 pthread_mutex_destroy(&ms_global.run_lock.lock);
145 pthread_cond_destroy(&ms_global.run_lock.cond);
146
147 pthread_mutex_destroy(&ms_global.quit_mutex);
148 pthread_mutex_destroy(&ms_global.seq_mutex);
149
150 if (ms_setting.stat_freq > 0)
151 {
152 pthread_mutex_destroy(&ms_statistic.stat_mutex);
153 }
154 } /* ms_sync_lock_destroy */
155
156
157 /* initialize the global structure */
158 static void ms_global_struct_init()
159 {
160 ms_sync_lock_init();
161 ms_global.finish_warmup= false;
162 ms_global.time_out= false;
163 }
164
165
166 /* destroy the global structure */
167 static void ms_global_struct_destroy()
168 {
169 ms_sync_lock_destroy();
170 }
171
172
173 /**
174 * output the version information
175 *
176 * @param command_name, the string of this process
177 */
178 static void ms_version_command(const char *command_name)
179 {
180 printf("%s v%u.%u\n", command_name, 1U, 0U);
181 exit(0);
182 }
183
184
185 /**
186 * get the description of the option
187 *
188 * @param option, option of command line
189 *
190 * @return char*, description of the command option
191 */
192 static const char *ms_lookup_help(ms_options_t option)
193 {
194 switch (option)
195 {
196 case OPT_SERVERS:
197 return
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";
200
201 case OPT_VERSION:
202 return "Display the version of the application and then exit.";
203
204 case OPT_HELP:
205 return "Display this message and then exit.";
206
207 case OPT_EXECUTE_NUMBER:
208 return "Number of operations(get and set) to execute for the\n"
209 " given test. Default 1000000.";
210
211 case OPT_THREAD_NUMBER:
212 return
213 "Number of threads to startup, better equal to CPU numbers. Default 8.";
214
215 case OPT_CONCURRENCY:
216 return "Number of concurrency to simulate with load. Default 128.";
217
218 case OPT_FIXED_LTH:
219 return "Fixed length of value.";
220
221 case OPT_VERIFY:
222 return "The proportion of date verification, e.g.: --verify=0.01";
223
224 case OPT_GETS_DIVISION:
225 return "Number of keys to multi-get once. Default 1, means single get.";
226
227 case OPT_TIME:
228 return
229 "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n"
230 " d-days e.g.: --time=2h.";
231
232 case OPT_CONFIG_CMD:
233 return
234 "Load the configure file to get command,key and value distribution list.";
235
236 case OPT_WINDOW_SIZE:
237 return
238 "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n"
239 " Default 10k.";
240
241 case OPT_UDP:
242 return
243 "UDP support, default memslap uses TCP, TCP port and UDP port of\n"
244 " server must be same.";
245
246 case OPT_EXPIRE:
247 return
248 "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n"
249 " Default no object with expire time";
250
251 case OPT_OVERWRITE:
252 return
253 "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n"
254 " Default never overwrite object.";
255
256 case OPT_STAT_FREQ:
257 return
258 "Frequency of dumping statistic information. suffix: s-seconds,\n"
259 " m-minutes, e.g.: --resp_freq=10s.";
260
261 case OPT_SOCK_PER_CONN:
262 return "Number of TCP socks per concurrency. Default 1.";
263
264 case OPT_RECONNECT:
265 return
266 "Reconnect support, when connection is closed it will be reconnected.";
267
268 case OPT_VERBOSE:
269 return
270 "Whether it outputs detailed information when verification fails.";
271
272 case OPT_FACEBOOK_TEST:
273 return
274 "Whether it enables facebook test feature, set with TCP and multi-get with UDP.";
275
276 case OPT_BINARY_PROTOCOL:
277 return
278 "Whether it enables binary protocol. Default with ASCII protocol.";
279
280 case OPT_TPS:
281 return "Expected throughput, suffix: K, e.g.: --tps=10k.";
282
283 case OPT_REP_WRITE_SRV:
284 return "The first nth servers can write data, e.g.: --rep_write=2.";
285
286 default:
287 return "Forgot to document this option :)";
288 } /* switch */
289 } /* ms_lookup_help */
290
291
292 /**
293 * output the help information
294 *
295 * @param command_name, the string of this process
296 * @param description, description of this process
297 * @param long_options, global options array
298 */
299 void ms_help_command(const char *command_name, const char *description)
300 {
301 char *help_message= NULL;
302
303 printf("%s v%u.%u\n", command_name, 1U, 0U);
304 printf(" %s\n\n", description);
305 printf(
306 "Usage:\n"
307 " memslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n"
308 "Options:\n");
309
310 for (int x= 0; long_options[x].name; x++)
311 {
312 printf(" -%c, --%s%c\n", long_options[x].val, long_options[x].name,
313 long_options[x].has_arg ? '=' : ' ');
314 if ((help_message= (char *)ms_lookup_help(long_options[x].val)) != NULL)
315 {
316 printf(" %s\n", help_message);
317 }
318 }
319
320 printf(
321 "\nExamples:\n"
322 " memslap -s 127.0.0.1:11211 -S 5s\n"
323 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
324 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
325 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
326 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
327 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
328 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
329
330 exit(0);
331 } /* ms_help_command */
332
333
334 /* used to parse the time string */
335 static int64_t ms_parse_time()
336 {
337 int64_t ret= 0;
338 char unit= optarg[strlen(optarg) - 1];
339
340 optarg[strlen(optarg) - 1]= '\0';
341 ret= atoi(optarg);
342
343 switch (unit)
344 {
345 case 'd':
346 case 'D':
347 ret*= 24;
348
349 case 'h':
350 case 'H':
351 ret*= 60;
352
353 case 'm':
354 case 'M':
355 ret*= 60;
356
357 case 's':
358 case 'S':
359 break;
360
361 default:
362 ret= -1;
363 break;
364 } /* switch */
365
366 return ret;
367 } /* ms_parse_time */
368
369
370 /* used to parse the size string */
371 static int64_t ms_parse_size()
372 {
373 int64_t ret= -1;
374 char unit= optarg[strlen(optarg) - 1];
375
376 optarg[strlen(optarg) - 1]= '\0';
377 ret= strtoll(optarg, (char **)NULL, 10);
378
379 switch (unit)
380 {
381 case 'k':
382 case 'K':
383 ret*= 1024;
384 break;
385
386 case 'm':
387 case 'M':
388 ret*= 1024 * 1024;
389 break;
390
391 case 'g':
392 case 'G':
393 ret*= 1024 * 1024 * 1024;
394 break;
395
396 default:
397 ret= -1;
398 break;
399 } /* switch */
400
401 return ret;
402 } /* ms_parse_size */
403
404
405 /* used to parse the options of command line */
406 static void ms_options_parse(int argc, char *argv[])
407 {
408 int option_index= 0;
409 int option_rv;
410
411 while ((option_rv= getopt_long(argc, argv, "VhURbaBs:x:T:c:X:v:d:"
412 "t:S:F:w:e:o:n:P:p:",
413 long_options, &option_index)) != -1)
414 {
415 switch (option_rv)
416 {
417 case 0:
418 break;
419
420 case OPT_VERSION: /* --version or -V */
421 ms_version_command(PROGRAM_NAME);
422 break;
423
424 case OPT_HELP: /* --help or -h */
425 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
426 break;
427
428 case OPT_SERVERS: /* --servers or -s */
429 ms_setting.srv_str= strdup(optarg);
430 break;
431
432 case OPT_CONCURRENCY: /* --concurrency or -c */
433 ms_setting.nconns= atoi(optarg);
434 if (ms_setting.nconns <= 0)
435 {
436 fprintf(stderr, "Concurrency must be greater than 0.:-)\n");
437 exit(1);
438 }
439 break;
440
441 case OPT_EXECUTE_NUMBER: /* --execute_number or -x */
442 ms_setting.exec_num= atoll(optarg);
443 if (ms_setting.exec_num <= 0)
444 {
445 fprintf(stderr, "Execute number must be greater than 0.:-)\n");
446 exit(1);
447 }
448 break;
449
450 case OPT_THREAD_NUMBER: /* --threads or -T */
451 ms_setting.nthreads= atoi(optarg);
452 if (ms_setting.nthreads <= 0)
453 {
454 fprintf(stderr, "Threads number must be greater than 0.:-)\n");
455 exit(1);
456 }
457 break;
458
459 case OPT_FIXED_LTH: /* --fixed_size or -X */
460 ms_setting.fixed_value_size= (size_t)atoi(optarg);
461 if ((ms_setting.fixed_value_size <= 0)
462 || (ms_setting.fixed_value_size > MAX_VALUE_SIZE))
463 {
464 fprintf(stderr, "Value size must be between 0 and 1M.:-)\n");
465 exit(1);
466 }
467 break;
468
469 case OPT_VERIFY: /* --verify or -v */
470 ms_setting.verify_percent= atof(optarg);
471 if ((ms_setting.verify_percent <= 0)
472 || (ms_setting.verify_percent > 1.0))
473 {
474 fprintf(stderr, "Data verification rate must be "
475 "greater than 0 and less than 1.0. :-)\n");
476 exit(1);
477 }
478 break;
479
480 case OPT_GETS_DIVISION: /* --division or -d */
481 ms_setting.mult_key_num= atoi(optarg);
482 if (ms_setting.mult_key_num <= 0)
483 {
484 fprintf(stderr, "Multi-get key number must be greater than 0.:-)\n");
485 exit(1);
486 }
487 break;
488
489 case OPT_TIME: /* --time or -t */
490 ms_setting.run_time= (int)ms_parse_time();
491 if (ms_setting.run_time == -1)
492 {
493 fprintf(stderr, "Please specify the run time. :-)\n"
494 "'s' for second, 'm' for minute, 'h' for hour, "
495 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
496 exit(1);
497 }
498
499 if (ms_setting.run_time == 0)
500 {
501 fprintf(stderr, "Running time can not be 0. :-)\n");
502 exit(1);
503 }
504 break;
505
506 case OPT_CONFIG_CMD: /* --cfg_cmd or -F */
507 ms_setting.cfg_file= strdup(optarg);
508 break;
509
510 case OPT_WINDOW_SIZE: /* --win_size or -w */
511 ms_setting.win_size= (size_t)ms_parse_size();
512 if (ms_setting.win_size == (size_t)-1)
513 {
514 fprintf(
515 stderr,
516 "Please specify the item window size. :-)\n"
517 "e.g.: --win_size=10k (means 10k task window size).\n");
518 exit(1);
519 }
520 break;
521
522 case OPT_UDP: /* --udp or -U*/
523 ms_setting.udp= true;
524 break;
525
526 case OPT_EXPIRE: /* --exp_verify or -e */
527 ms_setting.exp_ver_per= atof(optarg);
528 if ((ms_setting.exp_ver_per <= 0) || (ms_setting.exp_ver_per > 1.0))
529 {
530 fprintf(stderr, "Expire time verification rate must be "
531 "greater than 0 and less than 1.0. :-)\n");
532 exit(1);
533 }
534 break;
535
536 case OPT_OVERWRITE: /* --overwrite or -o */
537 ms_setting.overwrite_percent= atof(optarg);
538 if ((ms_setting.overwrite_percent <= 0)
539 || (ms_setting.overwrite_percent > 1.0))
540 {
541 fprintf(stderr, "Objects overwrite rate must be "
542 "greater than 0 and less than 1.0. :-)\n");
543 exit(1);
544 }
545 break;
546
547 case OPT_STAT_FREQ: /* --stat_freq or -S */
548 ms_setting.stat_freq= (int)ms_parse_time();
549 if (ms_setting.stat_freq == -1)
550 {
551 fprintf(stderr, "Please specify the frequency of dumping "
552 "statistic information. :-)\n"
553 "'s' for second, 'm' for minute, 'h' for hour, "
554 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
555 exit(1);
556 }
557
558 if (ms_setting.stat_freq == 0)
559 {
560 fprintf(stderr, "The frequency of dumping statistic information "
561 "can not be 0. :-)\n");
562 exit(1);
563 }
564 break;
565
566 case OPT_SOCK_PER_CONN: /* --conn_sock or -n */
567 ms_setting.sock_per_conn= atoi(optarg);
568 if (ms_setting.sock_per_conn <= 0)
569 {
570 fprintf(stderr, "Number of socks of each concurrency "
571 "must be greater than 0.:-)\n");
572 exit(1);
573 }
574 break;
575
576 case OPT_RECONNECT: /* --reconnect or -R */
577 ms_setting.reconnect= true;
578 break;
579
580 case OPT_VERBOSE: /* --verbose or -b */
581 ms_setting.verbose= true;
582 break;
583
584 case OPT_FACEBOOK_TEST: /* --facebook or -a */
585 ms_setting.facebook_test= true;
586 break;
587
588 case OPT_BINARY_PROTOCOL: /* --binary or -B */
589 ms_setting.binary_prot= true;
590 break;
591
592 case OPT_TPS: /* --tps or -P */
593 ms_setting.expected_tps= (int)ms_parse_size();
594 if (ms_setting.expected_tps == -1)
595 {
596 fprintf(stderr,
597 "Please specify the item expected throughput. :-)\n"
598 "e.g.: --tps=10k (means 10k throughput).\n");
599 exit(1);
600 }
601 break;
602
603 case OPT_REP_WRITE_SRV: /* --rep_write or -p */
604 ms_setting.rep_write_srv= atoi(optarg);
605 if (ms_setting.rep_write_srv <= 0)
606 {
607 fprintf(stderr,
608 "Number of replication writing server must be greater "
609 "than 0.:-)\n");
610 exit(1);
611 }
612 break;
613
614 case '?':
615 /* getopt_long already printed an error message. */
616 exit(1);
617
618 default:
619 abort();
620 } /* switch */
621 }
622 } /* ms_options_parse */
623
624
625 static int ms_check_para()
626 {
627 if (ms_setting.srv_str == NULL)
628 {
629 fprintf(stderr, "No Servers provided.\n\n");
630 return -1;
631 }
632
633 if (ms_setting.nconns % ms_setting.nthreads != 0)
634 {
635 fprintf(stderr, "Concurrency must be the multiples of threads count.\n");
636 return -1;
637 }
638
639 if (ms_setting.win_size % UNIT_ITEMS_COUNT != 0)
640 {
641 fprintf(stderr, "Window size must be the multiples of 1024.\n\n");
642 return -1;
643 }
644
645 return 0;
646 } /* ms_check_para */
647
648
649 /* initialize the statistic structure */
650 static void ms_statistic_init()
651 {
652 pthread_mutex_init(&ms_statistic.stat_mutex, NULL);
653 ms_init_stats(&ms_statistic.get_stat, "Get");
654 ms_init_stats(&ms_statistic.set_stat, "Set");
655 ms_init_stats(&ms_statistic.total_stat, "Total");
656 } /* ms_statistic_init */
657
658
659 /* initialize the global state structure */
660 static void ms_stats_init()
661 {
662 memset(&ms_stats, 0, sizeof(ms_stats_t));
663 if (ms_setting.stat_freq > 0)
664 {
665 ms_statistic_init();
666 }
667 } /* ms_stats_init */
668
669
670 /* use to output the statistic */
671 static void ms_print_statistics(int in_time)
672 {
673 int obj_size= (int)(ms_setting.avg_key_size + ms_setting.avg_val_size);
674
675 printf("\033[1;1H\033[2J\n");
676 ms_dump_format_stats(&ms_statistic.get_stat, in_time,
677 ms_setting.stat_freq, obj_size);
678 ms_dump_format_stats(&ms_statistic.set_stat, in_time,
679 ms_setting.stat_freq, obj_size);
680 ms_dump_format_stats(&ms_statistic.total_stat, in_time,
681 ms_setting.stat_freq, obj_size);
682 } /* ms_print_statistics */
683
684
685 /* used to print the states of memslap */
686 static void ms_print_memslap_stats(struct timeval *start_time,
687 struct timeval *end_time)
688 {
689 char buf[1024];
690 char *pos= buf;
691
692 pos+= sprintf(pos,
693 "cmd_get: %llu\n",
694 (unsigned long long)ms_stats.cmd_get);
695 pos+= sprintf(pos,
696 "cmd_set: %llu\n",
697 (unsigned long long)ms_stats.cmd_set);
698 pos+= sprintf(pos,
699 "get_misses: %llu\n",
700 (unsigned long long)ms_stats.get_misses);
701
702 if (ms_setting.verify_percent > 0)
703 {
704 pos+= sprintf(pos, "verify_misses: %llu\n",
705 (unsigned long long)ms_stats.vef_miss);
706 pos+= sprintf(pos, "verify_failed: %llu\n",
707 (unsigned long long)ms_stats.vef_failed);
708 }
709
710 if (ms_setting.exp_ver_per > 0)
711 {
712 pos+= sprintf(pos, "expired_get: %llu\n",
713 (unsigned long long)ms_stats.exp_get);
714 pos+= sprintf(pos, "unexpired_unget: %llu\n",
715 (unsigned long long)ms_stats.unexp_unget);
716 }
717
718 pos+= sprintf(pos,
719 "written_bytes: %llu\n",
720 (unsigned long long)ms_stats.bytes_written);
721 pos+= sprintf(pos,
722 "read_bytes: %llu\n",
723 (unsigned long long)ms_stats.bytes_read);
724 pos+= sprintf(pos,
725 "object_bytes: %llu\n",
726 (unsigned long long)ms_stats.obj_bytes);
727
728 if (ms_setting.udp || ms_setting.facebook_test)
729 {
730 pos+= sprintf(pos,
731 "packet_disorder: %llu\n",
732 (unsigned long long)ms_stats.pkt_disorder);
733 pos+= sprintf(pos,
734 "packet_drop: %llu\n",
735 (unsigned long long)ms_stats.pkt_drop);
736 pos+= sprintf(pos,
737 "udp_timeout: %llu\n",
738 (unsigned long long)ms_stats.udp_timeout);
739 }
740
741 if (ms_setting.stat_freq > 0)
742 {
743 ms_dump_stats(&ms_statistic.get_stat);
744 ms_dump_stats(&ms_statistic.set_stat);
745 ms_dump_stats(&ms_statistic.total_stat);
746 }
747
748 int64_t time_diff= ms_time_diff(start_time, end_time);
749 pos+= sprintf(
750 pos,
751 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
752 (double)time_diff / 1000000,
753 (unsigned long long)(ms_stats.cmd_get + ms_stats.cmd_set),
754 (ms_stats.cmd_get
755 + ms_stats.cmd_set) / ((long double)time_diff / 1000000),
756 (double)(
757 ms_stats.bytes_written
758 + ms_stats.bytes_read) / 1024 / 1024
759 / ((double)time_diff / 1000000));
760
761 fprintf(stdout, "%s", buf);
762 fflush(stdout);
763 } /* ms_print_memslap_stats */
764
765
766 /* the loop of the main thread, wait the work threads to complete */
767 static void ms_monitor_slap_mode()
768 {
769 int second= 0;
770 struct timeval start_time, end_time;
771
772 /* only when there is no set operation it need warm up */
773 if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR)
774 {
775 /* Wait all the connects complete warm up. */
776 pthread_mutex_lock(&ms_global.init_lock.lock);
777 while (ms_global.init_lock.count < ms_setting.nconns)
778 {
779 pthread_cond_wait(&ms_global.init_lock.cond,
780 &ms_global.init_lock.lock);
781 }
782 pthread_mutex_unlock(&ms_global.init_lock.lock);
783 }
784
785 ms_global.finish_warmup= true;
786
787 /* running in "run time" mode, user specify run time */
788 if (ms_setting.run_time > 0)
789 {
790 gettimeofday(&start_time, NULL);
791 while (1)
792 {
793 sleep(1);
794 second++;
795
796 if ((ms_setting.stat_freq > 0) && (second % ms_setting.stat_freq == 0)
797 && ((int32_t)ms_stats.active_conns >= ms_setting.nconns)
798 && (ms_stats.active_conns <= (uint32_t)INT_MAX))
799 {
800 ms_print_statistics(second);
801 }
802
803 if (ms_setting.run_time <= second)
804 {
805 ms_global.time_out= true;
806 break;
807 }
808
809 /* all connections disconnect */
810 if ((second > 5) && (ms_stats.active_conns == 0))
811 {
812 break;
813 }
814 }
815 gettimeofday(&end_time, NULL);
816 sleep(1); /* wait all threads clean up */
817 }
818 else
819 {
820 /* running in "execute number" mode, user specify execute number */
821 gettimeofday(&start_time, NULL);
822
823 /*
824 * We loop until we know that all connects have cleaned up.
825 */
826 pthread_mutex_lock(&ms_global.run_lock.lock);
827 while (ms_global.run_lock.count < ms_setting.nconns)
828 {
829 pthread_cond_wait(&ms_global.run_lock.cond, &ms_global.run_lock.lock);
830 }
831 pthread_mutex_unlock(&ms_global.run_lock.lock);
832
833 gettimeofday(&end_time, NULL);
834 }
835
836 ms_print_memslap_stats(&start_time, &end_time);
837 } /* ms_monitor_slap_mode */
838
839
840 /* the main function */
841 int main(int argc, char *argv[])
842 {
843 srandom((unsigned int)time(NULL));
844 ms_global_struct_init();
845
846 /* initialization */
847 ms_setting_init_pre();
848 ms_options_parse(argc, argv);
849 if (ms_check_para())
850 {
851 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
852 exit(1);
853 }
854 ms_setting_init_post();
855 ms_stats_init();
856 ms_thread_init();
857
858 /* waiting work thread complete its task */
859 ms_monitor_slap_mode();
860
861 /* clean up */
862 ms_thread_cleanup();
863 ms_global_struct_destroy();
864 ms_setting_cleanup();
865
866 return 0;
867 } /* main */