Fix allocation on stat so that it uses malloc() since a user will need to use free...
[awesomized/libmemcached] / libmemcached / stats.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libmemcached/common.h>
38
39 static const char *memcached_stat_keys[] = {
40 "pid",
41 "uptime",
42 "time",
43 "version",
44 "pointer_size",
45 "rusage_user",
46 "rusage_system",
47 "curr_items",
48 "total_items",
49 "bytes",
50 "curr_connections",
51 "total_connections",
52 "connection_structures",
53 "cmd_get",
54 "cmd_set",
55 "get_hits",
56 "get_misses",
57 "evictions",
58 "bytes_read",
59 "bytes_written",
60 "limit_maxbytes",
61 "threads",
62 NULL
63 };
64
65 struct local_context
66 {
67 memcached_stat_fn func;
68 void *context;
69 const char *args;
70 const size_t args_length;
71
72 local_context(memcached_stat_fn func_arg,
73 void *context_arg,
74 const char *args_arg,
75 const size_t args_length_arg) :
76 func(func_arg),
77 context(context_arg),
78 args(args_arg),
79 args_length(args_length_arg)
80 { }
81 };
82
83
84 static memcached_return_t set_data(memcached_stat_st *memc_stat, const char *key, const char *value)
85 {
86
87 if (strlen(key) < 1)
88 {
89 WATCHPOINT_STRING(key);
90 return MEMCACHED_UNKNOWN_STAT_KEY;
91 }
92 else if (strcmp("pid", key) == 0)
93 {
94 int64_t temp= strtoll(value, (char **)NULL, 10);
95
96 if (temp <= INT32_MAX and ( sizeof(pid_t) == sizeof(int32_t) ))
97 {
98 memc_stat->pid= pid_t(temp);
99 }
100 else if (temp > -1)
101 {
102 memc_stat->pid= pid_t(temp);
103 }
104 else
105 {
106 // If we got a value less then -1 then something went wrong in the
107 // protocol
108 }
109 }
110 else if (not strcmp("uptime", key))
111 {
112 memc_stat->uptime= strtoul(value, (char **)NULL, 10);
113 }
114 else if (not strcmp("time", key))
115 {
116 memc_stat->time= strtoul(value, (char **)NULL, 10);
117 }
118 else if (not strcmp("version", key))
119 {
120 memcpy(memc_stat->version, value, strlen(value));
121 memc_stat->version[strlen(value)]= 0;
122 }
123 else if (not strcmp("pointer_size", key))
124 {
125 memc_stat->pointer_size= strtoul(value, (char **)NULL, 10);
126 }
127 else if (not strcmp("rusage_user", key))
128 {
129 char *walk_ptr;
130 for (walk_ptr= (char*)value; (!ispunct(*walk_ptr)); walk_ptr++) {};
131 *walk_ptr= 0;
132 walk_ptr++;
133 memc_stat->rusage_user_seconds= strtoul(value, (char **)NULL, 10);
134 memc_stat->rusage_user_microseconds= strtoul(walk_ptr, (char **)NULL, 10);
135 }
136 else if (not strcmp("rusage_system", key))
137 {
138 char *walk_ptr;
139 for (walk_ptr= (char*)value; (!ispunct(*walk_ptr)); walk_ptr++) {};
140 *walk_ptr= 0;
141 walk_ptr++;
142 memc_stat->rusage_system_seconds= strtoul(value, (char **)NULL, 10);
143 memc_stat->rusage_system_microseconds= strtoul(walk_ptr, (char **)NULL, 10);
144 }
145 else if (not strcmp("curr_items", key))
146 {
147 memc_stat->curr_items= strtoul(value, (char **)NULL, 10);
148 }
149 else if (not strcmp("total_items", key))
150 {
151 memc_stat->total_items= strtoul(value, (char **)NULL, 10);
152 }
153 else if (not strcmp("bytes_read", key))
154 {
155 memc_stat->bytes_read= strtoull(value, (char **)NULL, 10);
156 }
157 else if (not strcmp("bytes_written", key))
158 {
159 memc_stat->bytes_written= strtoull(value, (char **)NULL, 10);
160 }
161 else if (not strcmp("bytes", key))
162 {
163 memc_stat->bytes= strtoull(value, (char **)NULL, 10);
164 }
165 else if (not strcmp("curr_connections", key))
166 {
167 memc_stat->curr_connections= strtoull(value, (char **)NULL, 10);
168 }
169 else if (not strcmp("total_connections", key))
170 {
171 memc_stat->total_connections= strtoull(value, (char **)NULL, 10);
172 }
173 else if (not strcmp("connection_structures", key))
174 {
175 memc_stat->connection_structures= strtoul(value, (char **)NULL, 10);
176 }
177 else if (not strcmp("cmd_get", key))
178 {
179 memc_stat->cmd_get= strtoull(value, (char **)NULL, 10);
180 }
181 else if (not strcmp("cmd_set", key))
182 {
183 memc_stat->cmd_set= strtoull(value, (char **)NULL, 10);
184 }
185 else if (not strcmp("get_hits", key))
186 {
187 memc_stat->get_hits= strtoull(value, (char **)NULL, 10);
188 }
189 else if (not strcmp("get_misses", key))
190 {
191 memc_stat->get_misses= strtoull(value, (char **)NULL, 10);
192 }
193 else if (not strcmp("evictions", key))
194 {
195 memc_stat->evictions= strtoull(value, (char **)NULL, 10);
196 }
197 else if (not strcmp("limit_maxbytes", key))
198 {
199 memc_stat->limit_maxbytes= strtoull(value, (char **)NULL, 10);
200 }
201 else if (not strcmp("threads", key))
202 {
203 memc_stat->threads= strtoul(value, (char **)NULL, 10);
204 }
205 else if (not (strcmp("delete_misses", key) == 0 or /* New stats in the 1.3 beta */
206 strcmp("delete_hits", key) == 0 or /* Just swallow them for now.. */
207 strcmp("incr_misses", key) == 0 or
208 strcmp("incr_hits", key) == 0 or
209 strcmp("decr_misses", key) == 0 or
210 strcmp("decr_hits", key) == 0 or
211 strcmp("cas_misses", key) == 0 or
212 strcmp("cas_hits", key) == 0 or
213 strcmp("cas_badval", key) == 0 or
214 strcmp("cmd_flush", key) == 0 or
215 strcmp("accepting_conns", key) == 0 or
216 strcmp("listen_disabled_num", key) == 0 or
217 strcmp("conn_yields", key) == 0 or
218 strcmp("auth_cmds", key) == 0 or
219 strcmp("auth_errors", key) == 0 or
220 strcmp("reclaimed", key) == 0))
221 {
222 WATCHPOINT_STRING(key);
223 /* return MEMCACHED_UNKNOWN_STAT_KEY; */
224 return MEMCACHED_SUCCESS;
225 }
226
227 return MEMCACHED_SUCCESS;
228 }
229
230 char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_stat,
231 const char *key, memcached_return_t *error)
232 {
233 char buffer[SMALL_STRING_LEN];
234 int length;
235
236 *error= MEMCACHED_SUCCESS;
237
238 if (memcmp("pid", key, sizeof("pid") -1) == 0)
239 {
240 length= snprintf(buffer, SMALL_STRING_LEN,"%lld", (signed long long)memc_stat->pid);
241 }
242 else if (not memcmp("uptime", key, sizeof("uptime") -1))
243 {
244 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->uptime);
245 }
246 else if (not memcmp("time", key, sizeof("time") -1))
247 {
248 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->time);
249 }
250 else if (not memcmp("version", key, sizeof("version") -1))
251 {
252 length= snprintf(buffer, SMALL_STRING_LEN,"%s", memc_stat->version);
253 }
254 else if (not memcmp("pointer_size", key, sizeof("pointer_size") -1))
255 {
256 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->pointer_size);
257 }
258 else if (not memcmp("rusage_user", key, sizeof("rusage_user") -1))
259 {
260 length= snprintf(buffer, SMALL_STRING_LEN,"%lu.%lu", memc_stat->rusage_user_seconds, memc_stat->rusage_user_microseconds);
261 }
262 else if (not memcmp("rusage_system", key, sizeof("rusage_system") -1))
263 {
264 length= snprintf(buffer, SMALL_STRING_LEN,"%lu.%lu", memc_stat->rusage_system_seconds, memc_stat->rusage_system_microseconds);
265 }
266 else if (not memcmp("curr_items", key, sizeof("curr_items") -1))
267 {
268 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->curr_items);
269 }
270 else if (not memcmp("total_items", key, sizeof("total_items") -1))
271 {
272 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->total_items);
273 }
274 else if (not memcmp("curr_connections", key, sizeof("curr_connections") -1))
275 {
276 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->curr_connections);
277 }
278 else if (not memcmp("total_connections", key, sizeof("total_connections") -1))
279 {
280 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->total_connections);
281 }
282 else if (not memcmp("connection_structures", key, sizeof("connection_structures") -1))
283 {
284 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->connection_structures);
285 }
286 else if (not memcmp("cmd_get", key, sizeof("cmd_get") -1))
287 {
288 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_get);
289 }
290 else if (not memcmp("cmd_set", key, sizeof("cmd_set") -1))
291 {
292 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_set);
293 }
294 else if (not memcmp("get_hits", key, sizeof("get_hits") -1))
295 {
296 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_hits);
297 }
298 else if (not memcmp("get_misses", key, sizeof("get_misses") -1))
299 {
300 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_misses);
301 }
302 else if (not memcmp("evictions", key, sizeof("evictions") -1))
303 {
304 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->evictions);
305 }
306 else if (not memcmp("bytes_read", key, sizeof("bytes_read") -1))
307 {
308 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_read);
309 }
310 else if (not memcmp("bytes_written", key, sizeof("bytes_written") -1))
311 {
312 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_written);
313 }
314 else if (not memcmp("bytes", key, sizeof("bytes") -1))
315 {
316 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes);
317 }
318 else if (not memcmp("limit_maxbytes", key, sizeof("limit_maxbytes") -1))
319 {
320 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->limit_maxbytes);
321 }
322 else if (not memcmp("threads", key, sizeof("threads") -1))
323 {
324 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->threads);
325 }
326 else
327 {
328 *error= MEMCACHED_NOTFOUND;
329 return NULL;
330 }
331
332 if (length >= SMALL_STRING_LEN || length < 0)
333 {
334 *error= MEMCACHED_FAILURE;
335 return NULL;
336 }
337
338 // User is responsible for free() memory, so use malloc()
339 char *ret= static_cast<char *>(malloc(size_t(length +1)));
340 memcpy(ret, buffer, (size_t) length);
341 ret[length]= '\0';
342
343 return ret;
344 }
345
346 static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
347 const char *args,
348 const size_t args_length,
349 org::libmemcached::Instance* instance,
350 struct local_context *check)
351 {
352 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
353 protocol_binary_request_stats request= {}; // = {.bytes= {0}};
354
355 initialize_binary_request(instance, request.message.header);
356
357 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT;
358 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
359
360 if (args_length)
361 {
362 request.message.header.request.keylen= htons(uint16_t(args_length));
363 request.message.header.request.bodylen= htonl(uint32_t( args_length));
364
365 libmemcached_io_vector_st vector[]=
366 {
367 { request.bytes, sizeof(request.bytes) },
368 { args, args_length }
369 };
370
371 if (memcached_vdo(instance, vector, 2, true) != MEMCACHED_SUCCESS)
372 {
373 memcached_io_reset(instance);
374 return MEMCACHED_WRITE_FAILURE;
375 }
376 }
377 else
378 {
379 libmemcached_io_vector_st vector[]=
380 {
381 { request.bytes, sizeof(request.bytes) }
382 };
383
384 if (memcached_vdo(instance, vector, 1, true) != MEMCACHED_SUCCESS)
385 {
386 memcached_io_reset(instance);
387 return MEMCACHED_WRITE_FAILURE;
388 }
389 }
390
391 memcached_server_response_decrement(instance);
392 while (1)
393 {
394 memcached_return_t rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
395
396 if (rc == MEMCACHED_END)
397 {
398 break;
399 }
400
401 if (rc != MEMCACHED_SUCCESS)
402 {
403 memcached_io_reset(instance);
404 return rc;
405 }
406
407 if (check && check->func)
408 {
409 size_t key_length= strlen(buffer);
410
411 check->func(instance,
412 buffer, key_length,
413 buffer+key_length+1, strlen(buffer+key_length+1),
414 check->context);
415 }
416
417 if (memc_stat)
418 {
419 if ((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY)
420 {
421 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
422 WATCHPOINT_ASSERT(0);
423 }
424 }
425 }
426
427 /*
428 * memcached_response will decrement the counter, so I need to reset it..
429 * todo: look at this and try to find a better solution.
430 * */
431 instance->cursor_active_= 0;
432
433 return MEMCACHED_SUCCESS;
434 }
435
436 static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
437 const char *args,
438 const size_t args_length,
439 org::libmemcached::Instance* instance,
440 struct local_context *check)
441 {
442 libmemcached_io_vector_st vector[]=
443 {
444 { memcached_literal_param("stats ") },
445 { args, args_length },
446 { memcached_literal_param("\r\n") }
447 };
448
449 memcached_return_t rc= memcached_vdo(instance, vector, 3, true);
450 if (memcached_success(rc))
451 {
452 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
453 while ((rc= memcached_response(instance, buffer, sizeof(buffer), NULL)) == MEMCACHED_STAT)
454 {
455 char *string_ptr= buffer;
456 string_ptr+= 5; /* Move past STAT */
457
458 char *end_ptr;
459 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {};
460 char *key= string_ptr;
461 key[size_t(end_ptr-string_ptr)]= 0;
462
463 string_ptr= end_ptr + 1;
464 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++) {};
465 char *value= string_ptr;
466 value[(size_t)(end_ptr -string_ptr)]= 0;
467 #if 0
468 bool check_bool= bool(check);
469 bool check_func_bool= bool(check) ? bool(check->func) : false;
470 fprintf(stderr, "%s:%d %s %s %d:%d\n", __FILE__, __LINE__, key, value, check_bool, check_func_bool);
471 #endif
472
473 if (check and check->func)
474 {
475 check->func(instance,
476 key, strlen(key),
477 value, strlen(value),
478 check->context);
479 }
480
481 if (memc_stat)
482 {
483 if((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY)
484 {
485 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
486 WATCHPOINT_ASSERT(0);
487 }
488 }
489 }
490 }
491
492 if (rc == MEMCACHED_ERROR)
493 {
494 return MEMCACHED_INVALID_ARGUMENTS;
495 }
496
497 if (rc == MEMCACHED_END)
498 {
499 return MEMCACHED_SUCCESS;
500 }
501
502 return rc;
503 }
504
505 memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_return_t *error)
506 {
507 memcached_return_t unused;
508 if (error == NULL)
509 {
510 error= &unused;
511 }
512
513 if (memcached_failed(*error= initialize_query(self, true)))
514 {
515 return NULL;
516 }
517
518 if (memcached_is_udp(self))
519 {
520 *error= memcached_set_error(*self, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
521 return NULL;
522 }
523
524 memcached_return_t rc;
525 size_t args_length= 0;
526 if (args)
527 {
528 args_length= strlen(args);
529 rc= memcached_validate_key_length(args_length, self->flags.binary_protocol);
530 if (memcached_failed(rc))
531 {
532 *error= memcached_set_error(*self, rc, MEMCACHED_AT);
533 return NULL;
534 }
535 }
536
537 WATCHPOINT_ASSERT(error);
538
539 memcached_stat_st *stats= libmemcached_xcalloc(self, memcached_server_count(self), memcached_stat_st);
540 if (stats == NULL)
541 {
542 *error= memcached_set_error(*self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
543 return NULL;
544 }
545
546 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS);
547 rc= MEMCACHED_SUCCESS;
548 for (uint32_t x= 0; x < memcached_server_count(self); x++)
549 {
550 memcached_stat_st* stat_instance= stats +x;
551
552 stat_instance->pid= -1;
553 stat_instance->root= self;
554
555 org::libmemcached::Instance* instance= memcached_instance_fetch(self, x);
556
557 memcached_return_t temp_return;
558 if (memcached_is_binary(self))
559 {
560 temp_return= binary_stats_fetch(stat_instance, args, args_length, instance, NULL);
561 }
562 else
563 {
564 temp_return= ascii_stats_fetch(stat_instance, args, args_length, instance, NULL);
565 }
566
567 // Special case where "args" is invalid
568 if (temp_return == MEMCACHED_INVALID_ARGUMENTS)
569 {
570 rc= MEMCACHED_INVALID_ARGUMENTS;
571 break;
572 }
573
574 if (memcached_failed(temp_return))
575 {
576 rc= MEMCACHED_SOME_ERRORS;
577 }
578 }
579
580 *error= rc;
581
582 return stats;
583 }
584
585 memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char *args,
586 const char *hostname, in_port_t port)
587 {
588 memcached_st memc;
589
590 memcached_stat_st unused_memc_stat;
591 if (memc_stat == NULL)
592 {
593 memc_stat= &unused_memc_stat;
594 }
595
596 memset(memc_stat, 0, sizeof(memcached_stat_st));
597
598 memcached_st *memc_ptr= memcached_create(&memc);
599 if (memc_ptr == NULL)
600 {
601 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
602 }
603
604 memcached_return_t rc;
605 if (memcached_failed(rc= memcached_server_add(&memc, hostname, port)))
606 {
607 memcached_free(&memc);
608 return rc;
609 }
610
611 if (memcached_success(rc= initialize_query(memc_ptr, true)))
612 {
613 size_t args_length= 0;
614 if (args)
615 {
616 args_length= strlen(args);
617 rc= memcached_validate_key_length(args_length, memc.flags.binary_protocol);
618 }
619
620 if (memcached_success(rc))
621 {
622 org::libmemcached::Instance* instance= memcached_instance_fetch(memc_ptr, 0);
623 if (memc.flags.binary_protocol)
624 {
625 rc= binary_stats_fetch(memc_stat, args, args_length, instance, NULL);
626 }
627 else
628 {
629 rc= ascii_stats_fetch(memc_stat, args, args_length, instance, NULL);
630 }
631 }
632 }
633
634 memcached_free(&memc);
635
636 return rc;
637 }
638
639 /*
640 We make a copy of the keys since at some point in the not so distant future
641 we will add support for "found" keys.
642 */
643 char ** memcached_stat_get_keys(memcached_st *memc,
644 memcached_stat_st *,
645 memcached_return_t *error)
646 {
647 if (memc)
648 {
649 char **list= static_cast<char **>(libmemcached_malloc(memc, sizeof(memcached_stat_keys)));
650 if (list == NULL)
651 {
652 if (error)
653 {
654 *error= memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
655 }
656
657 return NULL;
658 }
659
660 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
661
662 if (error)
663 {
664 *error= MEMCACHED_SUCCESS;
665 }
666
667 return list;
668 }
669
670 return NULL;
671 }
672
673 void memcached_stat_free(const memcached_st *, memcached_stat_st *memc_stat)
674 {
675 WATCHPOINT_ASSERT(memc_stat); // Be polite, but when debugging catch this as an error
676 if (memc_stat)
677 {
678 libmemcached_free(memc_stat->root, memc_stat);
679 }
680 }
681
682 static memcached_return_t call_stat_fn(memcached_st *memc,
683 org::libmemcached::Instance* instance,
684 void *context)
685 {
686 if (memc)
687 {
688 local_context *check= (struct local_context *)context;
689
690 if (memcached_is_binary(memc))
691 {
692 return binary_stats_fetch(NULL, check->args, check->args_length, instance, check);
693 }
694 else
695 {
696 return ascii_stats_fetch(NULL, check->args, check->args_length, instance, check);
697 }
698 }
699
700 return MEMCACHED_INVALID_ARGUMENTS;
701 }
702
703 memcached_return_t memcached_stat_execute(memcached_st *memc, const char *args, memcached_stat_fn func, void *context)
704 {
705 if (memcached_fatal(memcached_version(memc)))
706 {
707 return memcached_last_error(memc);
708 }
709
710 local_context check(func, context, args, args ? strlen(args) : 0);
711
712 return memcached_server_execute(memc, call_stat_fn, (void *)&check);
713 }