9d328ddcf62ae831908c57b89ffd0b91a908aeed
[m6w6/libmemcached] / contrib / bin / memaslap / ms_setting.c
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include "libmemcached/memcached.h"
19
20 #include <ctype.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <pwd.h>
24 #include <strings.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "ms_setting.h"
29 #include "ms_conn.h"
30
31 #define MAX_EXEC_NUM 0x4000000000000000 /* 1 << 62 */
32 #define ADDR_ALIGN(addr) ((addr + 15) & ~(16 - 1)) /* 16 bytes aligned */
33 #define RAND_CHAR_SIZE (10 * 1024 * 1024) /* 10M character table */
34 #define RESERVED_RAND_CHAR_SIZE (2 * 1024 * 1024) /* reserved 2M to avoid pointer sloping over */
35
36 #define DEFAULT_CONFIG_NAME ".memslap.cnf"
37
38 #define DEFAULT_THREADS_NUM 1 /* default start one thread */
39 #define DEFAULT_CONNS_NUM 16 /* default each thread with 16 connections */
40 #define DEFAULT_EXE_NUM 0 /* default execute number is 0 */
41 #define DEFAULT_VERIFY_RATE 0.0 /* default it doesn't do data verification */
42 #define DEFAULT_OVERWRITE_RATE 0.0 /* default it doesn't do overwrite */
43 #define DEFAULT_DIV 1 /* default it runs single get */
44 #define DEFAULT_RUN_TIME 600 /* default run time 10 minutes */
45 #define DEFAULT_WINDOW_SIZE (10 * UNIT_ITEMS_COUNT) /* default window size is 10k */
46 #define DEFAULT_SOCK_PER_CONN 1 /* default socks per connection is 1 */
47
48 /* Use this for string generation */
49 #define CHAR_COUNT 64 /* number of characters used to generate character table */
50 const char ALPHANUMBERICS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-";
51
52 ms_setting_st ms_setting; /* store the settings specified by user */
53
54 /* read setting from configuration file */
55 static void ms_get_serverlist(char *str);
56 static uint32_t ms_get_cpu_count(void);
57 ms_conf_type_t ms_get_conf_type(char *line);
58 static int ms_is_line_data(char *line);
59 static int ms_read_is_data(char *line, ssize_t nread);
60 static void ms_no_config_file(void);
61 static void ms_parse_cfg_file(char *cfg_file);
62
63 /* initialize setting structure */
64 static void ms_init_random_block(void);
65 static void ms_calc_avg_size(void);
66 static int ms_shuffle_distr(ms_distr_t *distr, int length);
67 static void ms_build_distr(void);
68 static void ms_print_setting(void);
69 static void ms_setting_slapmode_init_pre(void);
70 static void ms_setting_slapmode_init_post(void);
71
72 #if !defined(HAVE_GETLINE)
73 # include <limits.h>
74 static ssize_t getline(char **line, size_t *line_size, FILE *fp) {
75 char delim = '\n';
76 ssize_t result = 0;
77 size_t cur_len = 0;
78
79 if (line == NULL || line_size == NULL || fp == NULL) {
80 errno = EINVAL;
81 return -1;
82 }
83
84 if (*line == NULL || *line_size == 0) {
85 char *new_line;
86 *line_size = 120;
87 new_line = (char *) realloc(*line, *line_size);
88 if (new_line == NULL) {
89 result = -1;
90 return result;
91 }
92 *line = new_line;
93 }
94
95 for (;;) {
96 int i = getc(fp);
97 if (i == EOF) {
98 result = -1;
99 break;
100 }
101
102 /* Make enough space for len+1 (for final NUL) bytes. */
103 if (cur_len + 1 >= *line_size) {
104 size_t needed_max = SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
105 size_t needed = (2 * (*line_size)) + 1;
106 char *new_line;
107
108 if (needed_max < needed)
109 needed = needed_max;
110 if (cur_len + 1 >= needed) {
111 result = -1;
112 errno = EOVERFLOW;
113 return result;
114 }
115
116 new_line = (char *) realloc(*line, needed);
117 if (new_line == NULL) {
118 result = -1;
119 return result;
120 }
121
122 *line = new_line;
123 *line_size = needed;
124 }
125
126 (*line)[cur_len] = (char) i;
127 cur_len++;
128
129 if (i == delim)
130 break;
131 }
132 (*line)[cur_len] = '\0';
133 if (cur_len)
134 return (ssize_t) cur_len;
135 return result;
136 }
137 #endif
138
139 /**
140 * parse the server list string, and build the servers
141 * information structure array. this function is used to parse
142 * the command line options specified by user.
143 *
144 * @param str, the string of server list
145 */
146 static void ms_get_serverlist(char *str) {
147 ms_mcd_server_t *srvs = NULL;
148
149 /**
150 * Servers list format is like this. For example:
151 * "localhost:11108, localhost:11109"
152 */
153 memcached_server_st *server_pool;
154 server_pool = memcached_servers_parse(str);
155
156 for (uint32_t loop = 0; loop < memcached_server_list_count(server_pool); loop++) {
157 assert(ms_setting.srv_cnt < ms_setting.total_srv_cnt);
158 strcpy(ms_setting.servers[ms_setting.srv_cnt].srv_host_name, server_pool[loop].hostname);
159 ms_setting.servers[ms_setting.srv_cnt].srv_port = server_pool[loop].port;
160 ms_setting.servers[ms_setting.srv_cnt].disconn_cnt = 0;
161 ms_setting.servers[ms_setting.srv_cnt].reconn_cnt = 0;
162 ms_setting.srv_cnt++;
163
164 if (ms_setting.srv_cnt >= ms_setting.total_srv_cnt) {
165 srvs = (ms_mcd_server_t *) realloc(
166 ms_setting.servers, (size_t) ms_setting.total_srv_cnt * sizeof(ms_mcd_server_t) * 2);
167 if (srvs == NULL) {
168 fprintf(stderr, "Can't reallocate servers structure.\n");
169 exit(1);
170 }
171 ms_setting.servers = srvs;
172 ms_setting.total_srv_cnt *= 2;
173 }
174 }
175
176 memcached_server_free(server_pool);
177 } /* ms_get_serverlist */
178
179 /**
180 * used to get the CPU count of the current system
181 *
182 * @return return the cpu count if get, else return EXIT_FAILURE
183 */
184 static uint32_t ms_get_cpu_count() {
185 #ifdef HAVE__SC_NPROCESSORS_ONLN
186 return sysconf(_SC_NPROCESSORS_CONF);
187
188 #else
189 # ifdef HAVE_CPU_SET_T
190 int cpu_count = 0;
191 cpu_set_t cpu_set;
192
193 sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set);
194
195 for (int i = 0; i < (sizeof(cpu_set_t) * 8); i++) {
196 if (CPU_ISSET(i, &cpu_set)) {
197 cpu_count++;
198 }
199 }
200
201 return cpu_count;
202
203 # endif
204 #endif
205
206 /* the system with one cpu at least */
207 return EXIT_FAILURE;
208 } /* ms_get_cpu_count */
209
210 /**
211 * used to get the configure type based on the type string read
212 * from the configuration file.
213 *
214 * @param line, string of one line
215 *
216 * @return ms_conf_type_t
217 */
218 ms_conf_type_t ms_get_conf_type(char *line) {
219 if (!memcmp(line, "key", strlen("key"))) {
220 return CONF_KEY;
221 } else if (!memcmp(line, "value", strlen("value"))) {
222 return CONF_VALUE;
223 } else if (!memcmp(line, "cmd", strlen("cmd"))) {
224 return CONF_CMD;
225 } else {
226 return CONF_NULL;
227 }
228 } /* ms_get_conf_type */
229
230 /**
231 * judge whether the line is a line with useful data. used to
232 * parse the configuration file.
233 *
234 * @param line, string of one line
235 *
236 * @return if success, return EXIT_FAILURE, else return EXIT_SUCCESS
237 */
238 static int ms_is_line_data(char *line) {
239 assert(line);
240
241 char *begin_ptr = line;
242
243 while (isspace(*begin_ptr)) {
244 begin_ptr++;
245 }
246 if ((begin_ptr[0] == '\0') || (begin_ptr[0] == '#'))
247 return EXIT_SUCCESS;
248
249 return EXIT_FAILURE;
250 } /* ms_is_line_data */
251
252 /**
253 * function to bypass blank line and comments
254 *
255 * @param line, string of one line
256 * @param nread, length of the line
257 *
258 * @return if it's EOF or not line data, return EXIT_SUCCESS, else return EXIT_FAILURE
259 */
260 static int ms_read_is_data(char *line, ssize_t nread) {
261 if ((nread == EOF) || !ms_is_line_data(line))
262 return EXIT_SUCCESS;
263
264 return EXIT_FAILURE;
265 } /* ms_read_is_data */
266
267 /**
268 * if no configuration file, use this function to create the default
269 * configuration file.
270 */
271 static void ms_no_config_file() {
272 char userpath[PATH_MAX];
273 struct passwd *usr = NULL;
274 FILE *fd;
275
276 usr = getpwuid(getuid());
277
278 snprintf(userpath, PATH_MAX, "%s/%s", usr->pw_dir, DEFAULT_CONFIG_NAME);
279
280 if (access(userpath, F_OK | R_OK) == 0)
281 goto exit;
282
283 fd = fopen(userpath, "w+");
284
285 if (fd == NULL) {
286 fprintf(stderr, "Could not create default configure file %s\n", userpath);
287 perror("fopen");
288 exit(1);
289 }
290 fprintf(fd, "%s", DEFAULT_CONGIF_STR);
291 fclose(fd);
292
293 exit:
294 ms_setting.cfg_file = strdup(userpath);
295 } /* ms_no_config_file */
296
297 /**
298 * parse the configuration file
299 *
300 * @param cfg_file, the configuration file name
301 */
302 static void ms_parse_cfg_file(char *cfg_file) {
303 FILE *f;
304 size_t start_len, end_len;
305 double proportion;
306 char *line = NULL;
307 size_t read_len;
308 ssize_t nread;
309 int cmd_type;
310 ms_conf_type_t conf_type;
311 int end_of_file = 0;
312 ms_key_distr_t *key_distr = NULL;
313 ms_value_distr_t *val_distr = NULL;
314
315 if (cfg_file == NULL) {
316 ms_no_config_file();
317 cfg_file = ms_setting.cfg_file;
318 }
319
320 /*read key value configure file*/
321 if ((f = fopen(cfg_file, "r")) == NULL) {
322 fprintf(stderr, "Can not open file: '%s'.\n", cfg_file);
323 exit(1);
324 }
325
326 while (1) {
327 if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread))
328 && (nread != EOF)) /* bypass blank line */
329 continue;
330
331 if (nread == EOF) {
332 fprintf(stderr, "Bad configuration file, no configuration find.\n");
333 exit(1);
334 }
335 conf_type = ms_get_conf_type(line);
336 break;
337 }
338
339 while (!end_of_file) {
340 switch (conf_type) {
341 case CONF_KEY:
342 while (1) {
343 if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread))
344 && (nread != EOF)) /* bypass blank line */
345 continue;
346
347 if (nread != EOF) {
348 if (sscanf(line, "%zu %zu %lf ", &start_len, &end_len, &proportion) != 3) {
349 conf_type = ms_get_conf_type(line);
350 break;
351 }
352 ms_setting.key_distr[ms_setting.key_rng_cnt].start_len = start_len;
353 ms_setting.key_distr[ms_setting.key_rng_cnt].end_len = end_len;
354 ms_setting.key_distr[ms_setting.key_rng_cnt].key_prop = proportion;
355 ms_setting.key_rng_cnt++;
356
357 if (ms_setting.key_rng_cnt >= ms_setting.total_key_rng_cnt) {
358 key_distr = (ms_key_distr_t *) realloc(ms_setting.key_distr,
359 (size_t) ms_setting.total_key_rng_cnt
360 * sizeof(ms_key_distr_t) * 2);
361 if (key_distr == NULL) {
362 fprintf(stderr, "Can't reallocate key distribution structure.\n");
363 exit(1);
364 }
365 ms_setting.key_distr = key_distr;
366 ms_setting.total_key_rng_cnt *= 2;
367 }
368 continue;
369 }
370 end_of_file = 1;
371 break;
372 }
373 break;
374
375 case CONF_VALUE:
376 while (1) {
377 if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread))
378 && (nread != EOF)) /* bypass blank line */
379 continue;
380
381 if (nread != EOF) {
382 if (sscanf(line, "%zu %zu %lf", &start_len, &end_len, &proportion) != 3) {
383 conf_type = ms_get_conf_type(line);
384 break;
385 }
386 ms_setting.value_distr[ms_setting.val_rng_cnt].start_len = start_len;
387 ms_setting.value_distr[ms_setting.val_rng_cnt].end_len = end_len;
388 ms_setting.value_distr[ms_setting.val_rng_cnt].value_prop = proportion;
389 ms_setting.val_rng_cnt++;
390
391 if (ms_setting.val_rng_cnt >= ms_setting.total_val_rng_cnt) {
392 val_distr = (ms_value_distr_t *) realloc(ms_setting.value_distr,
393 (size_t) ms_setting.total_val_rng_cnt
394 * sizeof(ms_value_distr_t) * 2);
395 if (val_distr == NULL) {
396 fprintf(stderr, "Can't reallocate key distribution structure.\n");
397 exit(1);
398 }
399 ms_setting.value_distr = val_distr;
400 ms_setting.total_val_rng_cnt *= 2;
401 }
402 continue;
403 }
404 end_of_file = 1;
405 break;
406 }
407 break;
408
409 case CONF_CMD:
410 while (1) {
411 if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread))
412 && (nread != EOF)) /* bypass blank line */
413 continue;
414
415 if (nread != EOF) {
416 if (sscanf(line, "%d %lf", &cmd_type, &proportion) != 2) {
417 conf_type = ms_get_conf_type(line);
418 break;
419 }
420 if (cmd_type >= CMD_NULL) {
421 continue;
422 }
423 ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_type = cmd_type;
424 ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_prop = proportion;
425 ms_setting.cmd_used_count++;
426 continue;
427 }
428 end_of_file = 1;
429 break;
430 }
431
432 case CONF_NULL:
433 while (1) {
434 if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread))
435 && (nread != EOF)) /* bypass blank line */
436 continue;
437
438 if (nread != EOF) {
439 if ((conf_type = ms_get_conf_type(line)) != CONF_NULL) {
440 break;
441 }
442 continue;
443 }
444 end_of_file = 1;
445 break;
446 }
447 break;
448
449 default:
450 assert(0);
451 break;
452 } /* switch */
453 }
454
455 fclose(f);
456
457 if (line) {
458 free(line);
459 }
460 } /* ms_parse_cfg_file */
461
462 /* calculate the average size of key and value */
463 static void ms_calc_avg_size() {
464 double avg_val_size = 0.0;
465 double avg_key_size = 0.0;
466 double val_pro = 0.0;
467 double key_pro = 0.0;
468 double averge_len = 0.0;
469 size_t start_len = 0;
470 size_t end_len = 0;
471
472 for (int j = 0; j < ms_setting.val_rng_cnt; j++) {
473 val_pro = ms_setting.value_distr[j].value_prop;
474 start_len = ms_setting.value_distr[j].start_len;
475 end_len = ms_setting.value_distr[j].end_len;
476
477 averge_len = val_pro * ((double) (start_len + end_len)) / 2;
478 avg_val_size += averge_len;
479 }
480
481 for (int j = 0; j < ms_setting.key_rng_cnt; j++) {
482 key_pro = ms_setting.key_distr[j].key_prop;
483 start_len = ms_setting.key_distr[j].start_len;
484 end_len = ms_setting.key_distr[j].end_len;
485
486 averge_len = key_pro * ((double) (start_len + end_len)) / 2;
487 avg_key_size += averge_len;
488 }
489
490 ms_setting.avg_val_size = (size_t) avg_val_size;
491 ms_setting.avg_key_size = (size_t) avg_key_size;
492 } /* ms_calc_avg_size */
493
494 /**
495 * used to shuffle key and value distribution array to ensure
496 * (key, value) pair with different set.
497 *
498 * @param distr, pointer of distribution structure array
499 * @param length, length of the array
500 *
501 * @return always return EXIT_SUCCESS
502 */
503 static int ms_shuffle_distr(ms_distr_t *distr, int length) {
504 int i, j;
505 int tmp_offset;
506 size_t tmp_size;
507 int64_t rnd;
508
509 for (i = 0; i < length; i++) {
510 rnd = random();
511 j = (int) (rnd % (length - i)) + i;
512
513 switch (rnd % 3) {
514 case 0:
515 tmp_size = distr[j].key_size;
516 distr[j].key_size = distr[i].key_size;
517 distr[i].key_size = tmp_size;
518 break;
519
520 case 1:
521 tmp_offset = distr[j].key_offset;
522 distr[j].key_offset = distr[i].key_offset;
523 distr[i].key_offset = tmp_offset;
524 break;
525
526 case 2:
527 tmp_size = distr[j].value_size;
528 distr[j].value_size = distr[i].value_size;
529 distr[i].value_size = tmp_size;
530 break;
531
532 default:
533 break;
534 } /* switch */
535 }
536
537 return EXIT_SUCCESS;
538 } /* ms_shuffle_distr */
539
540 /**
541 * according to the key and value distribution, to build the
542 * (key, value) pair distribution. the (key, value) pair
543 * distribution array is global, each connection set or get
544 * object keeping this distribution, for the final result, we
545 * can reach the expected key and value distribution.
546 */
547 static void ms_build_distr() {
548 int offset = 0;
549 int end = 0;
550 int key_cnt = 0;
551 int value_cnt = 0;
552 size_t average_len = 0;
553 size_t diff_len = 0;
554 size_t start_len = 0;
555 size_t end_len = 0;
556 int rnd = 0;
557 ms_distr_t *distr = NULL;
558 int units = (int) ms_setting.win_size / UNIT_ITEMS_COUNT;
559
560 /* calculate average value size and key size */
561 ms_calc_avg_size();
562
563 ms_setting.char_blk_size = RAND_CHAR_SIZE;
564 int key_scope_size =
565 (int) ((ms_setting.char_blk_size - RESERVED_RAND_CHAR_SIZE) / UNIT_ITEMS_COUNT);
566
567 ms_setting.distr = (ms_distr_t *) malloc(sizeof(ms_distr_t) * ms_setting.win_size);
568 if (ms_setting.distr == NULL) {
569 fprintf(stderr, "Can't allocate distribution array.");
570 exit(1);
571 }
572
573 /**
574 * character block is divided by how many different key
575 * size, each different key size has the same size character
576 * range.
577 */
578 for (int m = 0; m < units; m++) {
579 for (int i = 0; i < UNIT_ITEMS_COUNT; i++) {
580 ms_setting.distr[m * UNIT_ITEMS_COUNT + i].key_offset = ADDR_ALIGN(key_scope_size * i);
581 }
582 }
583
584 /* initialize key size distribution */
585 for (int m = 0; m < units; m++) {
586 for (int j = 0; j < ms_setting.key_rng_cnt; j++) {
587 key_cnt = (int) (UNIT_ITEMS_COUNT * ms_setting.key_distr[j].key_prop);
588 start_len = ms_setting.key_distr[j].start_len;
589 end_len = ms_setting.key_distr[j].end_len;
590 if ((start_len < MIN_KEY_SIZE) || (end_len < MIN_KEY_SIZE)) {
591 fprintf(stderr, "key length must be greater than 16 bytes.\n");
592 exit(1);
593 }
594
595 if (!ms_setting.binary_prot_ && ((start_len > MAX_KEY_SIZE) || (end_len > MAX_KEY_SIZE))) {
596 fprintf(stderr, "key length must be less than 250 bytes.\n");
597 exit(1);
598 }
599
600 average_len = (start_len + end_len) / 2;
601 diff_len = (end_len - start_len) / 2;
602 for (int k = 0; k < key_cnt; k++) {
603 if (offset >= (m + 1) * UNIT_ITEMS_COUNT) {
604 break;
605 }
606 rnd = (int) random();
607 if (k % 2 == 0) {
608 ms_setting.distr[offset].key_size =
609 (diff_len == 0) ? average_len : average_len + (size_t) rnd % diff_len;
610 } else {
611 ms_setting.distr[offset].key_size =
612 (diff_len == 0) ? average_len : average_len - (size_t) rnd % diff_len;
613 }
614 offset++;
615 }
616 }
617
618 if (offset < (m + 1) * UNIT_ITEMS_COUNT) {
619 end = (m + 1) * UNIT_ITEMS_COUNT - offset;
620 for (int i = 0; i < end; i++) {
621 ms_setting.distr[offset].key_size = ms_setting.avg_key_size;
622 offset++;
623 }
624 }
625 }
626 offset = 0;
627
628 /* initialize value distribution */
629 if (ms_setting.fixed_value_size) {
630 for (int i = 0; i < units * UNIT_ITEMS_COUNT; i++) {
631 ms_setting.distr[i].value_size = ms_setting.fixed_value_size;
632 }
633 } else {
634 for (int m = 0; m < units; m++) {
635 for (int j = 0; j < ms_setting.val_rng_cnt; j++) {
636 value_cnt = (int) (UNIT_ITEMS_COUNT * ms_setting.value_distr[j].value_prop);
637 start_len = ms_setting.value_distr[j].start_len;
638 end_len = ms_setting.value_distr[j].end_len;
639 if ((start_len <= 0) || (end_len <= 0)) {
640 fprintf(stderr, "value length must be greater than 0 bytes.\n");
641 exit(1);
642 }
643
644 if ((start_len > MAX_VALUE_SIZE) || (end_len > MAX_VALUE_SIZE)) {
645 fprintf(stderr, "key length must be less than or equal to 1M.\n");
646 exit(1);
647 }
648
649 average_len = (start_len + end_len) / 2;
650 diff_len = (end_len - start_len) / 2;
651 for (int k = 0; k < value_cnt; k++) {
652 if (offset >= (m + 1) * UNIT_ITEMS_COUNT) {
653 break;
654 }
655 rnd = (int) random();
656 if (k % 2 == 0) {
657 ms_setting.distr[offset].value_size =
658 (diff_len == 0) ? average_len : average_len + (size_t) rnd % diff_len;
659 } else {
660 ms_setting.distr[offset].value_size =
661 (diff_len == 0) ? average_len : average_len - (size_t) rnd % diff_len;
662 }
663 offset++;
664 }
665 }
666
667 if (offset < (m + 1) * UNIT_ITEMS_COUNT) {
668 end = (m + 1) * UNIT_ITEMS_COUNT - offset;
669 for (int i = 0; i < end; i++) {
670 ms_setting.distr[offset++].value_size = ms_setting.avg_val_size;
671 }
672 }
673 }
674 }
675
676 /* shuffle distribution */
677 for (int i = 0; i < units; i++) {
678 distr = &ms_setting.distr[i * UNIT_ITEMS_COUNT];
679 for (int j = 0; j < 4; j++) {
680 ms_shuffle_distr(distr, UNIT_ITEMS_COUNT);
681 }
682 }
683 } /* ms_build_distr */
684
685 /**
686 * used to initialize the global character block. The character
687 * block is used to generate the suffix of the key and value. we
688 * only store a pointer in the character block for each key
689 * suffix or value string. It can save much memory to store key
690 * or value string.
691 */
692 static void ms_init_random_block() {
693 char *ptr = NULL;
694
695 assert(ms_setting.char_blk_size > 0);
696
697 ms_setting.char_block = (char *) malloc(ms_setting.char_blk_size);
698 if (ms_setting.char_block == NULL) {
699 fprintf(stderr, "Can't allocate global char block.");
700 exit(1);
701 }
702 ptr = ms_setting.char_block;
703
704 for (int i = 0; (size_t) i < ms_setting.char_blk_size; i++) {
705 *(ptr++) = ALPHANUMBERICS[random() % CHAR_COUNT];
706 }
707 } /* ms_init_random_block */
708
709 /**
710 * after initialization, call this function to output the main
711 * configuration user specified.
712 */
713 static void ms_print_setting() {
714 fprintf(stdout, "servers: %s\n", ms_setting.srv_str);
715 fprintf(stdout, "threads count: %d\n", ms_setting.nthreads);
716 fprintf(stdout, "concurrency: %d\n", ms_setting.nconns);
717 if (ms_setting.run_time > 0) {
718 fprintf(stdout, "run time: %ds\n", ms_setting.run_time);
719 } else {
720 fprintf(stdout, "execute number: %" PRId64 "\n", ms_setting.exec_num);
721 }
722 fprintf(stdout, "windows size: %" PRId64 "k\n", (int64_t)(ms_setting.win_size / 1024));
723 fprintf(stdout, "set proportion: set_prop=%.2f\n", ms_setting.cmd_distr[CMD_SET].cmd_prop);
724 fprintf(stdout, "get proportion: get_prop=%.2f\n", ms_setting.cmd_distr[CMD_GET].cmd_prop);
725 fflush(stdout);
726 } /* ms_print_setting */
727
728 /**
729 * previous part of slap mode initialization of setting structure
730 */
731 static void ms_setting_slapmode_init_pre() {
732 ms_setting.exec_num = DEFAULT_EXE_NUM;
733 ms_setting.verify_percent = DEFAULT_VERIFY_RATE;
734 ms_setting.exp_ver_per = DEFAULT_VERIFY_RATE;
735 ms_setting.overwrite_percent = DEFAULT_OVERWRITE_RATE;
736 ms_setting.mult_key_num = DEFAULT_DIV;
737 ms_setting.fixed_value_size = 0;
738 ms_setting.win_size = DEFAULT_WINDOW_SIZE;
739 ms_setting.udp = false;
740 ms_setting.reconnect = false;
741 ms_setting.verbose = false;
742 ms_setting.facebook_test = false;
743 ms_setting.binary_prot_ = false;
744 ms_setting.stat_freq = 0;
745 ms_setting.srv_str = NULL;
746 ms_setting.cfg_file = NULL;
747 ms_setting.sock_per_conn = DEFAULT_SOCK_PER_CONN;
748 ms_setting.expected_tps = 0;
749 ms_setting.rep_write_srv = 0;
750 } /* ms_setting_slapmode_init_pre */
751
752 /**
753 * previous part of initialization of setting structure
754 */
755 void ms_setting_init_pre() {
756 memset(&ms_setting, 0, sizeof(ms_setting));
757
758 /* common initialize */
759 ms_setting.ncpu = ms_get_cpu_count();
760 ms_setting.nthreads = DEFAULT_THREADS_NUM;
761 ms_setting.nconns = DEFAULT_CONNS_NUM;
762 ms_setting.run_time = DEFAULT_RUN_TIME;
763 ms_setting.total_srv_cnt = MCD_SRVS_NUM_INIT;
764 ms_setting.servers =
765 (ms_mcd_server_t *) malloc((size_t) ms_setting.total_srv_cnt * sizeof(ms_mcd_server_t));
766 if (ms_setting.servers == NULL) {
767 fprintf(stderr, "Can't allocate servers structure.\n");
768 exit(1);
769 }
770
771 ms_setting_slapmode_init_pre();
772 } /* ms_setting_init_pre */
773
774 /**
775 * post part of slap mode initialization of setting structure
776 */
777 static void ms_setting_slapmode_init_post() {
778 ms_setting.total_key_rng_cnt = KEY_RANGE_COUNT_INIT;
779 ms_setting.key_distr =
780 (ms_key_distr_t *) malloc((size_t) ms_setting.total_key_rng_cnt * sizeof(ms_key_distr_t));
781
782 if (ms_setting.key_distr == NULL) {
783 fprintf(stderr, "Can't allocate key distribution structure.\n");
784 exit(1);
785 }
786
787 ms_setting.total_val_rng_cnt = VALUE_RANGE_COUNT_INIT;
788
789 ms_setting.value_distr =
790 (ms_value_distr_t *) malloc((size_t) ms_setting.total_val_rng_cnt * sizeof(ms_value_distr_t));
791
792 if (ms_setting.value_distr == NULL) {
793 fprintf(stderr, "Can't allocate value distribution structure.\n");
794 exit(1);
795 }
796
797 ms_parse_cfg_file(ms_setting.cfg_file);
798
799 /* run time mode */
800 if ((ms_setting.exec_num == 0) && (ms_setting.run_time)) {
801 ms_setting.exec_num = (int64_t) MAX_EXEC_NUM;
802 } else {
803 /* execute number mode */
804 ms_setting.run_time = 0;
805 }
806
807 if (ms_setting.rep_write_srv > 0) {
808 /* for replication test, need enable reconnect feature */
809 ms_setting.reconnect = true;
810 }
811
812 if (ms_setting.facebook_test && (ms_setting.mult_key_num < 2)) {
813 fprintf(stderr,
814 "facebook test must work with multi-get, "
815 "please specify multi-get key number "
816 "with '--division' option.\n");
817 exit(1);
818 }
819
820 if (ms_setting.facebook_test && ms_setting.udp) {
821 fprintf(stderr, "facebook test couldn't work with UDP.\n");
822 exit(1);
823 }
824
825 if (ms_setting.udp && (ms_setting.sock_per_conn > 1)) {
826 fprintf(stderr,
827 "UDP doesn't support multi-socks "
828 "in one connection structure.\n");
829 exit(1);
830 }
831
832 if ((ms_setting.rep_write_srv > 0) && (ms_setting.srv_cnt < 2)) {
833 fprintf(stderr, "Please specify 2 servers at least for replication\n");
834 exit(1);
835 }
836
837 if ((ms_setting.rep_write_srv > 0) && (ms_setting.srv_cnt < ms_setting.rep_write_srv)) {
838 fprintf(stderr,
839 "Servers to do replication writing "
840 "is larger than the total servers\n");
841 exit(1);
842 }
843
844 if (ms_setting.udp && (ms_setting.rep_write_srv > 0)) {
845 fprintf(stderr, "UDP doesn't support replication.\n");
846 exit(1);
847 }
848
849 if (ms_setting.facebook_test && (ms_setting.rep_write_srv > 0)) {
850 fprintf(stderr, "facebook test couldn't work with replication.\n");
851 exit(1);
852 }
853
854 ms_build_distr();
855
856 /* initialize global character block */
857 ms_init_random_block();
858 ms_print_setting();
859 } /* ms_setting_slapmode_init_post */
860
861 /**
862 * post part of initialization of setting structure
863 */
864 void ms_setting_init_post() {
865 ms_get_serverlist(ms_setting.srv_str);
866 ms_setting_slapmode_init_post();
867 }
868
869 /**
870 * clean up the global setting structure
871 */
872 void ms_setting_cleanup() {
873 if (ms_setting.distr) {
874 free(ms_setting.distr);
875 }
876
877 if (ms_setting.char_block) {
878 free(ms_setting.char_block);
879 }
880
881 if (ms_setting.srv_str) {
882 free(ms_setting.srv_str);
883 }
884
885 if (ms_setting.cfg_file) {
886 free(ms_setting.cfg_file);
887 }
888
889 if (ms_setting.servers) {
890 free(ms_setting.servers);
891 }
892
893 if (ms_setting.key_distr) {
894 free(ms_setting.key_distr);
895 }
896
897 if (ms_setting.value_distr) {
898 free(ms_setting.value_distr);
899 }
900 } /* ms_setting_cleanup */