From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48678) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YJMyD-0007HG-1u for qemu-devel@nongnu.org; Thu, 05 Feb 2015 08:59:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YJMy8-0005mI-3y for qemu-devel@nongnu.org; Thu, 05 Feb 2015 08:59:49 -0500 Received: from smtp3.mundo-r.com ([212.51.32.191]:12193 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YJMy7-0005lr-T1 for qemu-devel@nongnu.org; Thu, 05 Feb 2015 08:59:44 -0500 Date: Thu, 5 Feb 2015 14:59:34 +0100 From: Alberto Garcia Message-ID: <20150205135934.GA18629@igalia.com> References: <1423140931-11823-1-git-send-email-berto@igalia.com> <54D374AE.2090500@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <54D374AE.2090500@redhat.com> Subject: Re: [Qemu-devel] [PATCH] block: Give always priority to unused entries in the qcow2 L2 cache List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz Cc: Kevin Wolf , qemu-devel@nongnu.org, Stefan Hajnoczi On Thu, Feb 05, 2015 at 08:48:30AM -0500, Max Reitz wrote: > >- c->entries[i].cache_hits /= 2; > >+ if (c->entries[i].cache_hits > 1) { > >+ c->entries[i].cache_hits /= 2; > >+ } > > } > > if (min_index == -1) { > > Hm, I can't see where the code is actually giving priority to unused > entries. qcow2_cache_find_entry_to_replace() is the only place which > selects the entry to be used Yes, and it looks for the one with the lowest cache hit count. That is the only criteria: if (c->entries[i].cache_hits < min_count) { min_index = i; min_count = c->entries[i].cache_hits; } If there are several with the same hit count then the first one is chosen. Since dividing the hit count by two everytime there's a cache miss can make it go down to zero, an existing entry with cache_hits == 0 will always be chosen before any empty one located afterwards in the array. By never allowing the hit count to go down to zero, we make sure that all unused entries are chosen first before a valid one is discarded. Berto