Don't use __attribute__((unused))
[m6w6/libmemcached] / libmemcached / stats.c
1 /*
2 */
3
4 #include "common.h"
5
6 static const char *memcached_stat_keys[] = {
7 "pid",
8 "uptime",
9 "time",
10 "version",
11 "pointer_size",
12 "rusage_user",
13 "rusage_system",
14 "curr_items",
15 "total_items",
16 "bytes",
17 "curr_connections",
18 "total_connections",
19 "connection_structures",
20 "cmd_get",
21 "cmd_set",
22 "get_hits",
23 "get_misses",
24 "evictions",
25 "bytes_read",
26 "bytes_written",
27 "limit_maxbytes",
28 "threads",
29 NULL
30 };
31
32 struct local_context
33 {
34 memcached_stat_fn func;
35 void *context;
36 const char *args;
37 };
38
39
40 static memcached_return_t set_data(memcached_stat_st *memc_stat, char *key, char *value)
41 {
42
43 if (strlen(key) < 1)
44 {
45 WATCHPOINT_STRING(key);
46 return MEMCACHED_UNKNOWN_STAT_KEY;
47 }
48 else if (!strcmp("pid", key))
49 {
50 memc_stat->pid= (uint32_t) strtol(value, (char **)NULL, 10);
51 }
52 else if (!strcmp("uptime", key))
53 {
54 memc_stat->uptime= (uint32_t) strtol(value, (char **)NULL, 10);
55 }
56 else if (!strcmp("time", key))
57 {
58 memc_stat->time= (uint32_t) strtol(value, (char **)NULL, 10);
59 }
60 else if (!strcmp("version", key))
61 {
62 memcpy(memc_stat->version, value, strlen(value));
63 memc_stat->version[strlen(value)]= 0;
64 }
65 else if (!strcmp("pointer_size", key))
66 {
67 memc_stat->pointer_size= (uint32_t) strtol(value, (char **)NULL, 10);
68 }
69 else if (!strcmp("rusage_user", key))
70 {
71 char *walk_ptr;
72 for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
73 *walk_ptr= 0;
74 walk_ptr++;
75 memc_stat->rusage_user_seconds= (uint32_t) strtol(value, (char **)NULL, 10);
76 memc_stat->rusage_user_microseconds= (uint32_t) strtol(walk_ptr, (char **)NULL, 10);
77 }
78 else if (!strcmp("rusage_system", key))
79 {
80 char *walk_ptr;
81 for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
82 *walk_ptr= 0;
83 walk_ptr++;
84 memc_stat->rusage_system_seconds= (uint32_t) strtol(value, (char **)NULL, 10);
85 memc_stat->rusage_system_microseconds= (uint32_t) strtol(walk_ptr, (char **)NULL, 10);
86 }
87 else if (!strcmp("curr_items", key))
88 {
89 memc_stat->curr_items= (uint32_t) strtol(value, (char **)NULL, 10);
90 }
91 else if (!strcmp("total_items", key))
92 {
93 memc_stat->total_items= (uint32_t) strtol(value, (char **)NULL, 10);
94 }
95 else if (!strcmp("bytes_read", key))
96 {
97 memc_stat->bytes_read= (uint32_t) strtoll(value, (char **)NULL, 10);
98 }
99 else if (!strcmp("bytes_written", key))
100 {
101 memc_stat->bytes_written= (uint32_t) strtoll(value, (char **)NULL, 10);
102 }
103 else if (!strcmp("bytes", key))
104 {
105 memc_stat->bytes= (uint32_t) strtoll(value, (char **)NULL, 10);
106 }
107 else if (!strcmp("curr_connections", key))
108 {
109 memc_stat->curr_connections= (uint32_t) strtoll(value, (char **)NULL, 10);
110 }
111 else if (!strcmp("total_connections", key))
112 {
113 memc_stat->total_connections= (uint32_t) strtoll(value, (char **)NULL, 10);
114 }
115 else if (!strcmp("connection_structures", key))
116 {
117 memc_stat->connection_structures= (uint32_t) strtol(value, (char **)NULL, 10);
118 }
119 else if (!strcmp("cmd_get", key))
120 {
121 memc_stat->cmd_get= (uint64_t) strtoll(value, (char **)NULL, 10);
122 }
123 else if (!strcmp("cmd_set", key))
124 {
125 memc_stat->cmd_set= (uint64_t) strtoll(value, (char **)NULL, 10);
126 }
127 else if (!strcmp("get_hits", key))
128 {
129 memc_stat->get_hits= (uint64_t) strtoll(value, (char **)NULL, 10);
130 }
131 else if (!strcmp("get_misses", key))
132 {
133 memc_stat->get_misses= (uint64_t)strtoll(value, (char **)NULL, 10);
134 }
135 else if (!strcmp("evictions", key))
136 {
137 memc_stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10);
138 }
139 else if (!strcmp("limit_maxbytes", key))
140 {
141 memc_stat->limit_maxbytes= (uint64_t) strtoll(value, (char **)NULL, 10);
142 }
143 else if (!strcmp("threads", key))
144 {
145 memc_stat->threads= (uint32_t) strtol(value, (char **)NULL, 10);
146 }
147 else if (!(strcmp("delete_misses", key) == 0 ||/* New stats in the 1.3 beta */
148 strcmp("delete_hits", key) == 0 ||/* Just swallow them for now.. */
149 strcmp("incr_misses", key) == 0 ||
150 strcmp("incr_hits", key) == 0 ||
151 strcmp("decr_misses", key) == 0 ||
152 strcmp("decr_hits", key) == 0 ||
153 strcmp("cas_misses", key) == 0 ||
154 strcmp("cas_hits", key) == 0 ||
155 strcmp("cas_badval", key) == 0 ||
156 strcmp("cmd_flush", key) == 0 ||
157 strcmp("accepting_conns", key) == 0 ||
158 strcmp("listen_disabled_num", key) == 0 ||
159 strcmp("conn_yields", key) == 0 ||
160 strcmp("auth_cmds", key) == 0 ||
161 strcmp("auth_errors", key) == 0 ||
162 strcmp("reclaimed", key) == 0))
163 {
164 WATCHPOINT_STRING(key);
165 /* return MEMCACHED_UNKNOWN_STAT_KEY; */
166 return MEMCACHED_SUCCESS;
167 }
168
169 return MEMCACHED_SUCCESS;
170 }
171
172 char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_stat,
173 const char *key, memcached_return_t *error)
174 {
175 char buffer[SMALL_STRING_LEN];
176 int length;
177 char *ret;
178
179 *error= MEMCACHED_SUCCESS;
180
181 if (!memcmp("pid", key, strlen("pid")))
182 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pid);
183 else if (!memcmp("uptime", key, strlen("uptime")))
184 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->uptime);
185 else if (!memcmp("time", key, strlen("time")))
186 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->time);
187 else if (!memcmp("version", key, strlen("version")))
188 length= snprintf(buffer, SMALL_STRING_LEN,"%s", memc_stat->version);
189 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
190 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pointer_size);
191 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
192 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_user_seconds, memc_stat->rusage_user_microseconds);
193 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
194 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_system_seconds, memc_stat->rusage_system_microseconds);
195 else if (!memcmp("curr_items", key, strlen("curr_items")))
196 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_items);
197 else if (!memcmp("total_items", key, strlen("total_items")))
198 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_items);
199 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
200 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_connections);
201 else if (!memcmp("total_connections", key, strlen("total_connections")))
202 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_connections);
203 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
204 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->connection_structures);
205 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
206 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_get);
207 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
208 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_set);
209 else if (!memcmp("get_hits", key, strlen("get_hits")))
210 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_hits);
211 else if (!memcmp("get_misses", key, strlen("get_misses")))
212 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_misses);
213 else if (!memcmp("evictions", key, strlen("evictions")))
214 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->evictions);
215 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
216 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_read);
217 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
218 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_written);
219 else if (!memcmp("bytes", key, strlen("bytes")))
220 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes);
221 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
222 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->limit_maxbytes);
223 else if (!memcmp("threads", key, strlen("threads")))
224 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->threads);
225 else
226 {
227 *error= MEMCACHED_NOTFOUND;
228 return NULL;
229 }
230
231 if (length >= SMALL_STRING_LEN || length < 0)
232 {
233 *error= MEMCACHED_FAILURE;
234 return NULL;
235 }
236
237 ret= libmemcached_malloc(ptr, (size_t) (length + 1));
238 memcpy(ret, buffer, (size_t) length);
239 ret[length]= '\0';
240
241 return ret;
242 }
243
244 static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
245 const char *args,
246 memcached_server_write_instance_st instance,
247 struct local_context *check)
248 {
249 memcached_return_t rc;
250
251 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
252 protocol_binary_request_stats request= {.bytes= {0}};
253 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
254 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT;
255 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
256
257 if (args != NULL)
258 {
259 size_t len= strlen(args);
260
261 rc= memcached_validate_key_length(len, true);
262 unlikely (rc != MEMCACHED_SUCCESS)
263 return rc;
264
265 request.message.header.request.keylen= htons((uint16_t)len);
266 request.message.header.request.bodylen= htonl((uint32_t) len);
267
268 struct libmemcached_io_vector_st vector[]=
269 {
270 { .length= sizeof(request.bytes), .buffer= request.bytes },
271 { .length= len, .buffer= args }
272 };
273
274 if (memcached_vdo(instance, vector, 2, true) != MEMCACHED_SUCCESS)
275 {
276 memcached_io_reset(instance);
277 return MEMCACHED_WRITE_FAILURE;
278 }
279 }
280 else
281 {
282 if (memcached_do(instance, request.bytes,
283 sizeof(request.bytes), true) != MEMCACHED_SUCCESS)
284 {
285 memcached_io_reset(instance);
286 return MEMCACHED_WRITE_FAILURE;
287 }
288 }
289
290 memcached_server_response_decrement(instance);
291 do
292 {
293 rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
294
295 if (rc == MEMCACHED_END)
296 break;
297
298 unlikely (rc != MEMCACHED_SUCCESS)
299 {
300 memcached_io_reset(instance);
301 return rc;
302 }
303
304 if (memc_stat)
305 {
306 unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY)
307 {
308 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
309 WATCHPOINT_ASSERT(0);
310 }
311 }
312
313 if (check && check->func)
314 {
315 size_t key_length= strlen(buffer);
316
317 check->func(instance,
318 buffer, key_length,
319 buffer+key_length+1, strlen(buffer+key_length+1),
320 check->context);
321 }
322 } while (1);
323
324 /* shit... memcached_response will decrement the counter, so I need to
325 ** reset it.. todo: look at this and try to find a better solution.
326 */
327 instance->cursor_active= 0;
328
329 return MEMCACHED_SUCCESS;
330 }
331
332 static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
333 const char *args,
334 memcached_server_write_instance_st instance,
335 struct local_context *check)
336 {
337 memcached_return_t rc;
338 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
339 int send_length;
340
341 if (args)
342 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
343 "stats %s\r\n", args);
344 else
345 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
346 "stats\r\n");
347
348 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
349 return MEMCACHED_WRITE_FAILURE;
350
351 rc= memcached_do(instance, buffer, (size_t)send_length, true);
352 if (rc != MEMCACHED_SUCCESS)
353 goto error;
354
355 while (1)
356 {
357 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
358
359 if (rc == MEMCACHED_STAT)
360 {
361 char *string_ptr, *end_ptr;
362 char *key, *value;
363
364 string_ptr= buffer;
365 string_ptr+= 5; /* Move past STAT */
366 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
367 key= string_ptr;
368 key[(size_t)(end_ptr-string_ptr)]= 0;
369
370 string_ptr= end_ptr + 1;
371 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++);
372 value= string_ptr;
373 value[(size_t)(end_ptr-string_ptr)]= 0;
374 string_ptr= end_ptr + 2;
375 if (memc_stat)
376 {
377 unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY)
378 {
379 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
380 WATCHPOINT_ASSERT(0);
381 }
382 }
383
384 if (check && check->func)
385 {
386 check->func(instance,
387 key, strlen(key),
388 value, strlen(value),
389 check->context);
390 }
391 }
392 else
393 break;
394 }
395
396 error:
397 if (rc == MEMCACHED_END)
398 return MEMCACHED_SUCCESS;
399 else
400 return rc;
401 }
402
403 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return_t *error)
404 {
405 memcached_return_t rc;
406 memcached_stat_st *stats;
407
408 unlikely (ptr->flags.use_udp)
409 {
410 *error= MEMCACHED_NOT_SUPPORTED;
411 return NULL;
412 }
413
414 stats= libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(memcached_stat_st));
415
416 if (! stats)
417 {
418 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
419 return NULL;
420 }
421
422 rc= MEMCACHED_SUCCESS;
423 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
424 {
425 memcached_return_t temp_return;
426 memcached_server_write_instance_st instance;
427 memcached_stat_st *stat_instance;
428
429 stat_instance= stats + x;
430
431 stat_instance->root= ptr;
432
433 instance= memcached_server_instance_fetch(ptr, x);
434
435 if (ptr->flags.binary_protocol)
436 {
437 temp_return= binary_stats_fetch(stat_instance, args, instance, NULL);
438 }
439 else
440 {
441 temp_return= ascii_stats_fetch(stat_instance, args, instance, NULL);
442 }
443
444 if (temp_return != MEMCACHED_SUCCESS)
445 rc= MEMCACHED_SOME_ERRORS;
446 }
447
448 *error= rc;
449 return stats;
450 }
451
452 memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char *args,
453 const char *hostname, in_port_t port)
454 {
455 memcached_return_t rc;
456 memcached_st memc;
457 memcached_st *memc_ptr;
458 memcached_server_write_instance_st instance;
459
460 memset(memc_stat, 0, sizeof(memcached_stat_st));
461
462 memc_ptr= memcached_create(&memc);
463 WATCHPOINT_ASSERT(memc_ptr);
464
465 memcached_server_add(&memc, hostname, port);
466
467 instance= memcached_server_instance_fetch(memc_ptr, 0);
468
469 if (memc.flags.binary_protocol)
470 {
471 rc= binary_stats_fetch(memc_stat, args, instance, NULL);
472 }
473 else
474 {
475 rc= ascii_stats_fetch(memc_stat, args, instance, NULL);
476 }
477
478 memcached_free(&memc);
479
480 return rc;
481 }
482
483 /*
484 We make a copy of the keys since at some point in the not so distant future
485 we will add support for "found" keys.
486 */
487 char ** memcached_stat_get_keys(const memcached_st *ptr,
488 memcached_stat_st *memc_stat,
489 memcached_return_t *error)
490 {
491 char **list;
492 size_t length= sizeof(memcached_stat_keys);
493
494 (void)memc_stat;
495
496 list= libmemcached_malloc(ptr, length);
497
498 if (! list)
499 {
500 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
501 return NULL;
502 }
503
504 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
505
506 *error= MEMCACHED_SUCCESS;
507
508 return list;
509 }
510
511 void memcached_stat_free(const memcached_st *ptr, memcached_stat_st *memc_stat)
512 {
513 if (memc_stat == NULL)
514 {
515 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
516 return;
517 }
518
519 if (memc_stat->root)
520 {
521 libmemcached_free(memc_stat->root, memc_stat);
522 }
523 else if (ptr)
524 {
525 libmemcached_free(ptr, memc_stat);
526 }
527 else
528 {
529 free(memc_stat);
530 }
531 }
532
533 static memcached_return_t call_stat_fn(memcached_st *ptr,
534 memcached_server_write_instance_st instance,
535 void *context)
536 {
537 memcached_return_t rc;
538 struct local_context *check= (struct local_context *)context;
539
540 if (ptr->flags.binary_protocol)
541 {
542 rc= binary_stats_fetch(NULL, check->args, instance, check);
543 }
544 else
545 {
546 rc= ascii_stats_fetch(NULL, check->args, instance, check);
547 }
548
549 return rc;
550 }
551
552 memcached_return_t memcached_stat_execute(memcached_st *memc, const char *args, memcached_stat_fn func, void *context)
553 {
554 memcached_version(memc);
555
556 struct local_context check= { .func= func, .context= context, .args= args };
557
558 return memcached_server_execute(memc, call_stat_fn, (void *)&check);
559 }