src/bin: apply clang-format
[m6w6/libmemcached] / src / bin / memstat.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <cstdio>
19 #include <cstring>
20 #include <ctime>
21 #include <iostream>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/types.h>
29
30 #include "libmemcached-1.0/memcached.h"
31
32 #include "client_options.h"
33 #include "utilities.h"
34
35 #define PROGRAM_NAME "memstat"
36 #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster."
37
38 /* Prototypes */
39 static void options_parse(int argc, char *argv[]);
40 static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat);
41 static void print_analysis_report(memcached_st *memc, memcached_analysis_st *report);
42
43 static bool opt_binary = false;
44 static bool opt_verbose = false;
45 static bool opt_server_version = false;
46 static bool opt_analyze = false;
47 static char *opt_servers = NULL;
48 static char *stat_args = NULL;
49 static char *analyze_mode = NULL;
50 static char *opt_username;
51 static char *opt_passwd;
52
53 static struct option long_options[] = {
54 {(OPTIONSTRING) "args", required_argument, NULL, OPT_STAT_ARGS},
55 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
56 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
57 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
58 {(OPTIONSTRING) "verbose", no_argument, NULL, OPT_VERBOSE},
59 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
60 {(OPTIONSTRING) "debug", no_argument, NULL, OPT_DEBUG},
61 {(OPTIONSTRING) "server-version", no_argument, NULL, OPT_SERVER_VERSION},
62 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
63 {(OPTIONSTRING) "analyze", optional_argument, NULL, OPT_ANALYZE},
64 {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME},
65 {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD},
66 {0, 0, 0, 0},
67 };
68
69 static memcached_return_t stat_printer(const memcached_instance_st *instance, const char *key,
70 size_t key_length, const char *value, size_t value_length,
71 void *context) {
72 static const memcached_instance_st *last = NULL;
73 (void) context;
74
75 if (last != instance) {
76 printf("Server: %s (%u)\n", memcached_server_name(instance),
77 (uint32_t) memcached_server_port(instance));
78 last = instance;
79 }
80
81 printf("\t %.*s: %.*s\n", (int) key_length, key, (int) value_length, value);
82
83 return MEMCACHED_SUCCESS;
84 }
85
86 static memcached_return_t server_print_callback(const memcached_st *,
87 const memcached_instance_st *instance, void *) {
88 std::cerr << memcached_server_name(instance) << ":" << memcached_server_port(instance) << " "
89 << int(memcached_server_major_version(instance)) << "."
90 << int(memcached_server_minor_version(instance)) << "."
91 << int(memcached_server_micro_version(instance)) << std::endl;
92
93 return MEMCACHED_SUCCESS;
94 }
95
96 int main(int argc, char *argv[]) {
97 options_parse(argc, argv);
98 initialize_sockets();
99
100 if (opt_servers == NULL) {
101 char *temp;
102 if ((temp = getenv("MEMCACHED_SERVERS"))) {
103 opt_servers = strdup(temp);
104 }
105
106 if (opt_servers == NULL) {
107 std::cerr << "No Servers provided" << std::endl;
108 return EXIT_FAILURE;
109 }
110 }
111
112 memcached_server_st *servers = memcached_servers_parse(opt_servers);
113 if (servers == NULL or memcached_server_list_count(servers) == 0) {
114 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
115 return EXIT_FAILURE;
116 }
117
118 if (opt_servers) {
119 free(opt_servers);
120 }
121
122 memcached_st *memc = memcached_create(NULL);
123 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
124
125 memcached_return_t rc = memcached_server_push(memc, servers);
126 memcached_server_list_free(servers);
127
128 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) {
129 memcached_free(memc);
130 std::cerr << "--username was supplied, but binary was not built with SASL support."
131 << std::endl;
132 return EXIT_FAILURE;
133 }
134
135 if (opt_username) {
136 memcached_return_t ret;
137 if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) {
138 std::cerr << memcached_last_error_message(memc) << std::endl;
139 memcached_free(memc);
140 return EXIT_FAILURE;
141 }
142 }
143
144 if (rc != MEMCACHED_SUCCESS and rc != MEMCACHED_SOME_ERRORS) {
145 printf("Failure to communicate with servers (%s)\n", memcached_strerror(memc, rc));
146 exit(EXIT_FAILURE);
147 }
148
149 if (opt_server_version) {
150 if (memcached_failed(memcached_version(memc))) {
151 std::cerr << "Unable to obtain server version" << std::endl;
152 exit(EXIT_FAILURE);
153 }
154
155 memcached_server_fn callbacks[1];
156 callbacks[0] = server_print_callback;
157 memcached_server_cursor(memc, callbacks, NULL, 1);
158 } else if (opt_analyze) {
159 memcached_stat_st *memc_stat = memcached_stat(memc, NULL, &rc);
160
161 if (memc_stat == NULL || rc != MEMCACHED_SUCCESS) {
162 std::cerr << memcached_last_error_message(memc) << std::endl;
163 exit(EXIT_FAILURE);
164 }
165
166 run_analyzer(memc, memc_stat);
167
168 memcached_stat_free(memc, memc_stat);
169 } else {
170 rc = memcached_stat_execute(memc, stat_args, stat_printer, NULL);
171 }
172
173 memcached_free(memc);
174
175 return rc == MEMCACHED_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
176 }
177
178 static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) {
179 memcached_return_t rc;
180
181 if (analyze_mode == NULL) {
182 memcached_analysis_st *report;
183 report = memcached_analyze(memc, memc_stat, &rc);
184 if (rc != MEMCACHED_SUCCESS || report == NULL) {
185 printf("Failure to analyze servers (%s)\n", memcached_strerror(memc, rc));
186 exit(1);
187 }
188 print_analysis_report(memc, report);
189 free(report);
190 } else if (strcmp(analyze_mode, "latency") == 0) {
191 uint32_t flags, server_count = memcached_server_count(memc);
192 uint32_t num_of_tests = 32;
193 const char *test_key = "libmemcached_test_key";
194
195 memcached_st **servers =
196 static_cast<memcached_st **>(malloc(sizeof(memcached_st *) * server_count));
197 if (servers == NULL) {
198 fprintf(stderr, "Failed to allocate memory\n");
199 return;
200 }
201
202 for (uint32_t x = 0; x < server_count; x++) {
203 const memcached_instance_st *instance = memcached_server_instance_by_position(memc, x);
204
205 if ((servers[x] = memcached_create(NULL)) == NULL) {
206 fprintf(stderr, "Failed to memcached_create()\n");
207 if (x > 0) {
208 memcached_free(servers[0]);
209 }
210 x--;
211
212 for (; x > 0; x--) {
213 memcached_free(servers[x]);
214 }
215
216 free(servers);
217
218 return;
219 }
220 memcached_server_add(servers[x], memcached_server_name(instance),
221 memcached_server_port(instance));
222 }
223
224 printf("Network Latency Test:\n\n");
225 struct timeval start_time, end_time;
226 uint32_t slowest_server = 0;
227 long elapsed_time, slowest_time = 0;
228
229 for (uint32_t x = 0; x < server_count; x++) {
230 const memcached_instance_st *instance = memcached_server_instance_by_position(memc, x);
231 gettimeofday(&start_time, NULL);
232
233 for (uint32_t y = 0; y < num_of_tests; y++) {
234 size_t vlen;
235 char *val = memcached_get(servers[x], test_key, strlen(test_key), &vlen, &flags, &rc);
236 if (rc != MEMCACHED_NOTFOUND and rc != MEMCACHED_SUCCESS) {
237 break;
238 }
239 free(val);
240 }
241 gettimeofday(&end_time, NULL);
242
243 elapsed_time = (long) timedif(end_time, start_time);
244 elapsed_time /= (long) num_of_tests;
245
246 if (elapsed_time > slowest_time) {
247 slowest_server = x;
248 slowest_time = elapsed_time;
249 }
250
251 if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS) {
252 printf("\t %s (%d) => failed to reach the server\n", memcached_server_name(instance),
253 memcached_server_port(instance));
254 } else {
255 printf("\t %s (%d) => %ld.%ld seconds\n", memcached_server_name(instance),
256 memcached_server_port(instance), elapsed_time / 1000, elapsed_time % 1000);
257 }
258 }
259
260 if (server_count > 1 && slowest_time > 0) {
261 const memcached_instance_st *slowest =
262 memcached_server_instance_by_position(memc, slowest_server);
263
264 printf("---\n");
265 printf("Slowest Server: %s (%d) => %ld.%ld seconds\n", memcached_server_name(slowest),
266 memcached_server_port(slowest), slowest_time / 1000, slowest_time % 1000);
267 }
268 printf("\n");
269
270 for (uint32_t x = 0; x < server_count; x++) {
271 memcached_free(servers[x]);
272 }
273
274 free(servers);
275 free(analyze_mode);
276 } else {
277 fprintf(stderr, "Invalid Analyzer Option provided\n");
278 free(analyze_mode);
279 }
280 }
281
282 static void print_analysis_report(memcached_st *memc, memcached_analysis_st *report)
283
284 {
285 uint32_t server_count = memcached_server_count(memc);
286 const memcached_instance_st *most_consumed_server =
287 memcached_server_instance_by_position(memc, report->most_consumed_server);
288 const memcached_instance_st *least_free_server =
289 memcached_server_instance_by_position(memc, report->least_free_server);
290 const memcached_instance_st *oldest_server =
291 memcached_server_instance_by_position(memc, report->oldest_server);
292
293 printf("Memcached Cluster Analysis Report\n\n");
294
295 printf("\tNumber of Servers Analyzed : %u\n", server_count);
296 printf("\tAverage Item Size (incl/overhead) : %u bytes\n", report->average_item_size);
297
298 if (server_count == 1) {
299 printf("\nFor a detailed report, you must supply multiple servers.\n");
300 return;
301 }
302
303 printf("\n");
304 printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
305 memcached_server_name(most_consumed_server),
306 (uint32_t) memcached_server_port(most_consumed_server),
307 (unsigned long long) report->most_used_bytes);
308 printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
309 memcached_server_name(least_free_server),
310 (uint32_t) memcached_server_port(least_free_server),
311 (unsigned long long) report->least_remaining_bytes);
312 printf("\tNode with longest uptime : %s:%u (%us)\n",
313 memcached_server_name(oldest_server), (uint32_t) memcached_server_port(oldest_server),
314 report->longest_uptime);
315 printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio);
316 printf("\n");
317 }
318
319 static void options_parse(int argc, char *argv[]) {
320 memcached_programs_help_st help_options[] = {
321 {0},
322 };
323
324 int option_index = 0;
325
326 bool opt_version = false;
327 bool opt_help = false;
328 while (1) {
329 int option_rv = getopt_long(argc, argv, "Vhvds:a", long_options, &option_index);
330
331 if (option_rv == -1)
332 break;
333
334 switch (option_rv) {
335 case 0: break;
336
337 case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = true; break;
338
339 case OPT_DEBUG: /* --debug or -d */ opt_verbose = true; break;
340
341 case OPT_BINARY: opt_binary = true; break;
342
343 case OPT_SERVER_VERSION: opt_server_version = true; break;
344
345 case OPT_VERSION: /* --version or -V */ opt_version = true; break;
346
347 case OPT_HELP: /* --help or -h */ opt_help = true; break;
348
349 case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break;
350
351 case OPT_STAT_ARGS: stat_args = strdup(optarg); break;
352
353 case OPT_ANALYZE: /* --analyze or -a */
354 opt_analyze = true;
355 analyze_mode = (optarg) ? strdup(optarg) : NULL;
356 break;
357
358 case OPT_QUIET: close_stdio(); break;
359
360 case OPT_USERNAME:
361 opt_username = optarg;
362 opt_binary = true;
363 break;
364
365 case OPT_PASSWD: opt_passwd = optarg; break;
366
367 case '?':
368 /* getopt_long already printed an error message. */
369 exit(1);
370 default: abort();
371 }
372 }
373
374 if (opt_version) {
375 version_command(PROGRAM_NAME);
376 exit(EXIT_SUCCESS);
377 }
378
379 if (opt_help) {
380 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
381 exit(EXIT_SUCCESS);
382 }
383 }