X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fstats.c;h=45b530e491cbfab745576ec6c34f5bd742a6aecf;hb=2a1e29bce497c25fad73b9d74db4a3daf74413b5;hp=fcafe2154c910ca47f29c3e6b76f59fb133f1421;hpb=ba24508f51eed2af4f0192589801db2f945eac7b;p=m6w6%2Flibmemcached diff --git a/libmemcached/stats.c b/libmemcached/stats.c index fcafe215..45b530e4 100644 --- a/libmemcached/stats.c +++ b/libmemcached/stats.c @@ -29,6 +29,13 @@ static const char *memcached_stat_keys[] = { NULL }; +struct local_context +{ + memcached_stat_fn func; + void *context; + const char *args; +}; + static memcached_return_t set_data(memcached_stat_st *memc_stat, char *key, char *value) { @@ -155,7 +162,8 @@ static memcached_return_t set_data(memcached_stat_st *memc_stat, char *key, char strcmp("reclaimed", key) == 0)) { WATCHPOINT_STRING(key); - return MEMCACHED_UNKNOWN_STAT_KEY; + /* return MEMCACHED_UNKNOWN_STAT_KEY; */ + return MEMCACHED_SUCCESS; } return MEMCACHED_SUCCESS; @@ -170,49 +178,49 @@ char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_ *error= MEMCACHED_SUCCESS; - if (!memcmp("pid", key, strlen("pid"))) + if (!memcmp("pid", key, sizeof("pid") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pid); - else if (!memcmp("uptime", key, strlen("uptime"))) + else if (!memcmp("uptime", key, sizeof("uptime") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->uptime); - else if (!memcmp("time", key, strlen("time"))) + else if (!memcmp("time", key, sizeof("time") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->time); - else if (!memcmp("version", key, strlen("version"))) + else if (!memcmp("version", key, sizeof("version") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%s", memc_stat->version); - else if (!memcmp("pointer_size", key, strlen("pointer_size"))) + else if (!memcmp("pointer_size", key, sizeof("pointer_size") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pointer_size); - else if (!memcmp("rusage_user", key, strlen("rusage_user"))) + else if (!memcmp("rusage_user", key, sizeof("rusage_user") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_user_seconds, memc_stat->rusage_user_microseconds); - else if (!memcmp("rusage_system", key, strlen("rusage_system"))) + else if (!memcmp("rusage_system", key, sizeof("rusage_system") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_system_seconds, memc_stat->rusage_system_microseconds); - else if (!memcmp("curr_items", key, strlen("curr_items"))) + else if (!memcmp("curr_items", key, sizeof("curr_items") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_items); - else if (!memcmp("total_items", key, strlen("total_items"))) + else if (!memcmp("total_items", key, sizeof("total_items") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_items); - else if (!memcmp("curr_connections", key, strlen("curr_connections"))) + else if (!memcmp("curr_connections", key, sizeof("curr_connections") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_connections); - else if (!memcmp("total_connections", key, strlen("total_connections"))) + else if (!memcmp("total_connections", key, sizeof("total_connections") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_connections); - else if (!memcmp("connection_structures", key, strlen("connection_structures"))) + else if (!memcmp("connection_structures", key, sizeof("connection_structures") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->connection_structures); - else if (!memcmp("cmd_get", key, strlen("cmd_get"))) + else if (!memcmp("cmd_get", key, sizeof("cmd_get") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_get); - else if (!memcmp("cmd_set", key, strlen("cmd_set"))) + else if (!memcmp("cmd_set", key, sizeof("cmd_set") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_set); - else if (!memcmp("get_hits", key, strlen("get_hits"))) + else if (!memcmp("get_hits", key, sizeof("get_hits") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_hits); - else if (!memcmp("get_misses", key, strlen("get_misses"))) + else if (!memcmp("get_misses", key, sizeof("get_misses") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_misses); - else if (!memcmp("evictions", key, strlen("evictions"))) + else if (!memcmp("evictions", key, sizeof("evictions") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->evictions); - else if (!memcmp("bytes_read", key, strlen("bytes_read"))) + else if (!memcmp("bytes_read", key, sizeof("bytes_read") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_read); - else if (!memcmp("bytes_written", key, strlen("bytes_written"))) + else if (!memcmp("bytes_written", key, sizeof("bytes_written") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_written); - else if (!memcmp("bytes", key, strlen("bytes"))) + else if (!memcmp("bytes", key, sizeof("bytes") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes); - else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) + else if (!memcmp("limit_maxbytes", key, sizeof("limit_maxbytes") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->limit_maxbytes); - else if (!memcmp("threads", key, strlen("threads"))) + else if (! memcmp("threads", key, sizeof("threads") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->threads); else { @@ -220,6 +228,12 @@ char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_ return NULL; } + if (length >= SMALL_STRING_LEN || length < 0) + { + *error= MEMCACHED_FAILURE; + return NULL; + } + ret= libmemcached_malloc(ptr, (size_t) (length + 1)); memcpy(ret, buffer, (size_t) length); ret[length]= '\0'; @@ -228,8 +242,9 @@ char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_ } static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, - char *args, - memcached_server_write_instance_st instance) + const char *args, + memcached_server_write_instance_st instance, + struct local_context *check) { memcached_return_t rc; @@ -250,9 +265,13 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, request.message.header.request.keylen= htons((uint16_t)len); request.message.header.request.bodylen= htonl((uint32_t) len); - if ((memcached_do(instance, request.bytes, - sizeof(request.bytes), false) != MEMCACHED_SUCCESS) || - (memcached_io_write(instance, args, len, true) == -1)) + struct libmemcached_io_vector_st vector[]= + { + { .length= sizeof(request.bytes), .buffer= request.bytes }, + { .length= len, .buffer= args } + }; + + if (memcached_vdo(instance, vector, 2, true) != MEMCACHED_SUCCESS) { memcached_io_reset(instance); return MEMCACHED_WRITE_FAILURE; @@ -271,8 +290,8 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, memcached_server_response_decrement(instance); do { - rc= memcached_response(instance, buffer, - sizeof(buffer), NULL); + rc= memcached_response(instance, buffer, sizeof(buffer), NULL); + if (rc == MEMCACHED_END) break; @@ -282,10 +301,23 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, return rc; } - unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY) + if (memc_stat) { - WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); - WATCHPOINT_ASSERT(0); + unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY) + { + WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); + WATCHPOINT_ASSERT(0); + } + } + + if (check && check->func) + { + size_t key_length= strlen(buffer); + + check->func(instance, + buffer, key_length, + buffer+key_length+1, strlen(buffer+key_length+1), + check->context); } } while (1); @@ -298,12 +330,13 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, } static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, - char *args, - memcached_server_write_instance_st instance) + const char *args, + memcached_server_write_instance_st instance, + struct local_context *check) { memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - size_t send_length; + int send_length; if (args) send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, @@ -312,10 +345,10 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "stats\r\n"); - if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) + if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0) return MEMCACHED_WRITE_FAILURE; - rc= memcached_do(instance, buffer, send_length, true); + rc= memcached_do(instance, buffer, (size_t)send_length, true); if (rc != MEMCACHED_SUCCESS) goto error; @@ -339,10 +372,21 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, value= string_ptr; value[(size_t)(end_ptr-string_ptr)]= 0; string_ptr= end_ptr + 2; - unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY) + if (memc_stat) { - WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); - WATCHPOINT_ASSERT(0); + unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY) + { + WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); + WATCHPOINT_ASSERT(0); + } + } + + if (check && check->func) + { + check->func(instance, + key, strlen(key), + value, strlen(value), + check->context); } } else @@ -356,27 +400,42 @@ error: return rc; } -memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return_t *error) +memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_return_t *error) { memcached_return_t rc; memcached_stat_st *stats; - unlikely (ptr->flags.use_udp) + if ((rc= initialize_query(self)) != MEMCACHED_SUCCESS) + { + if (error) + *error= rc; + + return NULL; + } + + WATCHPOINT_ASSERT(error); + + unlikely (self->flags.use_udp) { - *error= MEMCACHED_NOT_SUPPORTED; + if (error) + *error= MEMCACHED_NOT_SUPPORTED; + return NULL; } - stats= libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(memcached_stat_st)); + stats= libmemcached_calloc(self, memcached_server_count(self), sizeof(memcached_stat_st)); if (! stats) { - *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + if (error) + *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + return NULL; } + WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS); rc= MEMCACHED_SUCCESS; - for (uint32_t x= 0; x < memcached_server_count(ptr); x++) + for (uint32_t x= 0; x < memcached_server_count(self); x++) { memcached_return_t temp_return; memcached_server_write_instance_st instance; @@ -384,31 +443,32 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur stat_instance= stats + x; - stat_instance->root= ptr; + stat_instance->root= self; - instance= memcached_server_instance_fetch(ptr, x); + instance= memcached_server_instance_fetch(self, x); - if (ptr->flags.binary_protocol) + if (self->flags.binary_protocol) { - temp_return= binary_stats_fetch(stat_instance, args, instance); + temp_return= binary_stats_fetch(stat_instance, args, instance, NULL); } else { - temp_return= ascii_stats_fetch(stat_instance, args, instance); + temp_return= ascii_stats_fetch(stat_instance, args, instance, NULL); } if (temp_return != MEMCACHED_SUCCESS) rc= MEMCACHED_SOME_ERRORS; } - *error= rc; + if (error) + *error= rc; + return stats; } memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char *args, const char *hostname, in_port_t port) { - memcached_return_t rc; memcached_st memc; memcached_st *memc_ptr; memcached_server_write_instance_st instance; @@ -416,19 +476,26 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char memset(memc_stat, 0, sizeof(memcached_stat_st)); memc_ptr= memcached_create(&memc); - WATCHPOINT_ASSERT(memc_ptr); + if (! memc_ptr) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; memcached_server_add(&memc, hostname, port); + memcached_return_t rc; + if ((rc= initialize_query(memc_ptr)) != MEMCACHED_SUCCESS) + { + return rc; + } + instance= memcached_server_instance_fetch(memc_ptr, 0); if (memc.flags.binary_protocol) { - rc= binary_stats_fetch(memc_stat, args, instance); + rc= binary_stats_fetch(memc_stat, args, instance, NULL); } else { - rc= ascii_stats_fetch(memc_stat, args, instance); + rc= ascii_stats_fetch(memc_stat, args, instance, NULL); } memcached_free(&memc); @@ -485,3 +552,31 @@ void memcached_stat_free(const memcached_st *ptr, memcached_stat_st *memc_stat) free(memc_stat); } } + +static memcached_return_t call_stat_fn(memcached_st *ptr, + memcached_server_write_instance_st instance, + void *context) +{ + memcached_return_t rc; + struct local_context *check= (struct local_context *)context; + + if (ptr->flags.binary_protocol) + { + rc= binary_stats_fetch(NULL, check->args, instance, check); + } + else + { + rc= ascii_stats_fetch(NULL, check->args, instance, check); + } + + return rc; +} + +memcached_return_t memcached_stat_execute(memcached_st *memc, const char *args, memcached_stat_fn func, void *context) +{ + memcached_version(memc); + + struct local_context check= { .func= func, .context= context, .args= args }; + + return memcached_server_execute(memc, call_stat_fn, (void *)&check); +}