~dave-terei/libmemcached/sasl-fixes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
/*
 * File:   ms_setting.c
 * Author: Mingqiang Zhuang
 *
 * Created on February 10, 2009
 *
 * (c) Copyright 2009, Schooner Information Technology, Inc.
 * http://www.schoonerinfotech.com/
 *
 */

#include "config.h"

#include <libmemcached/memcached.h>

#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <pwd.h>
#include <strings.h>
#include <sys/types.h>
#include <unistd.h>



#include "ms_setting.h"
#include "ms_conn.h"

#define MAX_EXEC_NUM               0x4000000000000000      /* 1 << 62 */
#define ADDR_ALIGN(addr)    ((addr + 15) & ~(16 - 1))      /* 16 bytes aligned */
#define RAND_CHAR_SIZE             (10 * 1024 * 1024)      /* 10M character table */
#define RESERVED_RAND_CHAR_SIZE    (2 * 1024 * 1024)       /* reserved 2M to avoid pointer sloping over */

#define DEFAULT_CONFIG_NAME ".memslap.cnf"

#define DEFAULT_THREADS_NUM        1                       /* default start one thread */
#define DEFAULT_CONNS_NUM          16                      /* default each thread with 16 connections */
#define DEFAULT_EXE_NUM            0                       /* default execute number is 0 */
#define DEFAULT_VERIFY_RATE        0.0                     /* default it doesn't do data verification */
#define DEFAULT_OVERWRITE_RATE     0.0                     /* default it doesn't do overwrite */
#define DEFAULT_DIV                1                       /* default it runs single get */
#define DEFAULT_RUN_TIME           600                     /* default run time 10 minutes */
#define DEFAULT_WINDOW_SIZE        (10 * UNIT_ITEMS_COUNT) /* default window size is 10k */
#define DEFAULT_SOCK_PER_CONN      1                       /* default socks per connection is 1 */

/* Use this for string generation */
#define CHAR_COUNT                 64 /* number of characters used to generate character table */
const char ALPHANUMBERICS[]=
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-";

ms_setting_st ms_setting;       /* store the settings specified by user */


/* read setting from configuration file */
static void ms_get_serverlist(char *str);
static uint32_t ms_get_cpu_count(void);
ms_conf_type_t ms_get_conf_type(char *line);
static int ms_is_line_data(char *line);
static int ms_read_is_data(char *line, ssize_t nread);
static void ms_no_config_file(void);
static void ms_parse_cfg_file(char *cfg_file);


/* initialize setting structure */
static void ms_init_random_block(void);
static void ms_calc_avg_size(void);
static int ms_shuffle_distr(ms_distr_t *distr, int length);
static void ms_build_distr(void);
static void ms_print_setting(void);
static void ms_setting_slapmode_init_pre(void);
static void ms_setting_slapmode_init_post(void);

#if !defined(HAVE_GETLINE)
#include <limits.h>
static ssize_t getline (char **line, size_t *line_size, FILE *fp)
{
  char delim= '\n';
  ssize_t result= 0;
  size_t cur_len= 0;

  if (line == NULL || line_size == NULL || fp == NULL)
  {
    errno = EINVAL;
    return -1;
  }

  if (*line == NULL || *line_size == 0)
  {
    char *new_line;
    *line_size = 120;
    new_line= (char *) realloc (*line, *line_size);
    if (new_line == NULL)
    {
      result= -1;
      return result;
    }
    *line= new_line;
  }

  for (;;)
  {
    int i= getc(fp);
    if (i == EOF)
    {
      result = -1;
      break;
    }

    /* Make enough space for len+1 (for final NUL) bytes.  */
    if (cur_len + 1 >= *line_size)
    {
      size_t needed_max=
        SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
      size_t needed= (2 * (*line_size)) + 1;
      char *new_line;

      if (needed_max < needed)
        needed= needed_max;
      if (cur_len + 1 >= needed)
      {
        result= -1;
        errno= EOVERFLOW;
        return result;
      }

      new_line= (char *)realloc(*line, needed);
      if (new_line == NULL)
      {
        result= -1;
        return result;
      }

      *line= new_line;
      *line_size= needed;
    }

    (*line)[cur_len]= (char)i;
    cur_len++;

    if (i == delim)
      break;
  }
  (*line)[cur_len] = '\0';
  if (cur_len != 0)
    return (ssize_t)cur_len;
  return result;
}
#endif

