summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2013-08-06 15:00:43 +0200
committerantirez <antirez@gmail.com>2013-08-07 11:37:42 +0200
commit487e66557e93867ec06fb74debfd293622cb386e (patch)
treed119aaf1e25d1d9f1bd7a2df6dcdd7a7f5820cfd
parent2e4ec793132148ebe4e30ed76d39536b9a26727f (diff)
downloadredis-487e66557e93867ec06fb74debfd293622cb386e.tar.gz
Add per-db average TTL information in INFO output.
Example: db0:keys=221913,expires=221913,avg_ttl=655 The algorithm uses a running average with only two samples (current and previous). Keys found to be expired are considered at TTL zero even if the actual TTL can be negative. The TTL is reported in milliseconds.
-rw-r--r--src/redis.c33
-rw-r--r--src/redis.h1
2 files changed, 30 insertions, 4 deletions
diff --git a/src/redis.c b/src/redis.c
index 15a3e8e0d..605c8f3a8 100644
--- a/src/redis.c
+++ b/src/redis.c
@@ -716,10 +716,14 @@ void activeExpireCycle(int type) {
* of the keys were expired. */
do {
unsigned long num, slots;
- long long now;
+ long long now, ttl_sum;
+ int ttl_samples;
/* If there is nothing to expire try next DB ASAP. */
- if ((num = dictSize(db->expires)) == 0) break;
+ if ((num = dictSize(db->expires)) == 0) {
+ db->avg_ttl = 0;
+ break;
+ }
slots = dictSlots(db->expires);
now = mstime();
@@ -732,14 +736,33 @@ void activeExpireCycle(int type) {
/* The main collection cycle. Sample random keys among keys
* with an expire set, checking for expired ones. */
expired = 0;
+ ttl_sum = 0;
+ ttl_samples = 0;
+
if (num > ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP)
num = ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP;
+
while (num--) {
dictEntry *de;
+ long long ttl;
if ((de = dictGetRandomKey(db->expires)) == NULL) break;
+ ttl = dictGetSignedIntegerVal(de)-now;
if (activeExpireCycleTryExpire(db,de,now)) expired++;
+ if (ttl < 0) ttl = 0;
+ ttl_sum += ttl;
+ ttl_samples++;
}
+
+ /* Update the average TTL stats for this database. */
+ if (ttl_samples) {
+ long long avg_ttl = ttl_sum/ttl_samples;
+
+ if (db->avg_ttl == 0) db->avg_ttl = avg_ttl;
+ /* Smooth the value averaging with the previous one. */
+ db->avg_ttl = (db->avg_ttl+avg_ttl)/2;
+ }
+
/* We can't block forever here even if there are many keys to
* expire. So after a given amount of milliseconds return to the
* caller waiting for the other active expire cycle. */
@@ -1424,6 +1447,7 @@ void initServer() {
server.db[j].ready_keys = dictCreate(&setDictType,NULL);
server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
server.db[j].id = j;
+ server.db[j].avg_ttl = 0;
}
server.pubsub_channels = dictCreate(&keylistDictType,NULL);
server.pubsub_patterns = listCreate();
@@ -2327,8 +2351,9 @@ sds genRedisInfoString(char *section) {
keys = dictSize(server.db[j].dict);
vkeys = dictSize(server.db[j].expires);
if (keys || vkeys) {
- info = sdscatprintf(info, "db%d:keys=%lld,expires=%lld\r\n",
- j, keys, vkeys);
+ info = sdscatprintf(info,
+ "db%d:keys=%lld,expires=%lld,avg_ttl=%lld\r\n",
+ j, keys, vkeys, server.db[j].avg_ttl);
}
}
}
diff --git a/src/redis.h b/src/redis.h
index 0b4d1b770..8d3f69744 100644
--- a/src/redis.h
+++ b/src/redis.h
@@ -344,6 +344,7 @@ typedef struct redisDb {
dict *ready_keys; /* Blocked keys that received a PUSH */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
int id;
+ long long avg_ttl; /* Average TTL, just for stats */
} redisDb;
/* Client MULTI/EXEC state */