From 108e2cd600ed2c698222b4ac7d90301c23d4810a Mon Sep 17 00:00:00 2001 From: dormando Date: Wed, 28 Sep 2011 13:32:09 -0700 Subject: expose stats for the internal hash table Now users can tell how much memory is being used for the hash table structure. It also exposes the current hash power level, which is useful for presizing the structure. --- assoc.c | 13 +++++++++++++ doc/protocol.txt | 4 ++++ memcached.c | 4 ++++ memcached.h | 3 +++ 4 files changed, 24 insertions(+) diff --git a/assoc.c b/assoc.c index a0b8788..7ab3aec 100644 --- a/assoc.c +++ b/assoc.c @@ -64,6 +64,10 @@ void assoc_init(void) { fprintf(stderr, "Failed to init hashtable.\n"); exit(EXIT_FAILURE); } + STATS_LOCK(); + stats.hash_power_level = hashpower; + stats.hash_bytes = hashsize(hashpower) * sizeof(void *); + STATS_UNLOCK(); } item *assoc_find(const char *key, const size_t nkey) { @@ -126,6 +130,11 @@ static void assoc_expand(void) { hashpower++; expanding = true; expand_bucket = 0; + STATS_LOCK(); + stats.hash_power_level = hashpower; + stats.hash_bytes += hashsize(hashpower) * sizeof(void *); + stats.hash_is_expanding = 1; + STATS_UNLOCK(); pthread_cond_signal(&maintenance_cond); } else { primary_hashtable = old_hashtable; @@ -213,6 +222,10 @@ static void *assoc_maintenance_thread(void *arg) { if (expand_bucket == hashsize(hashpower - 1)) { expanding = false; free(old_hashtable); + STATS_LOCK(); + stats.hash_bytes -= hashsize(hashpower - 1) * sizeof(void *); + stats.hash_is_expanding = 0; + STATS_UNLOCK(); if (settings.verbose > 1) fprintf(stderr, "Hash table expansion done\n"); } diff --git a/doc/protocol.txt b/doc/protocol.txt index de21476..16a87eb 100644 --- a/doc/protocol.txt +++ b/doc/protocol.txt @@ -446,6 +446,10 @@ integers separated by a colon (treat this as a floating point number). | | | (see doc/threads.txt) | | conn_yields | 64u | Number of times any connection yielded to | | | | another due to hitting the -R limit. | +| hash_power_level | 32u | Current size multiplier for hash table | +| hash_bytes | 64u | Bytes currently used by hash tables | +| hash_is_expanding | bool | Indicates if the hash table is being | +| | | grown to a new size |-----------------------+---------+-------------------------------------------| Settings statistics diff --git a/memcached.c b/memcached.c index a578b10..4603aca 100644 --- a/memcached.c +++ b/memcached.c @@ -168,6 +168,7 @@ static void stats_init(void) { stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = stats.evictions = stats.reclaimed = 0; stats.touch_cmds = stats.touch_misses = stats.touch_hits = stats.rejected_conns = 0; stats.curr_bytes = stats.listen_disabled_num = 0; + stats.hash_power_level = stats.hash_bytes = stats.hash_is_expanding = 0; stats.accepting_conns = true; /* assuming we start in this state. */ /* make the time we started always be 2 seconds before we really @@ -2556,6 +2557,9 @@ static void server_stats(ADD_STAT add_stats, conn *c) { APPEND_STAT("listen_disabled_num", "%llu", (unsigned long long)stats.listen_disabled_num); APPEND_STAT("threads", "%d", settings.num_threads); APPEND_STAT("conn_yields", "%llu", (unsigned long long)thread_stats.conn_yields); + APPEND_STAT("hash_power_level", "%u", stats.hash_power_level); + APPEND_STAT("hash_bytes", "%llu", (unsigned long long)stats.hash_bytes); + APPEND_STAT("hash_is_expanding", "%u", stats.hash_is_expanding); STATS_UNLOCK(); } diff --git a/memcached.h b/memcached.h index 79b80d9..c151abe 100644 --- a/memcached.h +++ b/memcached.h @@ -257,6 +257,9 @@ struct stats { time_t started; /* when the process was started */ bool accepting_conns; /* whether we are currently accepting */ uint64_t listen_disabled_num; + unsigned int hash_power_level; /* Better hope it's not over 9000 */ + uint64_t hash_bytes; /* size used for hash tables */ + bool hash_is_expanding; /* If the hash table is being expanded */ }; #define MAX_VERBOSITY_LEVEL 2 -- cgit v1.2.1