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