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