Skip to content

Commit

Permalink
LFU: rename LFUDecrAndReturn to LFUGetCurrentCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
soloestoy committed Nov 28, 2017
1 parent f370c33 commit c9021d6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/db.c
Expand Up @@ -42,7 +42,7 @@
* Firstly, decrement the counter if the decrement time is reached.
* Then logarithmically increment the counter, and update the access time. */
void updateLFU(robj *val) {
unsigned long counter = LFUDecrAndReturn(val);
unsigned long counter = LFUGetCurrentCounter(val);
counter = LFULogIncr(counter);
val->lru = (LFUGetTimeInMinutes()<<8) | counter;
}
Expand Down
14 changes: 7 additions & 7 deletions src/evict.c
Expand Up @@ -194,7 +194,7 @@ void evictionPoolPopulate(int dbid, dict *sampledict, dict *keydict, struct evic
* first. So inside the pool we put objects using the inverted
* frequency subtracting the actual frequency to the maximum
* frequency of 255. */
idle = 255-LFUDecrAndReturn(o);
idle = 255-LFUGetCurrentCounter(o);
} else if (server.maxmemory_policy == MAXMEMORY_VOLATILE_TTL) {
/* In this case the sooner the expire the better. */
idle = ULLONG_MAX - (long)dictGetVal(de);
Expand Down Expand Up @@ -265,17 +265,17 @@ void evictionPoolPopulate(int dbid, dict *sampledict, dict *keydict, struct evic
*
* We split the 24 bits into two fields:
*
* 16 bits 8 bits
* +----------------+--------+
* + Last decr time | LOG_C |
* +----------------+--------+
* 16 bits 8 bits
* +------------------+--------+
* + Last access time | LOG_C |
* +------------------+--------+
*
* LOG_C is a logarithmic counter that provides an indication of the access
* frequency. However this field must also be decremented otherwise what used
* to be a frequently accessed key in the past, will remain ranked like that
* forever, while we want the algorithm to adapt to access pattern changes.
*
* So the remaining 16 bits are used in order to store the "decrement time",
* So the remaining 16 bits are used in order to store the "access time",
* a reduced-precision Unix time (we take 16 bits of the time converted
* in minutes since we don't care about wrapping around) where the LOG_C
* counter is halved if it has an high value, or just decremented if it
Expand Down Expand Up @@ -332,7 +332,7 @@ uint8_t LFULogIncr(uint8_t counter) {
* This function is used in order to scan the dataset for the best object
* to fit: as we check for the candidate, we incrementally decrement the
* counter of the scanned objects if needed. */
unsigned long LFUDecrAndReturn(robj *o) {
unsigned long LFUGetCurrentCounter(robj *o) {
unsigned long ldt = o->lru >> 8;
unsigned long counter = o->lru & 255;
long halve_times = server.lfu_decay_time ? LFUTimeElapsed(ldt) / server.lfu_decay_time : 0;
Expand Down
8 changes: 4 additions & 4 deletions src/object.c
Expand Up @@ -1031,15 +1031,15 @@ void objectCommand(client *c) {
} else if (!strcasecmp(c->argv[1]->ptr,"freq") && c->argc == 3) {
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
== NULL) return;
if (!(server.maxmemory_policy & MAXMEMORY_FLAG_LFU)) {
addReplyError(c,"An LFU maxmemory policy is not selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.");
if (server.maxmemory_policy & MAXMEMORY_FLAG_LRU) {
addReplyError(c,"An LRU maxmemory policy is selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.");
return;
}
/* LFUDecrAndReturn should be called
/* LFUGetCurrentCounter should be called
* in case of the key has not been accessed for a long time,
* because we update the access time only
* when the key is read or overwritten. */
addReplyLongLong(c,LFUDecrAndReturn(o));
addReplyLongLong(c,LFUGetCurrentCounter(o));
} else {
addReplyError(c,"Syntax error. Try OBJECT (refcount|encoding|idletime|freq)");
}
Expand Down
2 changes: 1 addition & 1 deletion src/server.h
Expand Up @@ -1802,7 +1802,7 @@ void evictionPoolAlloc(void);
#define LFU_INIT_VAL 5
unsigned long LFUGetTimeInMinutes(void);
uint8_t LFULogIncr(uint8_t value);
unsigned long LFUDecrAndReturn(robj *o);
unsigned long LFUGetCurrentCounter(robj *o);

/* Keys hashing / comparison functions for dict.c hash tables. */
uint64_t dictSdsHash(const void *key);
Expand Down

0 comments on commit c9021d6

Please sign in to comment.