From patchwork Tue Nov 15 06:37:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9429501 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1044960484 for ; Tue, 15 Nov 2016 11:21:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F2F8D2875F for ; Tue, 15 Nov 2016 11:21:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E792828990; Tue, 15 Nov 2016 11:21:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 429F32875F for ; Tue, 15 Nov 2016 11:21:46 +0000 (UTC) Received: from localhost ([::1]:45610 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6bo9-0002ug-Fn for patchwork-qemu-devel@patchwork.kernel.org; Tue, 15 Nov 2016 06:21:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45931) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6bd4-0002Qt-LW for qemu-devel@nongnu.org; Tue, 15 Nov 2016 06:10:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c6bcz-0000tm-ME for qemu-devel@nongnu.org; Tue, 15 Nov 2016 06:10:18 -0500 Received: from mail-db5eur01on0110.outbound.protection.outlook.com ([104.47.2.110]:24570 helo=EUR01-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c6bcz-0000px-CO; Tue, 15 Nov 2016 06:10:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=O852/Wbna9YLoNDMghjjLJIFsOOnwqcDIL1cOEk5Z0A=; b=MIYGb1TKYeNaRfK762TwrD6zjgSiSs+SX97Jue6kF7weEbMbUyDM2Fi+IaDPPmaYaXyNWqJAHdDO0zOgx7dPjVVxPqwNtUITUwU6Mg113zZA+PkPju9CUQQlelx81d+D443rFWaOTdcOvZn+dO6h4ZXnDf9Ytgnw2dzAHx6Cgkw= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.10) by VI1PR0802MB2558.eurprd08.prod.outlook.com (10.172.255.136) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.721.10; Tue, 15 Nov 2016 06:38:26 +0000 From: Pavel Butsykin To: , Date: Tue, 15 Nov 2016 09:37:09 +0300 Message-ID: <20161115063715.12561-13-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161115063715.12561-1-pbutsykin@virtuozzo.com> References: <20161115063715.12561-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.10] X-ClientProxiedBy: AM5PR0101CA0010.eurprd01.prod.exchangelabs.com (10.169.240.20) To VI1PR0802MB2558.eurprd08.prod.outlook.com (10.172.255.136) X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 2:fD2I9arcpK7hSh+mpfwln35dCM0uRR0mU89EYMAfysgWnkCt08XQGMy+bnMx2Zz3kUbFZgOvdmxzL38p+BdAW5xVVe5VWLhKWP/2y1E0bejgEnzKujbgE1vhgg1SrLkjPV24aLM9aRRasHiJA2T874JpHE0v6UqLYDLz7Vg8zE8=; 3:N8iyBsR4yR714yRI9A53WjCW5mrze6EQloq8Iv29xbbbbqOiItZBDUHrc7CJqRsIUGz7seJa/Eq4sPln9d/ixZd/QgfjQZBllR+dhLdRtLdJ2XBur9VEKtD4ZrtQNa9KidJC7/VOlqZ1SKUswc43uRGCNVY91e5U/bmMBtdhcXQ=; 25:VT/KADThB603ZQWgACuPoBFPfOlSRD49Fd77s79iZ60xiB1mzrbvaDQwQCc8PxWUpvI794qGZ5j8ys8JKpf4hAd8z8H/FV9X6kaqdCXNHZpfQIb+2dzvCgWNhU7lQlsTs2LdE3exzrzH7kF/eocA4YAt1/QfQOQQ/lX5gbrcTDVLaY4W5KON0MZdwQPckZE+KeE1WSUUsUKSNAOPKGE3exhYGV3ASW2JNEVEuvnvbxH6g0TaoaNsBEtlmXKCbNGvltHPNrNSqMlx2DcNt3HCXk4B4rbzEyKLvD2d0dIdQq6cDMjfENRg4WK+NOCrAAvOVpe5JeGGi//1wAE0jeEY7VKhWV7TPEO0jeF0ibk7pbAMZF005OfoGM97uDwKQiIhsA0DhKAUuunlGCvPxVL/JHv8haF7vDYHZoBhzoIAvWA= X-MS-Office365-Filtering-Correlation-Id: 11f77485-ddeb-4701-623e-08d40d220151 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:VI1PR0802MB2558; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 31:Qu+gwJ70dfk3DUicJcB0MQptWUYIrWtcBeuBs6g7MxFqGsj++vl55RG0QtOYtayLZM++reuDz5991omqQjavEfsc25kByeHN7P6uBxA5ZbrahtS6vg9WIPtprTiSBlz2QgCjPv0eRvTEM+t4jeuXEJw7lYkxdb1uvy0hSZs7zarRgPt3zf1AFkh8iXAqzanu/5yOIusujxE2TVPkCB44sQDiZyM/Y5PXXhXMlNGX7fs4jND0YJKSHbLpF51mG1NgmKx8c7XgH7Goc+DALawRpw==; 20:C3cLYNGykJxOsnZBoVEqicO0UwmksfzHoSCoQowo8Tl5joVH7jreHM3HGgb8TCg/FLIcyU2kpu/lm3OmftsV0mN5zSRCMra1JUSHK4UeS6sxLBNHraX2SgAWEx2IBRaxbxn2G6vycYV3V5jqerbAayTYwukJqnsJwrAjCDKljH0=; 4:YynQ5UU6gkL4Yd5DBdGcVml/kz2FZySz1z4Kf5hvs+v5zs5BqviDea/CyCLKmryc/AKUlYcgMnH07jUTOgAxvozE2i6wy86ZXtHn5MC/IH6znAKicSTUO1XNAS26gdGZAKVrJMy1AAfeDpUJubYtHDHTvbrSeErceY6pgy1SXFRV9orlfKfGFt0QNDnzW4IN+qYLUYhErTXwLyzDeusUTtYTGmrChh7J2udMMiv2R8/7dbSEST6FEvKiUvCMBdiwwUIJctjK9L7JhmFWLIdSCfGMDhFYvT/MVdth2c/2khoTrQB3bIMQxA1lD9d12LYg5lvRCflCeE5Vmk50yfUOz4AIGvmuP1ck4+tObZbAmDflwtPAujm6VT0mDshVLo2114Zwc6jOZuk/XFJlUpjeAuUaOGm1nxobRyNyL2NiOhPXjzXkYTEZbrgcYwueTVQA X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6060326)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6061324)(6043046); SRVR:VI1PR0802MB2558; BCL:0; PCL:0; RULEID:; SRVR:VI1PR0802MB2558; X-Forefront-PRVS: 012792EC17 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(7916002)(199003)(189002)(47776003)(105586002)(101416001)(1076002)(68736007)(106356001)(6666003)(76176999)(305945005)(4326007)(97736004)(66066001)(53416004)(189998001)(42186005)(69596002)(2906002)(7736002)(5001770100001)(7846002)(86362001)(48376002)(50226002)(2950100002)(50466002)(50986999)(5003940100001)(33646002)(77096005)(81156014)(8676002)(92566002)(81166006)(3846002)(36756003)(6116002)(5660300001); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR0802MB2558; H:pavelb-Z68P-DS3.sw.ru; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; VI1PR0802MB2558; 23:zoreDIU+9cDVoR4AF8BFwBfADerxrYpDJP0gae6?= =?us-ascii?Q?tYCOvyKbdkJ4+QcT4ob8Phd6iaWBd+0byUjoFeCrnMzMI7vJcAObxKbI8K76?= =?us-ascii?Q?KdnJn9x+g1utY5ujPyn3mPsmO0KEjenq4NfXYmut6kj+Nj+tqf81wvLD5S8G?= =?us-ascii?Q?gsmlqaKn4sV5pkPf9zI6IIBH1ZIjR3dgKMCJpdgMLiqzzNeug9GXwICwQvhI?= =?us-ascii?Q?r1LvZAdBCEZWjogtzRhqiIjmeFCFna5/pFWGIVsvv5wqiaRZJhjhX5bNbMQR?= =?us-ascii?Q?0EaKWRtF3GmlcYY95XmkTtIW/RkUNDYh0/Zgt7KYHZFdrfVq+SfhIRRuc98z?= =?us-ascii?Q?xjk9ISK10XmUuA4IXWdIscXEpRZsXiembCAOPtfz3lKp+ymEcforQM4PFg93?= =?us-ascii?Q?quOX7DR1TsMsiY5sj+PPmuNsvNtcMZqisisUrQsPpbJo9qr4444lCy+D8l8K?= =?us-ascii?Q?DUnpfIHFHJP6vYoQDOrpwcWYM7UdXvB9QrptoyT9PLcXrl7YaqPNgvljFb/k?= =?us-ascii?Q?AcT3OuHD2oSa6cmCIF3rS1dONEDkcuoo3MQbxKcl9XDySj1DCCZy3I11lKkx?= =?us-ascii?Q?FeqBPOQ9IBonfime6ZwWNoqRXnLtI04Vh895BgHP2T5O8zG7J7d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h59v8pLBfbxDt13WdX2VyJZfRIu7b8R/e/NQlWOMphBADxF5zF2ZGw2TUwiIWYs37YANbJPPHLflblw=; 24:bh2GPWgPjyA1nT7CEszuBZufMOQg0M+jW/15i6Oatru2OjCPMCvdql/PWgjG8veKauzI/n8NUNRt8RJEkLMt3XG6f+Vf75i5Y+vlYWL9u/A= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 7:+4cO73jNAMZ61YXqmnNOT33YjgWB57KrG02Ly4pHpCZnMvToxMxVMbL6lypPacSspv4beniY8SV7ktWOqJVTdI7F+RvTJ8VIdSG10YABU+XS64MQ+HpXGZLdh2LNEBke6KHI2SvZkEALh4aBKdAUCuOlJq3Xg7AIF4YDwvtVyoFqmGNdLOPDAHm93s2fVoWhNn6l7rEvKi0YLIINBqFUjK+3PlVFdDzNbTnluxyipStWpXJy3SPHqjPn3Ks5uxbJYd2CL1bX904rNLmYK73mAN/gto6s3dMuwSXzwRinGtjclymhWOcMV1TlbaTGoOIslfDHV2SUpihvSAjPx4Z0NYbCaSkDzdWRMkOkmaTlt48=; 20:nLKVHDP78nZXObrrHlhaYLay1dWZiWyaZVkxgK8bxlan9cOZp/7IUeWX4eTS7mLFyhs1xbg4XpXW3/OeOkJbPyfET3IrxBgBEjSjckCNpejOCmvueG4EGtaXvjnN5m1T5mTbCm27UVQ7cbnt752lznqkRt6SmJUCvEyE3ZqJwf8= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2016 06:38:26.1436 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0802MB2558 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.110 Subject: [Qemu-devel] [PATCH v1 12/18] block/pcache: add reading data from the cache 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: kwolf@redhat.com, famz@redhat.com, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Added read_cache_data() allowing to read data from node to qiov. And the simplest use case - read data from node provided that the node is not in flight and fully covers read request. Signed-off-by: Pavel Butsykin --- block/pcache.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 11 deletions(-) diff --git a/block/pcache.c b/block/pcache.c index 27ee6dd..0f918d0 100644 --- a/block/pcache.c +++ b/block/pcache.c @@ -80,13 +80,22 @@ typedef struct PCacheNode { int ref; } PCacheNode; -typedef struct PCacheAIOCB { +typedef struct PCacheAIOCBWrite { BlockDriverState *bs; Coroutine *co; uint64_t offset; uint64_t bytes; int ret; -} PCacheAIOCB; +} PCacheAIOCBWrite; + +typedef struct PCacheAIOCBRead { + BlockDriverState *bs; + Coroutine *co; + uint64_t offset; + uint64_t bytes; + QEMUIOVector *qiov; + int ret; +} PCacheAIOCBRead; typedef struct PCacheAIOCBReadahead { BlockDriverState *bs; @@ -112,9 +121,35 @@ static void pcache_node_unref(PCacheNode *node) } } -static void pcache_aio_cb(void *opaque, int ret) +static uint64_t ranges_overlap_size(uint64_t offset1, uint64_t size1, + uint64_t offset2, uint32_t size2) +{ + return MIN(offset1 + size1, offset2 + size2) - MAX(offset1, offset2); +} + +static void read_cache_data(PCacheAIOCBRead *acb, PCacheNode *node, + uint64_t offset, uint64_t bytes) +{ + uint64_t qiov_offs = 0, node_offs = 0; + uint64_t size; + uint64_t copy; + + if (offset < node->common.offset) { + qiov_offs = node->common.offset - offset; + } else { + node_offs = offset - node->common.offset; + } + size = ranges_overlap_size(offset, bytes, node->common.offset, + node->common.bytes); + + copy = qemu_iovec_from_buf(acb->qiov, qiov_offs, + node->data + node_offs, size); + assert(copy == size); +} + +static void pcache_aio_read_cb(void *opaque, int ret) { - PCacheAIOCB *acb = opaque; + PCacheAIOCBRead *acb = opaque; acb->ret = ret; qemu_coroutine_enter(acb->co); @@ -287,7 +322,7 @@ static PCacheNode *get_readahead_node(BlockDriverState *bs, RBCache *rbcache, static void coroutine_fn pcache_co_readahead(void *opaque) { - PCacheAIOCB *acb = g_memdup(opaque, sizeof(*acb)); + PCacheAIOCBRead *acb = g_memdup(opaque, sizeof(*acb)); BlockDriverState *bs = acb->bs; BDRVPCacheState *s = bs->opaque; uint64_t offset; @@ -327,32 +362,66 @@ out: free(acb); } -static void pcache_readahead_request(PCacheAIOCB *acb) +static void pcache_readahead_request(PCacheAIOCBRead *acb) { Coroutine *co = qemu_coroutine_create(pcache_co_readahead, acb); qemu_coroutine_enter(co); } +enum { + CACHE_MISS, + CACHE_HIT, + PARTIAL_CACHE_HIT, +}; + +static int pcache_lookup_data(PCacheAIOCBRead *acb) +{ + BDRVPCacheState *s = acb->bs->opaque; + + PCacheNode *node = rbcache_search(s->cache, acb->offset, acb->bytes); + if (node == NULL || node->status != NODE_STATUS_COMPLETED) { + return CACHE_MISS; + } + + /* Node covers the whole request */ + if (node->common.offset <= acb->offset && + node->common.offset + node->common.bytes >= acb->offset + acb->bytes) + { + read_cache_data(acb, node, acb->offset, acb->bytes); + return CACHE_HIT; + } + + return PARTIAL_CACHE_HIT; +} + static coroutine_fn int pcache_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) { BDRVPCacheState *s = bs->opaque; - PCacheAIOCB acb = { + PCacheAIOCBRead acb = { .co = qemu_coroutine_self(), .bs = bs, .offset = offset, .bytes = bytes, + .qiov = qiov, }; + int status; if (bytes > s->max_aio_size) { - bdrv_aio_preadv(bs->file, offset, qiov, bytes, pcache_aio_cb, &acb); + bdrv_aio_preadv(bs->file, offset, qiov, bytes, + pcache_aio_read_cb, &acb); goto out; } update_req_stats(s->req_stats, offset, bytes); - bdrv_aio_preadv(bs->file, offset, qiov, bytes, pcache_aio_cb, &acb); + status = pcache_lookup_data(&acb); + if (status == CACHE_HIT) { + return 0; + } + + bdrv_aio_preadv(bs->file, offset, qiov, bytes, pcache_aio_read_cb, &acb); pcache_readahead_request(&acb); @@ -364,7 +433,7 @@ out: static void pcache_aio_write_cb(void *opaque, int ret) { - PCacheAIOCB *acb = opaque; + PCacheAIOCBWrite *acb = opaque; BDRVPCacheState *s = acb->bs->opaque; uint64_t offset = acb->offset; uint64_t bytes = acb->bytes; @@ -400,7 +469,7 @@ static coroutine_fn int pcache_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) { - PCacheAIOCB acb = { + PCacheAIOCBWrite acb = { .co = qemu_coroutine_self(), .bs = bs, .offset = offset,