efda8affae122722bb2f5aae61db84ad7d31488d
[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
315 if ((help_message= (char *)ms_lookup_help(long_options[x].val)) != NULL)
316 {
317 printf(" %s\n", help_message);
318 }
319 }
320
321 printf(
322 "\nExamples:\n"
323 " memslap -s 127.0.0.1:11211 -S 5s\n"
324 " memslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n"
325 " memslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n"
326 " memslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n"
327 " memslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n"
328 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n"
329 " memslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n");
330
331 exit(0);
332 } /* ms_help_command */
333
334
335 /* used to parse the time string */
336 static int64_t ms_parse_time()
337 {
338 int64_t ret= 0;
339 char unit= optarg[strlen(optarg) - 1];
340
341 optarg[strlen(optarg) - 1]= '\0';
342 ret= atoi(optarg);
343
344 switch (unit)
345 {
346 case 'd':
347 case 'D':
348 ret*= 24;
349
350 case 'h':
351 case 'H':
352 ret*= 60;
353
354 case 'm':
355 case 'M':
356 ret*= 60;
357
358 case 's':
359 case 'S':
360 break;
361
362 default:
363 ret= -1;
364 break;
365 } /* switch */
366
367 return ret;
368 } /* ms_parse_time */
369
370
371 /* used to parse the size string */
372 static int64_t ms_parse_size()
373 {
374 int64_t ret= -1;
375 char unit= optarg[strlen(optarg) - 1];
376
377 optarg[strlen(optarg) - 1]= '\0';
378 ret= strtoll(optarg, (char **)NULL, 10);
379
380 switch (unit)
381 {
382 case 'k':
383 case 'K':
384 ret*= 1024;
385 break;
386
387 case 'm':
388 case 'M':
389 ret*= 1024 * 1024;
390 break;
391
392 case 'g':
393 case 'G':
394 ret*= 1024 * 1024 * 1024;
395 break;
396
397 default:
398 ret= -1;
399 break;
400 } /* switch */
401
402 return ret;
403 } /* ms_parse_size */
404
405
406 /* used to parse the options of command line */
407 static void ms_options_parse(int argc, char *argv[])
408 {
409 int option_index= 0;
410 int option_rv;
411
412 while ((option_rv= getopt_long(argc, argv, "VhURbaBs:x:T:c:X:v:d:"
413 "t:S:F:w:e:o:n:P:p:",
414 long_options, &option_index)) != -1)
415 {
416 switch (option_rv)
417 {
418 case 0:
419 break;
420
421 case OPT_VERSION: /* --version or -V */
422 ms_version_command(PROGRAM_NAME);
423 break;
424
425 case OPT_HELP: /* --help or -h */
426 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
427 break;
428
429 case OPT_SERVERS: /* --servers or -s */
430 ms_setting.srv_str= strdup(optarg);
431 break;
432
433 case OPT_CONCURRENCY: /* --concurrency or -c */
434 ms_setting.nconns= (int)strtol(optarg, (char **) NULL, 10);
435 if (ms_setting.nconns <= 0)
436 {
437 fprintf(stderr, "Concurrency must be greater than 0.:-)\n");
438 exit(1);
439 }
440 break;
441
442 case OPT_EXECUTE_NUMBER: /* --execute_number or -x */
443 ms_setting.exec_num= (int)strtol(optarg, (char **) NULL, 10);
444 if (ms_setting.exec_num <= 0)
445 {
446 fprintf(stderr, "Execute number must be greater than 0.:-)\n");
447 exit(1);
448 }
449 break;
450
451 case OPT_THREAD_NUMBER: /* --threads or -T */
452 ms_setting.nthreads= (int)strtol(optarg, (char **) NULL, 10);
453 if (ms_setting.nthreads <= 0)
454 {
455 fprintf(stderr, "Threads number must be greater than 0.:-)\n");
456 exit(1);
457 }
458 break;
459
460 case OPT_FIXED_LTH: /* --fixed_size or -X */
461 ms_setting.fixed_value_size= (size_t)strtoull(optarg, (char **) NULL, 10);
462 if ((ms_setting.fixed_value_size <= 0)
463 || (ms_setting.fixed_value_size > MAX_VALUE_SIZE))
464 {
465 fprintf(stderr, "Value size must be between 0 and 1M.:-)\n");
466 exit(1);
467 }
468 break;
469
470 case OPT_VERIFY: /* --verify or -v */
471 ms_setting.verify_percent= atof(optarg);
472 if ((ms_setting.verify_percent <= 0)
473 || (ms_setting.verify_percent > 1.0))
474 {
475 fprintf(stderr, "Data verification rate must be "
476 "greater than 0 and less than 1.0. :-)\n");
477 exit(1);
478 }
479 break;
480
481 case OPT_GETS_DIVISION: /* --division or -d */
482 ms_setting.mult_key_num= (int)strtol(optarg, (char **) NULL, 10);
483 if (ms_setting.mult_key_num <= 0)
484 {
485 fprintf(stderr, "Multi-get key number must be greater than 0.:-)\n");
486 exit(1);
487 }
488 break;
489
490 case OPT_TIME: /* --time or -t */
491 ms_setting.run_time= (int)ms_parse_time();
492 if (ms_setting.run_time == -1)
493 {
494 fprintf(stderr, "Please specify the run time. :-)\n"
495 "'s' for second, 'm' for minute, 'h' for hour, "
496 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
497 exit(1);
498 }
499
500 if (ms_setting.run_time == 0)
501 {
502 fprintf(stderr, "Running time can not be 0. :-)\n");
503 exit(1);
504 }
505 break;
506
507 case OPT_CONFIG_CMD: /* --cfg_cmd or -F */
508 ms_setting.cfg_file= strdup(optarg);
509 break;
510
511 case OPT_WINDOW_SIZE: /* --win_size or -w */
512 ms_setting.win_size= (size_t)ms_parse_size();
513 if (ms_setting.win_size == (size_t)-1)
514 {
515 fprintf(
516 stderr,
517 "Please specify the item window size. :-)\n"
518 "e.g.: --win_size=10k (means 10k task window size).\n");
519 exit(1);
520 }
521 break;
522
523 case OPT_UDP: /* --udp or -U*/
524 ms_setting.udp= true;
525 break;
526
527 case OPT_EXPIRE: /* --exp_verify or -e */
528 ms_setting.exp_ver_per= atof(optarg);
529 if ((ms_setting.exp_ver_per <= 0) || (ms_setting.exp_ver_per > 1.0))
530 {
531 fprintf(stderr, "Expire time verification rate must be "
532 "greater than 0 and less than 1.0. :-)\n");
533 exit(1);
534 }
535 break;
536
537 case OPT_OVERWRITE: /* --overwrite or -o */
538 ms_setting.overwrite_percent= atof(optarg);
539 if ((ms_setting.overwrite_percent <= 0)
540 || (ms_setting.overwrite_percent > 1.0))
541 {
542 fprintf(stderr, "Objects overwrite rate must be "
543 "greater than 0 and less than 1.0. :-)\n");
544 exit(1);
545 }
546 break;
547
548 case OPT_STAT_FREQ: /* --stat_freq or -S */
549 ms_setting.stat_freq= (int)ms_parse_time();
550 if (ms_setting.stat_freq == -1)
551 {
552 fprintf(stderr, "Please specify the frequency of dumping "
553 "statistic information. :-)\n"
554 "'s' for second, 'm' for minute, 'h' for hour, "
555 "'d' for day. e.g.: --time=24h (means 24 hours).\n");
556 exit(1);
557 }
558
559 if (ms_setting.stat_freq == 0)
560 {
561 fprintf(stderr, "The frequency of dumping statistic information "
562 "can not be 0. :-)\n");
563 exit(1);
564 }
565 break;
566
567 case OPT_SOCK_PER_CONN: /* --conn_sock or -n */
568 ms_setting.sock_per_conn= (int)strtol(optarg, (char **) NULL, 10);
569 if (ms_setting.sock_per_conn <= 0)
570 {
571 fprintf(stderr, "Number of socks of each concurrency "
572 "must be greater than 0.:-)\n");
573 exit(1);
574 }
575 break;
576
577 case OPT_RECONNECT: /* --reconnect or -R */
578 ms_setting.reconnect= true;
579 break;
580
581 case OPT_VERBOSE: /* --verbose or -b */
582 ms_setting.verbose= true;
583 break;
584
585 case OPT_FACEBOOK_TEST: /* --facebook or -a */
586 ms_setting.facebook_test= true;
587 break;
588
589 case OPT_BINARY_PROTOCOL: /* --binary or -B */
590 ms_setting.binary_prot= true;
591 break;
592
593 case OPT_TPS: /* --tps or -P */
594 ms_setting.expected_tps= (int)ms_parse_size();
595 if (ms_setting.expected_tps == -1)
596 {
597 fprintf(stderr,
598 "Please specify the item expected throughput. :-)\n"
599 "e.g.: --tps=10k (means 10k throughput).\n");
600 exit(1);
601 }
602 break;
603
604 case OPT_REP_WRITE_SRV: /* --rep_write or -p */
605 ms_setting.rep_write_srv= (int)strtol(optarg, (char **) NULL, 10);
606 if (ms_setting.rep_write_srv <= 0)
607 {
608 fprintf(stderr,
609 "Number of replication writing server must be greater "
610 "than 0.:-)\n");
611 exit(1);
612 }
613 break;
614
615 case '?':
616 /* getopt_long already printed an error message. */
617 exit(1);
618
619 default:
620 abort();
621 } /* switch */
622 }
623 } /* ms_options_parse */
624
625
626 static int ms_check_para()
627 {
628 if (ms_setting.srv_str == NULL)
629 {
630 char *temp;
631
632 if ((temp= getenv("MEMCACHED_SERVERS")))
633 {
634 ms_setting.srv_str= strdup(temp);
635 }
636 else
637 {
638 fprintf(stderr, "No Servers provided\n\n");
639 return -1;
640 }
641 }
642
643 if (ms_setting.nconns % ms_setting.nthreads != 0)
644 {
645 fprintf(stderr, "Concurrency must be the multiples of threads count.\n");
646 return -1;
647 }
648
649 if (ms_setting.win_size % UNIT_ITEMS_COUNT != 0)
650 {
651 fprintf(stderr, "Window size must be the multiples of 1024.\n\n");
652 return -1;
653 }
654
655 return 0;
656 } /* ms_check_para */
657
658
659 /* initialize the statistic structure */
660 static void ms_statistic_init()
661 {
662 pthread_mutex_init(&ms_statistic.stat_mutex, NULL);
663 ms_init_stats(&ms_statistic.get_stat, "Get");
664 ms_init_stats(&ms_statistic.set_stat, "Set");
665 ms_init_stats(&ms_statistic.total_stat, "Total");
666 } /* ms_statistic_init */
667
668
669 /* initialize the global state structure */
670 static void ms_stats_init()
671 {
672 memset(&ms_stats, 0, sizeof(ms_stats_t));
673 if (ms_setting.stat_freq > 0)
674 {
675 ms_statistic_init();
676 }
677 } /* ms_stats_init */
678
679
680 /* use to output the statistic */
681 static void ms_print_statistics(int in_time)
682 {
683 int obj_size= (int)(ms_setting.avg_key_size + ms_setting.avg_val_size);
684
685 printf("\033[1;1H\033[2J\n");
686 ms_dump_format_stats(&ms_statistic.get_stat, in_time,
687 ms_setting.stat_freq, obj_size);
688 ms_dump_format_stats(&ms_statistic.set_stat, in_time,
689 ms_setting.stat_freq, obj_size);
690 ms_dump_format_stats(&ms_statistic.total_stat, in_time,
691 ms_setting.stat_freq, obj_size);
692 } /* ms_print_statistics */
693
694
695 /* used to print the states of memslap */
696 static void ms_print_memslap_stats(struct timeval *start_time,
697 struct timeval *end_time)
698 {
699 char buf[1024];
700 char *pos= buf;
701
702 pos+= sprintf(pos,
703 "cmd_get: %llu\n",
704 (unsigned long long)ms_stats.cmd_get);
705 pos+= sprintf(pos,
706 "cmd_set: %llu\n",
707 (unsigned long long)ms_stats.cmd_set);
708 pos+= sprintf(pos,
709 "get_misses: %llu\n",
710 (unsigned long long)ms_stats.get_misses);
711
712 if (ms_setting.verify_percent > 0)
713 {
714 pos+= sprintf(pos, "verify_misses: %llu\n",
715 (unsigned long long)ms_stats.vef_miss);
716 pos+= sprintf(pos, "verify_failed: %llu\n",
717 (unsigned long long)ms_stats.vef_failed);
718 }
719
720 if (ms_setting.exp_ver_per > 0)
721 {
722 pos+= sprintf(pos, "expired_get: %llu\n",
723 (unsigned long long)ms_stats.exp_get);
724 pos+= sprintf(pos, "unexpired_unget: %llu\n",
725 (unsigned long long)ms_stats.unexp_unget);
726 }
727
728 pos+= sprintf(pos,
729 "written_bytes: %llu\n",
730 (unsigned long long)ms_stats.bytes_written);
731 pos+= sprintf(pos,
732 "read_bytes: %llu\n",
733 (unsigned long long)ms_stats.bytes_read);
734 pos+= sprintf(pos,
735 "object_bytes: %llu\n",
736 (unsigned long long)ms_stats.obj_bytes);
737
738 if (ms_setting.udp || ms_setting.facebook_test)
739 {
740 pos+= sprintf(pos,
741 "packet_disorder: %llu\n",
742 (unsigned long long)ms_stats.pkt_disorder);
743 pos+= sprintf(pos,
744 "packet_drop: %llu\n",
745 (unsigned long long)ms_stats.pkt_drop);
746 pos+= sprintf(pos,
747 "udp_timeout: %llu\n",
748 (unsigned long long)ms_stats.udp_timeout);
749 }
750
751 if (ms_setting.stat_freq > 0)
752 {
753 ms_dump_stats(&ms_statistic.get_stat);
754 ms_dump_stats(&ms_statistic.set_stat);
755 ms_dump_stats(&ms_statistic.total_stat);
756 }
757
758 int64_t time_diff= ms_time_diff(start_time, end_time);
759 pos+= sprintf(
760 pos,
761 "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
762 (double)time_diff / 1000000,
763 (unsigned long long)(ms_stats.cmd_get + ms_stats.cmd_set),
764 (ms_stats.cmd_get
765 + ms_stats.cmd_set) / ((long double)time_diff / 1000000),
766 (double)(
767 ms_stats.bytes_written
768 + ms_stats.bytes_read) / 1024 / 1024
769 / ((double)time_diff / 1000000));
770
771 fprintf(stdout, "%s", buf);
772 fflush(stdout);
773 } /* ms_print_memslap_stats */
774
775
776 /* the loop of the main thread, wait the work threads to complete */
777 static void ms_monitor_slap_mode()
778 {
779 int second= 0;
780 struct timeval start_time, end_time;
781
782 /* only when there is no set operation it need warm up */
783 if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR)
784 {
785 /* Wait all the connects complete warm up. */
786 pthread_mutex_lock(&ms_global.init_lock.lock);
787 while (ms_global.init_lock.count < ms_setting.nconns)
788 {
789 pthread_cond_wait(&ms_global.init_lock.cond,
790 &ms_global.init_lock.lock);
791 }
792 pthread_mutex_unlock(&ms_global.init_lock.lock);
793 }
794
795 ms_global.finish_warmup= true;
796
797 /* running in "run time" mode, user specify run time */
798 if (ms_setting.run_time > 0)
799 {
800 gettimeofday(&start_time, NULL);
801 while (1)
802 {
803 sleep(1);
804 second++;
805
806 if ((ms_setting.stat_freq > 0) && (second % ms_setting.stat_freq == 0)
807 && (ms_stats.active_conns >= ms_setting.nconns)
808 && (ms_stats.active_conns <= INT_MAX))
809 {
810 ms_print_statistics(second);
811 }
812
813 if (ms_setting.run_time <= second)
814 {
815 ms_global.time_out= true;
816 break;
817 }
818
819 /* all connections disconnect */
820 if ((second > 5) && (ms_stats.active_conns == 0))
821 {
822 break;
823 }
824 }
825 gettimeofday(&end_time, NULL);
826 sleep(1); /* wait all threads clean up */
827 }
828 else
829 {
830 /* running in "execute number" mode, user specify execute number */
831 gettimeofday(&start_time, NULL);
832
833 /*
834 * We loop until we know that all connects have cleaned up.
835 */
836 pthread_mutex_lock(&ms_global.run_lock.lock);
837 while (ms_global.run_lock.count < ms_setting.nconns)
838 {
839 pthread_cond_wait(&ms_global.run_lock.cond, &ms_global.run_lock.lock);
840 }
841 pthread_mutex_unlock(&ms_global.run_lock.lock);
842
843 gettimeofday(&end_time, NULL);
844 }
845
846 ms_print_memslap_stats(&start_time, &end_time);
847 } /* ms_monitor_slap_mode */
848
849
850 /* the main function */
851 int main(int argc, char *argv[])
852 {
853 srandom((unsigned int)time(NULL));
854 ms_global_struct_init();
855
856 /* initialization */
857 ms_setting_init_pre();
858 ms_options_parse(argc, argv);
859 if (ms_check_para())
860 {
861 ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION);
862 exit(1);
863 }
864 ms_setting_init_post();
865 ms_stats_init();
866 ms_thread_init();
867
868 /* waiting work thread complete its task */
869 ms_monitor_slap_mode();
870
871 /* clean up */
872 ms_thread_cleanup();
873 ms_global_struct_destroy();
874 ms_setting_cleanup();
875
876 return 0;
877 } /* main */