From patchwork Thu Oct 12 13:05:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 824822 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="EwBl3Bip"; dkim-atps=neutral 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 3yCWXK68llz9t2m for ; Fri, 13 Oct 2017 00:14:25 +1100 (AEDT) Received: from localhost ([::1]:45440 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2dJf-0003MO-Qe for incoming@patchwork.ozlabs.org; Thu, 12 Oct 2017 09:14:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2dG4-0000zw-Oh for qemu-devel@nongnu.org; Thu, 12 Oct 2017 09:10:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e2dG0-0000Yd-Kg for qemu-devel@nongnu.org; Thu, 12 Oct 2017 09:10:40 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:52202) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e2dG0-0007Rf-8O; Thu, 12 Oct 2017 09:10:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=lv+r8MoWJEFSl2ll/qCDr+AlwxRt/zt3p6tWwZKQOi8=; b=EwBl3Bip/x1HsCeQsVbcJZLYUjLa93gRku1dxyVRGxLIhGTBVpk4bV/21cJlSdPOQ9lAzrtOmlPOK6U6DiL8+D/4sRwaYRThGBeNk6LHrQNeHxt8jzMXj/8BHDfEf7Qd2xUiWIKWPatvllRt+xsZs3FMkZn/UNgA0D806PwH/GDA2z677ODdQgFRw0wyf0FuHzW+utB6WURbUm5y8FJ1ozaQj0W+mBv3UqXXntwKDHsSrnXodPCMspJnEHr+nMAbznmXXCHXFkvNQYlccCm95MzQyk4YVTzO2iVOzz1u6f+q+4TD52jT+bTLizQUf3eSforddFt7jN4knjW8Yi4afQ==; Received: from a88-114-101-76.elisa-laajakaista.fi ([88.114.101.76] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1e2dFO-0006MO-9j; Thu, 12 Oct 2017 15:09:58 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1e2dCy-0003ZQ-4n; Thu, 12 Oct 2017 16:07:28 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Thu, 12 Oct 2017 16:05:16 +0300 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 02/31] qcow2: Add table size field to Qcow2Cache X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , "Denis V . Lunev" , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" The table size in the qcow2 cache is currently equal to the cluster size. This doesn't allow us to use the cache memory efficiently, particularly with large cluster sizes, so we need to be able to have smaller cache tables that are independent from the cluster size. This patch adds a new field to Qcow2Cache that we can use instead of the cluster size. The current table size is still being initialized to the cluster size, so there are no semantic changes yet, but this patch will allow us to prepare the rest of the code and simplify a few function calls. Signed-off-by: Alberto Garcia --- block/qcow2-cache.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index 75746a7f43..72cb7148ff 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -39,6 +39,7 @@ struct Qcow2Cache { Qcow2CachedTable *entries; struct Qcow2Cache *depends; int size; + int table_size; bool depends_on_flush; void *table_array; uint64_t lru_counter; @@ -48,17 +49,15 @@ struct Qcow2Cache { static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs, Qcow2Cache *c, int table) { - BDRVQcow2State *s = bs->opaque; - return (uint8_t *) c->table_array + (size_t) table * s->cluster_size; + return (uint8_t *) c->table_array + (size_t) table * c->table_size; } static inline int qcow2_cache_get_table_idx(BlockDriverState *bs, Qcow2Cache *c, void *table) { - BDRVQcow2State *s = bs->opaque; ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array; - int idx = table_offset / s->cluster_size; - assert(idx >= 0 && idx < c->size && table_offset % s->cluster_size == 0); + int idx = table_offset / c->table_size; + assert(idx >= 0 && idx < c->size && table_offset % c->table_size == 0); return idx; } @@ -67,10 +66,9 @@ static void qcow2_cache_table_release(BlockDriverState *bs, Qcow2Cache *c, { /* Using MADV_DONTNEED to discard memory is a Linux-specific feature */ #ifdef CONFIG_LINUX - BDRVQcow2State *s = bs->opaque; void *t = qcow2_cache_get_table_addr(bs, c, i); int align = getpagesize(); - size_t mem_size = (size_t) s->cluster_size * num_tables; + size_t mem_size = (size_t) c->table_size * num_tables; size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t; size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align); if (length > 0) { @@ -120,9 +118,10 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) c = g_new0(Qcow2Cache, 1); c->size = num_tables; + c->table_size = s->cluster_size; c->entries = g_try_new0(Qcow2CachedTable, num_tables); c->table_array = qemu_try_blockalign(bs->file->bs, - (size_t) num_tables * s->cluster_size); + (size_t) num_tables * c->table_size); if (!c->entries || !c->table_array) { qemu_vfree(c->table_array); @@ -191,13 +190,13 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i) if (c == s->refcount_block_cache) { ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK, - c->entries[i].offset, s->cluster_size); + c->entries[i].offset, c->table_size); } else if (c == s->l2_table_cache) { ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2, - c->entries[i].offset, s->cluster_size); + c->entries[i].offset, c->table_size); } else { ret = qcow2_pre_write_overlap_check(bs, 0, - c->entries[i].offset, s->cluster_size); + c->entries[i].offset, c->table_size); } if (ret < 0) { @@ -211,7 +210,7 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i) } ret = bdrv_pwrite(bs->file, c->entries[i].offset, - qcow2_cache_get_table_addr(bs, c, i), s->cluster_size); + qcow2_cache_get_table_addr(bs, c, i), c->table_size); if (ret < 0) { return ret; } @@ -318,7 +317,7 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, offset, read_from_disk); /* Check if the table is already cached */ - i = lookup_index = (offset / s->cluster_size * 4) % c->size; + i = lookup_index = (offset / c->table_size * 4) % c->size; do { const Qcow2CachedTable *t = &c->entries[i]; if (t->offset == offset) { @@ -359,7 +358,7 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, ret = bdrv_pread(bs->file, offset, qcow2_cache_get_table_addr(bs, c, i), - s->cluster_size); + c->table_size); if (ret < 0) { return ret; }