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