6 static char *memcached_stat_keys
[] = {
19 "connection_structures",
33 static void set_data(memcached_stat_st
*stat
, char *key
, char *value
)
38 fprintf(stderr
, "Invalid key %s\n", key
);
40 else if (!strcmp("pid", key
))
42 stat
->pid
= strtol(value
, (char **)NULL
, 10);
44 else if (!strcmp("uptime", key
))
46 stat
->uptime
= strtol(value
, (char **)NULL
, 10);
48 else if (!strcmp("time", key
))
50 stat
->time
= strtol(value
, (char **)NULL
, 10);
52 else if (!strcmp("version", key
))
54 memcpy(stat
->version
, value
, strlen(value
));
55 stat
->version
[strlen(value
)]= 0;
57 else if (!strcmp("pointer_size", key
))
59 stat
->pointer_size
= strtol(value
, (char **)NULL
, 10);
61 else if (!strcmp("rusage_user", key
))
64 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
67 stat
->rusage_user_seconds
= strtol(value
, (char **)NULL
, 10);
68 stat
->rusage_user_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
70 else if (!strcmp("rusage_system", key
))
73 for (walk_ptr
= value
; (!ispunct(*walk_ptr
)); walk_ptr
++);
76 stat
->rusage_system_seconds
= strtol(value
, (char **)NULL
, 10);
77 stat
->rusage_system_microseconds
= strtol(walk_ptr
, (char **)NULL
, 10);
79 else if (!strcmp("curr_items", key
))
81 stat
->curr_items
= strtol(value
, (char **)NULL
, 10);
83 else if (!strcmp("total_items", key
))
85 stat
->total_items
= strtol(value
, (char **)NULL
, 10);
87 else if (!strcmp("bytes_read", key
))
89 stat
->bytes_read
= strtoll(value
, (char **)NULL
, 10);
91 else if (!strcmp("bytes_written", key
))
93 stat
->bytes_written
= strtoll(value
, (char **)NULL
, 10);
95 else if (!strcmp("bytes", key
))
97 stat
->bytes
= strtoll(value
, (char **)NULL
, 10);
99 else if (!strcmp("curr_connections", key
))
101 stat
->curr_connections
= strtoll(value
, (char **)NULL
, 10);
103 else if (!strcmp("total_connections", key
))
105 stat
->total_connections
= strtoll(value
, (char **)NULL
, 10);
107 else if (!strcmp("connection_structures", key
))
109 stat
->connection_structures
= strtol(value
, (char **)NULL
, 10);
111 else if (!strcmp("cmd_get", key
))
113 stat
->cmd_get
= strtoll(value
, (char **)NULL
, 10);
115 else if (!strcmp("cmd_set", key
))
117 stat
->cmd_set
= strtoll(value
, (char **)NULL
, 10);
119 else if (!strcmp("get_hits", key
))
121 stat
->get_hits
= strtoll(value
, (char **)NULL
, 10);
123 else if (!strcmp("get_misses", key
))
125 stat
->get_misses
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
127 else if (!strcmp("evictions", key
))
129 stat
->evictions
= (uint64_t)strtoll(value
, (char **)NULL
, 10);
131 else if (!strcmp("limit_maxbytes", key
))
133 stat
->limit_maxbytes
= strtoll(value
, (char **)NULL
, 10);
135 else if (!strcmp("threads", key
))
137 stat
->threads
= strtol(value
, (char **)NULL
, 10);
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))
149 fprintf(stderr
, "Unknown key %s\n", key
);
153 char *memcached_stat_get_value(memcached_st
*ptr
, memcached_stat_st
*stat
,
154 const char *key
, memcached_return
*error
)
156 char buffer
[SMALL_STRING_LEN
];
160 *error
= MEMCACHED_SUCCESS
;
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
);
208 *error
= MEMCACHED_NOTFOUND
;
212 if (ptr
->call_malloc
)
213 ret
= ptr
->call_malloc(ptr
, length
+ 1);
215 ret
= malloc(length
+ 1);
216 memcpy(ret
, buffer
, length
);
222 static memcached_return
binary_stats_fetch(memcached_st
*ptr
,
223 memcached_stat_st
*stat
,
225 unsigned int server_key
)
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
;
237 int len
= strlen(args
);
239 rc
= memcached_validate_key_length(len
, true);
240 unlikely (rc
!= MEMCACHED_SUCCESS
)
243 request
.message
.header
.request
.keylen
= htons((uint16_t)len
);
244 request
.message
.header
.request
.bodylen
= htonl(len
);
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))
250 memcached_io_reset(&ptr
->hosts
[server_key
]);
251 return MEMCACHED_WRITE_FAILURE
;
256 if (memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
257 sizeof(request
.bytes
), 1) != MEMCACHED_SUCCESS
)
259 memcached_io_reset(&ptr
->hosts
[server_key
]);
260 return MEMCACHED_WRITE_FAILURE
;
264 memcached_server_response_decrement(&ptr
->hosts
[server_key
]);
267 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
,
268 sizeof(buffer
), NULL
);
269 if (rc
== MEMCACHED_END
)
272 unlikely (rc
!= MEMCACHED_SUCCESS
)
274 memcached_io_reset(&ptr
->hosts
[server_key
]);
278 set_data(stat
, buffer
, buffer
+ strlen(buffer
) + 1);
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.
284 ptr
->hosts
[server_key
].cursor_active
= 0;
286 return MEMCACHED_SUCCESS
;
289 static memcached_return
ascii_stats_fetch(memcached_st
*ptr
,
290 memcached_stat_st
*stat
,
292 unsigned int server_key
)
295 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
299 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
300 "stats %s\r\n", args
);
302 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
305 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
306 return MEMCACHED_WRITE_FAILURE
;
308 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, 1);
309 if (rc
!= MEMCACHED_SUCCESS
)
314 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
316 if (rc
== MEMCACHED_STAT
)
318 char *string_ptr
, *end_ptr
;
322 string_ptr
+= 5; /* Move past STAT */
323 for (end_ptr
= string_ptr
; isgraph(*end_ptr
); end_ptr
++);
325 key
[(size_t)(end_ptr
-string_ptr
)]= 0;
327 string_ptr
= end_ptr
+ 1;
328 for (end_ptr
= string_ptr
; !(isspace(*end_ptr
)); end_ptr
++);
330 value
[(size_t)(end_ptr
-string_ptr
)]= 0;
331 string_ptr
= end_ptr
+ 2;
332 set_data(stat
, key
, value
);
339 if (rc
== MEMCACHED_END
)
340 return MEMCACHED_SUCCESS
;
345 memcached_stat_st
*memcached_stat(memcached_st
*ptr
, char *args
, memcached_return
*error
)
349 memcached_stat_st
*stats
;
351 if (ptr
->flags
& MEM_USE_UDP
)
353 *error
= MEMCACHED_NOT_SUPPORTED
;
357 if (ptr
->call_malloc
)
358 stats
= (memcached_stat_st
*)ptr
->call_malloc(ptr
, sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
360 stats
= (memcached_stat_st
*)malloc(sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
364 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
367 memset(stats
, 0, sizeof(memcached_stat_st
)*(ptr
->number_of_hosts
));
369 rc
= MEMCACHED_SUCCESS
;
370 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
372 memcached_return temp_return
;
374 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
375 temp_return
= binary_stats_fetch(ptr
, stats
+ x
, args
, x
);
377 temp_return
= ascii_stats_fetch(ptr
, stats
+ x
, args
, x
);
379 if (temp_return
!= MEMCACHED_SUCCESS
)
380 rc
= MEMCACHED_SOME_ERRORS
;
387 memcached_return
memcached_stat_servername(memcached_stat_st
*stat
, char *args
,
388 char *hostname
, unsigned int port
)
393 memcached_create(&memc
);
395 memcached_server_add(&memc
, hostname
, port
);
397 if (memc
.flags
& MEM_BINARY_PROTOCOL
)
398 rc
= binary_stats_fetch(&memc
, stat
, args
, 0);
400 rc
= ascii_stats_fetch(&memc
, stat
, args
, 0);
402 memcached_free(&memc
);
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.
411 char ** memcached_stat_get_keys(memcached_st
*ptr
, memcached_stat_st
*stat
__attribute__((unused
)),
412 memcached_return
*error
)
415 size_t length
= sizeof(memcached_stat_keys
);
417 if (ptr
->call_malloc
)
418 list
= (char **)ptr
->call_malloc(ptr
, length
);
420 list
= (char **)malloc(length
);
424 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
427 memset(list
, 0, sizeof(memcached_stat_keys
));
429 memcpy(list
, memcached_stat_keys
, sizeof(memcached_stat_keys
));
431 *error
= MEMCACHED_SUCCESS
;
436 void memcached_stat_free(memcached_st
*ptr
, memcached_stat_st
*stat
)
440 WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
444 if (ptr
&& ptr
->call_free
)
445 ptr
->call_free(ptr
, stat
);