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