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