Fix IO call to use bool.
[m6w6/libmemcached] / libmemcached / stats.c
1 /*
2 */
3
4 #include "common.h"
5
6 static const 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_t set_data(memcached_stat_st *memc_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 memc_stat->pid= (uint32_t) strtol(value, (char **)NULL, 10);
44 }
45 else if (!strcmp("uptime", key))
46 {
47 memc_stat->uptime= (uint32_t) strtol(value, (char **)NULL, 10);
48 }
49 else if (!strcmp("time", key))
50 {
51 memc_stat->time= (uint32_t) strtol(value, (char **)NULL, 10);
52 }
53 else if (!strcmp("version", key))
54 {
55 memcpy(memc_stat->version, value, strlen(value));
56 memc_stat->version[strlen(value)]= 0;
57 }
58 else if (!strcmp("pointer_size", key))
59 {
60 memc_stat->pointer_size= (uint32_t) 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 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);
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 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);
79 }
80 else if (!strcmp("curr_items", key))
81 {
82 memc_stat->curr_items= (uint32_t) strtol(value, (char **)NULL, 10);
83 }
84 else if (!strcmp("total_items", key))
85 {
86 memc_stat->total_items= (uint32_t) strtol(value, (char **)NULL, 10);
87 }
88 else if (!strcmp("bytes_read", key))
89 {
90 memc_stat->bytes_read= (uint32_t) strtoll(value, (char **)NULL, 10);
91 }
92 else if (!strcmp("bytes_written", key))
93 {
94 memc_stat->bytes_written= (uint32_t) strtoll(value, (char **)NULL, 10);
95 }
96 else if (!strcmp("bytes", key))
97 {
98 memc_stat->bytes= (uint32_t) strtoll(value, (char **)NULL, 10);
99 }
100 else if (!strcmp("curr_connections", key))
101 {
102 memc_stat->curr_connections= (uint32_t) strtoll(value, (char **)NULL, 10);
103 }
104 else if (!strcmp("total_connections", key))
105 {
106 memc_stat->total_connections= (uint32_t) strtoll(value, (char **)NULL, 10);
107 }
108 else if (!strcmp("connection_structures", key))
109 {
110 memc_stat->connection_structures= (uint32_t) strtol(value, (char **)NULL, 10);
111 }
112 else if (!strcmp("cmd_get", key))
113 {
114 memc_stat->cmd_get= (uint64_t) strtoll(value, (char **)NULL, 10);
115 }
116 else if (!strcmp("cmd_set", key))
117 {
118 memc_stat->cmd_set= (uint64_t) strtoll(value, (char **)NULL, 10);
119 }
120 else if (!strcmp("get_hits", key))
121 {
122 memc_stat->get_hits= (uint64_t) strtoll(value, (char **)NULL, 10);
123 }
124 else if (!strcmp("get_misses", key))
125 {
126 memc_stat->get_misses= (uint64_t)strtoll(value, (char **)NULL, 10);
127 }
128 else if (!strcmp("evictions", key))
129 {
130 memc_stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10);
131 }
132 else if (!strcmp("limit_maxbytes", key))
133 {
134 memc_stat->limit_maxbytes= (uint64_t) strtoll(value, (char **)NULL, 10);
135 }
136 else if (!strcmp("threads", key))
137 {
138 memc_stat->threads= (uint32_t) 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 strcmp("conn_yields", key) == 0 ||
153 strcmp("auth_cmds", key) == 0 ||
154 strcmp("auth_errors", key) == 0))
155 {
156 WATCHPOINT_STRING(key);
157 return MEMCACHED_UNKNOWN_STAT_KEY;
158 }
159
160 return MEMCACHED_SUCCESS;
161 }
162
163 char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *memc_stat,
164 const char *key, memcached_return_t *error)
165 {
166 char buffer[SMALL_STRING_LEN];
167 int length;
168 char *ret;
169
170 *error= MEMCACHED_SUCCESS;
171
172 if (!memcmp("pid", key, strlen("pid")))
173 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pid);
174 else if (!memcmp("uptime", key, strlen("uptime")))
175 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->uptime);
176 else if (!memcmp("time", key, strlen("time")))
177 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->time);
178 else if (!memcmp("version", key, strlen("version")))
179 length= snprintf(buffer, SMALL_STRING_LEN,"%s", memc_stat->version);
180 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
181 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pointer_size);
182 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
183 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_user_seconds, memc_stat->rusage_user_microseconds);
184 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
185 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_system_seconds, memc_stat->rusage_system_microseconds);
186 else if (!memcmp("curr_items", key, strlen("curr_items")))
187 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_items);
188 else if (!memcmp("total_items", key, strlen("total_items")))
189 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_items);
190 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
191 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_connections);
192 else if (!memcmp("total_connections", key, strlen("total_connections")))
193 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_connections);
194 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
195 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->connection_structures);
196 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
197 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_get);
198 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
199 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_set);
200 else if (!memcmp("get_hits", key, strlen("get_hits")))
201 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_hits);
202 else if (!memcmp("get_misses", key, strlen("get_misses")))
203 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_misses);
204 else if (!memcmp("evictions", key, strlen("evictions")))
205 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->evictions);
206 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
207 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_read);
208 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
209 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_written);
210 else if (!memcmp("bytes", key, strlen("bytes")))
211 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes);
212 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
213 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->limit_maxbytes);
214 else if (!memcmp("threads", key, strlen("threads")))
215 length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->threads);
216 else
217 {
218 *error= MEMCACHED_NOTFOUND;
219 return NULL;
220 }
221
222 ret= libmemcached_malloc(ptr, (size_t) (length + 1));
223 memcpy(ret, buffer, (size_t) length);
224 ret[length]= '\0';
225
226 return ret;
227 }
228
229 static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
230 char *args,
231 memcached_server_instance_st *instance)
232 {
233 memcached_return_t rc;
234
235 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
236 protocol_binary_request_stats request= {.bytes= {0}};
237 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
238 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT;
239 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
240
241 if (args != NULL)
242 {
243 size_t len= strlen(args);
244
245 rc= memcached_validate_key_length(len, true);
246 unlikely (rc != MEMCACHED_SUCCESS)
247 return rc;
248
249 request.message.header.request.keylen= htons((uint16_t)len);
250 request.message.header.request.bodylen= htonl((uint32_t) len);
251
252 if ((memcached_do(instance, request.bytes,
253 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
254 (memcached_io_write(instance, args, len, true) == -1))
255 {
256 memcached_io_reset(instance);
257 return MEMCACHED_WRITE_FAILURE;
258 }
259 }
260 else
261 {
262 if (memcached_do(instance, request.bytes,
263 sizeof(request.bytes), 1) != MEMCACHED_SUCCESS)
264 {
265 memcached_io_reset(instance);
266 return MEMCACHED_WRITE_FAILURE;
267 }
268 }
269
270 memcached_server_response_decrement(instance);
271 do
272 {
273 rc= memcached_response(instance, buffer,
274 sizeof(buffer), NULL);
275 if (rc == MEMCACHED_END)
276 break;
277
278 unlikely (rc != MEMCACHED_SUCCESS)
279 {
280 memcached_io_reset(instance);
281 return rc;
282 }
283
284 unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY)
285 {
286 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
287 WATCHPOINT_ASSERT(0);
288 }
289 } while (1);
290
291 /* shit... memcached_response will decrement the counter, so I need to
292 ** reset it.. todo: look at this and try to find a better solution.
293 */
294 instance->cursor_active= 0;
295
296 return MEMCACHED_SUCCESS;
297 }
298
299 static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
300 char *args,
301 memcached_server_instance_st *instance)
302 {
303 memcached_return_t rc;
304 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
305 size_t send_length;
306
307 if (args)
308 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
309 "stats %s\r\n", args);
310 else
311 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
312 "stats\r\n");
313
314 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
315 return MEMCACHED_WRITE_FAILURE;
316
317 rc= memcached_do(instance, buffer, send_length, 1);
318 if (rc != MEMCACHED_SUCCESS)
319 goto error;
320
321 while (1)
322 {
323 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
324
325 if (rc == MEMCACHED_STAT)
326 {
327 char *string_ptr, *end_ptr;
328 char *key, *value;
329
330 string_ptr= buffer;
331 string_ptr+= 5; /* Move past STAT */
332 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
333 key= string_ptr;
334 key[(size_t)(end_ptr-string_ptr)]= 0;
335
336 string_ptr= end_ptr + 1;
337 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++);
338 value= string_ptr;
339 value[(size_t)(end_ptr-string_ptr)]= 0;
340 string_ptr= end_ptr + 2;
341 unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY)
342 {
343 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
344 WATCHPOINT_ASSERT(0);
345 }
346 }
347 else
348 break;
349 }
350
351 error:
352 if (rc == MEMCACHED_END)
353 return MEMCACHED_SUCCESS;
354 else
355 return rc;
356 }
357
358 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return_t *error)
359 {
360 memcached_return_t rc;
361 memcached_stat_st *stats;
362
363 unlikely (ptr->flags.use_udp)
364 {
365 *error= MEMCACHED_NOT_SUPPORTED;
366 return NULL;
367 }
368
369 stats= libmemcached_calloc(ptr, memcached_server_count(ptr), 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 (uint32_t x= 0; x < memcached_server_count(ptr); x++)
379 {
380 memcached_return_t temp_return;
381 memcached_server_instance_st *instance;
382 memcached_stat_st *stat_instance;
383
384 stat_instance= stats + x;
385
386 stat_instance->root= ptr;
387
388 instance= memcached_server_instance_fetch(ptr, x);
389
390 if (ptr->flags.binary_protocol)
391 {
392 temp_return= binary_stats_fetch(stat_instance, args, instance);
393 }
394 else
395 {
396 temp_return= ascii_stats_fetch(stat_instance, args, instance);
397 }
398
399 if (temp_return != MEMCACHED_SUCCESS)
400 rc= MEMCACHED_SOME_ERRORS;
401 }
402
403 *error= rc;
404 return stats;
405 }
406
407 memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char *args,
408 const char *hostname, in_port_t port)
409 {
410 memcached_return_t rc;
411 memcached_st memc;
412 memcached_st *memc_ptr;
413 memcached_server_instance_st *instance;
414
415 memc_ptr= memcached_create(&memc);
416 WATCHPOINT_ASSERT(memc_ptr);
417
418 memcached_server_add(&memc, hostname, port);
419
420 instance= memcached_server_instance_fetch(memc_ptr, 0);
421
422 if (memc.flags.binary_protocol)
423 {
424 rc= binary_stats_fetch(memc_stat, args, instance);
425 }
426 else
427 {
428 rc= ascii_stats_fetch(memc_stat, args, instance);
429 }
430
431 memcached_free(&memc);
432
433 return rc;
434 }
435
436 /*
437 We make a copy of the keys since at some point in the not so distant future
438 we will add support for "found" keys.
439 */
440 char ** memcached_stat_get_keys(memcached_st *ptr,
441 memcached_stat_st *memc_stat,
442 memcached_return_t *error)
443 {
444 char **list;
445 size_t length= sizeof(memcached_stat_keys);
446
447 (void)memc_stat;
448
449 list= libmemcached_malloc(ptr, length);
450
451 if (! list)
452 {
453 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
454 return NULL;
455 }
456
457 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
458
459 *error= MEMCACHED_SUCCESS;
460
461 return list;
462 }
463
464 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *memc_stat)
465 {
466 if (memc_stat == NULL)
467 {
468 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
469 return;
470 }
471
472 if (memc_stat->root)
473 {
474 libmemcached_free(memc_stat->root, memc_stat);
475 }
476 else if (ptr)
477 {
478 libmemcached_free(ptr, memc_stat);
479 }
480 else
481 {
482 free(memc_stat);
483 }
484 }