6 static const char *memcached_stat_keys
[] = {
19 "connection_structures",
34 memcached_stat_fn func
;
38 local_context(memcached_stat_fn func_arg
,
40 const char *args_arg
) :
48 static memcached_return_t
set_data(memcached_stat_st
*memc_stat
, char *key
, char *value
)
53 WATCHPOINT_STRING(key
);
54 return MEMCACHED_UNKNOWN_STAT_KEY
;
56 else if (!strcmp("pid", key
))
58 memc_stat
->pid
= (uint32_t) strtol(value
, (char **)NULL
, 10);
60 else if (!strcmp("uptime", key
))
62 memc_stat
->uptime
= (uint32_t) strtol(value
, (char **)NULL
, 10);
64 else if (!strcmp("time", key
))
66 memc_stat
->time
= (uint32_t) strtol(value
, (char **)NULL
, 10);
68 else if (!strcmp("version", key
))
70 memcpy(memc_stat
->version
, value
, strlen(value
));
71 memc_stat
->version
[strlen(value
)]= 0;
73 else if (!strcmp("pointer_size", key
))
75 memc_stat
->pointer_size
= (uint32_t) strtol(value
, (char **)NULL
, 10);
77 else if (!strcmp("rusage_user", key
))
80 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); 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);
86 else if (!strcmp("rusage_system", key
))
89 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); 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);
95 else if (!strcmp("curr_items", key
))
97 memc_stat
->curr_items
= (uint32_t) strtol(value
, (char **)NULL
, 10);
99 else if (!strcmp("total_items", key
))
101 memc_stat
->total_items
= (uint32_t) strtol(value
, (char **)NULL
, 10);
103 else if (!strcmp("bytes_read", key
))
105 memc_stat
->bytes_read
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
107 else if (!strcmp("bytes_written", key
))
109 memc_stat
->bytes_written
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
111 else if (!strcmp("bytes", key
))
113 memc_stat
->bytes
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
115 else if (!strcmp("curr_connections", key
))
117 memc_stat
->curr_connections
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
119 else if (!strcmp("total_connections", key
))
121 memc_stat
->total_connections
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
123 else if (!strcmp("connection_structures", key
))
125 memc_stat
->connection_structures
= (uint32_t) strtol(value
, (char **)NULL
, 10);
127 else if (!strcmp("cmd_get", key
))
129 memc_stat
->cmd_get
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
131 else if (!strcmp("cmd_set", key
))
133 memc_stat
->cmd_set
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
135 else if (!strcmp("get_hits", key
))
137 memc_stat
->get_hits
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
139 else if (!strcmp("get_misses", key
))
141 memc_stat
->get_misses
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
143 else if (!strcmp("evictions", key
))
145 memc_stat
->evictions
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
147 else if (!strcmp("limit_maxbytes", key
))
149 memc_stat
->limit_maxbytes
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
151 else if (!strcmp("threads", key
))
153 memc_stat
->threads
= (uint32_t) strtol(value
, (char **)NULL
, 10);
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))
172 WATCHPOINT_STRING(key
);
173 /* return MEMCACHED_UNKNOWN_STAT_KEY; */
174 return MEMCACHED_SUCCESS
;
177 return MEMCACHED_SUCCESS
;
180 char *memcached_stat_get_value(const memcached_st
*ptr
, memcached_stat_st
*memc_stat
,
181 const char *key
, memcached_return_t
*error
)
183 char buffer
[SMALL_STRING_LEN
];
187 *error
= MEMCACHED_SUCCESS
;
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
);
235 *error
= MEMCACHED_NOTFOUND
;
239 if (length
>= SMALL_STRING_LEN
|| length
< 0)
241 *error
= MEMCACHED_FAILURE
;
245 ret
= static_cast<char *>(libmemcached_malloc(ptr
, (size_t) (length
+ 1)));
246 memcpy(ret
, buffer
, (size_t) length
);
252 static memcached_return_t
binary_stats_fetch(memcached_stat_st
*memc_stat
,
254 memcached_server_write_instance_st instance
,
255 struct local_context
*check
)
257 memcached_return_t rc
;
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
;
267 size_t len
= strlen(args
);
269 rc
= memcached_validate_key_length(len
, true);
270 unlikely (rc
!= MEMCACHED_SUCCESS
)
273 request
.message
.header
.request
.keylen
= htons((uint16_t)len
);
274 request
.message
.header
.request
.bodylen
= htonl((uint32_t) len
);
276 struct libmemcached_io_vector_st vector
[]=
278 { sizeof(request
.bytes
), request
.bytes
},
282 if (memcached_vdo(instance
, vector
, 2, true) != MEMCACHED_SUCCESS
)
284 memcached_io_reset(instance
);
285 return MEMCACHED_WRITE_FAILURE
;
290 if (memcached_do(instance
, request
.bytes
,
291 sizeof(request
.bytes
), true) != MEMCACHED_SUCCESS
)
293 memcached_io_reset(instance
);
294 return MEMCACHED_WRITE_FAILURE
;
298 memcached_server_response_decrement(instance
);
301 rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
);
303 if (rc
== MEMCACHED_END
)
306 unlikely (rc
!= MEMCACHED_SUCCESS
)
308 memcached_io_reset(instance
);
314 unlikely((set_data(memc_stat
, buffer
, buffer
+ strlen(buffer
) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY
)
316 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
317 WATCHPOINT_ASSERT(0);
321 if (check
&& check
->func
)
323 size_t key_length
= strlen(buffer
);
325 check
->func(instance
,
327 buffer
+key_length
+1, strlen(buffer
+key_length
+1),
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.
335 instance
->cursor_active
= 0;
337 return MEMCACHED_SUCCESS
;
340 static memcached_return_t
ascii_stats_fetch(memcached_stat_st
*memc_stat
,
342 memcached_server_write_instance_st instance
,
343 struct local_context
*check
)
345 memcached_return_t rc
;
346 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
350 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
351 "stats %s\r\n", args
);
353 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
356 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
357 return MEMCACHED_WRITE_FAILURE
;
359 rc
= memcached_do(instance
, buffer
, (size_t)send_length
, true);
360 if (rc
!= MEMCACHED_SUCCESS
)
365 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
367 if (rc
== MEMCACHED_STAT
)
369 char *string_ptr
, *end_ptr
;
373 string_ptr
+= 5; /* Move past STAT */
374 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++) {};
376 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
378 string_ptr
= end_ptr
+ 1;
379 for (end_ptr
= string_ptr
; !(isspace(*end_ptr
)); end_ptr
++) {};
381 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
382 string_ptr
= end_ptr
+ 2;
385 unlikely((set_data(memc_stat
, key
, value
)) == MEMCACHED_UNKNOWN_STAT_KEY
)
387 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
388 WATCHPOINT_ASSERT(0);
392 if (check
&& check
->func
)
394 check
->func(instance
,
396 value
, strlen(value
),
407 if (rc
== MEMCACHED_END
)
408 return MEMCACHED_SUCCESS
;
413 memcached_stat_st
*memcached_stat(memcached_st
*self
, char *args
, memcached_return_t
*error
)
415 memcached_return_t rc
;
416 memcached_stat_st
*stats
;
418 if ((rc
= initialize_query(self
)) != MEMCACHED_SUCCESS
)
426 WATCHPOINT_ASSERT(error
);
428 unlikely (self
->flags
.use_udp
)
431 *error
= MEMCACHED_NOT_SUPPORTED
;
436 stats
= static_cast<memcached_stat_st
*>(libmemcached_calloc(self
, memcached_server_count(self
), sizeof(memcached_stat_st
)));
441 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
446 WATCHPOINT_ASSERT(rc
== MEMCACHED_SUCCESS
);
447 rc
= MEMCACHED_SUCCESS
;
448 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
450 memcached_return_t temp_return
;
451 memcached_server_write_instance_st instance
;
452 memcached_stat_st
*stat_instance
;
454 stat_instance
= stats
+ x
;
456 stat_instance
->root
= self
;
458 instance
= memcached_server_instance_fetch(self
, x
);
460 if (self
->flags
.binary_protocol
)
462 temp_return
= binary_stats_fetch(stat_instance
, args
, instance
, NULL
);
466 temp_return
= ascii_stats_fetch(stat_instance
, args
, instance
, NULL
);
469 if (temp_return
!= MEMCACHED_SUCCESS
)
470 rc
= MEMCACHED_SOME_ERRORS
;
479 memcached_return_t
memcached_stat_servername(memcached_stat_st
*memc_stat
, char *args
,
480 const char *hostname
, in_port_t port
)
483 memcached_st
*memc_ptr
;
484 memcached_server_write_instance_st instance
;
486 memset(memc_stat
, 0, sizeof(memcached_stat_st
));
488 memc_ptr
= memcached_create(&memc
);
490 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
492 memcached_server_add(&memc
, hostname
, port
);
494 memcached_return_t rc
;
495 if ((rc
= initialize_query(memc_ptr
)) != MEMCACHED_SUCCESS
)
500 instance
= memcached_server_instance_fetch(memc_ptr
, 0);
502 if (memc
.flags
.binary_protocol
)
504 rc
= binary_stats_fetch(memc_stat
, args
, instance
, NULL
);
508 rc
= ascii_stats_fetch(memc_stat
, args
, instance
, NULL
);
511 memcached_free(&memc
);
517 We make a copy of the keys since at some point in the not so distant future
518 we will add support for "found" keys.
520 char ** memcached_stat_get_keys(const memcached_st
*ptr
,
521 memcached_stat_st
*memc_stat
,
522 memcached_return_t
*error
)
525 size_t length
= sizeof(memcached_stat_keys
);
529 list
= static_cast<char **>(libmemcached_malloc(ptr
, length
));
533 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
537 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
539 *error
= MEMCACHED_SUCCESS
;
544 void memcached_stat_free(const memcached_st
*ptr
, memcached_stat_st
*memc_stat
)
546 if (memc_stat
== NULL
)
548 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
554 libmemcached_free(memc_stat
->root
, memc_stat
);
558 libmemcached_free(ptr
, memc_stat
);
561 static memcached_return_t
call_stat_fn(memcached_st
*ptr
,
562 memcached_server_write_instance_st instance
,
565 memcached_return_t rc
;
566 struct local_context
*check
= (struct local_context
*)context
;
568 if (ptr
->flags
.binary_protocol
)
570 rc
= binary_stats_fetch(NULL
, check
->args
, instance
, check
);
574 rc
= ascii_stats_fetch(NULL
, check
->args
, instance
, check
);
580 memcached_return_t
memcached_stat_execute(memcached_st
*memc
, const char *args
, memcached_stat_fn func
, void *context
)
582 memcached_version(memc
);
584 struct local_context
check(func
, context
, args
);
586 return memcached_server_execute(memc
, call_stat_fn
, (void *)&check
);