bae9407642ee56e23508d1e427c4d5b3c7d2d8b9
[awesomized/libmemcached] / clients / utilities.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11 #include <config.h>
12
13 #include <clients/utilities.h>
14
15 #include <cstdio>
16 #include <cassert>
17 #include <cstdlib>
18 #include <cstring>
19 #include <ctype.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25
26 long int timedif(struct timeval a, struct timeval b)
27 {
28 long us, s;
29
30 us = (int)(a.tv_usec - b.tv_usec);
31 us /= 1000;
32 s = (int)(a.tv_sec - b.tv_sec);
33 s *= 1000;
34 return s + us;
35 }
36
37 void version_command(const char *command_name)
38 {
39 printf("%s v%u.%u\n", command_name, 1U, 0U);
40 exit(EXIT_SUCCESS);
41 }
42
43 void close_stdio(void)
44 {
45 int fd;
46 if ((fd = open("/dev/null", O_RDWR, 0)) < 0)
47 {
48 return;
49 }
50 else
51 {
52 if (dup2(fd, STDIN_FILENO) < 0)
53 {
54 return;
55 }
56
57 if (dup2(fd, STDOUT_FILENO) < 0)
58 {
59 return;
60 }
61
62 if (dup2(fd, STDERR_FILENO) < 0)
63 {
64 return;
65 }
66
67 if (fd > STDERR_FILENO)
68 {
69 close(fd);
70 }
71 }
72 }
73
74
75 static const char *lookup_help(memcached_options option)
76 {
77 switch (option)
78 {
79 case OPT_SERVERS: return("List which servers you wish to connect to.");
80 case OPT_VERSION: return("Display the version of the application and then exit.");
81 case OPT_HELP: return("Display this message and then exit.");
82 case OPT_VERBOSE: return("Give more details on the progression of the application.");
83 case OPT_QUIET: return("stderr and stdin will be closed at application startup.");
84 case OPT_DEBUG: return("Provide output only useful for debugging.");
85 case OPT_FLAG: return("Provide flag information for storage operation.");
86 case OPT_EXPIRE: return("Set the expire option for the object.");
87 case OPT_SET: return("Use set command with memcached when storing.");
88 case OPT_REPLACE: return("Use replace command with memcached when storing.");
89 case OPT_ADD: return("Use add command with memcached when storing.");
90 case OPT_SLAP_EXECUTE_NUMBER: return("Number of times to execute the given test.");
91 case OPT_SLAP_INITIAL_LOAD: return("Number of key pairs to load before executing tests.");
92 case OPT_SLAP_TEST: return("Test to run (currently \"get\" or \"set\").");
93 case OPT_SLAP_CONCURRENCY: return("Number of users to simulate with load.");
94 case OPT_SLAP_NON_BLOCK: return("Set TCP up to use non-blocking IO.");
95 case OPT_SLAP_TCP_NODELAY: return("Set TCP socket up to use nodelay.");
96 case OPT_FLUSH: return("Flush servers before running tests.");
97 case OPT_HASH: return("Select hash type.");
98 case OPT_BINARY: return("Switch to binary protocol.");
99 case OPT_ANALYZE: return("Analyze the provided servers.");
100 case OPT_UDP: return("Use UDP protocol when communicating with server.");
101 case OPT_BUFFER: return("Enable request buffering.");
102 case OPT_USERNAME: return "Username to use for SASL authentication";
103 case OPT_PASSWD: return "Password to use for SASL authentication";
104 case OPT_FILE: return "Path to file in which to save result";
105 case OPT_STAT_ARGS: return "Argument for statistics";
106 case OPT_SERVER_VERSION: return "Memcached daemon software version";
107 default:
108 break;
109 };
110
111 assert(0);
112 return "forgot to document this function :)";
113 }
114
115 void help_command(const char *command_name, const char *description,
116 const struct option *long_options,
117 memcached_programs_help_st *options)
118 {
119 unsigned int x;
120 (void)options;
121
122 printf("%s v%u.%u\n\n", command_name, 1U, 0U);
123 printf("\t%s\n\n", description);
124 printf("Current options. A '=' means the option takes a value.\n\n");
125
126 for (x= 0; long_options[x].name; x++)
127 {
128 const char *help_message;
129
130 printf("\t --%s%c\n", long_options[x].name,
131 long_options[x].has_arg ? '=' : ' ');
132 if ((help_message= lookup_help(memcached_options(long_options[x].val))))
133 printf("\t\t%s\n", help_message);
134 }
135
136 printf("\n");
137 exit(EXIT_SUCCESS);
138 }
139
140 void process_hash_option(memcached_st *memc, char *opt_hash)
141 {
142 uint64_t set;
143 memcached_return_t rc;
144
145 if (opt_hash == NULL)
146 return;
147
148 set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
149 if (!strcasecmp(opt_hash, "CRC"))
150 {
151 set= MEMCACHED_HASH_CRC;
152 }
153 else if (!strcasecmp(opt_hash, "FNV1_64"))
154 {
155 set= MEMCACHED_HASH_FNV1_64;
156 }
157 else if (!strcasecmp(opt_hash, "FNV1A_64"))
158 {
159 set= MEMCACHED_HASH_FNV1A_64;
160 }
161 else if (!strcasecmp(opt_hash, "FNV1_32"))
162 {
163 set= MEMCACHED_HASH_FNV1_32;
164 }
165 else if (!strcasecmp(opt_hash, "FNV1A_32"))
166 {
167 set= MEMCACHED_HASH_FNV1A_32;
168 }
169 else
170 {
171 fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
172 exit(EXIT_FAILURE);
173 }
174
175 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
176 if (rc != MEMCACHED_SUCCESS)
177 {
178 fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
179 exit(EXIT_FAILURE);
180 }
181 }
182
183 void initialize_sockets(void)
184 {
185 /* Define the function for all platforms to avoid #ifdefs in each program */
186 #if defined(WIN32) && WIN32
187 WSADATA wsaData;
188 if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
189 {
190 fprintf(stderr, "Socket Initialization Error. Program aborted\n");
191 exit(EXIT_FAILURE);
192 }
193 #endif
194 }