/**
 * parse the server list string, and build the servers
 * information structure array. this function is used to parse
 * the command line options specified by user.
 *
 * @param str, the string of server list
 */
static void ms_get_serverlist(char *str)
{
  ms_mcd_server_t *srvs= NULL;

  /**
   * Servers list format is like this. For example:
   * "localhost:11108, localhost:11109"
   */
  memcached_server_st *server_pool;
  server_pool = memcached_servers_parse(str);

  for (uint32_t loop= 0; loop < memcached_server_list_count(server_pool); loop++)
  {
    assert(ms_setting.srv_cnt < ms_setting.total_srv_cnt);
    strcpy(ms_setting.servers[ms_setting.srv_cnt].srv_host_name, server_pool[loop].hostname);
    ms_setting.servers[ms_setting.srv_cnt].srv_port= server_pool[loop].port;
    ms_setting.servers[ms_setting.srv_cnt].disconn_cnt= 0;
    ms_setting.servers[ms_setting.srv_cnt].reconn_cnt= 0;
    ms_setting.srv_cnt++;

    if (ms_setting.srv_cnt >= ms_setting.total_srv_cnt)
    {
      srvs= (ms_mcd_server_t *)realloc( ms_setting.servers,
                                        (size_t)ms_setting.total_srv_cnt * sizeof(ms_mcd_server_t) * 2);
      if (srvs == NULL)
      {
        fprintf(stderr, "Can't reallocate servers structure.\n");
        exit(1);
      }
      ms_setting.servers= srvs;
      ms_setting.total_srv_cnt*= 2;
    }
  }

  memcached_server_free(server_pool);
} /* ms_get_serverlist */


/**
 * used to get the CPU count of the current system
 *
 * @return return the cpu count if get, else return EXIT_FAILURE
 */
static uint32_t ms_get_cpu_count()
{
#ifdef HAVE__SC_NPROCESSORS_ONLN
  return sysconf(_SC_NPROCESSORS_CONF);

#else
# ifdef HAVE_CPU_SET_T
  int cpu_count= 0;
  cpu_set_t cpu_set;

  sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set);

  for (int i= 0; i < (sizeof(cpu_set_t) * 8); i++)
  {
    if (CPU_ISSET(i, &cpu_set))
    {
      cpu_count++;
    }
  }

  return cpu_count;

# endif
#endif

  /* the system with one cpu at least */
  return EXIT_FAILURE;
} /* ms_get_cpu_count */


/**
 * used to get the configure type based on the type string read
 * from the configuration file.
 *
 * @param line, string of one line
 *
 * @return ms_conf_type_t
 */
ms_conf_type_t ms_get_conf_type(char *line)
{
  if (! memcmp(line, "key", strlen("key")))
  {
    return CONF_KEY;
  }
  else if (! memcmp(line, "value", strlen("value")))
  {
    return CONF_VALUE;
  }
  else if (! memcmp(line, "cmd", strlen("cmd")))
  {
    return CONF_CMD;
  }
  else
  {
    return CONF_NULL;
  }
} /* ms_get_conf_type */


/**
 * judge whether the line is a line with useful data. used to
 * parse the configuration file.
 *
 * @param line, string of one line
 *
 * @return if success, return EXIT_FAILURE, else return EXIT_SUCCESS
 */
