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