Skip to content

Commit

Permalink
expose stats for the internal hash table
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dormando committed Sep 28, 2011
1 parent 2096c49 commit 108e2cd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions assoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down
4 changes: 4 additions & 0 deletions doc/protocol.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 108e2cd

Please sign in to comment.