6 static char *memcached_stat_keys
[] = {
19 "connection_structures",
33 static memcached_return
set_data(memcached_stat_st
*stat
, char *key
, char *value
)
38 WATCHPOINT_STRING(key
);
39 return MEMCACHED_UNKNOWN_STAT_KEY
;
41 else if (!strcmp("pid", key
))
43 stat
->pid
= strtol(value
, (char **)NULL
, 10);
45 else if (!strcmp("uptime", key
))
47 stat
->uptime
= strtol(value
, (char **)NULL
, 10);
49 else if (!strcmp("time", key
))
51 stat
->time
= strtol(value
, (char **)NULL
, 10);
53 else if (!strcmp("version", key
))
55 memcpy(stat
->version
, value
, strlen(value
));
56 stat
->version
[strlen(value
)]= 0;
58 else if (!strcmp("pointer_size", key
))
60 stat
->pointer_size
= strtol(value
, (char **)NULL
, 10);
62 else if (!strcmp("rusage_user", key
))
65 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
68 stat
->rusage_user_seconds
= strtol(value
, (char **)NULL
, 10);
69 stat
->rusage_user_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
71 else if (!strcmp("rusage_system", key
))
74 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
77 stat
->rusage_system_seconds
= strtol(value
, (char **)NULL
, 10);
78 stat
->rusage_system_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
80 else if (!strcmp("curr_items", key
))
82 stat
->curr_items
= strtol(value
, (char **)NULL
, 10);
84 else if (!strcmp("total_items", key
))
86 stat
->total_items
= strtol(value
, (char **)NULL
, 10);
88 else if (!strcmp("bytes_read", key
))
90 stat
->bytes_read
= strtoll(value
, (char **)NULL
, 10);
92 else if (!strcmp("bytes_written", key
))
94 stat
->bytes_written
= strtoll(value
, (char **)NULL
, 10);
96 else if (!strcmp("bytes", key
))
98 stat
->bytes
= strtoll(value
, (char **)NULL
, 10);
100 else if (!strcmp("curr_connections", key
))
102 stat
->curr_connections
= strtoll(value
, (char **)NULL
, 10);
104 else if (!strcmp("total_connections", key
))
106 stat
->total_connections
= strtoll(value
, (char **)NULL
, 10);
108 else if (!strcmp("connection_structures", key
))
110 stat
->connection_structures
= strtol(value
, (char **)NULL
, 10);
112 else if (!strcmp("cmd_get", key
))
114 stat
->cmd_get
= strtoll(value
, (char **)NULL
, 10);
116 else if (!strcmp("cmd_set", key
))
118 stat
->cmd_set
= strtoll(value
, (char **)NULL
, 10);
120 else if (!strcmp("get_hits", key
))
122 stat
->get_hits
= strtoll(value
, (char **)NULL
, 10);
124 else if (!strcmp("get_misses", key
))
126 stat
->get_misses
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
128 else if (!strcmp("evictions", key
))
130 stat
->evictions
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
132 else if (!strcmp("limit_maxbytes", key
))
134 stat
->limit_maxbytes
= strtoll(value
, (char **)NULL
, 10);
136 else if (!strcmp("threads", key
))
138 stat
->threads
= 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))
153 WATCHPOINT_STRING(key
);
154 return MEMCACHED_UNKNOWN_STAT_KEY
;
157 return MEMCACHED_SUCCESS
;
160 char *memcached_stat_get_value(memcached_st
*ptr
, memcached_stat_st
*stat
,
161 const char *key
, memcached_return
*error
)
163 char buffer
[SMALL_STRING_LEN
];
167 *error
= MEMCACHED_SUCCESS
;
169 if (!memcmp("pid", key
, strlen("pid")))
170 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pid
);
171 else if (!memcmp("uptime", key
, strlen("uptime")))
172 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->uptime
);
173 else if (!memcmp("time", key
, strlen("time")))
174 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->time
);
175 else if (!memcmp("version", key
, strlen("version")))
176 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%s", stat
->version
);
177 else if (!memcmp("pointer_size", key
, strlen("pointer_size")))
178 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->pointer_size
);
179 else if (!memcmp("rusage_user", key
, strlen("rusage_user")))
180 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", stat
->rusage_user_seconds
, stat
->rusage_user_microseconds
);
181 else if (!memcmp("rusage_system", key
, strlen("rusage_system")))
182 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u.%u", stat
->rusage_system_seconds
, stat
->rusage_system_microseconds
);
183 else if (!memcmp("curr_items", key
, strlen("curr_items")))
184 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_items
);
185 else if (!memcmp("total_items", key
, strlen("total_items")))
186 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_items
);
187 else if (!memcmp("bytes", key
, strlen("bytes")))
188 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes
);
189 else if (!memcmp("curr_connections", key
, strlen("curr_connections")))
190 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->curr_connections
);
191 else if (!memcmp("total_connections", key
, strlen("total_connections")))
192 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->total_connections
);
193 else if (!memcmp("connection_structures", key
, strlen("connection_structures")))
194 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->connection_structures
);
195 else if (!memcmp("cmd_get", key
, strlen("cmd_get")))
196 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->cmd_get
);
197 else if (!memcmp("cmd_set", key
, strlen("cmd_set")))
198 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->cmd_set
);
199 else if (!memcmp("get_hits", key
, strlen("get_hits")))
200 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->get_hits
);
201 else if (!memcmp("get_misses", key
, strlen("get_misses")))
202 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->get_misses
);
203 else if (!memcmp("evictions", key
, strlen("evictions")))
204 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->evictions
);
205 else if (!memcmp("bytes_read", key
, strlen("bytes_read")))
206 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes_read
);
207 else if (!memcmp("bytes_written", key
, strlen("bytes_written")))
208 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->bytes_written
);
209 else if (!memcmp("limit_maxbytes", key
, strlen("limit_maxbytes")))
210 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%llu", (unsigned long long)stat
->limit_maxbytes
);
211 else if (!memcmp("threads", key
, strlen("threads")))
212 length
= snprintf(buffer
, SMALL_STRING_LEN
,"%u", stat
->threads
);
215 *error
= MEMCACHED_NOTFOUND
;
219 if (ptr
->call_malloc
)
220 ret
= ptr
->call_malloc(ptr
, length
+ 1);
222 ret
= malloc(length
+ 1);
223 memcpy(ret
, buffer
, length
);
229 static memcached_return
binary_stats_fetch(memcached_st
*ptr
,
230 memcached_stat_st
*stat
,
232 unsigned int server_key
)
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 int 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(len
);
253 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
254 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
255 (memcached_io_write(&ptr
->hosts
[server_key
], args
, len
, 1) == -1))
257 memcached_io_reset(&ptr
->hosts
[server_key
]);
258 return MEMCACHED_WRITE_FAILURE
;
263 if (memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
264 sizeof(request
.bytes
), 1) != MEMCACHED_SUCCESS
)
266 memcached_io_reset(&ptr
->hosts
[server_key
]);
267 return MEMCACHED_WRITE_FAILURE
;
271 memcached_server_response_decrement(&ptr
->hosts
[server_key
]);
274 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
,
275 sizeof(buffer
), NULL
);
276 if (rc
== MEMCACHED_END
)
279 unlikely (rc
!= MEMCACHED_SUCCESS
)
281 memcached_io_reset(&ptr
->hosts
[server_key
]);
285 unlikely((set_data(stat
, buffer
, buffer
+ strlen(buffer
) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY
)
287 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
288 WATCHPOINT_ASSERT(0);
292 /* shit... memcached_response will decrement the counter, so I need to
293 ** reset it.. todo: look at this and try to find a better solution.
295 ptr
->hosts
[server_key
].cursor_active
= 0;
297 return MEMCACHED_SUCCESS
;
300 static memcached_return
ascii_stats_fetch(memcached_st
*ptr
,
301 memcached_stat_st
*stat
,
303 unsigned int server_key
)
306 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
310 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
311 "stats %s\r\n", args
);
313 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
316 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
317 return MEMCACHED_WRITE_FAILURE
;
319 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, 1);
320 if (rc
!= MEMCACHED_SUCCESS
)
325 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
327 if (rc
== MEMCACHED_STAT
)
329 char *string_ptr
, *end_ptr
;
333 string_ptr
+= 5; /* Move past STAT */
334 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++);
336 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
338 string_ptr
= end_ptr
+ 1;
339 for (end_ptr
= string_ptr
; !(isspace(*end_ptr
)); end_ptr
++);
341 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
342 string_ptr
= end_ptr
+ 2;
343 unlikely((set_data(stat
, key
, value
)) == MEMCACHED_UNKNOWN_STAT_KEY
)
345 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY
);
346 WATCHPOINT_ASSERT(0);
354 if (rc
== MEMCACHED_END
)
355 return MEMCACHED_SUCCESS
;
360 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
364 memcached_stat_st
*stats
;
366 unlikely (ptr
->flags
& MEM_USE_UDP
)
368 *error
= MEMCACHED_NOT_SUPPORTED
;
372 if (ptr
->call_malloc
)
373 stats
= (memcached_stat_st
*)ptr
->call_malloc(ptr
, sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
375 stats
= (memcached_stat_st
*)calloc(1, sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
379 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
383 rc
= MEMCACHED_SUCCESS
;
384 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
386 memcached_return temp_return
;
388 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
389 temp_return
= binary_stats_fetch(ptr
, stats
+ x
, args
, x
);
391 temp_return
= ascii_stats_fetch(ptr
, stats
+ x
, args
, x
);
393 if (temp_return
!= MEMCACHED_SUCCESS
)
394 rc
= MEMCACHED_SOME_ERRORS
;
401 memcached_return
memcached_stat_servername(memcached_stat_st
*stat
, char *args
,
402 char *hostname
, unsigned int port
)
407 memcached_create(&memc
);
409 memcached_server_add(&memc
, hostname
, port
);
411 if (memc
.flags
& MEM_BINARY_PROTOCOL
)
412 rc
= binary_stats_fetch(&memc
, stat
, args
, 0);
414 rc
= ascii_stats_fetch(&memc
, stat
, args
, 0);
416 memcached_free(&memc
);
422 We make a copy of the keys since at some point in the not so distant future
423 we will add support for "found" keys.
425 char ** memcached_stat_get_keys(memcached_st
*ptr
, memcached_stat_st
*stat
__attribute__((unused
)),
426 memcached_return
*error
)
429 size_t length
= sizeof(memcached_stat_keys
);
431 if (ptr
->call_malloc
)
432 list
= (char **)ptr
->call_malloc(ptr
, length
);
434 list
= (char **)calloc(1, length
);
438 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
442 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
444 *error
= MEMCACHED_SUCCESS
;
449 void memcached_stat_free(memcached_st
*ptr
, memcached_stat_st
*stat
)
453 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
457 if (ptr
&& ptr
->call_free
)
458 ptr
->call_free(ptr
, stat
);