summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2013-08-06 15:00:43 +0200
committerantirez <antirez@gmail.com>2013-08-06 15:36:43 +0200
commitfa48b1fa3247ee9f3a52536488b3e591347feed4 (patch)
tree80ba3006f5c8875350e993b196e2cecfbb23bb20
parent31d0f341000fc18b81717607ba82496ca285fb80 (diff)
downloadredis-fa48b1fa3247ee9f3a52536488b3e591347feed4.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 1f1f43ae5..0696c8ee3 100644
--- a/src/redis.c
+++ b/src/redis.c
@@ -733,10 +733,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();
@@ -749,14 +753,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. */
@@ -1490,6 +1513,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();
@@ -2496,8 +2520,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 984dadc23..a3028689b 100644
--- a/src/redis.h
+++ b/src/redis.h
@@ -398,6 +398,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 */