From patchwork Mon May 11 12:54:56 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 470779 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 50FAF1400B7 for ; Mon, 11 May 2015 23:06:09 +1000 (AEST) Received: from localhost ([::1]:37209 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YrnPL-0007Pk-31 for incoming@patchwork.ozlabs.org; Mon, 11 May 2015 09:06:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59078) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YrnFd-0007m8-0E for qemu-devel@nongnu.org; Mon, 11 May 2015 08:56:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YrnFY-0005Wn-Vu for qemu-devel@nongnu.org; Mon, 11 May 2015 08:56:04 -0400 Received: from smtp3.mundo-r.com ([212.51.32.191]:32157 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YrnFY-0005LU-7R; Mon, 11 May 2015 08:56:00 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ai0IAKOlUFVbdWOb/2dsb2JhbABcgw+BMrJvAQEBAQEBBQGBBAGYVgKBL0wBAQEBAQGBC4QhAQEEJ1IQPxI8GxmIMAHHMAErhhaKKAcWhBcFhl+FOI0Kg3+MaIlvI2FmQhyBVWyCRgEBAQ X-IPAS-Result: Ai0IAKOlUFVbdWOb/2dsb2JhbABcgw+BMrJvAQEBAQEBBQGBBAGYVgKBL0wBAQEBAQGBC4QhAQEEJ1IQPxI8GxmIMAHHMAErhhaKKAcWhBcFhl+FOI0Kg3+MaIlvI2FmQhyBVWyCRgEBAQ X-IronPort-AV: E=Sophos;i="5.13,407,1427752800"; d="scan'208";a="348478596" Received: from fanzine.igalia.com ([91.117.99.155]) by smtp4.mundo-r.com with ESMTP; 11 May 2015 14:55:20 +0200 Received: from dsl-hkibrasgw4-50df50-128.dhcp.inet.fi ([80.223.80.128] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim) id 1YrnEu-0006Ev-0u; Mon, 11 May 2015 14:55:20 +0200 Received: from berto by perseus.local with local (Exim 4.85) (envelope-from ) id 1YrnEg-0002MV-RY; Mon, 11 May 2015 15:55:06 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Mon, 11 May 2015 15:54:56 +0300 Message-Id: X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 212.51.32.191 Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 4/8] qcow2: remove qcow2_cache_find_entry_to_replace() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org A cache miss means that the whole array was traversed and the entry we were looking for was not found, so there's no need to traverse it again in order to select an entry to replace. Signed-off-by: Alberto Garcia Reviewed-by: Stefan Hajnoczi Reviewed-by: Max Reitz --- block/qcow2-cache.c | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index 3a31895..2035cd8 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -242,51 +242,38 @@ int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c) return 0; } -static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c) -{ - int i; - uint64_t min_lru_counter = UINT64_MAX; - int min_index = -1; - - - for (i = 0; i < c->size; i++) { - if (c->entries[i].ref) { - continue; - } - - if (c->entries[i].lru_counter < min_lru_counter) { - min_index = i; - min_lru_counter = c->entries[i].lru_counter; - } - } - - if (min_index == -1) { - /* This can't happen in current synchronous code, but leave the check - * here as a reminder for whoever starts using AIO with the cache */ - abort(); - } - return min_index; -} - static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, void **table, bool read_from_disk) { BDRVQcowState *s = bs->opaque; int i; int ret; + uint64_t min_lru_counter = UINT64_MAX; + int min_lru_index = -1; trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache, offset, read_from_disk); /* Check if the table is already cached */ for (i = 0; i < c->size; i++) { - if (c->entries[i].offset == offset) { + const Qcow2CachedTable *t = &c->entries[i]; + if (t->offset == offset) { goto found; } + if (t->ref == 0 && t->lru_counter < min_lru_counter) { + min_lru_counter = t->lru_counter; + min_lru_index = i; + } + } + + if (min_lru_index == -1) { + /* This can't happen in current synchronous code, but leave the check + * here as a reminder for whoever starts using AIO with the cache */ + abort(); } - /* If not, write a table back and replace it */ - i = qcow2_cache_find_entry_to_replace(c); + /* Cache miss: write a table back and replace it */ + i = min_lru_index; trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(), c == s->l2_table_cache, i); if (i < 0) {