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