Update for mavericks
[m6w6/libmemcached] / clients / memstat.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 * Authors:
12 * Brian Aker
13 * Toru Maesaka
14 */
15 #include <mem_config.h>
16
17 #include <cstdio>
18 #include <cstring>
19 #include <ctime>
20 #include <iostream>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/types.h>
28
29 #include <libmemcached-1.0/memcached.h>
30
31 #include "client_options.h"
32 #include "utilities.h"
33
34 #define PROGRAM_NAME "memstat"
35 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
36
37 /* Prototypes */
38 static void options_parse(int argc, char *argv[]);
39 static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat);
40 static void print_analysis_report(memcached_st *memc,
41 memcached_analysis_st *report);
42
43 static bool opt_binary= false;
44 static bool opt_verbose= false;
45 static bool opt_server_version= false;
46 static bool opt_analyze= false;
47 static char *opt_servers= NULL;
48 static char *stat_args= NULL;
49 static char *analyze_mode= NULL;
50 static char *opt_username;
51 static char *opt_passwd;
52
53 static struct option long_options[]=
54 {
55 {(OPTIONSTRING)"args", required_argument, NULL, OPT_STAT_ARGS},
56 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
57 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
58 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
59 {(OPTIONSTRING)"verbose", no_argument, NULL, OPT_VERBOSE},
60 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
61 {(OPTIONSTRING)"debug", no_argument, NULL, OPT_DEBUG},
62 {(OPTIONSTRING)"server-version", no_argument, NULL, OPT_SERVER_VERSION},
63 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
64 {(OPTIONSTRING)"analyze", optional_argument, NULL, OPT_ANALYZE},
65 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
66 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
67 {0, 0, 0, 0},
68 };
69
70
71 static memcached_return_t stat_printer(const memcached_instance_st * instance,
72 const char *key, size_t key_length,
73 const char *value, size_t value_length,
74 void *context)
75 {
76 static const memcached_instance_st * last= NULL;
77 (void)context;
78
79 if (last != instance)
80 {
81 printf("Server: %s (%u)\n", memcached_server_name(instance),
82 (uint32_t)memcached_server_port(instance));
83 last= instance;
84 }
85
86 printf("\t %.*s: %.*s\n", (int)key_length, key, (int)value_length, value);
87
88 return MEMCACHED_SUCCESS;
89 }
90
91 static memcached_return_t server_print_callback(const memcached_st *,
92 const memcached_instance_st * instance,
93 void *)
94 {
95 std::cerr << memcached_server_name(instance) << ":" << memcached_server_port(instance) <<
96 " " << int(memcached_server_major_version(instance)) <<
97 "." << int(memcached_server_minor_version(instance)) <<
98 "." << int(memcached_server_micro_version(instance)) << std::endl;
99
100 return MEMCACHED_SUCCESS;
101 }
102
103 int main(int argc, char *argv[])
104 {
105 options_parse(argc, argv);
106 initialize_sockets();
107
108 if (opt_servers == NULL)
109 {
110 char *temp;
111 if ((temp= getenv("MEMCACHED_SERVERS")))
112 {
113 opt_servers= strdup(temp);
114 }
115
116 if (opt_servers == NULL)
117 {
118 std::cerr << "No Servers provided" << std::endl;
119 return EXIT_FAILURE;
120 }
121 }
122
123 memcached_server_st* servers= memcached_servers_parse(opt_servers);
124 if (servers == NULL or memcached_server_list_count(servers) == 0)
125 {
126 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
127 return EXIT_FAILURE;
128 }
129
130 if (opt_servers)
131 {
132 free(opt_servers);
133 }
134
135 memcached_st *memc= memcached_create(NULL);
136 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
137
138 memcached_return_t rc= memcached_server_push(memc, servers);
139 memcached_server_list_free(servers);
140
141 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
142 {
143 memcached_free(memc);
144 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
145 return EXIT_FAILURE;
146 }
147
148 if (opt_username)
149 {
150 memcached_return_t ret;
151 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
152 {
153 std::cerr << memcached_last_error_message(memc) << std::endl;
154 memcached_free(memc);
155 return EXIT_FAILURE;
156 }
157 }
158
159 if (rc != MEMCACHED_SUCCESS and rc != MEMCACHED_SOME_ERRORS)
160 {
161 printf("Failure to communicate with servers (%s)\n",
162 memcached_strerror(memc, rc));
163 exit(EXIT_FAILURE);
164 }
165
166 if (opt_server_version)
167 {
168 if (memcached_failed(memcached_version(memc)))
169 {
170 std::cerr << "Unable to obtain server version";
171 exit(EXIT_FAILURE);
172 }
173
174 memcached_server_fn callbacks[1];
175 callbacks[0]= server_print_callback;
176 memcached_server_cursor(memc, callbacks, NULL, 1);
177 }
178 else if (opt_analyze)
179 {
180 memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc);
181
182 if (memc_stat == NULL)
183 {
184 exit(EXIT_FAILURE);
185 }
186
187 run_analyzer(memc, memc_stat);
188
189 memcached_stat_free(memc, memc_stat);
190 }
191 else
192 {
193 rc= memcached_stat_execute(memc, stat_args, stat_printer, NULL);
194 }
195
196 memcached_free(memc);
197
198 return rc == MEMCACHED_SUCCESS ? EXIT_SUCCESS: EXIT_FAILURE;
199 }
200
201 static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat)
202 {
203 memcached_return_t rc;
204
205 if (analyze_mode == NULL)
206 {
207 memcached_analysis_st *report;
208 report= memcached_analyze(memc, memc_stat, &rc);
209 if (rc != MEMCACHED_SUCCESS || report == NULL)
210 {
211 printf("Failure to analyze servers (%s)\n",
212 memcached_strerror(memc, rc));
213 exit(1);
214 }
215 print_analysis_report(memc, report);
216 free(report);
217 }
218 else if (strcmp(analyze_mode, "latency") == 0)
219 {
220 uint32_t flags, server_count= memcached_server_count(memc);
221 uint32_t num_of_tests= 32;
222 const char *test_key= "libmemcached_test_key";
223
224 memcached_st **servers= static_cast<memcached_st**>(malloc(sizeof(memcached_st*) * server_count));
225 if (servers == NULL)
226 {
227 fprintf(stderr, "Failed to allocate memory\n");
228 return;
229 }
230
231 for (uint32_t x= 0; x < server_count; x++)
232 {
233 const memcached_instance_st * instance=
234 memcached_server_instance_by_position(memc, x);
235
236 if ((servers[x]= memcached_create(NULL)) == NULL)
237 {
238 fprintf(stderr, "Failed to memcached_create()\n");
239 if (x > 0)
240 {
241 memcached_free(servers[0]);
242 }
243 x--;
244
245 for (; x > 0; x--)
246 {
247 memcached_free(servers[x]);
248 }
249
250 free(servers);
251
252 return;
253 }
254 memcached_server_add(servers[x],
255 memcached_server_name(instance),
256 memcached_server_port(instance));
257 }
258
259 printf("Network Latency Test:\n\n");
260 struct timeval start_time, end_time;
261 uint32_t slowest_server= 0;
262 long elapsed_time, slowest_time= 0;
263
264 for (uint32_t x= 0; x < server_count; x++)
265 {
266 const memcached_instance_st * instance=
267 memcached_server_instance_by_position(memc, x);
268 gettimeofday(&start_time, NULL);
269
270 for (uint32_t y= 0; y < num_of_tests; y++)
271 {
272 size_t vlen;
273 char *val= memcached_get(servers[x], test_key, strlen(test_key),
274 &vlen, &flags, &rc);
275 if (rc != MEMCACHED_NOTFOUND and rc != MEMCACHED_SUCCESS)
276 {
277 break;
278 }
279 free(val);
280 }
281 gettimeofday(&end_time, NULL);
282
283 elapsed_time= (long) timedif(end_time, start_time);
284 elapsed_time /= (long) num_of_tests;
285
286 if (elapsed_time > slowest_time)
287 {
288 slowest_server= x;
289 slowest_time= elapsed_time;
290 }
291
292 if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS)
293 {
294 printf("\t %s (%d) => failed to reach the server\n",
295 memcached_server_name(instance),
296 memcached_server_port(instance));
297 }
298 else
299 {
300 printf("\t %s (%d) => %ld.%ld seconds\n",
301 memcached_server_name(instance),
302 memcached_server_port(instance),
303 elapsed_time / 1000, elapsed_time % 1000);
304 }
305 }
306
307 if (server_count > 1 && slowest_time > 0)
308 {
309 const memcached_instance_st * slowest=
310 memcached_server_instance_by_position(memc, slowest_server);
311
312 printf("---\n");
313 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n",
314 memcached_server_name(slowest),
315 memcached_server_port(slowest),
316 slowest_time / 1000, slowest_time % 1000);
317 }
318 printf("\n");
319
320 for (uint32_t x= 0; x < server_count; x++)
321 {
322 memcached_free(servers[x]);
323 }
324
325 free(servers);
326 free(analyze_mode);
327 }
328 else
329 {
330 fprintf(stderr, "Invalid Analyzer Option provided\n");
331 free(analyze_mode);
332 }
333 }
334
335 static void print_analysis_report(memcached_st *memc,
336 memcached_analysis_st *report)
337
338 {
339 uint32_t server_count= memcached_server_count(memc);
340 const memcached_instance_st * most_consumed_server= memcached_server_instance_by_position(memc, report->most_consumed_server);
341 const memcached_instance_st * least_free_server= memcached_server_instance_by_position(memc, report->least_free_server);
342 const memcached_instance_st * oldest_server= memcached_server_instance_by_position(memc, report->oldest_server);
343
344 printf("Memcached Cluster Analysis Report\n\n");
345
346 printf("\tNumber of Servers Analyzed : %u\n", server_count);
347 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
348 report->average_item_size);
349
350 if (server_count == 1)
351 {
352 printf("\nFor a detailed report, you must supply multiple servers.\n");
353 return;
354 }
355
356 printf("\n");
357 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
358 memcached_server_name(most_consumed_server),
359 (uint32_t)memcached_server_port(most_consumed_server),
360 (unsigned long long)report->most_used_bytes);
361 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
362 memcached_server_name(least_free_server),
363 (uint32_t)memcached_server_port(least_free_server),
364 (unsigned long long)report->least_remaining_bytes);
365 printf("\tNode with longest uptime : %s:%u (%us)\n",
366 memcached_server_name(oldest_server),
367 (uint32_t)memcached_server_port(oldest_server),
368 report->longest_uptime);
369 printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio);
370 printf("\n");
371 }
372
373 static void options_parse(int argc, char *argv[])
374 {
375 memcached_programs_help_st help_options[]=
376 {
377 {0},
378 };
379
380 int option_index= 0;
381
382 bool opt_version= false;
383 bool opt_help= false;
384 while (1)
385 {
386 int option_rv= getopt_long(argc, argv, "Vhvds:a", long_options, &option_index);
387
388 if (option_rv == -1)
389 break;
390
391 switch (option_rv)
392 {
393 case 0:
394 break;
395
396 case OPT_VERBOSE: /* --verbose or -v */
397 opt_verbose= true;
398 break;
399
400 case OPT_DEBUG: /* --debug or -d */
401 opt_verbose= true;
402 break;
403
404 case OPT_BINARY:
405 opt_binary= true;
406 break;
407
408 case OPT_SERVER_VERSION:
409 opt_server_version= true;
410 break;
411
412 case OPT_VERSION: /* --version or -V */
413 opt_version= true;
414 break;
415
416 case OPT_HELP: /* --help or -h */
417 opt_help= true;
418 break;
419
420 case OPT_SERVERS: /* --servers or -s */
421 opt_servers= strdup(optarg);
422 break;
423
424 case OPT_STAT_ARGS:
425 stat_args= strdup(optarg);
426 break;
427
428 case OPT_ANALYZE: /* --analyze or -a */
429 opt_analyze= OPT_ANALYZE;
430 analyze_mode= (optarg) ? strdup(optarg) : NULL;
431 break;
432
433 case OPT_QUIET:
434 close_stdio();
435 break;
436
437 case OPT_USERNAME:
438 opt_username= optarg;
439 opt_binary= true;
440 break;
441
442 case OPT_PASSWD:
443 opt_passwd= optarg;
444 break;
445
446 case '?':
447 /* getopt_long already printed an error message. */
448 exit(1);
449 default:
450 abort();
451 }
452 }
453
454 if (opt_version)
455 {
456 version_command(PROGRAM_NAME);
457 exit(EXIT_SUCCESS);
458 }
459
460 if (opt_help)
461 {
462 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
463 exit(EXIT_SUCCESS);
464 }
465 }