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.
17 #include <sys/types.h>
19 #include <sys/types.h>
25 #include <libmemcached/memcached.h>
27 #include "client_options.h"
28 #include "utilities.h"
30 #define PROGRAM_NAME "memstat"
31 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
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
);
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
;
45 static struct option long_options
[]=
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
},
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
,
63 static memcached_server_instance_st last
= NULL
;
68 printf("Server: %s (%u)\n", memcached_server_name(instance
),
69 (uint32_t)memcached_server_port(instance
));
73 printf("\t %.*s: %.*s\n", (int)key_length
, key
, (int)value_length
, value
);
75 return MEMCACHED_SUCCESS
;
78 int main(int argc
, char *argv
[])
80 memcached_return_t rc
;
82 memcached_server_st
*servers
;
84 options_parse(argc
, argv
);
91 if ((temp
= getenv("MEMCACHED_SERVERS")))
92 opt_servers
= strdup(temp
);
95 fprintf(stderr
, "No Servers provided\n\n");
96 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, 0);
101 memc
= memcached_create(NULL
);
103 servers
= memcached_servers_parse(opt_servers
);
104 rc
= memcached_server_push(memc
, servers
);
105 memcached_server_list_free(servers
);
107 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_SOME_ERRORS
)
109 printf("Failure to communicate with servers (%s)\n",
110 memcached_strerror(memc
, rc
));
116 memcached_stat_st
*memc_stat
;
118 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
123 run_analyzer(memc
, memc_stat
);
125 memcached_stat_free(memc
, memc_stat
);
129 rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
134 memcached_free(memc
);
136 return rc
== MEMCACHED_SUCCESS
? 0: -1;
139 static void run_analyzer(memcached_st
*memc
, memcached_stat_st
*memc_stat
)
141 memcached_return_t rc
;
143 if (analyze_mode
== NULL
)
145 memcached_analysis_st
*report
;
146 report
= memcached_analyze(memc
, memc_stat
, &rc
);
147 if (rc
!= MEMCACHED_SUCCESS
|| report
== NULL
)
149 printf("Failure to analyze servers (%s)\n",
150 memcached_strerror(memc
, rc
));
153 print_analysis_report(memc
, report
);
156 else if (strcmp(analyze_mode
, "latency") == 0)
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";
163 servers
= malloc(sizeof(memcached_st
*) * server_count
);
166 fprintf(stderr
, "Failed to allocate memory\n");
170 for (uint32_t x
= 0; x
< server_count
; x
++)
172 memcached_server_instance_st instance
=
173 memcached_server_instance_by_position(memc
, x
);
175 if ((servers
[x
]= memcached_create(NULL
)) == NULL
)
177 fprintf(stderr
, "Failed to memcached_create()\n");
179 memcached_free(servers
[0]);
182 memcached_free(servers
[x
]);
187 memcached_server_add(servers
[x
],
188 memcached_server_name(instance
),
189 memcached_server_port(instance
));
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;
197 for (uint32_t x
= 0; x
< server_count
; x
++)
199 memcached_server_instance_st instance
=
200 memcached_server_instance_by_position(memc
, x
);
201 gettimeofday(&start_time
, NULL
);
203 for (uint32_t y
= 0; y
< num_of_tests
; y
++)
206 char *val
= memcached_get(servers
[x
], test_key
, strlen(test_key
),
208 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
212 gettimeofday(&end_time
, NULL
);
214 elapsed_time
= (long) timedif(end_time
, start_time
);
215 elapsed_time
/= (long) num_of_tests
;
217 if (elapsed_time
> slowest_time
)
220 slowest_time
= elapsed_time
;
223 if (rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_SUCCESS
)
225 printf("\t %s (%d) => failed to reach the server\n",
226 memcached_server_name(instance
),
227 memcached_server_port(instance
));
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);
238 if (server_count
> 1 && slowest_time
> 0)
240 memcached_server_instance_st slowest
=
241 memcached_server_instance_by_position(memc
, slowest_server
);
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);
251 for (uint32_t x
= 0; x
< server_count
; x
++)
252 memcached_free(servers
[x
]);
259 fprintf(stderr
, "Invalid Analyzer Option provided\n");
264 static void print_analysis_report(memcached_st
*memc
,
265 memcached_analysis_st
*report
)
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
);
273 printf("Memcached Cluster Analysis Report\n\n");
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
);
279 if (server_count
== 1)
281 printf("\nFor a detailed report, you must supply multiple servers.\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
);
302 static void options_parse(int argc
, char *argv
[])
304 memcached_programs_help_st help_options
[]=
314 option_rv
= getopt_long(argc
, argv
, "Vhvds:a", long_options
, &option_index
);
315 if (option_rv
== -1) break;
320 case OPT_VERBOSE
: /* --verbose or -v */
321 opt_verbose
= OPT_VERBOSE
;
323 case OPT_DEBUG
: /* --debug or -d */
324 opt_verbose
= OPT_DEBUG
;
326 case OPT_VERSION
: /* --version or -V */
327 version_command(PROGRAM_NAME
);
329 case OPT_HELP
: /* --help or -h */
330 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
332 case OPT_SERVERS
: /* --servers or -s */
333 opt_servers
= strdup(optarg
);
335 case OPT_ANALYZE
: /* --analyze or -a */
336 opt_analyze
= OPT_ANALYZE
;
337 analyze_mode
= (optarg
) ? strdup(optarg
) : NULL
;
340 /* getopt_long already printed an error message. */