static int ms_is_line_data(char *line)
{
  assert(line != NULL);

  char *begin_ptr= line;

  while (isspace(*begin_ptr))
  {
    begin_ptr++;
  }
  if ((begin_ptr[0] == '\0') || (begin_ptr[0] == '#'))
    return EXIT_SUCCESS;

  return EXIT_FAILURE;
} /* ms_is_line_data */


/**
 * function to bypass blank line and comments
 *
 * @param line, string of one line
 * @param nread, length of the line
 *
 * @return if it's EOF or not line data, return EXIT_SUCCESS, else return EXIT_FAILURE
 */
static int ms_read_is_data(char *line, ssize_t nread)
{
  if ((nread == EOF) || ! ms_is_line_data(line))
    return EXIT_SUCCESS;

  return EXIT_FAILURE;
} /* ms_read_is_data */


/**
 *  if no configuration file, use this function to create the default
 *  configuration file.
 */
static void ms_no_config_file()
{
  char userpath[PATH_MAX];
  struct passwd *usr= NULL;
  FILE *fd;

  usr= getpwuid(getuid());

  snprintf(userpath, PATH_MAX, "%s/%s", usr->pw_dir, DEFAULT_CONFIG_NAME);

  if (access (userpath, F_OK | R_OK) == 0)
    goto exit;

  fd= fopen(userpath, "w+");

  if (fd == NULL)
  {
    fprintf(stderr, "Could not create default configure file %s\n", userpath);
    perror(strerror(errno));
    exit(1);
  }
  fprintf(fd, "%s", DEFAULT_CONGIF_STR);
  fclose(fd);

exit:
  ms_setting.cfg_file= strdup(userpath);
} /* ms_no_config_file */


/**
 * parse the configuration file
 *
 * @param cfg_file, the configuration file name
 */
