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