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