static void ms_parse_cfg_file(char *cfg_file)
{
  FILE *f;
  size_t start_len, end_len;
  double proportion;
  char *line= NULL;
  size_t  read_len;
  ssize_t nread;
  int cmd_type;
  ms_conf_type_t conf_type;
  int end_of_file= 0;
  ms_key_distr_t *key_distr= NULL;
  ms_value_distr_t *val_distr= NULL;

  if (cfg_file == NULL)
  {
    ms_no_config_file();
    cfg_file= ms_setting.cfg_file;
  }

  /*read key value configure file*/
  if ((f= fopen(cfg_file, "r")) == NULL)
  {
    fprintf(stderr, "Can not open file: '%s'.\n", cfg_file);
    exit(1);
  }

  while (1)
  {
    if ((((nread= getline(&line, &read_len, f)) == 1)
         || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */
      continue;

    if (nread == EOF)
    {
      fprintf(stderr, "Bad configuration file, no configuration find.\n");
      exit(1);
    }
    conf_type= ms_get_conf_type(line);
    break;
  }

  while (! end_of_file)
  {
    switch (conf_type)
    {
    case CONF_KEY:
      while (1)
      {
        if ((((nread= getline(&line, &read_len, f)) == 1)
             || ! ms_read_is_data(line, nread)) && (nread != EOF))     /* bypass blank line */
          continue;

        if (nread != EOF)
        {
          if (sscanf(line, "%zu %zu %lf ", &start_len,
                     &end_len, &proportion) != 3)
          {
            conf_type= ms_get_conf_type(line);
            break;
          }
          ms_setting.key_distr[ms_setting.key_rng_cnt].start_len= start_len;
          ms_setting.key_distr[ms_setting.key_rng_cnt].end_len= end_len;
          ms_setting.key_distr[ms_setting.key_rng_cnt].key_prop= proportion;
          ms_setting.key_rng_cnt++;

          if (ms_setting.key_rng_cnt >= ms_setting.total_key_rng_cnt)
          {
            key_distr= (ms_key_distr_t *)realloc(
              ms_setting.key_distr,
              (size_t)ms_setting.
                 total_key_rng_cnt * sizeof(ms_key_distr_t) * 2);
            if (key_distr == NULL)
            {
              fprintf(stderr,
                      "Can't reallocate key distribution structure.\n");
              exit(1);
            }
            ms_setting.key_distr= key_distr;
            ms_setting.total_key_rng_cnt*= 2;
          }
          continue;
        }
        end_of_file= 1;
        break;
      }
      break;

    case CONF_VALUE:
      while (1)
      {
        if ((((nread= getline(&line, &read_len, f)) == 1)
             || ! ms_read_is_data(line, nread)) && (nread != EOF))     /* bypass blank line */
          continue;

        if (nread != EOF)
        {
          if (sscanf(line, "%zu %zu %lf", &start_len, &end_len,
                     &proportion) != 3)
          {
            conf_type= ms_get_conf_type(line);
            break;
          }
          ms_setting.value_distr[ms_setting.val_rng_cnt].start_len=
            start_len;
          ms_setting.value_distr[ms_setting.val_rng_cnt].end_len= end_len;
          ms_setting.value_distr[ms_setting.val_rng_cnt].value_prop=
            proportion;
          ms_setting.val_rng_cnt++;

          if (ms_setting.val_rng_cnt >= ms_setting.total_val_rng_cnt)
          {
            val_distr= (ms_value_distr_t *)realloc(
              ms_setting.value_distr,
              (size_t)ms_setting.
                 total_val_rng_cnt * sizeof(ms_value_distr_t) * 2);
            if (val_distr == NULL)
            {
              fprintf(stderr,
                      "Can't reallocate key distribution structure.\n");
              exit(1);
            }
            ms_setting.value_distr= val_distr;
            ms_setting.total_val_rng_cnt*= 2;
          }
          continue;
        }
        end_of_file= 1;
        break;
      }
      break;

    case CONF_CMD:
      while (1)
      {
        if ((((nread= getline(&line, &read_len, f)) == 1)
             || ! ms_read_is_data(line, nread)) && (nread != EOF))     /* bypass blank line */
          continue;

        if (nread != EOF)
        {
          if (sscanf(line, "%d %lf", &cmd_type, &proportion) != 2)
          {
            conf_type= ms_get_conf_type(line);
            break;
          }
          if (cmd_type >= CMD_NULL)
          {
            continue;
          }
          ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_type=
            cmd_type;
          ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_prop=
            proportion;
          ms_setting.cmd_used_count++;
          continue;
        }
        end_of_file= 1;
        break;
      }

    case CONF_NULL:
      while (1)
      {
        if ((((nread= getline(&line, &read_len, f)) == 1)
             || ! ms_read_is_data(line, nread)) && (nread != EOF))     /* bypass blank line */
          continue;

        if (nread != EOF)
        {
          if ((conf_type= ms_get_conf_type(line)) != CONF_NULL)
          {
            break;
          }
          continue;
        }
        end_of_file= 1;
        break;
      }
      break;

    default:
      assert(0);
      break;
    } /* switch */
  }

  fclose(f);

  if (line != NULL)
  {
    free(line);
  }
} /* ms_parse_cfg_file */


/* calculate the average size of key and value */
static void ms_calc_avg_size()
{
  double avg_val_size= 0.0;
  double avg_key_size= 0.0;
  double val_pro= 0.0;
  double key_pro= 0.0;
  double averge_len= 0.0;
  size_t start_len= 0;
  size_t end_len= 0;

  for (int j= 0; j < ms_setting.val_rng_cnt; j++)
  {
    val_pro= ms_setting.value_distr[j].value_prop;
    start_len= ms_setting.value_distr[j].start_len;
    end_len= ms_setting.value_distr[j].end_len;

    averge_len= val_pro * ((double)(start_len + end_len)) / 2;
    avg_val_size+= averge_len;
  }

  for (int j= 0; j < ms_setting.key_rng_cnt; j++)
  {
    key_pro= ms_setting.key_distr[j].key_prop;
    start_len= ms_setting.key_distr[j].start_len;
    end_len= ms_setting.key_distr[j].end_len;

    averge_len= key_pro * ((double)(start_len + end_len)) / 2;
    avg_key_size+= averge_len;
  }

  ms_setting.avg_val_size= (size_t)avg_val_size;
  ms_setting.avg_key_size= (size_t)avg_key_size;
} /* ms_calc_avg_size */


/**
 * used to shuffle key and value distribution array to ensure
 * (key, value) pair with different set.
 *
 * @param distr, pointer of distribution structure array
 * @param length, length of the array
 *
 * @return always return EXIT_SUCCESS
 */
static int ms_shuffle_distr(ms_distr_t *distr, int length)
{
  int i, j;
  int tmp_offset;
  size_t  tmp_size;
  int64_t rnd;

  for (i= 0; i < length; i++)
  {
    rnd= random();
    j= (int)(rnd % (length - i)) + i;

    switch (rnd % 3)
    {
    case 0:
      tmp_size= distr[j].key_size;
      distr[j].key_size= distr[i].key_size;
      distr[i].key_size= tmp_size;
      break;

    case 1:
      tmp_offset= distr[j].key_offset;
      distr[j].key_offset= distr[i].key_offset;
      distr[i].key_offset= tmp_offset;
      break;

    case 2:
      tmp_size= distr[j].value_size;
      distr[j].value_size= distr[i].value_size;
      distr[i].value_size= tmp_size;
      break;

    default:
      break;
    } /* switch */
  }

  return EXIT_SUCCESS;
} /* ms_shuffle_distr */


/**
 * according to the key and value distribution, to build the
 * (key, value) pair distribution. the (key, value) pair
 * distribution array is global, each connection set or get
 * object keeping this distribution, for the final result, we
 * can reach the expected key and value distribution.
 */
static void ms_build_distr()
{
  int offset= 0;
  int end= 0;
  int key_cnt= 0;
  int value_cnt= 0;
  size_t average_len= 0;
  size_t diff_len= 0;
  size_t start_len= 0;
  size_t end_len= 0;
  int rnd= 0;
  ms_distr_t *distr= NULL;
  int units= (int)ms_setting.win_size / UNIT_ITEMS_COUNT;

  /* calculate average value size and key size */
  ms_calc_avg_size();

  ms_setting.char_blk_size= RAND_CHAR_SIZE;
  int key_scope_size=
    (int)((ms_setting.char_blk_size - RESERVED_RAND_CHAR_SIZE)
          / UNIT_ITEMS_COUNT);

  ms_setting.distr= (ms_distr_t *)malloc(
    sizeof(ms_distr_t) * ms_setting.win_size);
  if (ms_setting.distr == NULL)
  {
    fprintf(stderr, "Can't allocate distribution array.");
    exit(1);
  }

  /**
   *  character block is divided by how many different key
   *  size, each different key size has the same size character
   *  range.
   */
  for (int m= 0; m < units; m++)
  {
    for (int i= 0; i < UNIT_ITEMS_COUNT; i++)
    {
      ms_setting.distr[m * UNIT_ITEMS_COUNT + i].key_offset=
        ADDR_ALIGN(key_scope_size * i);
    }
  }

  /* initialize key size distribution */
  for (int m= 0; m < units; m++)
  {
    for (int j= 0; j < ms_setting.key_rng_cnt; j++)
    {
      key_cnt= (int)(UNIT_ITEMS_COUNT * ms_setting.key_distr[j].key_prop);
      start_len= ms_setting.key_distr[j].start_len;
      end_len= ms_setting.key_distr[j].end_len;
      if ((start_len < MIN_KEY_SIZE) || (end_len < MIN_KEY_SIZE))
      {
        fprintf(stderr, "key length must be greater than 16 bytes.\n");
        exit(1);
      }

      if (! ms_setting.binary_prot_
          && ((start_len > MAX_KEY_SIZE) || (end_len > MAX_KEY_SIZE)))
      {
        fprintf(stderr, "key length must be less than 250 bytes.\n");
        exit(1);
      }

      average_len= (start_len + end_len) / 2;
      diff_len= (end_len - start_len) / 2;
      for (int k= 0; k < key_cnt; k++)
      {
        if (offset >= (m + 1) * UNIT_ITEMS_COUNT)
        {
          break;
        }
        rnd= (int)random();
        if (k % 2 == 0)
        {
          ms_setting.distr[offset].key_size=
            (diff_len == 0) ? average_len :
            average_len + (size_t)rnd
            % diff_len;
        }
        else
        {
          ms_setting.distr[offset].key_size=
            (diff_len == 0) ? average_len :
            average_len - (size_t)rnd
            % diff_len;
        }
        offset++;
      }
    }

    if (offset < (m + 1) * UNIT_ITEMS_COUNT)
    {
      end= (m + 1) * UNIT_ITEMS_COUNT - offset;
      for (int i= 0; i < end; i++)
      {
        ms_setting.distr[offset].key_size= ms_setting.avg_key_size;
        offset++;
      }
    }
  }
  offset= 0;

  /* initialize value distribution */
  if (ms_setting.fixed_value_size != 0)
  {
    for (int i= 0; i < units * UNIT_ITEMS_COUNT; i++)
    {
      ms_setting.distr[i].value_size= ms_setting.fixed_value_size;
    }
  }
  else
  {
    for (int m= 0; m < units; m++)
    {
      for (int j= 0; j < ms_setting.val_rng_cnt; j++)
      {
        value_cnt=
          (int)(UNIT_ITEMS_COUNT * ms_setting.value_distr[j].value_prop);
        start_len= ms_setting.value_distr[j].start_len;
        end_len= ms_setting.value_distr[j].end_len;
        if ((start_len <= 0) || (end_len <= 0))
        {
          fprintf(stderr, "value length must be greater than 0 bytes.\n");
          exit(1);
        }

        if ((start_len > MAX_VALUE_SIZE) || (end_len > MAX_VALUE_SIZE))
        {
          fprintf(stderr, "key length must be less than or equal to 1M.\n");
          exit(1);
        }

        average_len= (start_len + end_len) / 2;
        diff_len= (end_len - start_len) / 2;
        for (int k= 0; k < value_cnt; k++)
        {
          if (offset >= (m + 1) * UNIT_ITEMS_COUNT)
          {
            break;
          }
          rnd= (int)random();
          if (k % 2 == 0)
          {
            ms_setting.distr[offset].value_size=
              (diff_len == 0) ? average_len :
              average_len
              + (size_t)rnd % diff_len;
          }
          else
          {
            ms_setting.distr[offset].value_size=
              (diff_len == 0) ? average_len :
              average_len
              - (size_t)rnd % diff_len;
          }
          offset++;
        }
      }

      if (offset < (m + 1) * UNIT_ITEMS_COUNT)
      {
        end= (m + 1) * UNIT_ITEMS_COUNT - offset;
        for (int i= 0; i < end; i++)
        {
          ms_setting.distr[offset++].value_size= ms_setting.avg_val_size;
        }
      }
    }
  }

  /* shuffle distribution */
  for (int i= 0; i < units; i++)
  {
    distr= &ms_setting.distr[i * UNIT_ITEMS_COUNT];
    for (int j= 0; j < 4; j++)
    {
      ms_shuffle_distr(distr, UNIT_ITEMS_COUNT);
    }
  }
} /* ms_build_distr */


/**
 * used to initialize the global character block. The character
 * block is used to generate the suffix of the key and value. we
 * only store a pointer in the character block for each key
 * suffix or value string. It can save much memory to store key
 * or value string.
 */
static void ms_init_random_block()
{
  char *ptr= NULL;

  assert(ms_setting.char_blk_size > 0);

  ms_setting.char_block= (char *)malloc(ms_setting.char_blk_size);
  if (ms_setting.char_block == NULL)
  {
    fprintf(stderr, "Can't allocate global char block.");
    exit(1);
  }
  ptr= ms_setting.char_block;

  for (int i= 0; (size_t)i < ms_setting.char_blk_size; i++)
  {
    *(ptr++)= ALPHANUMBERICS[random() % CHAR_COUNT];
  }
} /* ms_init_random_block */


/**
 * after initialization, call this function to output the main
 * configuration user specified.
 */
static void ms_print_setting()
{
  fprintf(stdout, "servers : %s\n", ms_setting.srv_str);
  fprintf(stdout, "threads count: %d\n", ms_setting.nthreads);
  fprintf(stdout, "concurrency: %d\n", ms_setting.nconns);
  if (ms_setting.run_time > 0)
  {
    fprintf(stdout, "run time: %ds\n", ms_setting.run_time);
  }
  else
  {
    fprintf(stdout, "execute number: %" PRId64 "\n", ms_setting.exec_num);
  }
  fprintf(stdout, "windows size: %" PRId64 "k\n",
          (int64_t)(ms_setting.win_size / 1024));
  fprintf(stdout, "set proportion: set_prop=%.2f\n",
          ms_setting.cmd_distr[CMD_SET].cmd_prop);
  fprintf(stdout, "get proportion: get_prop=%.2f\n",
          ms_setting.cmd_distr[CMD_GET].cmd_prop);
  fflush(stdout);
} /* ms_print_setting */


/**
 * previous part of slap mode initialization of setting structure
 */
static void ms_setting_slapmode_init_pre()
{
  ms_setting.exec_num= DEFAULT_EXE_NUM;
  ms_setting.verify_percent= DEFAULT_VERIFY_RATE;
  ms_setting.exp_ver_per= DEFAULT_VERIFY_RATE;
  ms_setting.overwrite_percent= DEFAULT_OVERWRITE_RATE;
  ms_setting.mult_key_num= DEFAULT_DIV;
  ms_setting.fixed_value_size= 0;
  ms_setting.win_size= DEFAULT_WINDOW_SIZE;
  ms_setting.udp= false;
  ms_setting.reconnect= false;
  ms_setting.verbose= false;
  ms_setting.facebook_test= false;
  ms_setting.binary_prot_= false;
  ms_setting.stat_freq= 0;
  ms_setting.srv_str= NULL;
  ms_setting.cfg_file= NULL;
  ms_setting.sock_per_conn= DEFAULT_SOCK_PER_CONN;
  ms_setting.expected_tps= 0;
  ms_setting.rep_write_srv= 0;
} /* ms_setting_slapmode_init_pre */


/**
 * previous part of initialization of setting structure
 */
void ms_setting_init_pre()
{
  memset(&ms_setting, 0, sizeof(ms_setting));

  /* common initialize */
  ms_setting.ncpu= ms_get_cpu_count();
  ms_setting.nthreads= DEFAULT_THREADS_NUM;
  ms_setting.nconns= DEFAULT_CONNS_NUM;
  ms_setting.run_time= DEFAULT_RUN_TIME;
  ms_setting.total_srv_cnt= MCD_SRVS_NUM_INIT;
  ms_setting.servers= (ms_mcd_server_t *)malloc(
    (size_t)ms_setting.total_srv_cnt
    * sizeof(ms_mcd_server_t));
  if (ms_setting.servers == NULL)
  {
    fprintf(stderr, "Can't allocate servers structure.\n");
    exit(1);
  }

  ms_setting_slapmode_init_pre();
} /* ms_setting_init_pre */


/**
 * post part of slap mode initialization of setting structure
 */
static void ms_setting_slapmode_init_post()
{
  ms_setting.total_key_rng_cnt= KEY_RANGE_COUNT_INIT;
  ms_setting.key_distr=
    (ms_key_distr_t *)malloc((size_t)ms_setting.total_key_rng_cnt * sizeof(ms_key_distr_t));

  if (ms_setting.key_distr == NULL)
  {
    fprintf(stderr, "Can't allocate key distribution structure.\n");
    exit(1);
  }

  ms_setting.total_val_rng_cnt= VALUE_RANGE_COUNT_INIT;

  ms_setting.value_distr=
    (ms_value_distr_t *)malloc((size_t)ms_setting.total_val_rng_cnt * sizeof( ms_value_distr_t));

  if (ms_setting.value_distr == NULL)
  {
    fprintf(stderr, "Can't allocate value distribution structure.\n");
    exit(1);
  }

  ms_parse_cfg_file(ms_setting.cfg_file);

  /* run time mode */
  if ((ms_setting.exec_num == 0) && (ms_setting.run_time != 0))
  {
    ms_setting.exec_num= (int64_t)MAX_EXEC_NUM;
  }
  else
  {
    /* execute number mode */
    ms_setting.run_time= 0;
  }

  if (ms_setting.rep_write_srv > 0)
  {
    /* for replication test, need enable reconnect feature */
    ms_setting.reconnect= true;
  }

  if (ms_setting.facebook_test && (ms_setting.mult_key_num < 2))
  {
    fprintf(stderr, "facebook test must work with multi-get, "
                    "please specify multi-get key number "
                    "with '--division' option.\n");
    exit(1);
  }

  if (ms_setting.facebook_test && ms_setting.udp)
  {
    fprintf(stderr, "facebook test couldn't work with UDP.\n");
    exit(1);
  }

  if (ms_setting.udp && (ms_setting.sock_per_conn > 1))
  {
    fprintf(stderr, "UDP doesn't support multi-socks "
                    "in one connection structure.\n");
    exit(1);
  }

  if ((ms_setting.rep_write_srv > 0) && (ms_setting.srv_cnt < 2))
  {
    fprintf(stderr, "Please specify 2 servers at least for replication\n");
    exit(1);
  }

  if ((ms_setting.rep_write_srv > 0)
      && (ms_setting.srv_cnt < ms_setting.rep_write_srv))
  {
    fprintf(stderr, "Servers to do replication writing "
                    "is larger than the total servers\n");
    exit(1);
  }

  if (ms_setting.udp && (ms_setting.rep_write_srv > 0))
  {
    fprintf(stderr, "UDP doesn't support replication.\n");
    exit(1);
  }

  if (ms_setting.facebook_test && (ms_setting.rep_write_srv > 0))
  {
    fprintf(stderr, "facebook test couldn't work with replication.\n");
    exit(1);
  }

  ms_build_distr();

  /* initialize global character block */
  ms_init_random_block();
  ms_print_setting();
} /* ms_setting_slapmode_init_post */


/**
 * post part of initialization of setting structure
 */
void ms_setting_init_post()
{
  ms_get_serverlist(ms_setting.srv_str);
  ms_setting_slapmode_init_post();
}


/**
 * clean up the global setting structure
 */
void ms_setting_cleanup()
{
  if (ms_setting.distr != NULL)
  {
    free(ms_setting.distr);
  }

  if (ms_setting.char_block != NULL)
  {
    free(ms_setting.char_block);
  }

  if (ms_setting.srv_str != NULL)
  {
    free(ms_setting.srv_str);
  }

  if (ms_setting.cfg_file != NULL)
  {
    free(ms_setting.cfg_file);
  }

  if (ms_setting.servers != NULL)
  {
    free(ms_setting.servers);
  }

  if (ms_setting.key_distr != NULL)
  {
    free(ms_setting.key_distr);
  }

  if (ms_setting.value_distr != NULL)
  {
    free(ms_setting.value_distr);
  }
} /* ms_setting_cleanup */