2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - 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 +--------------------------------------------------------------------+
16 #include "mem_config.h"
18 #include "libmemcached/memcached.h"
27 #include <sys/types.h>
32 #include "ms_setting.h"
35 #define MAX_EXEC_NUM 0x4000000000000000 /* 1 << 62 */
36 #define ADDR_ALIGN(addr) ((addr + 15) & ~(16 - 1)) /* 16 bytes aligned */
37 #define RAND_CHAR_SIZE (10 * 1024 * 1024) /* 10M character table */
38 #define RESERVED_RAND_CHAR_SIZE (2 * 1024 * 1024) /* reserved 2M to avoid pointer sloping over */
40 #define DEFAULT_CONFIG_NAME ".memslap.cnf"
42 #define DEFAULT_THREADS_NUM 1 /* default start one thread */
43 #define DEFAULT_CONNS_NUM 16 /* default each thread with 16 connections */
44 #define DEFAULT_EXE_NUM 0 /* default execute number is 0 */
45 #define DEFAULT_VERIFY_RATE 0.0 /* default it doesn't do data verification */
46 #define DEFAULT_OVERWRITE_RATE 0.0 /* default it doesn't do overwrite */
47 #define DEFAULT_DIV 1 /* default it runs single get */
48 #define DEFAULT_RUN_TIME 600 /* default run time 10 minutes */
49 #define DEFAULT_WINDOW_SIZE (10 * UNIT_ITEMS_COUNT) /* default window size is 10k */
50 #define DEFAULT_SOCK_PER_CONN 1 /* default socks per connection is 1 */
52 /* Use this for string generation */
53 #define CHAR_COUNT 64 /* number of characters used to generate character table */
54 const char ALPHANUMBERICS
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-";
56 ms_setting_st ms_setting
; /* store the settings specified by user */
58 /* read setting from configuration file */
59 static void ms_get_serverlist(char *str
);
60 static uint32_t ms_get_cpu_count(void);
61 ms_conf_type_t
ms_get_conf_type(char *line
);
62 static int ms_is_line_data(char *line
);
63 static int ms_read_is_data(char *line
, ssize_t nread
);
64 static void ms_no_config_file(void);
65 static void ms_parse_cfg_file(char *cfg_file
);
67 /* initialize setting structure */
68 static void ms_init_random_block(void);
69 static void ms_calc_avg_size(void);
70 static int ms_shuffle_distr(ms_distr_t
*distr
, int length
);
71 static void ms_build_distr(void);
72 static void ms_print_setting(void);
73 static void ms_setting_slapmode_init_pre(void);
74 static void ms_setting_slapmode_init_post(void);
76 #if !defined(HAVE_GETLINE)
78 static ssize_t
getline(char **line
, size_t *line_size
, FILE *fp
) {
83 if (line
== NULL
|| line_size
== NULL
|| fp
== NULL
) {
88 if (*line
== NULL
|| *line_size
== 0) {
91 new_line
= (char *) realloc(*line
, *line_size
);
92 if (new_line
== NULL
) {
106 /* Make enough space for len+1 (for final NUL) bytes. */
107 if (cur_len
+ 1 >= *line_size
) {
108 size_t needed_max
= SSIZE_MAX
< SIZE_MAX
? (size_t) SSIZE_MAX
+ 1 : SIZE_MAX
;
109 size_t needed
= (2 * (*line_size
)) + 1;
112 if (needed_max
< needed
)
114 if (cur_len
+ 1 >= needed
) {
120 new_line
= (char *) realloc(*line
, needed
);
121 if (new_line
== NULL
) {
130 (*line
)[cur_len
] = (char) i
;
136 (*line
)[cur_len
] = '\0';
138 return (ssize_t
) cur_len
;
144 * parse the server list string, and build the servers
145 * information structure array. this function is used to parse
146 * the command line options specified by user.
148 * @param str, the string of server list
150 static void ms_get_serverlist(char *str
) {
151 ms_mcd_server_t
*srvs
= NULL
;
154 * Servers list format is like this. For example:
155 * "localhost:11108, localhost:11109"
157 memcached_server_st
*server_pool
;
158 server_pool
= memcached_servers_parse(str
);
160 for (uint32_t loop
= 0; loop
< memcached_server_list_count(server_pool
); loop
++) {
161 assert(ms_setting
.srv_cnt
< ms_setting
.total_srv_cnt
);
162 strcpy(ms_setting
.servers
[ms_setting
.srv_cnt
].srv_host_name
, server_pool
[loop
].hostname
);
163 ms_setting
.servers
[ms_setting
.srv_cnt
].srv_port
= server_pool
[loop
].port
;
164 ms_setting
.servers
[ms_setting
.srv_cnt
].disconn_cnt
= 0;
165 ms_setting
.servers
[ms_setting
.srv_cnt
].reconn_cnt
= 0;
166 ms_setting
.srv_cnt
++;
168 if (ms_setting
.srv_cnt
>= ms_setting
.total_srv_cnt
) {
169 srvs
= (ms_mcd_server_t
*) realloc(
170 ms_setting
.servers
, (size_t) ms_setting
.total_srv_cnt
* sizeof(ms_mcd_server_t
) * 2);
172 fprintf(stderr
, "Can't reallocate servers structure.\n");
175 ms_setting
.servers
= srvs
;
176 ms_setting
.total_srv_cnt
*= 2;
180 memcached_server_free(server_pool
);
181 } /* ms_get_serverlist */
184 * used to get the CPU count of the current system
186 * @return return the cpu count if possible, else return 1
188 static uint32_t ms_get_cpu_count() {
189 #ifdef HAVE_CPU_SET_T
190 unsigned cpu_count
= 0;
193 sched_getaffinity(0, sizeof(cpu_set_t
), &cpu_set
);
195 for (unsigned i
= 0; i
< (sizeof(cpu_set_t
) * 8); i
++) {
196 if (CPU_ISSET(i
, &cpu_set
)) {
202 #elif defined HAVE__SC_NPROCESSORS_ONLN
203 return sysconf(_SC_NPROCESSORS_CONF
);
206 /* the system with one cpu at least */
208 } /* ms_get_cpu_count */
211 * used to get the configure type based on the type string read
212 * from the configuration file.
214 * @param line, string of one line
216 * @return ms_conf_type_t
218 ms_conf_type_t
ms_get_conf_type(char *line
) {
219 if (!memcmp(line
, "key", strlen("key"))) {
221 } else if (!memcmp(line
, "value", strlen("value"))) {
223 } else if (!memcmp(line
, "cmd", strlen("cmd"))) {
228 } /* ms_get_conf_type */
231 * judge whether the line is a line with useful data. used to
232 * parse the configuration file.
234 * @param line, string of one line
236 * @return if success, return EXIT_FAILURE, else return EXIT_SUCCESS
238 static int ms_is_line_data(char *line
) {
241 char *begin_ptr
= line
;
243 while (isspace(*begin_ptr
)) {
246 if ((begin_ptr
[0] == '\0') || (begin_ptr
[0] == '#'))
250 } /* ms_is_line_data */
253 * function to bypass blank line and comments
255 * @param line, string of one line
256 * @param nread, length of the line
258 * @return if it's EOF or not line data, return EXIT_SUCCESS, else return EXIT_FAILURE
260 static int ms_read_is_data(char *line
, ssize_t nread
) {
261 if ((nread
== EOF
) || !ms_is_line_data(line
))
265 } /* ms_read_is_data */
268 * if no configuration file, use this function to create the default
269 * configuration file.
271 static void ms_no_config_file() {
272 char userpath
[PATH_MAX
];
273 struct passwd
*usr
= NULL
;
276 usr
= getpwuid(getuid());
278 snprintf(userpath
, PATH_MAX
, "%s/%s", usr
->pw_dir
, DEFAULT_CONFIG_NAME
);
280 if (access(userpath
, F_OK
| R_OK
) == 0)
283 fd
= fopen(userpath
, "w+");
286 fprintf(stderr
, "Could not create default configure file %s\n", userpath
);
290 fprintf(fd
, "%s", DEFAULT_CONGIF_STR
);
294 ms_setting
.cfg_file
= strdup(userpath
);
295 } /* ms_no_config_file */
298 * parse the configuration file
300 * @param cfg_file, the configuration file name
302 static void ms_parse_cfg_file(char *cfg_file
) {
304 size_t start_len
, end_len
;
310 ms_conf_type_t conf_type
;
312 ms_key_distr_t
*key_distr
= NULL
;
313 ms_value_distr_t
*val_distr
= NULL
;
315 if (cfg_file
== NULL
) {
317 cfg_file
= ms_setting
.cfg_file
;
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
);
327 if ((((nread
= getline(&line
, &read_len
, f
)) == 1) || !ms_read_is_data(line
, nread
))
328 && (nread
!= EOF
)) /* bypass blank line */
332 fprintf(stderr
, "Bad configuration file, no configuration find.\n");
335 conf_type
= ms_get_conf_type(line
);
339 while (!end_of_file
) {
343 if ((((nread
= getline(&line
, &read_len
, f
)) == 1) || !ms_read_is_data(line
, nread
))
344 && (nread
!= EOF
)) /* bypass blank line */
348 if (sscanf(line
, "%zu %zu %lf ", &start_len
, &end_len
, &proportion
) != 3) {
349 conf_type
= ms_get_conf_type(line
);
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
++;
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");
365 ms_setting
.key_distr
= key_distr
;
366 ms_setting
.total_key_rng_cnt
*= 2;
377 if ((((nread
= getline(&line
, &read_len
, f
)) == 1) || !ms_read_is_data(line
, nread
))
378 && (nread
!= EOF
)) /* bypass blank line */
382 if (sscanf(line
, "%zu %zu %lf", &start_len
, &end_len
, &proportion
) != 3) {
383 conf_type
= ms_get_conf_type(line
);
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
++;
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");
399 ms_setting
.value_distr
= val_distr
;
400 ms_setting
.total_val_rng_cnt
*= 2;
411 if ((((nread
= getline(&line
, &read_len
, f
)) == 1) || !ms_read_is_data(line
, nread
))
412 && (nread
!= EOF
)) /* bypass blank line */
416 if (sscanf(line
, "%d %lf", &cmd_type
, &proportion
) != 2) {
417 conf_type
= ms_get_conf_type(line
);
420 if (cmd_type
>= CMD_NULL
) {
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
++;
434 if ((((nread
= getline(&line
, &read_len
, f
)) == 1) || !ms_read_is_data(line
, nread
))
435 && (nread
!= EOF
)) /* bypass blank line */
439 if ((conf_type
= ms_get_conf_type(line
)) != CONF_NULL
) {
460 } /* ms_parse_cfg_file */
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;
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
;
477 averge_len
= val_pro
* ((double) (start_len
+ end_len
)) / 2;
478 avg_val_size
+= averge_len
;
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
;
486 averge_len
= key_pro
* ((double) (start_len
+ end_len
)) / 2;
487 avg_key_size
+= averge_len
;
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 */
495 * used to shuffle key and value distribution array to ensure
496 * (key, value) pair with different set.
498 * @param distr, pointer of distribution structure array
499 * @param length, length of the array
501 * @return always return EXIT_SUCCESS
503 static int ms_shuffle_distr(ms_distr_t
*distr
, int length
) {
509 for (i
= 0; i
< length
; i
++) {
511 j
= (int) (rnd
% (length
- i
)) + i
;
515 tmp_size
= distr
[j
].key_size
;
516 distr
[j
].key_size
= distr
[i
].key_size
;
517 distr
[i
].key_size
= tmp_size
;
521 tmp_offset
= distr
[j
].key_offset
;
522 distr
[j
].key_offset
= distr
[i
].key_offset
;
523 distr
[i
].key_offset
= tmp_offset
;
527 tmp_size
= distr
[j
].value_size
;
528 distr
[j
].value_size
= distr
[i
].value_size
;
529 distr
[i
].value_size
= tmp_size
;
538 } /* ms_shuffle_distr */
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.
547 static void ms_build_distr() {
552 size_t average_len
= 0;
554 size_t start_len
= 0;
557 ms_distr_t
*distr
= NULL
;
558 int units
= (int) ms_setting
.win_size
/ UNIT_ITEMS_COUNT
;
560 /* calculate average value size and key size */
563 ms_setting
.char_blk_size
= RAND_CHAR_SIZE
;
565 (int) ((ms_setting
.char_blk_size
- RESERVED_RAND_CHAR_SIZE
) / UNIT_ITEMS_COUNT
);
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.");
574 * character block is divided by how many different key
575 * size, each different key size has the same size character
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
);
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");
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");
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
) {
606 rnd
= (int) random();
608 ms_setting
.distr
[offset
].key_size
=
609 (diff_len
== 0) ? average_len
: average_len
+ (size_t) rnd
% diff_len
;
611 ms_setting
.distr
[offset
].key_size
=
612 (diff_len
== 0) ? average_len
: average_len
- (size_t) rnd
% diff_len
;
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
;
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
;
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");
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");
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
) {
655 rnd
= (int) random();
657 ms_setting
.distr
[offset
].value_size
=
658 (diff_len
== 0) ? average_len
: average_len
+ (size_t) rnd
% diff_len
;
660 ms_setting
.distr
[offset
].value_size
=
661 (diff_len
== 0) ? average_len
: average_len
- (size_t) rnd
% diff_len
;
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
;
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
);
683 } /* ms_build_distr */
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
692 static void ms_init_random_block() {
695 assert(ms_setting
.char_blk_size
> 0);
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.");
702 ptr
= ms_setting
.char_block
;
704 for (int i
= 0; (size_t) i
< ms_setting
.char_blk_size
; i
++) {
705 *(ptr
++) = ALPHANUMBERICS
[random() % CHAR_COUNT
];
707 } /* ms_init_random_block */
710 * after initialization, call this function to output the main
711 * configuration user specified.
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
);
720 fprintf(stdout
, "execute number: %" PRId64
"\n", ms_setting
.exec_num
);
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
);
726 } /* ms_print_setting */
729 * previous part of slap mode initialization of setting structure
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 */
753 * previous part of initialization of setting structure
755 void ms_setting_init_pre() {
756 memset(&ms_setting
, 0, sizeof(ms_setting
));
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
;
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");
771 ms_setting_slapmode_init_pre();
772 } /* ms_setting_init_pre */
775 * post part of slap mode initialization of setting structure
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
));
782 if (ms_setting
.key_distr
== NULL
) {
783 fprintf(stderr
, "Can't allocate key distribution structure.\n");
787 ms_setting
.total_val_rng_cnt
= VALUE_RANGE_COUNT_INIT
;
789 ms_setting
.value_distr
=
790 (ms_value_distr_t
*) malloc((size_t) ms_setting
.total_val_rng_cnt
* sizeof(ms_value_distr_t
));
792 if (ms_setting
.value_distr
== NULL
) {
793 fprintf(stderr
, "Can't allocate value distribution structure.\n");
797 ms_parse_cfg_file(ms_setting
.cfg_file
);
800 if ((ms_setting
.exec_num
== 0) && (ms_setting
.run_time
)) {
801 ms_setting
.exec_num
= (int64_t) MAX_EXEC_NUM
;
803 /* execute number mode */
804 ms_setting
.run_time
= 0;
807 if (ms_setting
.rep_write_srv
> 0) {
808 /* for replication test, need enable reconnect feature */
809 ms_setting
.reconnect
= true;
812 if (ms_setting
.facebook_test
&& (ms_setting
.mult_key_num
< 2)) {
814 "facebook test must work with multi-get, "
815 "please specify multi-get key number "
816 "with '--division' option.\n");
820 if (ms_setting
.facebook_test
&& ms_setting
.udp
) {
821 fprintf(stderr
, "facebook test couldn't work with UDP.\n");
825 if (ms_setting
.udp
&& (ms_setting
.sock_per_conn
> 1)) {
827 "UDP doesn't support multi-socks "
828 "in one connection structure.\n");
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");
837 if ((ms_setting
.rep_write_srv
> 0) && (ms_setting
.srv_cnt
< ms_setting
.rep_write_srv
)) {
839 "Servers to do replication writing "
840 "is larger than the total servers\n");
844 if (ms_setting
.udp
&& (ms_setting
.rep_write_srv
> 0)) {
845 fprintf(stderr
, "UDP doesn't support replication.\n");
849 if (ms_setting
.facebook_test
&& (ms_setting
.rep_write_srv
> 0)) {
850 fprintf(stderr
, "facebook test couldn't work with replication.\n");
856 /* initialize global character block */
857 ms_init_random_block();
859 } /* ms_setting_slapmode_init_post */
862 * post part of initialization of setting structure
864 void ms_setting_init_post() {
865 ms_get_serverlist(ms_setting
.srv_str
);
866 ms_setting_slapmode_init_post();
870 * clean up the global setting structure
872 void ms_setting_cleanup() {
873 if (ms_setting
.distr
) {
874 free(ms_setting
.distr
);
877 if (ms_setting
.char_block
) {
878 free(ms_setting
.char_block
);
881 if (ms_setting
.srv_str
) {
882 free(ms_setting
.srv_str
);
885 if (ms_setting
.cfg_file
) {
886 free(ms_setting
.cfg_file
);
889 if (ms_setting
.servers
) {
890 free(ms_setting
.servers
);
893 if (ms_setting
.key_distr
) {
894 free(ms_setting
.key_distr
);
897 if (ms_setting
.value_distr
) {
898 free(ms_setting
.value_distr
);
900 } /* ms_setting_cleanup */