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