2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
15 #include "libmemcached/common.h"
17 #include <sys/types.h>
19 #include <sys/types.h>
26 #include <libmemcached/memcached.h>
28 #include "client_options.h"
29 #include "utilities.h"
31 #define PROGRAM_NAME "memstat"
32 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
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
);
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
;
50 static struct option long_options
[]=
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
},
62 int main(int argc
, char *argv
[])
64 memcached_return_t rc
;
66 memcached_stat_st
*memc_stat
;
67 memcached_server_st
*servers
;
68 memcached_server_st
*server_list
;
70 options_parse(argc
, argv
);
76 if ((temp
= getenv("MEMCACHED_SERVERS")))
77 opt_servers
= strdup(temp
);
80 fprintf(stderr
, "No Servers provided\n\n");
81 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, 0);
86 memc
= memcached_create(NULL
);
88 servers
= memcached_servers_parse(opt_servers
);
89 memcached_server_push(memc
, servers
);
90 memcached_server_list_free(servers
);
92 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
94 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
96 printf("Failure to communicate with servers (%s)\n",
97 memcached_strerror(memc
, rc
));
101 server_list
= memcached_server_list(memc
);
104 run_analyzer(memc
, memc_stat
, server_list
);
106 print_server_listing(memc
, memc_stat
, server_list
);
111 memcached_free(memc
);
116 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
,
117 memcached_server_st
*server_list
)
119 memcached_return_t rc
;
121 if (analyze_mode
== NULL
)
123 memcached_analysis_st
*report
;
124 report
= memcached_analyze(memc
, memc_stat
, &rc
);
125 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
127 printf("Failure to analyze servers (%s)\n",
128 memcached_strerror(memc
, rc
));
131 print_analysis_report(memc
, report
, server_list
);
134 else if (strcmp(analyze_mode
, "latency") == 0)
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";
141 servers
= malloc(sizeof(memcached_st
*) * server_count
);
144 fprintf(stderr
, "Failed to allocate memory\n");
148 for (x
= 0; x
< server_count
; x
++)
150 if((servers
[x
]= memcached_create(NULL
)) == NULL
)
152 fprintf(stderr
, "Failed to memcached_create()\n");
154 memcached_free(servers
[0]);
157 memcached_free(servers
[x
]);
162 memcached_server_add(servers
[x
],
163 memcached_server_name(memc
, server_list
[x
]),
164 memcached_server_port(memc
, server_list
[x
]));
167 printf("Network Latency Test:\n\n");
168 struct timeval start_time
, end_time
;
169 long elapsed_time
, slowest_time
= 0, slowest_server
= 0;
171 for (x
= 0; x
< server_count
; x
++)
173 gettimeofday(&start_time
, NULL
);
174 for (y
= 0; y
< num_of_tests
; y
++)
177 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
),
179 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
183 gettimeofday(&end_time
, NULL
);
185 elapsed_time
= (long) timedif(end_time
, start_time
);
186 elapsed_time
/= (long) num_of_tests
;
188 if (elapsed_time
> slowest_time
)
190 slowest_server
= (long)x
;
191 slowest_time
= elapsed_time
;
194 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
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
]));
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);
209 if (server_count
> 1 && slowest_time
> 0)
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);
219 for (x
= 0; x
< server_count
; x
++)
220 memcached_free(servers
[x
]);
227 fprintf(stderr
, "Invalid Analyzer Option provided\n");
232 static void print_server_listing(memcached_st
*memc
, memcached_stat_st
*memc_stat
,
233 memcached_server_st
*server_list
)
236 memcached_return_t rc
;
238 printf("Listing %u Server\n\n", memcached_server_count(memc
));
239 for (x
= 0; x
< memcached_server_count(memc
); x
++)
244 list
= memcached_stat_get_keys(memc
, &memc_stat
[x
], &rc
);
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
++)
250 char *value
= memcached_stat_get_value(memc
, &memc_stat
[x
], *ptr
, &rc
);
252 printf("\t %s: %s\n", *ptr
, value
);
261 static void print_analysis_report(memcached_st
*memc
,
262 memcached_analysis_st
*report
,
263 memcached_server_st
*server_list
)
265 uint32_t server_count
= memcached_server_count(memc
);
267 printf("Memcached Cluster Analysis Report\n\n");
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
);
273 if (server_count
== 1)
275 printf("\nFor a detailed report, you must supply multiple servers.\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
);
296 static void options_parse(int argc
, char *argv
[])
298 memcached_programs_help_st help_options
[]=
308 option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
309 if (option_rv
== -1) break;
314 case OPT_VERBOSE
: /* --verbose or -v */
315 opt_verbose
= OPT_VERBOSE
;
317 case OPT_DEBUG
: /* --debug or -d */
318 opt_verbose
= OPT_DEBUG
;
320 case OPT_VERSION
: /* --version or -V */
321 version_command(PROGRAM_NAME
);
323 case OPT_HELP
: /* --help or -h */
324 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
326 case OPT_SERVERS
: /* --servers or -s */
327 opt_servers
= strdup(optarg
);
329 case OPT_ANALYZE
: /* --analyze or -a */
330 opt_analyze
= OPT_ANALYZE
;
331 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
334 /* getopt_long already printed an error message. */