d52f781a641199d03fe857238cb40f7fa3555a4e
[m6w6/libmemcached] / libmemcached / memcached_stats.c
1 /*
2 */
3
4 #include "common.h"
5
6 static char *memcached_stat_keys[] = {
7 "pid",
8 "uptime",
9 "time",
10 "version",
11 "pointer_size",
12 "rusage_user",
13 "rusage_system",
14 "curr_items",
15 "total_items",
16 "bytes",
17 "curr_connections",
18 "total_connections",
19 "connection_structures",
20 "cmd_get",
21 "cmd_set",
22 "get_hits",
23 "get_misses",
24 "evictions",
25 "bytes_read",
26 "bytes_written",
27 "limit_maxbytes",
28 "threads",
29 NULL
30 };
31
32
33 static memcached_return set_data(memcached_stat_st *stat, char *key, char *value)
34 {
35
36 if(strlen(key) < 1)
37 {
38 WATCHPOINT_STRING(key);
39 return MEMCACHED_UNKNOWN_STAT_KEY;
40 }
41 else if (!strcmp("pid", key))
42 {
43 stat->pid= strtol(value, (char **)NULL, 10);
44 }
45 else if (!strcmp("uptime", key))
46 {
47 stat->uptime= strtol(value, (char **)NULL, 10);
48 }
49 else if (!strcmp("time", key))
50 {
51 stat->time= strtol(value, (char **)NULL, 10);
52 }
53 else if (!strcmp("version", key))
54 {
55 memcpy(stat->version, value, strlen(value));
56 stat->version[strlen(value)]= 0;
57 }
58 else if (!strcmp("pointer_size", key))
59 {
60 stat->pointer_size= strtol(value, (char **)NULL, 10);
61 }
62 else if (!strcmp("rusage_user", key))
63 {
64 char *walk_ptr;
65 for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
66 *walk_ptr= 0;
67 walk_ptr++;
68 stat->rusage_user_seconds= strtol(value, (char **)NULL, 10);
69 stat->rusage_user_microseconds= strtol(walk_ptr, (char **)NULL, 10);
70 }
71 else if (!strcmp("rusage_system", key))
72 {
73 char *walk_ptr;
74 for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
75 *walk_ptr= 0;
76 walk_ptr++;
77 stat->rusage_system_seconds= strtol(value, (char **)NULL, 10);
78 stat->rusage_system_microseconds= strtol(walk_ptr, (char **)NULL, 10);
79 }
80 else if (!strcmp("curr_items", key))
81 {
82 stat->curr_items= strtol(value, (char **)NULL, 10);
83 }
84 else if (!strcmp("total_items", key))
85 {
86 stat->total_items= strtol(value, (char **)NULL, 10);
87 }
88 else if (!strcmp("bytes_read", key))
89 {
90 stat->bytes_read= strtoll(value, (char **)NULL, 10);
91 }
92 else if (!strcmp("bytes_written", key))
93 {
94 stat->bytes_written= strtoll(value, (char **)NULL, 10);
95 }
96 else if (!strcmp("bytes", key))
97 {
98 stat->bytes= strtoll(value, (char **)NULL, 10);
99 }
100 else if (!strcmp("curr_connections", key))
101 {
102 stat->curr_connections= strtoll(value, (char **)NULL, 10);
103 }
104 else if (!strcmp("total_connections", key))
105 {
106 stat->total_connections= strtoll(value, (char **)NULL, 10);
107 }
108 else if (!strcmp("connection_structures", key))
109 {
110 stat->connection_structures= strtol(value, (char **)NULL, 10);
111 }
112 else if (!strcmp("cmd_get", key))
113 {
114 stat->cmd_get= strtoll(value, (char **)NULL, 10);
115 }
116 else if (!strcmp("cmd_set", key))
117 {
118 stat->cmd_set= strtoll(value, (char **)NULL, 10);
119 }
120 else if (!strcmp("get_hits", key))
121 {
122 stat->get_hits= strtoll(value, (char **)NULL, 10);
123 }
124 else if (!strcmp("get_misses", key))
125 {
126 stat->get_misses= (uint64_t)strtoll(value, (char **)NULL, 10);
127 }
128 else if (!strcmp("evictions", key))
129 {
130 stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10);
131 }
132 else if (!strcmp("limit_maxbytes", key))
133 {
134 stat->limit_maxbytes= strtoll(value, (char **)NULL, 10);
135 }
136 else if (!strcmp("threads", key))
137 {
138 stat->threads= strtol(value, (char **)NULL, 10);
139 }
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 {
153 WATCHPOINT_STRING(key);
154 return MEMCACHED_UNKNOWN_STAT_KEY;
155 }
156
157 return MEMCACHED_SUCCESS;
158 }
159
160 char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat,
161 const char *key, memcached_return *error)
162 {
163 char buffer[SMALL_STRING_LEN];
164 size_t length;
165 char *ret;
166
167 *error= MEMCACHED_SUCCESS;
168
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);
213 else
214 {
215 *error= MEMCACHED_NOTFOUND;
216 return NULL;
217 }
218
219 ret= ptr->call_malloc(ptr, length + 1);
220 memcpy(ret, buffer, length);
221 ret[length]= '\0';
222
223 return ret;
224 }
225
226 static memcached_return binary_stats_fetch(memcached_st *ptr,
227 memcached_stat_st *stat,
228 char *args,
229 unsigned int server_key)
230 {
231 memcached_return rc;
232
233 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
234 protocol_binary_request_stats request= {.bytes= {0}};
235 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
236 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT;
237 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
238
239 if (args != NULL)
240 {
241 int len= strlen(args);
242
243 rc= memcached_validate_key_length(len, true);
244 unlikely (rc != MEMCACHED_SUCCESS)
245 return rc;
246
247 request.message.header.request.keylen= htons((uint16_t)len);
248 request.message.header.request.bodylen= htonl(len);
249
250 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
251 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
252 (memcached_io_write(&ptr->hosts[server_key], args, len, 1) == -1))
253 {
254 memcached_io_reset(&ptr->hosts[server_key]);
255 return MEMCACHED_WRITE_FAILURE;
256 }
257 }
258 else
259 {
260 if (memcached_do(&ptr->hosts[server_key], request.bytes,
261 sizeof(request.bytes), 1) != MEMCACHED_SUCCESS)
262 {
263 memcached_io_reset(&ptr->hosts[server_key]);
264 return MEMCACHED_WRITE_FAILURE;
265 }
266 }
267
268 memcached_server_response_decrement(&ptr->hosts[server_key]);
269 do
270 {
271 rc= memcached_response(&ptr->hosts[server_key], buffer,
272 sizeof(buffer), NULL);
273 if (rc == MEMCACHED_END)
274 break;
275
276 unlikely (rc != MEMCACHED_SUCCESS)
277 {
278 memcached_io_reset(&ptr->hosts[server_key]);
279 return rc;
280 }
281
282 unlikely((set_data(stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY)
283 {
284 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
285 WATCHPOINT_ASSERT(0);
286 }
287 } while (1);
288
289 /* shit... memcached_response will decrement the counter, so I need to
290 ** reset it.. todo: look at this and try to find a better solution.
291 */
292 ptr->hosts[server_key].cursor_active= 0;
293
294 return MEMCACHED_SUCCESS;
295 }
296
297 static memcached_return ascii_stats_fetch(memcached_st *ptr,
298 memcached_stat_st *stat,
299 char *args,
300 unsigned int server_key)
301 {
302 memcached_return rc;
303 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
304 size_t send_length;
305
306 if (args)
307 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
308 "stats %s\r\n", args);
309 else
310 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
311 "stats\r\n");
312
313 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
314 return MEMCACHED_WRITE_FAILURE;
315
316 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
317 if (rc != MEMCACHED_SUCCESS)
318 goto error;
319
320 while (1)
321 {
322 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
323
324 if (rc == MEMCACHED_STAT)
325 {
326 char *string_ptr, *end_ptr;
327 char *key, *value;
328
329 string_ptr= buffer;
330 string_ptr+= 5; /* Move past STAT */
331 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
332 key= string_ptr;
333 key[(size_t)(end_ptr-string_ptr)]= 0;
334
335 string_ptr= end_ptr + 1;
336 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++);
337 value= string_ptr;
338 value[(size_t)(end_ptr-string_ptr)]= 0;
339 string_ptr= end_ptr + 2;
340 unlikely((set_data(stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY)
341 {
342 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
343 WATCHPOINT_ASSERT(0);
344 }
345 }
346 else
347 break;
348 }
349
350 error:
351 if (rc == MEMCACHED_END)
352 return MEMCACHED_SUCCESS;
353 else
354 return rc;
355 }
356
357 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
358 {
359 unsigned int x;
360 memcached_return rc;
361 memcached_stat_st *stats;
362
363 unlikely (ptr->flags & MEM_USE_UDP)
364 {
365 *error= MEMCACHED_NOT_SUPPORTED;
366 return NULL;
367 }
368
369 stats= ptr->call_calloc(ptr, ptr->number_of_hosts, sizeof(memcached_stat_st));
370
371 if (!stats)
372 {
373 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
374 return NULL;
375 }
376
377 rc= MEMCACHED_SUCCESS;
378 for (x= 0; x < ptr->number_of_hosts; x++)
379 {
380 memcached_return temp_return;
381
382 if (ptr->flags & MEM_BINARY_PROTOCOL)
383 temp_return= binary_stats_fetch(ptr, stats + x, args, x);
384 else
385 temp_return= ascii_stats_fetch(ptr, stats + x, args, x);
386
387 if (temp_return != MEMCACHED_SUCCESS)
388 rc= MEMCACHED_SOME_ERRORS;
389 }
390
391 *error= rc;
392 return stats;
393 }
394
395 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
396 char *hostname, unsigned int port)
397 {
398 memcached_return rc;
399 memcached_st memc;
400
401 memcached_create(&memc);
402
403 memcached_server_add(&memc, hostname, port);
404
405 if (memc.flags & MEM_BINARY_PROTOCOL)
406 rc= binary_stats_fetch(&memc, stat, args, 0);
407 else
408 rc= ascii_stats_fetch(&memc, stat, args, 0);
409
410 memcached_free(&memc);
411
412 return rc;
413 }
414
415 /*
416 We make a copy of the keys since at some point in the not so distant future
417 we will add support for "found" keys.
418 */
419 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat __attribute__((unused)),
420 memcached_return *error)
421 {
422 char **list;
423 size_t length= sizeof(memcached_stat_keys);
424
425 list= ptr->call_malloc(ptr, length);
426
427 if (!list)
428 {
429 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
430 return NULL;
431 }
432
433 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
434
435 *error= MEMCACHED_SUCCESS;
436
437 return list;
438 }
439
440 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
441 {
442 if (stat == NULL)
443 {
444 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
445 return;
446 }
447
448 if (ptr)
449 ptr->call_free(ptr, stat);
450 else
451 free(stat);
452 }