From 5bc7e019e1519ebb569b2964341c3440ef28645c Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 26 Jan 2016 15:28:22 +0100 Subject: [PATCH] Use a smoother running average for avg_ttl in INFO. Reported here: https://www.reddit.com/r/redis/comments/42r0i0/avg_ttl_varies_a_lot/ --- src/server.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/server.c b/src/server.c index e39ee4c74..aac39d983 100644 --- a/src/server.c +++ b/src/server.c @@ -864,18 +864,22 @@ void activeExpireCycle(int type) { 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++; + if (ttl > 0) { + /* We want the average TTL of keys yet not expired. */ + ttl_sum += ttl; + ttl_samples++; + } } /* Update the average TTL stats for this database. */ if (ttl_samples) { long long avg_ttl = ttl_sum/ttl_samples; + /* Do a simple running average with a few samples. + * We just use the current estimate with a weight of 2% + * and the previous estimate with a weight of 98%. */ 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; + db->avg_ttl = (db->avg_ttl/50)*49 + (avg_ttl/50); } /* We can't block forever here even if there are many keys to