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.
25 #include <sys/types.h>
26 #include <sys/types.h>
28 #include <libmemcached/memcached.h>
30 #include "client_options.h"
31 #include "utilities.h"
33 #define PROGRAM_NAME "memstat"
34 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
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
);
42 static int opt_verbose
= 0;
43 static int opt_displayflag
= 0;
44 static int opt_analyze
= 0;
45 static char *opt_servers
= NULL
;
46 static char *stat_args
= NULL
;
47 static char *analyze_mode
= NULL
;
49 static struct option long_options
[]=
51 {(OPTIONSTRING
)"args", required_argument
, NULL
, OPT_STAT_ARGS
},
52 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
53 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
54 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
55 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
56 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
57 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
58 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
59 {(OPTIONSTRING
)"analyze", optional_argument
, NULL
, OPT_ANALYZE
},
64 static memcached_return_t
stat_printer(memcached_server_instance_st instance
,
65 const char *key
, size_t key_length
,
66 const char *value
, size_t value_length
,
69 static memcached_server_instance_st last
= NULL
;
74 printf("Server: %s (%u)\n", memcached_server_name(instance
),
75 (uint32_t)memcached_server_port(instance
));
79 printf("\t %.*s: %.*s\n", (int)key_length
, key
, (int)value_length
, value
);
81 return MEMCACHED_SUCCESS
;
84 int main(int argc
, char *argv
[])
86 options_parse(argc
, argv
);
89 if (opt_servers
== false)
92 if ((temp
= getenv("MEMCACHED_SERVERS")))
94 opt_servers
= strdup(temp
);
98 std::cerr
<< "No Servers provided" << std::endl
;
103 memcached_st
*memc
= memcached_create(NULL
);
105 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
108 memcached_return_t rc
= memcached_server_push(memc
, servers
);
109 memcached_server_list_free(servers
);
111 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
113 printf("Failure to communicate with servers (%s)\n",
114 memcached_strerror(memc
, rc
));
120 memcached_stat_st
*memc_stat
;
122 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
127 run_analyzer(memc
, memc_stat
);
129 memcached_stat_free(memc
, memc_stat
);
133 rc
= memcached_stat_execute(memc
, stat_args
, stat_printer
, NULL
);
136 memcached_free(memc
);
138 return rc
== MEMCACHED_SUCCESS
? EXIT_SUCCESS
: EXIT_FAILURE
;
141 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
)
143 memcached_return_t rc
;
145 if (analyze_mode
== NULL
)
147 memcached_analysis_st
*report
;
148 report
= memcached_analyze(memc
, memc_stat
, &rc
);
149 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
151 printf("Failure to analyze servers (%s)\n",
152 memcached_strerror(memc
, rc
));
155 print_analysis_report(memc
, report
);
158 else if (strcmp(analyze_mode
, "latency") == 0)
160 uint32_t flags
, server_count
= memcached_server_count(memc
);
161 uint32_t num_of_tests
= 32;
162 const char *test_key
= "libmemcached_test_key";
164 memcached_st
**servers
;
165 servers
= static_cast<memcached_st
**>(malloc(sizeof(memcached_st
*) * server_count
));
168 fprintf(stderr
, "Failed to allocate memory\n");
172 for (uint32_t x
= 0; x
< server_count
; x
++)
174 memcached_server_instance_st instance
=
175 memcached_server_instance_by_position(memc
, x
);
177 if ((servers
[x
]= memcached_create(NULL
)) == NULL
)
179 fprintf(stderr
, "Failed to memcached_create()\n");
181 memcached_free(servers
[0]);
185 memcached_free(servers
[x
]);
190 memcached_server_add(servers
[x
],
191 memcached_server_name(instance
),
192 memcached_server_port(instance
));
195 printf("Network Latency Test:\n\n");
196 struct timeval start_time
, end_time
;
197 uint32_t slowest_server
= 0;
198 long elapsed_time
, slowest_time
= 0;
200 for (uint32_t x
= 0; x
< server_count
; x
++)
202 memcached_server_instance_st instance
=
203 memcached_server_instance_by_position(memc
, x
);
204 gettimeofday(&start_time
, NULL
);
206 for (uint32_t y
= 0; y
< num_of_tests
; y
++)
209 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
),
211 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
215 gettimeofday(&end_time
, NULL
);
217 elapsed_time
= (long) timedif(end_time
, start_time
);
218 elapsed_time
/= (long) num_of_tests
;
220 if (elapsed_time
> slowest_time
)
223 slowest_time
= elapsed_time
;
226 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
228 printf("\t %s (%d) => failed to reach the server\n",
229 memcached_server_name(instance
),
230 memcached_server_port(instance
));
234 printf("\t %s (%d) => %ld.%ld seconds\n",
235 memcached_server_name(instance
),
236 memcached_server_port(instance
),
237 elapsed_time
/ 1000, elapsed_time
% 1000);
241 if (server_count
> 1 && slowest_time
> 0)
243 memcached_server_instance_st slowest
=
244 memcached_server_instance_by_position(memc
, slowest_server
);
247 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n",
248 memcached_server_name(slowest
),
249 memcached_server_port(slowest
),
250 slowest_time
/ 1000, slowest_time
% 1000);
254 for (uint32_t x
= 0; x
< server_count
; x
++)
255 memcached_free(servers
[x
]);
262 fprintf(stderr
, "Invalid Analyzer Option provided\n");
267 static void print_analysis_report(memcached_st
*memc
,
268 memcached_analysis_st
*report
)
271 uint32_t server_count
= memcached_server_count(memc
);
272 memcached_server_instance_st most_consumed_server
= memcached_server_instance_by_position(memc
, report
->most_consumed_server
);
273 memcached_server_instance_st least_free_server
= memcached_server_instance_by_position(memc
, report
->least_free_server
);
274 memcached_server_instance_st oldest_server
= memcached_server_instance_by_position(memc
, report
->oldest_server
);
276 printf("Memcached Cluster Analysis Report\n\n");
278 printf("\tNumber of Servers Analyzed : %u\n", server_count
);
279 printf("\tAverage Item Size (incl/overhead) : %u bytes\n",
280 report
->average_item_size
);
282 if (server_count
== 1)
284 printf("\nFor a detailed report, you must supply multiple servers.\n");
289 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
290 memcached_server_name(most_consumed_server
),
291 (uint32_t)memcached_server_port(most_consumed_server
),
292 (unsigned long long)report
->most_used_bytes
);
293 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
294 memcached_server_name(least_free_server
),
295 (uint32_t)memcached_server_port(least_free_server
),
296 (unsigned long long)report
->least_remaining_bytes
);
297 printf("\tNode with longest uptime : %s:%u (%us)\n",
298 memcached_server_name(oldest_server
),
299 (uint32_t)memcached_server_port(oldest_server
),
300 report
->longest_uptime
);
301 printf("\tPool-wide Hit Ratio : %1.f%%\n", report
->pool_hit_ratio
);
305 static void options_parse(int argc
, char *argv
[])
307 memcached_programs_help_st help_options
[]=
314 bool opt_version
= false;
315 bool opt_help
= false;
318 int option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
328 case OPT_VERBOSE
: /* --verbose or -v */
329 opt_verbose
= OPT_VERBOSE
;
332 case OPT_DEBUG
: /* --debug or -d */
333 opt_verbose
= OPT_DEBUG
;
336 case OPT_VERSION
: /* --version or -V */
340 case OPT_HELP
: /* --help or -h */
344 case OPT_SERVERS
: /* --servers or -s */
345 opt_servers
= strdup(optarg
);
349 stat_args
= strdup(optarg
);
352 case OPT_ANALYZE
: /* --analyze or -a */
353 opt_analyze
= OPT_ANALYZE
;
354 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
362 /* getopt_long already printed an error message. */
371 version_command(PROGRAM_NAME
);
377 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);