From b8aaaefdcb0b10d22668da2b530b67f55ffa653a Mon Sep 17 00:00:00 2001 From: Date: Tue, 25 Sep 2007 08:13:26 +0200 Subject: [PATCH] Started to abstract out the stats calls. --- include/memcached.h | 3 + lib/memcached_response.c | 82 +++++++++--------- lib/memcached_stats.c | 175 +++++++++++++++++++++++++++++++++++--- lib/memcached_verbosity.c | 2 +- tests/output.res | 26 ++++++ tests/test.c | 17 ++++ 6 files changed, 253 insertions(+), 52 deletions(-) diff --git a/include/memcached.h b/include/memcached.h index a9c64359..fc732c27 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -161,6 +161,9 @@ memcached_return memcached_response(memcached_st *ptr, char *buffer, size_t buffer_length, unsigned int server_key); unsigned int memcached_generate_hash(char *key, size_t key_length); +memcached_return memcached_stat_get_value(memcached_stat_st *stat, char *key, + char *value, size_t value_length); +char ** memcached_stat_get_keys(memcached_stat_st *stat, memcached_return *error); #ifdef __cplusplus } diff --git a/lib/memcached_response.c b/lib/memcached_response.c index 1d0937af..91ae3fe6 100644 --- a/lib/memcached_response.c +++ b/lib/memcached_response.c @@ -15,6 +15,7 @@ memcached_return memcached_response(memcached_st *ptr, char *buffer_ptr; memset(buffer, 0, buffer_length); + send_length= 0; buffer_ptr= buffer; while (1) @@ -31,47 +32,48 @@ memcached_return memcached_response(memcached_st *ptr, buffer_ptr++; } - if (send_length) - switch(buffer[0]) + switch(buffer[0]) + { + case 'V': /* VALUE */ + return MEMCACHED_SUCCESS; + case 'O': /* OK */ + return MEMCACHED_SUCCESS; + case 'S': /* STORED STATS SERVER_ERROR */ + { + if (buffer[1] == 'T') /* STORED STATS */ + return MEMCACHED_SUCCESS; + else if (buffer[1] == 'E') + return MEMCACHED_SERVER_ERROR; + else + return MEMCACHED_UNKNOWN_READ_FAILURE; + } + case 'D': /* DELETED */ + return MEMCACHED_SUCCESS; + case 'N': /* NOT_FOUND */ { - case 'V': /* VALUE */ - return MEMCACHED_SUCCESS; - case 'O': /* OK */ - return MEMCACHED_SUCCESS; - case 'S': /* STORED STATS SERVER_ERROR */ - { - if (buffer[1] == 'T') /* STORED STATS */ - return MEMCACHED_SUCCESS; - else if (buffer[1] == 'E') - return MEMCACHED_SERVER_ERROR; - else - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - case 'D': /* DELETED */ - return MEMCACHED_SUCCESS; - case 'N': /* NOT_FOUND */ - { - if (buffer[4] == 'F') - return MEMCACHED_NOTFOUND; - else if (buffer[4] == 'S') - return MEMCACHED_NOTSTORED; - else - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - case 'E': /* PROTOCOL ERROR or END */ - { - if (buffer[1] == 'N') - return MEMCACHED_NOTFOUND; - else if (buffer[1] == 'R') - return MEMCACHED_PROTOCOL_ERROR; - else - return MEMCACHED_UNKNOWN_READ_FAILURE; - } - case 'C': /* CLIENT ERROR */ - return MEMCACHED_CLIENT_ERROR; - default: - return MEMCACHED_UNKNOWN_READ_FAILURE; + if (buffer[4] == 'F') + return MEMCACHED_NOTFOUND; + else if (buffer[4] == 'S') + return MEMCACHED_NOTSTORED; + else + return MEMCACHED_UNKNOWN_READ_FAILURE; } + case 'E': /* PROTOCOL ERROR or END */ + { + if (buffer[1] == 'N') + return MEMCACHED_NOTFOUND; + else if (buffer[1] == 'R') + return MEMCACHED_PROTOCOL_ERROR; + else + return MEMCACHED_UNKNOWN_READ_FAILURE; + } + case 'C': /* CLIENT ERROR */ + return MEMCACHED_CLIENT_ERROR; + default: + return MEMCACHED_UNKNOWN_READ_FAILURE; + + return MEMCACHED_READ_FAILURE; + } - return MEMCACHED_READ_FAILURE; + return MEMCACHED_SUCCESS; } diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index 86714759..48e62cce 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -3,6 +3,37 @@ #include +static char *memcached_stat_keys[] = { + "pid", + "uptime", + "time", + "version", + "pointer_size", + "rusage_user", + "rusage_system", + "rusage_user_seconds", + "rusage_user_microseconds", + "rusage_system_seconds", + "rusage_system_microseconds", + "curr_items", + "total_items", + "bytes", + "curr_connections", + "total_connections", + "connection_structures", + "cmd_get", + "cmd_set", + "get_hits", + "get_misses", + "evictions", + "bytes_read", + "bytes_written", + "limit_maxbytes", + "threads", + NULL +}; + + static void set_data(memcached_stat_st *stat, char *key, char *value) { if (!memcmp("pid", key, strlen("pid"))) @@ -115,6 +146,125 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) } } +memcached_return memcached_stat_get_value(memcached_stat_st *stat, char *key, + char *value, size_t value_length) +{ + memcached_return rc; + + rc= MEMCACHED_SUCCESS; + + if (!memcmp("pid", key, strlen("pid"))) + { + snprintf(value, value_length,"%u", stat->pid); + } + else if (!memcmp("uptime", key, strlen("uptime"))) + { + snprintf(value, value_length,"%u", stat->uptime); + } + else if (!memcmp("time", key, strlen("time"))) + { + snprintf(value, value_length,"%llu", (unsigned long long)stat->time); + } + else if (!memcmp("version", key, strlen("version"))) + { + snprintf(value, value_length,"%s", stat->version); + } + else if (!memcmp("pointer_size", key, strlen("pointer_size"))) + { + snprintf(value, value_length,"%u", stat->pointer_size); + } + else if (!memcmp("rusage_user", key, strlen("rusage_user"))) + { + snprintf(value, value_length,"%u", stat->rusage_user); + } + else if (!memcmp("rusage_system", key, strlen("rusage_system"))) + { + snprintf(value, value_length,"%u", stat->rusage_system); + } + else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds"))) + { + snprintf(value, value_length,"%u", stat->rusage_user_seconds); + } + else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds"))) + { + snprintf(value, value_length,"%u", stat->rusage_user_microseconds); + } + else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds"))) + { + snprintf(value, value_length,"%u", stat->rusage_system_seconds); + } + else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds"))) + { + snprintf(value, value_length,"%u", stat->rusage_system_microseconds); + } + else if (!memcmp("curr_items", key, strlen("curr_items"))) + { + snprintf(value, value_length,"%u", stat->curr_items); + } + else if (!memcmp("total_items", key, strlen("total_items"))) + { + snprintf(value, value_length,"%u", stat->total_items); + } + else if (!memcmp("bytes", key, strlen("bytes"))) + { + snprintf(value, value_length,"%llu", stat->bytes); + } + else if (!memcmp("curr_connections", key, strlen("curr_connections"))) + { + snprintf(value, value_length,"%u", stat->curr_connections); + } + else if (!memcmp("total_connections", key, strlen("total_connections"))) + { + snprintf(value, value_length,"%u", stat->total_connections); + } + else if (!memcmp("connection_structures", key, strlen("connection_structures"))) + { + snprintf(value, value_length,"%u", stat->connection_structures); + } + else if (!memcmp("cmd_get", key, strlen("cmd_get"))) + { + snprintf(value, value_length,"%llu", stat->cmd_get); + } + else if (!memcmp("cmd_set", key, strlen("cmd_set"))) + { + snprintf(value, value_length,"%llu", stat->cmd_set); + } + else if (!memcmp("get_hits", key, strlen("get_hits"))) + { + snprintf(value, value_length,"%llu", stat->get_hits); + } + else if (!memcmp("get_misses", key, strlen("get_misses"))) + { + snprintf(value, value_length,"%llu", stat->get_misses); + } + else if (!memcmp("evictions", key, strlen("evictions"))) + { + snprintf(value, value_length,"%llu", stat->evictions); + } + else if (!memcmp("bytes_read", key, strlen("bytes_read"))) + { + snprintf(value, value_length,"%llu", stat->bytes_read); + } + else if (!memcmp("bytes_written", key, strlen("bytes_written"))) + { + snprintf(value, value_length,"%llu", stat->bytes_written); + } + else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) + { + snprintf(value, value_length,"%u", stat->limit_maxbytes); + } + else if (!memcmp("threads", key, strlen("threads"))) + { + snprintf(value, value_length,"%u", stat->threads); + } + else + { + rc= MEMCACHED_NOTFOUND; + } + + return rc; +} + static memcached_return memcached_stats_fetch(memcached_st *ptr, memcached_stat_st *stat, char *args, @@ -141,18 +291,8 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, sent_length= write(ptr->hosts[server_key].fd, buffer, send_length); - if (sent_length == -1) - { - fprintf(stderr, "error %s: write: %m\n", __FUNCTION__); - return MEMCACHED_WRITE_FAILURE; - } - - if (sent_length != send_length) - { - fprintf(stderr, "error %s: short write %d %d: %m\n", - __FUNCTION__, sent_length, send_length); + if (sent_length == -1 || sent_length != send_length) return MEMCACHED_WRITE_FAILURE; - } rc= memcached_response(ptr, buffer, HUGE_STRING_LEN, 0); @@ -236,3 +376,16 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, return rc; } + +/* + 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_stat_st *stat, memcached_return *error) +{ + char **list= (char **)malloc(sizeof(memcached_stat_keys)); + + memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys)); + + return list; +} diff --git a/lib/memcached_verbosity.c b/lib/memcached_verbosity.c index 25876d4f..b68fbbd4 100644 --- a/lib/memcached_verbosity.c +++ b/lib/memcached_verbosity.c @@ -3,7 +3,7 @@ memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) { unsigned int x; - size_t send_length, sent_length; + size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; diff --git a/tests/output.res b/tests/output.res index e69de29b..445d3783 100644 --- a/tests/output.res +++ b/tests/output.res @@ -0,0 +1,26 @@ +Found key pid +Found key uptime +Found key time +Found key version +Found key pointer_size +Found key rusage_user +Found key rusage_system +Found key rusage_user_seconds +Found key rusage_user_microseconds +Found key rusage_system_seconds +Found key rusage_system_microseconds +Found key curr_items +Found key total_items +Found key bytes +Found key curr_connections +Found key total_connections +Found key connection_structures +Found key cmd_get +Found key cmd_set +Found key get_hits +Found key get_misses +Found key evictions +Found key bytes_read +Found key bytes_written +Found key limit_maxbytes +Found key threads diff --git a/tests/test.c b/tests/test.c index af27e8cd..64f8dece 100644 --- a/tests/test.c +++ b/tests/test.c @@ -344,6 +344,22 @@ void mget_test(void) memcached_deinit(memc); } +void get_stats_keys(void) +{ + char **list; + char **ptr; + memcached_stat_st stat; + memcached_return rc; + + list= memcached_stat_get_keys(&stat, &rc); + assert(rc == MEMCACHED_SUCCESS); + for (ptr= list; *ptr; ptr++) + printf("Found key %s\n", *ptr); + + free(list); + +} + int main(void) { @@ -367,6 +383,7 @@ int main(void) decrement_test(); quit_test(); mget_test(); + get_stats_keys(); /* Clean up whatever we might have left */ flush_test(); -- 2.30.2