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