Skip to content

Commit 8e8cb37

Browse files
bertoggkevmw
authored andcommitted
block: Give always priority to unused entries in the qcow2 L2 cache
The current algorithm to replace entries from the L2 cache gives priority to newer hits by dividing the hit count of all existing entries by two everytime there is a cache miss. However, if there are several cache misses the hit count of the existing entries can easily go down to 0. This will result in those entries being replaced even when there are others that have never been used. This problem is more noticeable with larger disk images and cache sizes, since the chances of having several misses before the cache is full are higher. If we make sure that the hit count can never go down to 0 again, unused entries will always have priority. Signed-off-by: Alberto Garcia <berto@igalia.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1 parent fa21e6f commit 8e8cb37

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

block/qcow2-cache.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
253253

254254
/* Give newer hits priority */
255255
/* TODO Check how to optimize the replacement strategy */
256-
c->entries[i].cache_hits /= 2;
256+
if (c->entries[i].cache_hits > 1) {
257+
c->entries[i].cache_hits /= 2;
258+
}
257259
}
258260

259261
if (min_index == -1) {

0 commit comments

Comments
 (0)