1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
37 #include <libmemcached/common.h>
39 static const char *memcached_stat_keys
[] = {
52 "connection_structures",
67 memcached_stat_fn func
;
70 const size_t args_length
;
72 local_context(memcached_stat_fn func_arg
,
75 const size_t args_length_arg
) :
79 args_length(args_length_arg
)
84 static memcached_return_t
set_data(memcached_stat_st
*memc_stat
, const char *key
, const char *value
)
89 WATCHPOINT_STRING(key
);
90 return MEMCACHED_UNKNOWN_STAT_KEY
;
92 else if (strcmp("pid", key
) == 0)
94 int64_t temp
= strtoll(value
, (char **)NULL
, 10);
96 if (temp
<= INT32_MAX
and ( sizeof(pid_t
) == sizeof(int32_t) ))
98 memc_stat
->pid
= pid_t(temp
);
102 memc_stat
->pid
= pid_t(temp
);
106 // If we got a value less then -1 then something went wrong in the
110 else if (not strcmp("uptime", key
))
112 memc_stat
->uptime
= strtoul(value
, (char **)NULL
, 10);
114 else if (not strcmp("time", key
))
116 memc_stat
->time
= strtoul(value
, (char **)NULL
, 10);
118 else if (not strcmp("version", key
))
120 memcpy(memc_stat
->version
, value
, strlen(value
));
121 memc_stat
->version
[strlen(value
)]= 0;
123 else if (not strcmp("pointer_size", key
))
125 memc_stat
->pointer_size
= strtoul(value
, (char **)NULL
, 10);
127 else if (not strcmp("rusage_user", key
))
130 for (walk_ptr
= (char*)value
; (!ispunct(*walk_ptr
)); 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);
136 else if (not strcmp("rusage_system", key
))
139 for (walk_ptr
= (char*)value
; (!ispunct(*walk_ptr
)); 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);
145 else if (not strcmp("curr_items", key
))
147 memc_stat
->curr_items
= strtoul(value
, (char **)NULL
, 10);
149 else if (not strcmp("total_items", key
))
151 memc_stat
->total_items
= strtoul(value
, (char **)NULL
, 10);
153 else if (not strcmp("bytes_read", key
))
155 memc_stat
->bytes_read
= strtoull(value
, (char **)NULL
, 10);
157 else if (not strcmp("bytes_written", key
))
159 memc_stat
->bytes_written
= strtoull(value
, (char **)NULL
, 10);
161 else if (not strcmp("bytes", key
))
163 memc_stat
->bytes
= strtoull(value
, (char **)NULL
, 10);
165 else if (not strcmp("curr_connections", key
))
167 memc_stat
->curr_connections
= strtoull(value
, (char **)NULL
, 10);
169 else if (not strcmp("total_connections", key
))
171 memc_stat
->total_connections
= strtoull(value
, (char **)NULL
, 10);
173 else if (not strcmp("connection_structures", key
))
175 memc_stat
->connection_structures
= strtoul(value
, (char **)NULL
, 10);
177 else if (not strcmp("cmd_get", key
))
179 memc_stat
->cmd_get
= strtoull(value
, (char **)NULL
, 10);
181 else if (not strcmp("cmd_set", key
))
183 memc_stat
->cmd_set
= strtoull(value
, (char **)NULL
, 10);
185 else if (not strcmp("get_hits", key
))
187 memc_stat
->get_hits
= strtoull(value
, (char **)NULL
, 10);
189 else if (not strcmp("get_misses", key
))
191 memc_stat
->get_misses
= strtoull(value
, (char **)NULL
, 10);
193 else if (not strcmp("evictions", key
))
195 memc_stat
->evictions
= strtoull(value
, (char **)NULL
, 10);
197 else if (not strcmp("limit_maxbytes", key
))
199 memc_stat
->limit_maxbytes
= strtoull(value
, (char **)NULL
, 10);
201 else if (not strcmp("threads", key
))
203 memc_stat
->threads
= strtoul(value
, (char **)NULL
, 10);
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))
222 WATCHPOINT_STRING(key
);
223 /* return MEMCACHED_UNKNOWN_STAT_KEY; */
224 return MEMCACHED_SUCCESS
;
227 return MEMCACHED_SUCCESS
;
230 char *memcached_stat_get_value(const memcached_st
*, memcached_stat_st
*memc_stat
,
231 const char *key
, memcached_return_t
*error
)
233 memcached_return_t not_used
;
239 if (memc_stat
== NULL
)
241 *error
= MEMCACHED_INVALID_ARGUMENTS
;
245 char buffer
[SMALL_STRING_LEN
];
248 *error
= MEMCACHED_SUCCESS
;
250 if (memcmp("pid", key
, sizeof("pid") -1) == 0)
252 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lld", (signed long long)memc_stat
->pid
);
254 else if (not memcmp("uptime", key
, sizeof("uptime") -1))
256 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->uptime
);
258 else if (not memcmp("time", key
, sizeof("time") -1))
260 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->time
);
262 else if (not memcmp("version", key
, sizeof("version") -1))
264 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%s", memc_stat
->version
);
266 else if (not memcmp("pointer_size", key
, sizeof("pointer_size") -1))
268 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->pointer_size
);
270 else if (not memcmp("rusage_user", key
, sizeof("rusage_user") -1))
272 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu.%lu", memc_stat
->rusage_user_seconds
, memc_stat
->rusage_user_microseconds
);
274 else if (not memcmp("rusage_system", key
, sizeof("rusage_system") -1))
276 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu.%lu", memc_stat
->rusage_system_seconds
, memc_stat
->rusage_system_microseconds
);
278 else if (not memcmp("curr_items", key
, sizeof("curr_items") -1))
280 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->curr_items
);
282 else if (not memcmp("total_items", key
, sizeof("total_items") -1))
284 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->total_items
);
286 else if (not memcmp("curr_connections", key
, sizeof("curr_connections") -1))
288 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->curr_connections
);
290 else if (not memcmp("total_connections", key
, sizeof("total_connections") -1))
292 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->total_connections
);
294 else if (not memcmp("connection_structures", key
, sizeof("connection_structures") -1))
296 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->connection_structures
);
298 else if (not memcmp("cmd_get", key
, sizeof("cmd_get") -1))
300 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->cmd_get
);
302 else if (not memcmp("cmd_set", key
, sizeof("cmd_set") -1))
304 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->cmd_set
);
306 else if (not memcmp("get_hits", key
, sizeof("get_hits") -1))
308 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->get_hits
);
310 else if (not memcmp("get_misses", key
, sizeof("get_misses") -1))
312 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->get_misses
);
314 else if (not memcmp("evictions", key
, sizeof("evictions") -1))
316 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->evictions
);
318 else if (not memcmp("bytes_read", key
, sizeof("bytes_read") -1))
320 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->bytes_read
);
322 else if (not memcmp("bytes_written", key
, sizeof("bytes_written") -1))
324 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->bytes_written
);
326 else if (not memcmp("bytes", key
, sizeof("bytes") -1))
328 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->bytes
);
330 else if (not memcmp("limit_maxbytes", key
, sizeof("limit_maxbytes") -1))
332 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->limit_maxbytes
);
334 else if (not memcmp("threads", key
, sizeof("threads") -1))
336 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%lu", memc_stat
->threads
);
340 *error
= MEMCACHED_NOTFOUND
;
344 if (length
>= SMALL_STRING_LEN
|| length
< 0)
346 *error
= MEMCACHED_FAILURE
;
350 // User is responsible for free() memory, so use malloc()
351 char *ret
= static_cast<char *>(malloc(size_t(length
+1)));
352 memcpy(ret
, buffer
, (size_t) length
);
358 static memcached_return_t
binary_stats_fetch(memcached_stat_st
*memc_stat
,
360 const size_t args_length
,
361 org::libmemcached::Instance
* instance
,
362 struct local_context
*check
)
364 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
365 protocol_binary_request_stats request
= {}; // = {.bytes= {0}};
367 initialize_binary_request(instance
, request
.message
.header
);
369 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_STAT
;
370 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
374 request
.message
.header
.request
.keylen
= htons(uint16_t(args_length
));
375 request
.message
.header
.request
.bodylen
= htonl(uint32_t( args_length
));
377 libmemcached_io_vector_st vector
[]=
379 { request
.bytes
, sizeof(request
.bytes
) },
380 { args
, args_length
}
383 if (memcached_vdo(instance
, vector
, 2, true) != MEMCACHED_SUCCESS
)
385 memcached_io_reset(instance
);
386 return MEMCACHED_WRITE_FAILURE
;
391 libmemcached_io_vector_st vector
[]=
393 { request
.bytes
, sizeof(request
.bytes
) }
396 if (memcached_vdo(instance
, vector
, 1, true) != MEMCACHED_SUCCESS
)
398 memcached_io_reset(instance
);
399 return MEMCACHED_WRITE_FAILURE
;
403 memcached_server_response_decrement(instance
);
406 memcached_return_t rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
);
408 if (rc
== MEMCACHED_END
)
413 if (rc
!= MEMCACHED_SUCCESS
)
415 memcached_io_reset(instance
);
419 if (check
&& check
->func
)
421 size_t key_length
= strlen(buffer
);
423 check
->func(instance
,
425 buffer
+key_length
+1, strlen(buffer
+key_length
+1),
431 if ((set_data(memc_stat
, buffer
, buffer
+ strlen(buffer
) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY
)
433 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
434 WATCHPOINT_ASSERT(0);
440 * memcached_response will decrement the counter, so I need to reset it..
441 * todo: look at this and try to find a better solution.
443 instance
->cursor_active_
= 0;
445 return MEMCACHED_SUCCESS
;
448 static memcached_return_t
ascii_stats_fetch(memcached_stat_st
*memc_stat
,
450 const size_t args_length
,
451 org::libmemcached::Instance
* instance
,
452 struct local_context
*check
)
454 libmemcached_io_vector_st vector
[]=
456 { memcached_literal_param("stats ") },
457 { args
, args_length
},
458 { memcached_literal_param("\r\n") }
461 memcached_return_t rc
= memcached_vdo(instance
, vector
, 3, true);
462 if (memcached_success(rc
))
464 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
465 while ((rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
)) == MEMCACHED_STAT
)
467 char *string_ptr
= buffer
;
468 string_ptr
+= 5; /* Move past STAT */
471 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++) {};
472 char *key
= string_ptr
;
473 key
[size_t(end_ptr
-string_ptr
)]= 0;
475 string_ptr
= end_ptr
+ 1;
476 for (end_ptr
= string_ptr
; !(isspace(*end_ptr
)); end_ptr
++) {};
477 char *value
= string_ptr
;
478 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
480 bool check_bool
= bool(check
);
481 bool check_func_bool
= bool(check
) ? bool(check
->func
) : false;
482 fprintf(stderr
, "%s:%d %s %s %d:%d\n", __FILE__
, __LINE__
, key
, value
, check_bool
, check_func_bool
);
485 if (check
and check
->func
)
487 check
->func(instance
,
489 value
, strlen(value
),
495 if((set_data(memc_stat
, key
, value
)) == MEMCACHED_UNKNOWN_STAT_KEY
)
497 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
498 WATCHPOINT_ASSERT(0);
504 if (rc
== MEMCACHED_ERROR
)
506 return MEMCACHED_INVALID_ARGUMENTS
;
509 if (rc
== MEMCACHED_END
)
511 return MEMCACHED_SUCCESS
;
517 memcached_stat_st
*memcached_stat(memcached_st
*self
, char *args
, memcached_return_t
*error
)
519 memcached_return_t unused
;
525 if (memcached_failed(*error
= initialize_query(self
, true)))
530 if (memcached_is_udp(self
))
532 *error
= memcached_set_error(*self
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
);
536 memcached_return_t rc
;
537 size_t args_length
= 0;
540 args_length
= strlen(args
);
541 rc
= memcached_validate_key_length(args_length
, self
->flags
.binary_protocol
);
542 if (memcached_failed(rc
))
544 *error
= memcached_set_error(*self
, rc
, MEMCACHED_AT
);
549 WATCHPOINT_ASSERT(error
);
551 memcached_stat_st
*stats
= libmemcached_xcalloc(self
, memcached_server_count(self
), memcached_stat_st
);
554 *error
= memcached_set_error(*self
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
558 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
559 rc
= MEMCACHED_SUCCESS
;
560 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
562 memcached_stat_st
* stat_instance
= stats
+x
;
564 stat_instance
->pid
= -1;
565 stat_instance
->root
= self
;
567 org::libmemcached::Instance
* instance
= memcached_instance_fetch(self
, x
);
569 memcached_return_t temp_return
;
570 if (memcached_is_binary(self
))
572 temp_return
= binary_stats_fetch(stat_instance
, args
, args_length
, instance
, NULL
);
576 temp_return
= ascii_stats_fetch(stat_instance
, args
, args_length
, instance
, NULL
);
579 // Special case where "args" is invalid
580 if (temp_return
== MEMCACHED_INVALID_ARGUMENTS
)
582 rc
= MEMCACHED_INVALID_ARGUMENTS
;
586 if (memcached_failed(temp_return
))
588 rc
= MEMCACHED_SOME_ERRORS
;
597 memcached_return_t
memcached_stat_servername(memcached_stat_st
*memc_stat
, char *args
,
598 const char *hostname
, in_port_t port
)
602 memcached_stat_st unused_memc_stat
;
603 if (memc_stat
== NULL
)
605 memc_stat
= &unused_memc_stat
;
608 memset(memc_stat
, 0, sizeof(memcached_stat_st
));
610 memcached_st
*memc_ptr
= memcached_create(&memc
);
611 if (memc_ptr
== NULL
)
613 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
616 memcached_return_t rc
;
617 if (memcached_failed(rc
= memcached_server_add(&memc
, hostname
, port
)))
619 memcached_free(&memc
);
623 if (memcached_success(rc
= initialize_query(memc_ptr
, true)))
625 size_t args_length
= 0;
628 args_length
= strlen(args
);
629 rc
= memcached_validate_key_length(args_length
, memc
.flags
.binary_protocol
);
632 if (memcached_success(rc
))
634 org::libmemcached::Instance
* instance
= memcached_instance_fetch(memc_ptr
, 0);
635 if (memc
.flags
.binary_protocol
)
637 rc
= binary_stats_fetch(memc_stat
, args
, args_length
, instance
, NULL
);
641 rc
= ascii_stats_fetch(memc_stat
, args
, args_length
, instance
, NULL
);
646 memcached_free(&memc
);
652 We make a copy of the keys since at some point in the not so distant future
653 we will add support for "found" keys.
655 char ** memcached_stat_get_keys(memcached_st
*memc
,
657 memcached_return_t
*error
)
661 char **list
= static_cast<char **>(libmemcached_malloc(memc
, sizeof(memcached_stat_keys
)));
666 *error
= memcached_set_error(*memc
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
672 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
676 *error
= MEMCACHED_SUCCESS
;
685 void memcached_stat_free(const memcached_st
*, memcached_stat_st
*memc_stat
)
687 WATCHPOINT_ASSERT(memc_stat
); // Be polite, but when debugging catch this as an error
690 libmemcached_free(memc_stat
->root
, memc_stat
);
694 static memcached_return_t
call_stat_fn(memcached_st
*memc
,
695 org::libmemcached::Instance
* instance
,
700 local_context
*check
= (struct local_context
*)context
;
702 if (memcached_is_binary(memc
))
704 return binary_stats_fetch(NULL
, check
->args
, check
->args_length
, instance
, check
);
708 return ascii_stats_fetch(NULL
, check
->args
, check
->args_length
, instance
, check
);
712 return MEMCACHED_INVALID_ARGUMENTS
;
715 memcached_return_t
memcached_stat_execute(memcached_st
*memc
, const char *args
, memcached_stat_fn func
, void *context
)
717 if (memcached_fatal(memcached_version(memc
)))
719 return memcached_last_error(memc
);
722 local_context
check(func
, context
, args
, args
? strlen(args
) : 0);
724 return memcached_server_execute(memc
, call_stat_fn
, (void *)&check
);