X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fmemcached_stats.c;h=1c8c174f8c5d333f33ca95ea891b7065a1cadfa5;hb=93bcca389f1f69f53141b51acc91633ddca2bd94;hp=a1ef99f3a945b374bdf7b5ad1d3bbc05e7da4a26;hpb=34a8c3858f30b02568c87f56a827f618aba6d6be;p=awesomized%2Flibmemcached diff --git a/libmemcached/memcached_stats.c b/libmemcached/memcached_stats.c index a1ef99f3..1c8c174f 100644 --- a/libmemcached/memcached_stats.c +++ b/libmemcached/memcached_stats.c @@ -84,6 +84,14 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) { stat->total_items= strtol(value, (char **)NULL, 10); } + else if (!strcmp("bytes_read", key)) + { + stat->bytes_read= strtoll(value, (char **)NULL, 10); + } + else if (!strcmp("bytes_written", key)) + { + stat->bytes_written= strtoll(value, (char **)NULL, 10); + } else if (!strcmp("bytes", key)) { stat->bytes= strtoll(value, (char **)NULL, 10); @@ -120,30 +128,30 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) { stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10); } - else if (!strcmp("bytes_read", key)) - { - stat->bytes_read= strtoll(value, (char **)NULL, 10); - } - else if (!strcmp("bytes_written", key)) - { - stat->bytes_written= strtoll(value, (char **)NULL, 10); - } else if (!strcmp("limit_maxbytes", key)) { - stat->limit_maxbytes= strtol(value, (char **)NULL, 10); + stat->limit_maxbytes= strtoll(value, (char **)NULL, 10); } else if (!strcmp("threads", key)) { - stat->threads= strtol(key, (char **)NULL, 10); + stat->threads= strtol(value, (char **)NULL, 10); } - else + else if (!(strcmp("delete_misses", key) == 0 ||/* New stats in the 1.3 beta */ + strcmp("delete_hits", key) == 0 ||/* Just swallow them for now.. */ + strcmp("incr_misses", key) == 0 || + strcmp("incr_hits", key) == 0 || + strcmp("decr_misses", key) == 0 || + strcmp("decr_hits", key) == 0 || + strcmp("cas_misses", key) == 0 || + strcmp("cas_hits", key) == 0 || + strcmp("cas_badval", key) == 0)) { fprintf(stderr, "Unknown key %s\n", key); } } char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, - char *key, memcached_return *error) + const char *key, memcached_return *error) { char buffer[SMALL_STRING_LEN]; size_t length; @@ -192,7 +200,7 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, else if (!memcmp("bytes_written", key, strlen("bytes_written"))) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written); else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) - length= snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->limit_maxbytes); + length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->limit_maxbytes); else if (!memcmp("threads", key, strlen("threads"))) length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads); else @@ -211,7 +219,74 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, return ret; } -static memcached_return memcached_stats_fetch(memcached_st *ptr, +static memcached_return binary_stats_fetch(memcached_st *ptr, + memcached_stat_st *stat, + char *args, + unsigned int server_key) +{ + memcached_return rc; + + char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + protocol_binary_request_stats request= {.bytes= {0}}; + request.message.header.request.magic= PROTOCOL_BINARY_REQ; + request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT; + request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; + + if (args != NULL) + { + int len= strlen(args); + + rc= memcached_validate_key_length(len, true); + unlikely (rc != MEMCACHED_SUCCESS) + return rc; + + request.message.header.request.keylen= htons((uint16_t)len); + request.message.header.request.bodylen= htonl(len); + + if ((memcached_do(&ptr->hosts[server_key], request.bytes, + sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) || + (memcached_io_write(&ptr->hosts[server_key], args, len, 1) == -1)) + { + memcached_io_reset(&ptr->hosts[server_key]); + return MEMCACHED_WRITE_FAILURE; + } + } + else + { + if (memcached_do(&ptr->hosts[server_key], request.bytes, + sizeof(request.bytes), 1) != MEMCACHED_SUCCESS) + { + memcached_io_reset(&ptr->hosts[server_key]); + return MEMCACHED_WRITE_FAILURE; + } + } + + memcached_server_response_decrement(&ptr->hosts[server_key]); + do + { + rc= memcached_response(&ptr->hosts[server_key], buffer, + sizeof(buffer), NULL); + if (rc == MEMCACHED_END) + break; + + unlikely (rc != MEMCACHED_SUCCESS) + { + memcached_io_reset(&ptr->hosts[server_key]); + return rc; + } + + set_data(stat, buffer, buffer + strlen(buffer) + 1); + } while (1); + + /* shit... memcached_response will decrement the counter, so I need to + ** reset it.. todo: look at this and try to find a better solution. + */ + ptr->hosts[server_key].cursor_active= 0; + + return MEMCACHED_SUCCESS; +} + +static memcached_return ascii_stats_fetch(memcached_st *ptr, memcached_stat_st *stat, char *args, unsigned int server_key) @@ -273,6 +348,12 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur memcached_return rc; memcached_stat_st *stats; + if (ptr->flags & MEM_USE_UDP) + { + *error= MEMCACHED_NOT_SUPPORTED; + return NULL; + } + if (ptr->call_malloc) stats= (memcached_stat_st *)ptr->call_malloc(ptr, sizeof(memcached_stat_st)*(ptr->number_of_hosts)); else @@ -281,11 +362,6 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur if (!stats) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; - if (ptr->call_free) - ptr->call_free(ptr, stats); - else - free(stats); - return NULL; } memset(stats, 0, sizeof(memcached_stat_st)*(ptr->number_of_hosts)); @@ -294,8 +370,12 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur for (x= 0; x < ptr->number_of_hosts; x++) { memcached_return temp_return; - - temp_return= memcached_stats_fetch(ptr, stats + x, args, x); + + if (ptr->flags & MEM_BINARY_PROTOCOL) + temp_return= binary_stats_fetch(ptr, stats + x, args, x); + else + temp_return= ascii_stats_fetch(ptr, stats + x, args, x); + if (temp_return != MEMCACHED_SUCCESS) rc= MEMCACHED_SOME_ERRORS; } @@ -314,7 +394,10 @@ memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args, memcached_server_add(&memc, hostname, port); - rc= memcached_stats_fetch(&memc, stat, args, 0); + if (memc.flags & MEM_BINARY_PROTOCOL) + rc= binary_stats_fetch(&memc, stat, args, 0); + else + rc= ascii_stats_fetch(&memc, stat, args, 0); memcached_free(&memc); @@ -325,7 +408,7 @@ memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args, We make a copy of the keys since at some point in the not so distant future we will add support for "found" keys. */ -char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat, +char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat __attribute__((unused)), memcached_return *error) { char **list;