From: Brian Aker Date: Sun, 25 Nov 2007 20:52:06 +0000 (-0800) Subject: Cleanup of stats code (fixed byte alignment issue) X-Git-Tag: 0.11~5 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=da3a1b9ec585148509e4e7006d4c48ec7cf5ecef;p=awesomized%2Flibmemcached Cleanup of stats code (fixed byte alignment issue) --- diff --git a/include/memcached.h b/include/memcached.h index 122b6f97..bf50f456 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -124,32 +124,30 @@ struct memcached_server_st { }; struct memcached_stat_st { - unsigned int pid; - unsigned int uptime; - unsigned int threads; - time_t time; + uint32_t pid; + uint32_t uptime; + uint32_t threads; + uint32_t time; + uint32_t pointer_size; + uint32_t rusage_user_seconds; + uint32_t rusage_user_microseconds; + uint32_t rusage_system_seconds; + uint32_t rusage_system_microseconds; + uint32_t curr_items; + uint32_t total_items; + uint32_t limit_maxbytes; + uint32_t curr_connections; + uint32_t total_connections; + uint32_t connection_structures; + uint64_t bytes; + uint64_t cmd_get; + uint64_t cmd_set; + uint64_t get_hits; + uint64_t get_misses; + uint64_t evictions; + uint64_t bytes_read; + uint64_t bytes_written; char version[MEMCACHED_VERSION_STRING]; - unsigned int pointer_size; - unsigned int rusage_user; - unsigned int rusage_system; - unsigned int rusage_user_seconds; - unsigned int rusage_user_microseconds; - unsigned int rusage_system_seconds; - unsigned int rusage_system_microseconds; - unsigned int curr_items; - unsigned int total_items; - unsigned long long bytes; - unsigned int curr_connections; - unsigned int total_connections; - unsigned int connection_structures; - unsigned long long cmd_get; - unsigned long long cmd_set; - unsigned long long get_hits; - unsigned long long get_misses; - unsigned long long evictions; - unsigned long long bytes_read; - unsigned long long bytes_written; - unsigned int limit_maxbytes; }; struct memcached_string_st { diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index a2dc17a3..43574be5 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -11,10 +11,6 @@ static char *memcached_stat_keys[] = { "pointer_size", "rusage_user", "rusage_system", - "rusage_user_seconds", - "rusage_user_microseconds", - "rusage_system_seconds", - "rusage_system_microseconds", "curr_items", "total_items", "bytes", @@ -46,83 +42,78 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) } else if (!memcmp("time", key, strlen("time"))) { - stat->time= strtoll(value, (char **)NULL, 10); + stat->time= strtol(value, (char **)NULL, 10); } else if (!memcmp("version", key, strlen("version"))) { - memcpy(stat->version, value, 8); + memcpy(stat->version, value, strlen(value)); + stat->version[strlen(value)]= 0; } else if (!memcmp("pointer_size", key, strlen("pointer_size"))) { - stat->pointer_size= strtoll(value, (char **)NULL, 10); + stat->pointer_size= strtol(value, (char **)NULL, 10); } else if (!memcmp("rusage_user", key, strlen("rusage_user"))) { - stat->rusage_user= strtoll(value, (char **)NULL, 10); + char *walk_ptr; + for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++); + *walk_ptr= 0; + walk_ptr++; + stat->rusage_user_seconds= strtol(value, (char **)NULL, 10); + stat->rusage_user_microseconds= strtol(walk_ptr, (char **)NULL, 10); } else if (!memcmp("rusage_system", key, strlen("rusage_system"))) { - stat->rusage_system= strtoll(value, (char **)NULL, 10); - } - else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds"))) - { - stat->rusage_user_seconds= strtoll(value, (char **)NULL, 10); - } - else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds"))) - { - stat->rusage_user_microseconds= strtoll(value, (char **)NULL, 10); - } - else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds"))) - { - stat->rusage_system_seconds= strtoll(value, (char **)NULL, 10); - } - else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds"))) - { - stat->rusage_system_microseconds= strtoll(value, (char **)NULL, 10); + char *walk_ptr; + for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++); + *walk_ptr= 0; + walk_ptr++; + stat->rusage_system_seconds= strtol(value, (char **)NULL, 10); + stat->rusage_system_microseconds= strtol(walk_ptr, (char **)NULL, 10); } else if (!memcmp("curr_items", key, strlen("curr_items"))) { - stat->curr_items= strtoll(value, (char **)NULL, 10); + stat->curr_items= strtol(value, (char **)NULL, 10); } else if (!memcmp("total_items", key, strlen("total_items"))) { - stat->total_items= strtoll(value, (char **)NULL, 10); + stat->total_items= strtol(value, (char **)NULL, 10); } else if (!memcmp("bytes", key, strlen("bytes"))) { - stat->bytes= strtoll(value, (char **)NULL, 10); + //stat->bytes= strtoll(value, (char **)NULL, 10); } else if (!memcmp("curr_connections", key, strlen("curr_connections"))) { - stat->curr_connections= strtoll(value, (char **)NULL, 10); + //stat->curr_connections= strtoll(value, (char **)NULL, 10); } else if (!memcmp("total_connections", key, strlen("total_connections"))) { - stat->total_connections= strtoll(value, (char **)NULL, 10); + //stat->total_connections= strtoll(value, (char **)NULL, 10); } else if (!memcmp("connection_structures", key, strlen("connection_structures"))) { - stat->connection_structures= strtoll(value, (char **)NULL, 10); + //stat->connection_structures= strtol(value, (char **)NULL, 10); } else if (!memcmp("cmd_get", key, strlen("cmd_get"))) { - stat->cmd_get= strtoll(value, (char **)NULL, 10); + //stat->cmd_get= strtoll(value, (char **)NULL, 10); } else if (!memcmp("cmd_set", key, strlen("cmd_set"))) { - stat->cmd_set= strtoll(value, (char **)NULL, 10); + //stat->cmd_set= strtoll(value, (char **)NULL, 10); } else if (!memcmp("get_hits", key, strlen("get_hits"))) { - stat->get_hits= strtoll(value, (char **)NULL, 10); + //stat->get_hits= strtoll(value, (char **)NULL, 10); } else if (!memcmp("get_misses", key, strlen("get_misses"))) { - stat->get_misses= strtoll(value, (char **)NULL, 10); + //stat->get_misses= (uint64_t)strtoll(value, (char **)NULL, 10); } else if (!memcmp("evictions", key, strlen("evictions"))) { - stat->evictions= strtoll(value, (char **)NULL, 10); + //stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10); } else if (!memcmp("bytes_read", key, strlen("bytes_read"))) { @@ -134,7 +125,7 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) } else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) { - stat->limit_maxbytes= strtoll(value, (char **)NULL, 10); + //stat->limit_maxbytes= strtol(value, (char **)NULL, 10); } else if (!memcmp("threads", key, strlen("threads"))) { @@ -162,10 +153,6 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version); else if (!memcmp("pointer_size", key, strlen("pointer_size"))) snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size); - else if (!memcmp("rusage_user", key, strlen("rusage_user"))) - snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user); - else if (!memcmp("rusage_system", key, strlen("rusage_system"))) - snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system); else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds"))) snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_seconds); else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds"))) @@ -179,7 +166,7 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, else if (!memcmp("total_items", key, strlen("total_items"))) snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items); else if (!memcmp("bytes", key, strlen("bytes"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes); else if (!memcmp("curr_connections", key, strlen("curr_connections"))) snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections); else if (!memcmp("total_connections", key, strlen("total_connections"))) @@ -187,19 +174,19 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, else if (!memcmp("connection_structures", key, strlen("connection_structures"))) snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures); else if (!memcmp("cmd_get", key, strlen("cmd_get"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_get); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_get); else if (!memcmp("cmd_set", key, strlen("cmd_set"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_set); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_set); else if (!memcmp("get_hits", key, strlen("get_hits"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_hits); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_hits); else if (!memcmp("get_misses", key, strlen("get_misses"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_misses); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_misses); else if (!memcmp("evictions", key, strlen("evictions"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->evictions); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->evictions); else if (!memcmp("bytes_read", key, strlen("bytes_read"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_read); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_read); else if (!memcmp("bytes_written", key, strlen("bytes_written"))) - snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_written); + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written); else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes); else if (!memcmp("threads", key, strlen("threads"))) @@ -222,11 +209,6 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; size_t send_length; - rc= memcached_connect(ptr, server_key); - - if (rc != MEMCACHED_SUCCESS) - return rc; - if (args) send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "stats %s\r\n", args); @@ -252,12 +234,12 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, string_ptr= buffer; string_ptr+= 5; /* Move past STAT */ - for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++); + for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++); key= string_ptr; key[(size_t)(end_ptr-string_ptr)]= 0; string_ptr= end_ptr + 1; - for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++); + for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++); value= string_ptr; value[(size_t)(end_ptr-string_ptr)]= 0; string_ptr= end_ptr + 2; @@ -280,20 +262,21 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur memcached_return rc; memcached_stat_st *stats; - stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1)); + stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts)); if (!stats) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; free(stats); return NULL; } - memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1)); + memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts)); rc= MEMCACHED_SUCCESS; for (x= 0; x < ptr->number_of_hosts; x++) { memcached_return temp_return; - temp_return= memcached_stats_fetch(ptr, stats+x, args, x); + + temp_return= memcached_stats_fetch(ptr, stats + x, args, x); if (temp_return != MEMCACHED_SUCCESS) rc= MEMCACHED_SOME_ERRORS; } @@ -326,15 +309,16 @@ memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args, char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat, memcached_return *error) { - char **list= (char **)malloc(sizeof(memcached_stat_keys)); - - memset(list, 0, sizeof(memcached_stat_keys)); + char **list; + size_t length= sizeof(memcached_stat_keys); + list= (char **)malloc(length); if (!list) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; return NULL; } + memset(list, 0, sizeof(memcached_stat_keys)); memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys)); diff --git a/tests/function.c b/tests/function.c index 1f536e88..4ab8fc26 100644 --- a/tests/function.c +++ b/tests/function.c @@ -820,10 +820,9 @@ uint8_t get_stats(memcached_st *memc) for (x= 0; x < memcached_server_count(memc); x++) { - list= memcached_stat_get_keys(memc, &stat[x], &rc); + list= memcached_stat_get_keys(memc, stat+x, &rc); assert(rc == MEMCACHED_SUCCESS); - for (ptr= list; *ptr; ptr++) - printf("Found key %s\n", *ptr); + for (ptr= list; *ptr; ptr++); free(list); } diff --git a/tests/test.c b/tests/test.c index 9ffe21c7..e5f4bc84 100644 --- a/tests/test.c +++ b/tests/test.c @@ -14,7 +14,7 @@ #include "test.h" #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10 -#define TEST_SERVERS 2 +#define TEST_SERVERS 3 long int timedif(struct timeval a, struct timeval b) { @@ -158,9 +158,7 @@ int main(int argc, char *argv[]) if (next->pre) { memcached_return rc; - WATCHPOINT_STRING(next->name); rc= next->pre(memc); - WATCHPOINT; if (rc != MEMCACHED_SUCCESS) {