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