From: Alberto Garcia Date: Thu, 5 Feb 2015 12:55:31 +0000 (+0200) Subject: block: Give always priority to unused entries in the qcow2 L2 cache X-Git-Tag: v2.3.0-rc0~66^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8e8cb375e0964b4ed099cb8563029028db26a834;p=thirdparty%2Fqemu.git 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 Reviewed-by: Max Reitz Signed-off-by: Kevin Wolf --- diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index 904f6b1f44d..b1155492a35 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -253,7 +253,9 @@ static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c) /* Give newer hits priority */ /* TODO Check how to optimize the replacement strategy */ - c->entries[i].cache_hits /= 2; + if (c->entries[i].cache_hits > 1) { + c->entries[i].cache_hits /= 2; + } } if (min_index == -1) {