6 static const char *memcached_stat_keys
[] = {
19 "connection_structures",
33 static memcached_return_t
set_data(memcached_stat_st
*memc_stat
, char *key
, char *value
)
38 WATCHPOINT_STRING(key
);
39 return MEMCACHED_UNKNOWN_STAT_KEY
;
41 else if (!strcmp("pid", key
))
43 memc_stat
->pid
= (uint32_t) strtol(value
, (char **)NULL
, 10);
45 else if (!strcmp("uptime", key
))
47 memc_stat
->uptime
= (uint32_t) strtol(value
, (char **)NULL
, 10);
49 else if (!strcmp("time", key
))
51 memc_stat
->time
= (uint32_t) strtol(value
, (char **)NULL
, 10);
53 else if (!strcmp("version", key
))
55 memcpy(memc_stat
->version
, value
, strlen(value
));
56 memc_stat
->version
[strlen(value
)]= 0;
58 else if (!strcmp("pointer_size", key
))
60 memc_stat
->pointer_size
= (uint32_t) strtol(value
, (char **)NULL
, 10);
62 else if (!strcmp("rusage_user", key
))
65 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
68 memc_stat
->rusage_user_seconds
= (uint32_t) strtol(value
, (char **)NULL
, 10);
69 memc_stat
->rusage_user_microseconds
= (uint32_t) strtol(walk_ptr
, (char **)NULL
, 10);
71 else if (!strcmp("rusage_system", key
))
74 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
77 memc_stat
->rusage_system_seconds
= (uint32_t) strtol(value
, (char **)NULL
, 10);
78 memc_stat
->rusage_system_microseconds
= (uint32_t) strtol(walk_ptr
, (char **)NULL
, 10);
80 else if (!strcmp("curr_items", key
))
82 memc_stat
->curr_items
= (uint32_t) strtol(value
, (char **)NULL
, 10);
84 else if (!strcmp("total_items", key
))
86 memc_stat
->total_items
= (uint32_t) strtol(value
, (char **)NULL
, 10);
88 else if (!strcmp("bytes_read", key
))
90 memc_stat
->bytes_read
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
92 else if (!strcmp("bytes_written", key
))
94 memc_stat
->bytes_written
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
96 else if (!strcmp("bytes", key
))
98 memc_stat
->bytes
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
100 else if (!strcmp("curr_connections", key
))
102 memc_stat
->curr_connections
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
104 else if (!strcmp("total_connections", key
))
106 memc_stat
->total_connections
= (uint32_t) strtoll(value
, (char **)NULL
, 10);
108 else if (!strcmp("connection_structures", key
))
110 memc_stat
->connection_structures
= (uint32_t) strtol(value
, (char **)NULL
, 10);
112 else if (!strcmp("cmd_get", key
))
114 memc_stat
->cmd_get
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
116 else if (!strcmp("cmd_set", key
))
118 memc_stat
->cmd_set
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
120 else if (!strcmp("get_hits", key
))
122 memc_stat
->get_hits
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
124 else if (!strcmp("get_misses", key
))
126 memc_stat
->get_misses
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
128 else if (!strcmp("evictions", key
))
130 memc_stat
->evictions
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
132 else if (!strcmp("limit_maxbytes", key
))
134 memc_stat
->limit_maxbytes
= (uint64_t) strtoll(value
, (char **)NULL
, 10);
136 else if (!strcmp("threads", key
))
138 memc_stat
->threads
= (uint32_t) strtol(value
, (char **)NULL
, 10);
140 else if (!(strcmp("delete_misses", key
) == 0 ||/* New stats in the 1.3 beta */
141 strcmp("delete_hits", key
) == 0 ||/* Just swallow them for now.. */
142 strcmp("incr_misses", key
) == 0 ||
143 strcmp("incr_hits", key
) == 0 ||
144 strcmp("decr_misses", key
) == 0 ||
145 strcmp("decr_hits", key
) == 0 ||
146 strcmp("cas_misses", key
) == 0 ||
147 strcmp("cas_hits", key
) == 0 ||
148 strcmp("cas_badval", key
) == 0 ||
149 strcmp("cmd_flush", key
) == 0 ||
150 strcmp("accepting_conns", key
) == 0 ||
151 strcmp("listen_disabled_num", key
) == 0 ||
152 strcmp("conn_yields", key
) == 0 ||
153 strcmp("auth_cmds", key
) == 0 ||
154 strcmp("auth_errors", key
) == 0 ||
155 strcmp("reclaimed", key
) == 0))
157 WATCHPOINT_STRING(key
);
158 return MEMCACHED_UNKNOWN_STAT_KEY
;
161 return MEMCACHED_SUCCESS
;
164 char *memcached_stat_get_value(const memcached_st
*ptr
, memcached_stat_st
*memc_stat
,
165 const char *key
, memcached_return_t
*error
)
167 char buffer
[SMALL_STRING_LEN
];
171 *error
= MEMCACHED_SUCCESS
;
173 if (!memcmp("pid", key
, strlen("pid")))
174 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->pid
);
175 else if (!memcmp("uptime", key
, strlen("uptime")))
176 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->uptime
);
177 else if (!memcmp("time", key
, strlen("time")))
178 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->time
);
179 else if (!memcmp("version", key
, strlen("version")))
180 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%s", memc_stat
->version
);
181 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
182 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->pointer_size
);
183 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
184 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", memc_stat
->rusage_user_seconds
, memc_stat
->rusage_user_microseconds
);
185 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
186 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", memc_stat
->rusage_system_seconds
, memc_stat
->rusage_system_microseconds
);
187 else if (!memcmp("curr_items", key
, strlen("curr_items")))
188 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->curr_items
);
189 else if (!memcmp("total_items", key
, strlen("total_items")))
190 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->total_items
);
191 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
192 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->curr_connections
);
193 else if (!memcmp("total_connections", key
, strlen("total_connections")))
194 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->total_connections
);
195 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
196 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->connection_structures
);
197 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
198 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->cmd_get
);
199 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
200 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->cmd_set
);
201 else if (!memcmp("get_hits", key
, strlen("get_hits")))
202 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->get_hits
);
203 else if (!memcmp("get_misses", key
, strlen("get_misses")))
204 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->get_misses
);
205 else if (!memcmp("evictions", key
, strlen("evictions")))
206 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->evictions
);
207 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
208 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->bytes_read
);
209 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
210 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->bytes_written
);
211 else if (!memcmp("bytes", key
, strlen("bytes")))
212 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->bytes
);
213 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
214 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)memc_stat
->limit_maxbytes
);
215 else if (!memcmp("threads", key
, strlen("threads")))
216 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", memc_stat
->threads
);
219 *error
= MEMCACHED_NOTFOUND
;
223 ret
= libmemcached_malloc(ptr
, (size_t) (length
+ 1));
224 memcpy(ret
, buffer
, (size_t) length
);
230 static memcached_return_t
binary_stats_fetch(memcached_stat_st
*memc_stat
,
232 memcached_server_write_instance_st instance
)
234 memcached_return_t rc
;
236 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
237 protocol_binary_request_stats request
= {.bytes
= {0}};
238 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
239 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_STAT
;
240 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
244 size_t len
= strlen(args
);
246 rc
= memcached_validate_key_length(len
, true);
247 unlikely (rc
!= MEMCACHED_SUCCESS
)
250 request
.message
.header
.request
.keylen
= htons((uint16_t)len
);
251 request
.message
.header
.request
.bodylen
= htonl((uint32_t) len
);
253 struct __write_vector_st vector
[]=
255 { .length
= sizeof(request
.bytes
), .buffer
= request
.bytes
},
256 { .length
= len
, .buffer
= args
}
259 if (memcached_vdo(instance
, vector
, 2, true) != MEMCACHED_SUCCESS
)
261 memcached_io_reset(instance
);
262 return MEMCACHED_WRITE_FAILURE
;
267 if (memcached_do(instance
, request
.bytes
,
268 sizeof(request
.bytes
), true) != MEMCACHED_SUCCESS
)
270 memcached_io_reset(instance
);
271 return MEMCACHED_WRITE_FAILURE
;
275 memcached_server_response_decrement(instance
);
278 rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
);
280 if (rc
== MEMCACHED_END
)
283 unlikely (rc
!= MEMCACHED_SUCCESS
)
285 memcached_io_reset(instance
);
289 unlikely((set_data(memc_stat
, buffer
, buffer
+ strlen(buffer
) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY
)
291 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
292 WATCHPOINT_ASSERT(0);
296 /* shit... memcached_response will decrement the counter, so I need to
297 ** reset it.. todo: look at this and try to find a better solution.
299 instance
->cursor_active
= 0;
301 return MEMCACHED_SUCCESS
;
304 static memcached_return_t
ascii_stats_fetch(memcached_stat_st
*memc_stat
,
306 memcached_server_write_instance_st instance
)
308 memcached_return_t rc
;
309 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
313 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
314 "stats %s\r\n", args
);
316 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
319 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
320 return MEMCACHED_WRITE_FAILURE
;
322 rc
= memcached_do(instance
, buffer
, send_length
, true);
323 if (rc
!= MEMCACHED_SUCCESS
)
328 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
330 if (rc
== MEMCACHED_STAT
)
332 char *string_ptr
, *end_ptr
;
336 string_ptr
+= 5; /* Move past STAT */
337 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++);
339 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
341 string_ptr
= end_ptr
+ 1;
342 for (end_ptr
= string_ptr
; !(isspace(*end_ptr
)); end_ptr
++);
344 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
345 string_ptr
= end_ptr
+ 2;
346 unlikely((set_data(memc_stat
, key
, value
)) == MEMCACHED_UNKNOWN_STAT_KEY
)
348 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
349 WATCHPOINT_ASSERT(0);
357 if (rc
== MEMCACHED_END
)
358 return MEMCACHED_SUCCESS
;
363 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return_t
*error
)
365 memcached_return_t rc
;
366 memcached_stat_st
*stats
;
368 unlikely (ptr
->flags
.use_udp
)
370 *error
= MEMCACHED_NOT_SUPPORTED
;
374 stats
= libmemcached_calloc(ptr
, memcached_server_count(ptr
), sizeof(memcached_stat_st
));
378 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
382 rc
= MEMCACHED_SUCCESS
;
383 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
385 memcached_return_t temp_return
;
386 memcached_server_write_instance_st instance
;
387 memcached_stat_st
*stat_instance
;
389 stat_instance
= stats
+ x
;
391 stat_instance
->root
= ptr
;
393 instance
= memcached_server_instance_fetch(ptr
, x
);
395 if (ptr
->flags
.binary_protocol
)
397 temp_return
= binary_stats_fetch(stat_instance
, args
, instance
);
401 temp_return
= ascii_stats_fetch(stat_instance
, args
, instance
);
404 if (temp_return
!= MEMCACHED_SUCCESS
)
405 rc
= MEMCACHED_SOME_ERRORS
;
412 memcached_return_t
memcached_stat_servername(memcached_stat_st
*memc_stat
, char *args
,
413 const char *hostname
, in_port_t port
)
415 memcached_return_t rc
;
417 memcached_st
*memc_ptr
;
418 memcached_server_write_instance_st instance
;
420 memset(memc_stat
, 0, sizeof(memcached_stat_st
));
422 memc_ptr
= memcached_create(&memc
);
423 WATCHPOINT_ASSERT(memc_ptr
);
425 memcached_server_add(&memc
, hostname
, port
);
427 instance
= memcached_server_instance_fetch(memc_ptr
, 0);
429 if (memc
.flags
.binary_protocol
)
431 rc
= binary_stats_fetch(memc_stat
, args
, instance
);
435 rc
= ascii_stats_fetch(memc_stat
, args
, instance
);
438 memcached_free(&memc
);
444 We make a copy of the keys since at some point in the not so distant future
445 we will add support for "found" keys.
447 char ** memcached_stat_get_keys(const memcached_st
*ptr
,
448 memcached_stat_st
*memc_stat
,
449 memcached_return_t
*error
)
452 size_t length
= sizeof(memcached_stat_keys
);
456 list
= libmemcached_malloc(ptr
, length
);
460 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
464 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
466 *error
= MEMCACHED_SUCCESS
;
471 void memcached_stat_free(const memcached_st
*ptr
, memcached_stat_st
*memc_stat
)
473 if (memc_stat
== NULL
)
475 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
481 libmemcached_free(memc_stat
->root
, memc_stat
);
485 libmemcached_free(ptr
, memc_stat
);