This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
mc.c
1366 lines (1155 loc) · 41.5 KB
/
mc.c
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
/*
* twemcache - Twitter memcached.
* Copyright (c) 2012, Twitter, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Twitter nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* memcached - memory caching daemon
*
* http://www.danga.com/memcached/
*
* Copyright 2003 Danga Interactive, Inc. All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* Authors:
* Anatoly Vorobey <mellon@pobox.com>
* Brad Fitzpatrick <brad@danga.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <ctype.h>
#include <pwd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <getopt.h>
#include <mc_core.h>
#define MC_CHUNK_SIZE ITEM_CHUNK_SIZE
#define MC_SLAB_SIZE SLAB_SIZE
#define MC_SLAB_PREALLOC false
#define MC_LOCK_PAGES false
#define MC_DAEMONIZE false
#define MC_MAXIMIZE_CORE false
#define MC_DISABLE_CAS false
#define MC_LOG_FILE NULL
#define MC_LOG_DEFAULT LOG_NOTICE
#define MC_LOG_MIN LOG_EMERG
#define MC_LOG_MAX LOG_PVERB
#define MC_STATS_MIN_INTVL STATS_MIN_INTVL
#define MC_STATS_MAX_INTVL STATS_MAX_INTVL
#define MC_STATS_INTVL STATS_DEFAULT_INTVL
#define MC_HASH_MAX_POWER HASH_MAX_POWER
#define MC_KLOG_INTVL KLOG_DEFAULT_INTVL
#define MC_KLOG_SMP_RATE KLOG_DEFAULT_SMP_RATE
#define MC_KLOG_ENTRY KLOG_DEFAULT_ENTRY
#define MC_KLOG_FILE NULL
#define MC_KLOG_BACKUP NULL
#define MC_KLOG_BACKUP_SUF ".old"
#define MC_WORKERS 2
#define MC_PID_FILE NULL
#define MC_USER NULL
#define MC_REQ_PER_EVENT 20
#define MC_MAX_CONNS 1024
#define MC_BACKLOG 1024
#define MC_TCP_PORT 11211
#define MC_UDP_PORT 11211
#define MC_INTERFACE NULL
#define MC_UNIX_PATH NULL
#define MC_ACCESS_MASK 0700
#define MC_EVICT EVICT_RS
#define MC_EVICT_STR "random slab"
#define MC_FACTOR 1.25
#define MC_MAXBYTES (64 * MB)
struct settings settings; /* twemcache settings */
static int show_help; /* show twemcache help? */
static int show_version; /* show twemcache version? */
static int show_stats_description; /* show twemcache stats description? */
static int show_sizes; /* show twemcache struct sizes? */
static int parse_profile; /* parse profile? */
static char *profile_optarg; /* profile optarg */
static struct option long_options[] = {
{ "help", no_argument, NULL, 'h' }, /* help */
{ "version", no_argument, NULL, 'V' }, /* version */
{ "prealloc", no_argument, NULL, 'E' }, /* preallocate slabs */
{ "use-large-pages", no_argument, NULL, 'L' }, /* use large memory pages */
{ "lock-pages", no_argument, NULL, 'k' }, /* lock all pages */
{ "daemonize", no_argument, NULL, 'd' }, /* daemon mode */
{ "maximize-core-limit", no_argument, NULL, 'r' }, /* maximize corefile limit */
{ "disable-cas", no_argument, NULL, 'C' }, /* disable cas */
{ "describe-stats", no_argument, NULL, 'D' }, /* print stats description and exit */
{ "show-sizes", no_argument, NULL, 'S' }, /* print slab & item struct sizes and exit */
{ "output", required_argument, NULL, 'o' }, /* output logfile */
{ "verbosity", required_argument, NULL, 'v' }, /* log verbosity level */
{ "stats-aggr-interval", required_argument, NULL, 'A' }, /* stats aggregation interval in usec */
{ "klog-entry", required_argument, NULL, 'x' }, /* command logging entry number */
{ "klog-file", required_argument, NULL, 'X' }, /* command logging file */
{ "klog-sample-rate", required_argument, NULL, 'y' }, /* command logging sampling rate */
{ "hash-power", required_argument, NULL, 'e' }, /* fixed sized hash table, as power of 2 */
{ "threads", required_argument, NULL, 't' }, /* # of threads */
{ "pidfile", required_argument, NULL, 'P' }, /* pid file */
{ "user", required_argument, NULL, 'u' }, /* user identity to run as */
{ "max-requests", required_argument, NULL, 'R' }, /* max request per event */
{ "max-conns", required_argument, NULL, 'c' }, /* max simultaneous connections */
{ "backlog", required_argument, NULL, 'b' }, /* tcp backlog queue limit */
{ "port", required_argument, NULL, 'p' }, /* tcp port number to listen on */
{ "udp-port", required_argument, NULL, 'U' }, /* udp port number to listen on */
{ "interface", required_argument, NULL, 'l' }, /* interface to listen on */
{ "unix-path", required_argument, NULL, 's' }, /* unix socket path to listen on */
{ "access-mask", required_argument, NULL, 'a' }, /* access mask for unix socket */
{ "eviction-strategy", required_argument, NULL, 'M' }, /* eviction strategy on OOM */
{ "factor", required_argument, NULL, 'f' }, /* growth factor for slab items */
{ "max-memory", required_argument, NULL, 'm' }, /* max memory for all items in MB */
{ "min-item-chunk-size", required_argument, NULL, 'n' }, /* min item chunk size */
{ "slab-size", required_argument, NULL, 'I' }, /* slab size */
{ "slab-profile", required_argument, NULL, 'z' }, /* profile of slab item sizes */
{ NULL, 0, NULL, 0 }
};
static char short_options[] =
"h" /* help */
"V" /* version */
"E" /* preallocate slabs */
"L" /* use large memory pages */
"k" /* lock all pages */
"d" /* daemon mode */
"r" /* maximize corefile limit */
"C" /* disable cas */
"D" /* print stats description and exit */
"S" /* print slab & item struct sizes and exit */
"o:" /* output logfile */
"v:" /* log verbosity level */
"A:" /* stats aggregation interval in msec */
"e:" /* hash power */
"t:" /* # of threads */
"P:" /* pid file */
"u:" /* user identity to run as */
"x:" /* command logging entry number */
"X:" /* command logging file */
"y:" /* command logging sample rate */
"R:" /* max request per event */
"c:" /* max simultaneous connections */
"b:" /* tcp backlog queue limit */
"p:" /* tcp port number to listen on */
"U:" /* udp port number to listen on */
"l:" /* interface to listen on */
"s:" /* unix socket path to listen on */
"a:" /* access mask for unix socket */
"M:" /* eviction strategy on OOM */
"f:" /* growth factor for slab items */
"m:" /* max memory for all items in MB */
"n:" /* min item size */
"I:" /* max item size */
"z:" /* profile of slab item sizes */
;
static void
mc_show_usage(void)
{
log_stderr(
"Usage:" CRLF
"twemcache [-?hVCELdkrDS]" CRLF
" [-o output file] [-v verbosity level]" CRLF
" [-A stats aggr interval] [-t threads] [-P pid file] [-u user]" CRLF
" [-e hash power] [-M eviction strategy]" CRLF
" [-x command log entry] [-X command log file] [-y command log sample rate]" CRLF
" [-p port] [-U udp port] [-R max requests] [-c max conns] [-b backlog]" CRLF
" [-l interface] [-s unix path] [-a access mask]" CRLF
" [-m max memory] [-f factor] [-n min item chunk size] [-I slab size]" CRLF
" [-z slab profile]"
"");
log_stderr(
"Options:" CRLF
" -h, --help show twemcache version, usage, options, and exit" CRLF
" -V, --version show version and exit" CRLF
" -E, --prealloc preallocate memory for all slabs" CRLF
" -L, --use-large-pages use large pages if available" CRLF
" -k, --lock-pages preallocate all slab memory and lock the pages" CRLF
" -d, --daemonize run twemcache as a daemon" CRLF
" -r, --maximize-core-limit maximize core file limit" CRLF
" -C, --disable-cas disable cas field in an item, this saves 8 bytes" CRLF
" per item, but cas/gets commands will not work" CRLF
" -D, --describe-stats show version, name and description of each stats" CRLF
" metric, and exit" CRLF
" -S, --show-sizes show version, item overhead, minimum item size," CRLF
" slab overhead, default slab size, and exit"
"");
log_stderr(
" -o, --output=S set the debug logging file (default: %s)" CRLF
" -v, --verbosity=N set the debug logging level (default: %d," CRLF
" min: %d, max: %d)" CRLF
" -A, --stats-aggr-interval=N aggregate stats every N usec (default: %d)" CRLF
" to get a snapshot used by `stats' commands" CRLF
" -e, --hash-power=N create a fixed sized hash table, number of" CRLF
" entries as power of 2 (default: 0, i.e. dynamic)" CRLF
" -t, --threads=N set the number of worker threads (default: %d)" CRLF
" -P, --pidfile=S store pid in a file (default: %s)" CRLF
" -u, --user=S user identity to run twemcache as, set this" CRLF
" option if and only if run twemcache as root"
"",
MC_LOG_FILE != NULL ? MC_LOG_FILE : "stderr",
MC_LOG_DEFAULT, MC_LOG_MIN, MC_LOG_MAX,
MC_STATS_INTVL,
MC_WORKERS,
MC_PID_FILE != NULL ? MC_PID_FILE : "not stored"
);
log_stderr(
" -x, --klog-entry=N size the thread-local command log buffer to hold" CRLF
" at least this many entires (default: %d)" CRLF
" -X, --klog-file=S log commands to file S (default: %s)" CRLF
" -y, --klog-sample-rate=N set the command logging to sample one in every N" CRLF
" commands (default: %d)"
"",
MC_KLOG_ENTRY,
MC_KLOG_FILE != NULL ? MC_KLOG_FILE : "disabled",
MC_KLOG_SMP_RATE
);
log_stderr(
" -R, --max-requests=N set the maximum number of requests per event" CRLF
" (default: %d)" CRLF
" -c, --max-conns=N maximum open connections (default: %d)" CRLF
" -b, --backlog=N the backlog argument of listen() applied to the" CRLF
" listening socket (default %d)" CRLF
" -p, --port=N set the tcp port to listen on (default: %d)" CRLF
" -U, --udp-port=N set the udp port to listen on (default: %d)" CRLF
" -l, --interface=S interface to listen on (default: %s)" CRLF
" -s, --unix-path=S set unix socket file path (default: %s)" CRLF
" -a, --access-mask=O access mask of the unix socket file in octal" CRLF
" (default: %04o)"
"",
MC_REQ_PER_EVENT, MC_MAX_CONNS, MC_BACKLOG,
MC_TCP_PORT, MC_UDP_PORT,
MC_INTERFACE != NULL ? MC_INTERFACE : "all interfaces",
MC_UNIX_PATH != NULL ? MC_UNIX_PATH : "disabled", MC_ACCESS_MASK
);
log_stderr(
" -M, --eviction-strategy=N use eviction strategy N when running out of free" CRLF
" memory for items (default: %d, i.e. %s)" CRLF
" -f, --factor=D set the exponential growth factor in deciding" CRLF
" item chunk size for each slab class" CRLF
" (default: %g)" CRLF
" -m, --max-memory=N set the maximum memory allocated to slabs, in MB" CRLF
" (default: %d)" CRLF
" -n, --min-item-chunk-size=N set the minimum item chunk size, in bytes" CRLF
" (default: %d)" CRLF
" -I, --slab-size=N slab size, in bytes (default: %d)" CRLF
" -z, --slab-profile=S specify all item chunk sizes to be supported," CRLF
" e.g. -z 128,256,1024,8192 (default: off)" CRLF
"",
MC_EVICT, MC_EVICT_STR,
MC_FACTOR, MC_MAXBYTES / MB,
MC_CHUNK_SIZE,
SLAB_SIZE
);
}
static rstatus_t
mc_create_pidfile(void)
{
rstatus_t status;
char pid[MC_UINTMAX_MAXLEN];
int fd, pid_len;
ssize_t n;
fd = open(settings.pid_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
log_error("opening pid file '%s' failed: %s", settings.pid_filename,
strerror(errno));
return MC_ERROR;
}
settings.pid_file = 1;
pid_len = mc_snprintf(pid, MC_UINTMAX_MAXLEN, "%d", settings.pid);
n = mc_write(fd, pid, pid_len);
if (n < 0) {
log_error("write to pid file '%s' failed: %s", settings.pid_filename,
strerror(errno));
return MC_ERROR;
}
status = close(fd);
if (status < 0) {
log_error("close pid file '%s' failed: %s", settings.pid_filename,
strerror(errno));
return MC_ERROR;
}
return MC_OK;
}
static void
mc_remove_pidfile(void)
{
int status;
if (!settings.pid_file) {
return;
}
status = unlink(settings.pid_filename);
if (status < 0) {
log_error("unlink of pid file '%s' failed, ignored: %s",
settings.pid_filename, strerror(errno));
}
}
static rstatus_t
mc_daemonize(int dump_core)
{
rstatus_t status;
pid_t pid, sid;
int fd;
/* 1st fork detaches child from terminal */
pid = fork();
switch (pid) {
case -1:
log_error("fork() failed: %s", strerror(errno));
return MC_ERROR;
case 0:
break;
default:
/* parent terminates */
_exit(0);
}
/* 1st child continues and becomes the session and process group leader */
sid = setsid();
if (sid < 0) {
return MC_ERROR;
}
if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
log_error("signal(SIGHUP, SIG_IGN) failed: %s", strerror(errno));
return MC_ERROR;
}
/* 2nd fork turns child into a non-session leader: cannot acquire terminal */
pid = fork();
switch (pid) {
case -1:
log_error("fork() failed: %s", strerror(errno));
return MC_ERROR;
case 0:
break;
default:
/* 1st child terminates */
_exit(0);
}
/* change working directory */
if (dump_core == 0) {
status = chdir("/");
if (status < 0) {
log_error("chdir(\"/\") failed: %s", strerror(errno));
return MC_ERROR;
}
}
/* clear file mode creation mask */
umask(0);
/* redirect stdin, stdout and stderr to "/dev/null" */
fd = open("/dev/null", O_RDWR);
if (fd < 0) {
log_error("open(\"/dev/null\") failed: %s", strerror(errno));
return MC_ERROR;
}
status = dup2(fd, STDIN_FILENO);
if (status < 0) {
log_error("dup2(%d, STDIN) failed: %s", fd, strerror(errno));
close(fd);
return MC_ERROR;
}
status = dup2(fd, STDOUT_FILENO);
if (status < 0) {
log_error("dup2(%d, STDOUT) failed: %s", fd, strerror(errno));
close(fd);
return MC_ERROR;
}
status = dup2(fd, STDERR_FILENO);
if (status < 0) {
log_error("dup2(%d, STDERR) failed: %s", fd, strerror(errno));
close(fd);
return MC_ERROR;
}
if (fd > STDERR_FILENO) {
status = close(fd);
if (status < 0) {
log_error("close(%d) failed: %s", fd, strerror(errno));
return MC_ERROR;
}
}
return MC_OK;
}
/*
* On systems that supports multiple page sizes we may reduce the
* number of TLB-misses by using the biggest available page size
*/
static int
mc_enable_large_pages(void)
{
#ifdef MC_LARGE_PAGES
int ret, i, avail;
size_t sizes[32];
size_t max = sizes[0];
struct memcntl_mha arg = {0};
avail = getpagesizes(sizes, 32);
if (avail < 0) {
log_error("getpagesizes failed: %s, using default page size",
strerror(errno));
return -1;
}
for (i = 1; i < avail; i++) {
if (max < sizes[i]) {
max = sizes[i];
}
}
arg.mha_flags = 0;
arg.mha_pagesize = max;
arg.mha_cmd = MHA_MAPSIZE_BSSBRK;
ret = memcntl(0, 0, MC_HAT_ADVISE, (caddr_t)&arg, 0, 0);
if (ret < 0) {
log_error("memctl to to set large pages failed: %s, using default "
"page size", strerror(errno));
return ret;
}
#endif
return 0;
}
static rstatus_t
mc_lock_page(void)
{
#ifdef MC_MLOCKALL
int status;
status = mlockall(MCL_CURRENT | MCL_FUTURE);
if (status < 0) {
log_stderr("twemcache: -k option to mlockall failed: %s",
strerror(errno));
return MC_ERROR;
}
return MC_OK;
#else
log_stderr("twemcache: -k option to mlockall not supported");
return MC_ERROR;
#endif
}
static void
mc_set_default_options(void)
{
settings.prealloc = MC_SLAB_PREALLOC;
settings.lock_page = MC_LOCK_PAGES;
settings.daemonize = MC_DAEMONIZE;
settings.max_corefile = MC_MAXIMIZE_CORE;
settings.use_cas = MC_DISABLE_CAS ? false : true;
settings.log_filename = MC_LOG_FILE;
settings.verbose = MC_LOG_DEFAULT;
stats_set_interval(MC_STATS_INTVL);
settings.klog_name = MC_KLOG_FILE;
settings.klog_backup = MC_KLOG_BACKUP;
settings.klog_sampling_rate = MC_KLOG_SMP_RATE;
settings.klog_entry = MC_KLOG_ENTRY;
klog_set_interval(MC_KLOG_INTVL);
settings.klog_running = false;
settings.num_workers = MC_WORKERS;
settings.username = MC_USER;
settings.reqs_per_event = MC_REQ_PER_EVENT;
settings.maxconns = MC_MAX_CONNS;
settings.backlog = MC_BACKLOG;
settings.port = MC_TCP_PORT;
settings.udpport = MC_UDP_PORT;
settings.interface = MC_INTERFACE;
settings.socketpath = MC_UNIX_PATH;
settings.access = MC_ACCESS_MASK;
settings.evict_opt = MC_EVICT;
settings.use_freeq = true;
settings.use_lruq = true;
settings.factor = MC_FACTOR;
settings.maxbytes = MC_MAXBYTES;
settings.chunk_size = MC_CHUNK_SIZE;
settings.slab_size = MC_SLAB_SIZE;
settings.hash_power = 0;
settings.accepting_conns = true;
settings.oldest_live = 0;
settings.pid = 0;
settings.pid_filename = MC_PID_FILE;
settings.pid_file = 0;
memset(settings.profile, 0, sizeof(settings.profile));
settings.profile_last_id = SLABCLASS_MAX_ID;
}
static rstatus_t
mc_get_options(int argc, char **argv)
{
int c, value, factor;
size_t len;
bool tcp_specified, udp_specified;
tcp_specified = false;
udp_specified = false;
opterr = 0;
for (;;) {
c = getopt_long(argc, argv, short_options, long_options, NULL);
if (c == -1) {
/* no more options */
break;
}
switch (c) {
case 'h':
show_version = 1;
show_help = 1;
break;
case 'V':
show_version = 1;
break;
case 'E':
settings.prealloc = true;
break;
case 'L':
if (mc_enable_large_pages() == 0) {
settings.prealloc = true;
}
break;
case 'k':
settings.lock_page = true;
settings.prealloc = true;
break;
case 'd':
settings.daemonize = true;
break;
case 'r':
settings.max_corefile = true;
break;
case 'C':
settings.use_cas = false;
break;
case 'D':
show_stats_description = 1;
show_version = 1;
break;
case 'S':
show_sizes = 1;
show_version = 1;
break;
case 'o':
settings.log_filename = optarg;
break;
case 'v':
value = mc_atoi(optarg, strlen(optarg));
if (value < 0) {
log_stderr("twemcache: option -v requires a number");
return MC_ERROR;
}
settings.verbose = value;
break;
case 'A':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -A requires a non zero number");
return MC_ERROR;
}
if (value < MC_STATS_MIN_INTVL) {
log_stderr("twemcache: stats aggregation interval cannot be "
"less than %d usec", MC_STATS_MIN_INTVL);
return MC_ERROR;
}
if (value > MC_STATS_MAX_INTVL) {
log_stderr("twemcache: stats aggregation interval cannot exceed"
" %d usec", MC_STATS_MAX_INTVL);
return MC_ERROR;
}
stats_set_interval(value);
break;
case 'e':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -e requires a positive number");
return MC_ERROR;
}
if (value > MC_HASH_MAX_POWER) {
log_stderr("twemcache: hash power cannot be greater than %d",
MC_HASH_MAX_POWER);
return MC_ERROR;
}
settings.hash_power = value;
break;
case 'x':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -x requires a positive number");
return MC_ERROR;
}
settings.klog_entry = value;
break;
case 'X':
settings.klog_name = optarg;
len = strlen(optarg) + sizeof(MC_KLOG_BACKUP_SUF);
settings.klog_backup = mc_alloc(len);
if (settings.klog_backup == NULL) {
log_stderr("twemcache: cannot generate klog backup filename");
return MC_ERROR;
}
value = mc_snprintf(settings.klog_backup, len, "%s"MC_KLOG_BACKUP_SUF,
settings.klog_name);
ASSERT(value < len);
settings.klog_running = true;
break;
case 'y':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -y requires a positive number");
return MC_ERROR;
}
settings.klog_sampling_rate = value;
break;
case 't':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -t requires a non zero number");
return MC_ERROR;
}
settings.num_workers = value;
break;
case 'P':
settings.pid_filename = optarg;
break;
case 'u':
settings.username = optarg;
break;
case 'R':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -R requires a non zero number");
return MC_ERROR;
}
settings.reqs_per_event = value;
break;
case 'c':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -c requires a non zero number");
return MC_ERROR;
}
settings.maxconns = value;
break;
case 'b':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -b requires a non zero number");
return MC_ERROR;
}
settings.backlog = value;
break;
case 'p':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -p requires a non zero number");
return MC_ERROR;
}
if (!mc_valid_port(value)) {
log_stderr("twemcache: option -p value %d is not a valid "
"port", value);
}
settings.port = value;
tcp_specified = true;
break;
case 'U':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -U requires a non number");
return MC_ERROR;
}
if (!mc_valid_port(value)) {
log_stderr("twemcache: option -U value %d is not a valid "
"port", value);
}
settings.udpport = value;
udp_specified = true;
break;
case 'l':
settings.interface = optarg;
break;
case 's':
settings.socketpath = optarg;
break;
case 'a':
if (!mc_str2oct(optarg, &value)) {
log_stderr("twemcache: option -a requires an octal number");
return MC_ERROR;
}
settings.access = value;
break;
case 'M':
value = mc_atoi(optarg, strlen(optarg));
if (value < 0) {
log_stderr("twemcache: option -M requires a number");
return MC_ERROR;
}
if (value >= EVICT_INVALID || value < EVICT_NONE) {
log_stderr("twemcache: option -M value %d is not a valid "
"eviction strategy", value);
return MC_ERROR;
}
settings.evict_opt = value;
if (value == EVICT_CS) {
settings.use_freeq = false;
settings.use_lruq = false;
}
break;
case 'f':
settings.factor = atof(optarg);
if (settings.factor <= 1.0) {
log_stderr("twemcache: factor must be greater than 1.0");
return MC_ERROR;
}
break;
case 'm':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -m requires a non zero number");
return MC_ERROR;
}
settings.maxbytes = (size_t)value * 1024 * 1024;
break;
case 'n':
value = mc_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("twemcache: option -n requires a non zero number");
return MC_ERROR;
}
if (value < ITEM_MIN_CHUNK_SIZE) {
log_stderr("twemcache: minimum item chunk size cannot be less "
"than %zu", ITEM_MIN_CHUNK_SIZE);
return MC_ERROR;
}
if (value % MC_ALIGNMENT != 0) {
log_stderr("twemcache: minimum item chunk size must be %zu "
"bytes aligned", MC_ALIGNMENT);
return MC_ERROR;
}
settings.chunk_size = value;
break;
case 'I':
len = strlen(optarg);
switch (optarg[len - 1]) {
case 'k':
len--;
factor = KB;
break;
case 'm':
case 'M':
len--;
factor = MB;
break;
default:
factor = 1;
}
value = mc_atoi(optarg, len);
if (value <= 0) {
log_stderr("twemcache: option -I requires a non zero number");
return MC_ERROR;
}
settings.slab_size = (size_t)value * factor;
if (value % MC_ALIGNMENT != 0) {
log_stderr("twemcache: value of option -I must be %zu bytes "
"aligned", MC_ALIGNMENT);
return MC_ERROR;
}
if (settings.slab_size < SLAB_MIN_SIZE) {
log_stderr("twemcache: slab size must be at least %zu bytes",
SLAB_MIN_SIZE);
return MC_ERROR;
}
if (settings.slab_size > SLAB_MAX_SIZE) {
log_stderr("twemcache: slab size cannot be larger than %zu "
"bytes", SLAB_MAX_SIZE);
return MC_ERROR;
}
break;
case 'z':
parse_profile = 1;
profile_optarg = optarg;
break;
case '?':
switch (optopt) {
case 'o':
case 'P':
log_stderr("twemcache: option -%c requires a file name", optopt);
break;
case 's':
log_stderr("twemcache: option -%c requires a path name", optopt);
break;
case 'v':
case 'A':
case 'e':
case 't':
case 'R':
case 'c':
case 'b':
case 'p':
case 'U':
case 'a':
case 'M':
case 'f':
case 'm':
case 'n':
log_stderr("twemcache: option -%c requires a number", optopt);
break;
case 'u':
case 'l':
case 'I':
case 'z':
log_stderr("twemcache: option -%c requires a string", optopt);
break;
default:
log_stderr("twemcache: invalid option -- '%c'", optopt);
break;
}
return MC_ERROR;
default:
log_stderr("twemcache: invalid option -- '%c'", optopt);
return MC_ERROR;
}
}
if (tcp_specified && !udp_specified) {
settings.udpport = settings.port;
} else if (udp_specified && !tcp_specified) {
settings.port = settings.udpport;
}
return MC_OK;
}
static rstatus_t
mc_maximize_core(void)
{
int status;
struct rlimit rlim, new_rlim; /* current and new rlimit */
status = getrlimit(RLIMIT_CORE, &rlim);
if (status < 0) {
log_stderr("twemcache: getrlimit(RLIMIT_CORE) failed: %s",
strerror(errno));
return MC_ERROR;
}
/*
* First try raising to infinity. If that fails, try bringing
* the soft limit to the current hard limit.
*/
new_rlim.rlim_cur = RLIM_INFINITY;
new_rlim.rlim_max = RLIM_INFINITY;
status = setrlimit(RLIMIT_CORE, &new_rlim);
if (status < 0) {
new_rlim.rlim_cur = rlim.rlim_max;
new_rlim.rlim_max = rlim.rlim_max;
status = setrlimit(RLIMIT_CORE, &new_rlim);
if (status == 0) {
return MC_OK;
}
log_stderr("twemcache: setrlimit(RLIMIT_CORE, %"PRIu64") failed: %s",