1c8c174f8c5d333f33ca95ea891b7065a1cadfa5
[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 {
149 fprintf(stderr, "Unknown key %s\n", key);
150 }
151 }
152
153 char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat,
154 const char *key, memcached_return *error)
155 {
156 char buffer[SMALL_STRING_LEN];
157 size_t length;
158 char *ret;
159
160 *error= MEMCACHED_SUCCESS;
161
162 if (!memcmp("pid", key, strlen("pid")))
163 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
164 else if (!memcmp("uptime", key, strlen("uptime")))
165 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
166 else if (!memcmp("time", key, strlen("time")))
167 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
168 else if (!memcmp("version", key, strlen("version")))
169 length= snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
170 else if (!memcmp("pointer_size", key, strlen("pointer_size")))
171 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
172 else if (!memcmp("rusage_user", key, strlen("rusage_user")))
173 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_user_seconds, stat->rusage_user_microseconds);
174 else if (!memcmp("rusage_system", key, strlen("rusage_system")))
175 length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_system_seconds, stat->rusage_system_microseconds);
176 else if (!memcmp("curr_items", key, strlen("curr_items")))
177 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
178 else if (!memcmp("total_items", key, strlen("total_items")))
179 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
180 else if (!memcmp("bytes", key, strlen("bytes")))
181 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes);
182 else if (!memcmp("curr_connections", key, strlen("curr_connections")))
183 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
184 else if (!memcmp("total_connections", key, strlen("total_connections")))
185 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
186 else if (!memcmp("connection_structures", key, strlen("connection_structures")))
187 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
188 else if (!memcmp("cmd_get", key, strlen("cmd_get")))
189 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_get);
190 else if (!memcmp("cmd_set", key, strlen("cmd_set")))
191 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_set);
192 else if (!memcmp("get_hits", key, strlen("get_hits")))
193 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_hits);
194 else if (!memcmp("get_misses", key, strlen("get_misses")))
195 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_misses);
196 else if (!memcmp("evictions", key, strlen("evictions")))
197 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->evictions);
198 else if (!memcmp("bytes_read", key, strlen("bytes_read")))
199 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_read);
200 else if (!memcmp("bytes_written", key, strlen("bytes_written")))
201 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written);
202 else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
203 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->limit_maxbytes);
204 else if (!memcmp("threads", key, strlen("threads")))
205 length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
206 else
207 {
208 *error= MEMCACHED_NOTFOUND;
209 return NULL;
210 }
211
212 if (ptr->call_malloc)
213 ret= ptr->call_malloc(ptr, length + 1);
214 else
215 ret= malloc(length + 1);
216 memcpy(ret, buffer, length);
217 ret[length]= '\0';
218
219 return ret;
220 }
221
222 static memcached_return binary_stats_fetch(memcached_st *ptr,
223 memcached_stat_st *stat,
224 char *args,
225 unsigned int server_key)
226 {
227 memcached_return rc;
228
229 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
230 protocol_binary_request_stats request= {.bytes= {0}};
231 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
232 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT;
233 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
234
235 if (args != NULL)
236 {
237 int len= strlen(args);
238
239 rc= memcached_validate_key_length(len, true);
240 unlikely (rc != MEMCACHED_SUCCESS)
241 return rc;
242
243 request.message.header.request.keylen= htons((uint16_t)len);
244 request.message.header.request.bodylen= htonl(len);
245
246 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
247 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
248 (memcached_io_write(&ptr->hosts[server_key], args, len, 1) == -1))
249 {
250 memcached_io_reset(&ptr->hosts[server_key]);
251 return MEMCACHED_WRITE_FAILURE;
252 }
253 }
254 else
255 {
256 if (memcached_do(&ptr->hosts[server_key], request.bytes,
257 sizeof(request.bytes), 1) != MEMCACHED_SUCCESS)
258 {
259 memcached_io_reset(&ptr->hosts[server_key]);
260 return MEMCACHED_WRITE_FAILURE;
261 }
262 }
263
264 memcached_server_response_decrement(&ptr->hosts[server_key]);
265 do
266 {
267 rc= memcached_response(&ptr->hosts[server_key], buffer,
268 sizeof(buffer), NULL);
269 if (rc == MEMCACHED_END)
270 break;
271
272 unlikely (rc != MEMCACHED_SUCCESS)
273 {
274 memcached_io_reset(&ptr->hosts[server_key]);
275 return rc;
276 }
277
278 set_data(stat, buffer, buffer + strlen(buffer) + 1);
279 } while (1);
280
281 /* shit... memcached_response will decrement the counter, so I need to
282 ** reset it.. todo: look at this and try to find a better solution.
283 */
284 ptr->hosts[server_key].cursor_active= 0;
285
286 return MEMCACHED_SUCCESS;
287 }
288
289 static memcached_return ascii_stats_fetch(memcached_st *ptr,
290 memcached_stat_st *stat,
291 char *args,
292 unsigned int server_key)
293 {
294 memcached_return rc;
295 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
296 size_t send_length;
297
298 if (args)
299 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
300 "stats %s\r\n", args);
301 else
302 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
303 "stats\r\n");
304
305 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
306 return MEMCACHED_WRITE_FAILURE;
307
308 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
309 if (rc != MEMCACHED_SUCCESS)
310 goto error;
311
312 while (1)
313 {
314 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
315
316 if (rc == MEMCACHED_STAT)
317 {
318 char *string_ptr, *end_ptr;
319 char *key, *value;
320
321 string_ptr= buffer;
322 string_ptr+= 5; /* Move past STAT */
323 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
324 key= string_ptr;
325 key[(size_t)(end_ptr-string_ptr)]= 0;
326
327 string_ptr= end_ptr + 1;
328 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++);
329 value= string_ptr;
330 value[(size_t)(end_ptr-string_ptr)]= 0;
331 string_ptr= end_ptr + 2;
332 set_data(stat, key, value);
333 }
334 else
335 break;
336 }
337
338 error:
339 if (rc == MEMCACHED_END)
340 return MEMCACHED_SUCCESS;
341 else
342 return rc;
343 }
344
345 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
346 {
347 unsigned int x;
348 memcached_return rc;
349 memcached_stat_st *stats;
350
351 if (ptr->flags & MEM_USE_UDP)
352 {
353 *error= MEMCACHED_NOT_SUPPORTED;
354 return NULL;
355 }
356
357 if (ptr->call_malloc)
358 stats= (memcached_stat_st *)ptr->call_malloc(ptr, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
359 else
360 stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts));
361
362 if (!stats)
363 {
364 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
365 return NULL;
366 }
367 memset(stats, 0, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
368
369 rc= MEMCACHED_SUCCESS;
370 for (x= 0; x < ptr->number_of_hosts; x++)
371 {
372 memcached_return temp_return;
373
374 if (ptr->flags & MEM_BINARY_PROTOCOL)
375 temp_return= binary_stats_fetch(ptr, stats + x, args, x);
376 else
377 temp_return= ascii_stats_fetch(ptr, stats + x, args, x);
378
379 if (temp_return != MEMCACHED_SUCCESS)
380 rc= MEMCACHED_SOME_ERRORS;
381 }
382
383 *error= rc;
384 return stats;
385 }
386
387 memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args,
388 char *hostname, unsigned int port)
389 {
390 memcached_return rc;
391 memcached_st memc;
392
393 memcached_create(&memc);
394
395 memcached_server_add(&memc, hostname, port);
396
397 if (memc.flags & MEM_BINARY_PROTOCOL)
398 rc= binary_stats_fetch(&memc, stat, args, 0);
399 else
400 rc= ascii_stats_fetch(&memc, stat, args, 0);
401
402 memcached_free(&memc);
403
404 return rc;
405 }
406
407 /*
408 We make a copy of the keys since at some point in the not so distant future
409 we will add support for "found" keys.
410 */
411 char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat __attribute__((unused)),
412 memcached_return *error)
413 {
414 char **list;
415 size_t length= sizeof(memcached_stat_keys);
416
417 if (ptr->call_malloc)
418 list= (char **)ptr->call_malloc(ptr, length);
419 else
420 list= (char **)malloc(length);
421
422 if (!list)
423 {
424 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
425 return NULL;
426 }
427 memset(list, 0, sizeof(memcached_stat_keys));
428
429 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
430
431 *error= MEMCACHED_SUCCESS;
432
433 return list;
434 }
435
436 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
437 {
438 if (stat == NULL)
439 {
440 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
441 return;
442 }
443
444 if (ptr && ptr->call_free)
445 ptr->call_free(ptr, stat);
446 else
447 free(stat);
448 }