From patchwork Tue Nov 15 06:37:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9429577 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 C66E060484 for ; Tue, 15 Nov 2016 12:12:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B1F8228B27 for ; Tue, 15 Nov 2016 12:12:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A6A4928BBB; Tue, 15 Nov 2016 12:12:57 +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 738B428B27 for ; Tue, 15 Nov 2016 12:12:56 +0000 (UTC) Received: from localhost ([::1]:45875 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6cbf-0002CH-PA for patchwork-qemu-devel@patchwork.kernel.org; Tue, 15 Nov 2016 07:12:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59046) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6caz-0001vi-OO for qemu-devel@nongnu.org; Tue, 15 Nov 2016 07:12:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c6caw-0006QV-FZ for qemu-devel@nongnu.org; Tue, 15 Nov 2016 07:12:13 -0500 Received: from mail-db5eur01on0116.outbound.protection.outlook.com ([104.47.2.116]:51904 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 1c6caw-0006QK-5r; Tue, 15 Nov 2016 07:12:10 -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=KHe1m9p/VzT3LOUCl0K/DAsMnB8xh8QWnheKnmyZmC0=; b=NBvM0bvf5tJyOSnKEQ1e8JaT1SV3x7JgoYSsEgqnqhgwXkBcLPe1K1u+SnKpgmi2foAkdB9H0HXYS9oDmzgYtsPC1uAe6dWNp14Xw7W0u5bJhDeL/jm5HjNIgRrpLMMwcM7XHoKEUOGpo5rzqmt5wtWu0tXegICzRQhd/njUlME= 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:22 +0000 From: Pavel Butsykin To: , Date: Tue, 15 Nov 2016 09:37:06 +0300 Message-ID: <20161115063715.12561-10-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:bxze1Gcr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jtrNKq6onXOa5ueSRArm6xD684S4JI3/5W2yLTF5FygbxYY9TQ5q+/YI3Lx1qHqoW1rVxj0Pp3vNHtrA/zqUisM+OPbx36J7wLM+aWPLUmwYyUizFXxPkPmsdwcDbPMJIsvPeJASPxqKsvgCbK0EFKmixMizKnH7abf80J5kJA==; 20:QiRKdvy4chk9vE11NIuUjaPRMPleqQ7z/2IsnUz2jjZAi2/8HeCBxhPQPfezROGN+hTcUoRAAztBt6ibAJ6/3Mbz2/EWeMZyJEOvVWrt2DTj+iH87UseYM1zfHFxJqy2K1BSEuXijIUqD/cF/Sc8MPiMDxEFmjbF8TAH+kewriA=; 4:MfUAzV6fW5Cb/6yFRJAiy3Twt8G8y1JtA0ZP4pvHZxBSgyceN/lTTImIpMVSDpPqK7PJKjZ+3lx6h8Y9OLVSYGwykmoDi0+5QgFi691QdkQtNcFq3zXREIkrbSDYjKcnjNdygKhSQcSPNr21xksY3fDVPG0ZLKaCCCZCd4n0JCHdLxEjpSg+dfke+2hYAQbY5/M/0Svy4+zVM6gMmrGI47zF1GJpP36gKcuDcik5655P68pPfyFc0grGMrROncIaeedgwiQOeJZI0y//r30q41FHehRPGWvHRTTEx+cEp+OQ4cHrkzs2R83mVzuiYaBl2Fxl/oeWy5YvYNVUkmKaasL0OF9aRMPhpZP8161rnKX+XdvgTXve5d3JNo/mvV+lmmjCHi+lVaiUXBwA594Jc+xTtMIgXjVZL8UZBSVM9/+1s7uU4mBIDT100mtjhzbk 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:Ysluau6L+jBiswhhDrDoTqnSD8FwR6QN+n7vYPJ?= =?us-ascii?Q?tsAVKAbdLjpmqGoaPBA/ekAkjLRvFwosZxQp28nWcf5/osbocnuVHyFmMu8g?= =?us-ascii?Q?aDEoRKfHpq0tlDYNomZKSMLn93fJu/vhkhOP9wOXCTgN2jVEXeF6gk8uj/Rj?= =?us-ascii?Q?vsNGf35e8AUX3nkaJ9ypQs6UoUKWty8ME1dydBgHBQQSd+MGrNlBHm/A3fqb?= =?us-ascii?Q?434bduSeQoLbnUinKNhYjfKrBU55gfDkiond8gDaHlirVqcxl//5AAEXffa2?= =?us-ascii?Q?z6gEHAJCHZ523AL0VEYBYDJNplfAKyKtago9xXSpCNUi745a5H2UPFV78UMv?= =?us-ascii?Q?F+69f5+vox0Q2HfshZg8zcQm4QIrK3KSfT7yx0J3z/H/bPEiJ76wpE+/vP5d?= =?us-ascii?Q?G7Fu9wVrxqgq/meIQG8YYyRDKNcipok8xXsV/K/Z73SEXprpyr1B4eTIQcWy?= =?us-ascii?Q?fzoc1b2T5avcRkrQcoaI1EPfy1NPrq22hDdWAkDIjv84bN8XP1pV3zpyx1YS?= =?us-ascii?Q?GLT8disAAlTCRfrZrrRh9r0W7kRVStXI/d0yeb//FDDDBIhyHQb9HDPZa6ue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3JfeQEL87aGQg9dWkKxudei8OPt/9wqhl+jTgulIhqmJ1kc++YR5zx3rQitsWHCxLQl19LWNqgXnqz2pGC9DEcnFRMTCFiYkL4+MdvTweywy3LQuKfUyg7MvZuEYYfVPELdD/0PshGdWJW80SbjGfGVrh9ybCyr/5tMouvB5/nY=; 24:bizTTF1BQ0uPWIJAOXbX9gKx5d5a+LOEixdK3ScgOBO8yIBNAejCRzpoUh5jvaA+ROeR7LNWnHOGQk/RisLtO+Ka7HEsi66pfy7FPLiMV2k= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 7:iupxIql1CE0RGZyshdur5U9FVe+h3e1NzoYwdQosY9r1qvsqOhxchgkxMceABkTo40Sxv6LY1x0wTvb2IesnbeIejkSWQ4nDocltYg/jWZ8633OclRVqfjywsycIr0BMI+ixh7zgBJ3ap2bc8+7G3VvvWXvmYyFXLrxoGJTLuZrLay7u3fJlfcBjE4rf4VIRzNH/XnEPEXciBaviaYK5KnRxBf6+DjZeIZ2gt3nJPcXu8ySIqoeeEG3fTDjMKT+SdjRPEiD3W7cJgy+9EEqEO6JBNcvzlcTrhwYD6d9hnmYXRjnae8IOcbvgEHVwDFjZbPqxt60STQYF45KOsqYIQ+IzSFCQG/r43CLO5FnqxLo=; 20:oZqWgAixRRnlXmeyZRa7Onm/DhrEz2gebNDpTg44qSAK6hSmZ7tF+D3eGO0aYi+c2Ok3XHe7gINISUVwhG6DZFk7AcYlanTmPLHQ931F028L/RjpEf9xgfQi2XJZbwYTL60Sbxo8ThGPfjRZydFhkyFi9DlNHvluyh4Eprx6KK0= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2016 06:38:22.3124 (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.116 Subject: [Qemu-devel] [PATCH v1 09/18] block/pcache: add AIO readahead 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 This patch adds readahead data to the cache. Here the readahead is a separate asynchronous request, which doesn't depend on completion of filtered read requests. The readahead is done only by the condition, if before the current request there's sequential read data enough size. This information can give the request statistics, of course this method of detection is not very reliable, but in most cases it'll work. Signed-off-by: Pavel Butsykin --- block/pcache.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 227 insertions(+), 2 deletions(-) diff --git a/block/pcache.c b/block/pcache.c index dd598f3..3717037 100644 --- a/block/pcache.c +++ b/block/pcache.c @@ -17,6 +17,8 @@ #define PCACHE_OPT_STATS_SIZE "pcache-stats-size" #define PCACHE_OPT_MAX_AIO_SIZE "pcache-max-aio-size" +#define PCACHE_OPT_CACHE_SIZE "pcache-full-size" +#define PCACHE_OPT_READAHEAD_SIZE "pcache-readahead-size" static QemuOptsList runtime_opts = { .name = "pcache", @@ -37,6 +39,16 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_SIZE, .help = "Maximum size of aio which is handled by pcache", }, + { + .name = PCACHE_OPT_CACHE_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Total cache size", + }, + { + .name = PCACHE_OPT_READAHEAD_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Prefetch cache readahead size", + }, { /* end of list */ } }, }; @@ -45,17 +57,61 @@ static QemuOptsList runtime_opts = { #define MB_BITS 20 #define PCACHE_DEFAULT_STATS_SIZE (3 << MB_BITS) #define PCACHE_DEFAULT_MAX_AIO_SIZE (64 << KB_BITS) +#define PCACHE_DEFAULT_CACHE_SIZE (4 << MB_BITS) +#define PCACHE_DEFAULT_READAHEAD_SIZE (128 << KB_BITS) typedef struct BDRVPCacheState { RBCache *req_stats; + RBCache *cache; uint64_t max_aio_size; + uint64_t readahead_size; } BDRVPCacheState; +typedef struct PCacheNode { + RBCacheNode common; + uint8_t *data; + enum { + NODE_STATUS_NEW = 0, + NODE_STATUS_INFLIGHT = 1, + NODE_STATUS_COMPLETED = 2, + NODE_STATUS_REMOVE = 3, + NODE_STATUS_DELETED = 4, /* only for debugging */ + } status; + int ref; +} PCacheNode; + typedef struct PCacheAIOCB { + BlockDriverState *bs; Coroutine *co; + uint64_t offset; + uint64_t bytes; int ret; } PCacheAIOCB; +typedef struct PCacheAIOCBReadahead { + BlockDriverState *bs; + Coroutine *co; + QEMUIOVector qiov; + PCacheNode *node; +} PCacheAIOCBReadahead; + +static inline void pcache_node_ref(PCacheNode *node) +{ + node->ref++; +} + +static void pcache_node_unref(PCacheNode *node) +{ + assert(node->ref > 0); + if (--node->ref == 0) { + assert(node->status == NODE_STATUS_REMOVE); + node->status = NODE_STATUS_DELETED; + + g_free(node->data); + g_free(node); + } +} + static void pcache_aio_cb(void *opaque, int ret) { PCacheAIOCB *acb = opaque; @@ -64,6 +120,27 @@ static void pcache_aio_cb(void *opaque, int ret) qemu_coroutine_enter(acb->co); } +static void pcache_aio_readahead_cb(void *opaque, int ret) +{ + PCacheAIOCBReadahead *acb = opaque; + PCacheNode *node = acb->node; + + assert(node->status == NODE_STATUS_INFLIGHT || + node->status == NODE_STATUS_REMOVE); + + if (node->status == NODE_STATUS_INFLIGHT) { + if (ret == 0) { + node->status = NODE_STATUS_COMPLETED; + } else { + BDRVPCacheState *s = acb->bs->opaque; + rbcache_remove(s->cache, &node->common); + } + } + pcache_node_unref(node); + + qemu_coroutine_enter(acb->co); +} + static void update_req_stats(RBCache *rbcache, uint64_t offset, uint64_t bytes) { do { @@ -98,6 +175,138 @@ static void update_req_stats(RBCache *rbcache, uint64_t offset, uint64_t bytes) } while (true); } +static bool check_request_sequence(BDRVPCacheState *s, uint64_t offset) +{ + uint64_t cache_line_size = s->readahead_size; + uint64_t check_offset; + + if (offset <= cache_line_size) { + return false; + } + check_offset = offset - cache_line_size; + + do { + RBCacheNode *node = rbcache_search(s->req_stats, check_offset, + offset - check_offset); + if (node == NULL) { + return false; + } + if (node->offset > check_offset) { + return false; + } + check_offset = node->offset + node->bytes; + } while (check_offset < offset); + + return true; +} + +static void pcache_node_free(RBCacheNode *rbnode, void *opaque) +{ + PCacheNode *node = container_of(rbnode, PCacheNode, common); + + assert(node->status == NODE_STATUS_INFLIGHT || + node->status == NODE_STATUS_COMPLETED); + + node->status = NODE_STATUS_REMOVE; + pcache_node_unref(node); +} + +static RBCacheNode *pcache_node_alloc(uint64_t offset, uint64_t bytes, + void *opaque) +{ + PCacheNode *node = g_malloc(sizeof(*node)); + + node->data = g_malloc(bytes); + node->status = NODE_STATUS_NEW; + node->ref = 1; + + return &node->common; +} + +#define PCACHE_STEPS_FORWARD 2 + +static PCacheNode *get_readahead_node(BlockDriverState *bs, RBCache *rbcache, + uint64_t offset, uint64_t bytes) +{ + uint32_t count = PCACHE_STEPS_FORWARD; + + int64_t total_bytes = bdrv_getlength(bs); + if (total_bytes < 0) { + return NULL; + } + + while (count--) { + PCacheNode *node; + + if (total_bytes <= offset + bytes) { + break; + } + + node = rbcache_search_and_insert(rbcache, offset, bytes); + if (node->status == NODE_STATUS_NEW) { + return node; + } + /* The range less than the readahead size is not cached to reduce + * fragmentation of the cache. If the data is already cached, then we + * just step over it. + */ + if (offset <= node->common.offset && !count--) { + break; + } + offset = node->common.offset + node->common.bytes; + }; + + return NULL; +} + +static void coroutine_fn pcache_co_readahead(void *opaque) +{ + PCacheAIOCB *acb = g_memdup(opaque, sizeof(*acb)); + BlockDriverState *bs = acb->bs; + BDRVPCacheState *s = bs->opaque; + uint64_t offset; + uint64_t bytes; + PCacheAIOCBReadahead readahead_acb; + PCacheNode *node; + + if (!check_request_sequence(s, acb->offset)) { + goto out; + } + + offset = acb->offset + acb->bytes; + bytes = s->readahead_size; + + node = get_readahead_node(bs, s->cache, offset, bytes); + if (node == NULL) { + goto out; + } + + readahead_acb = (PCacheAIOCBReadahead) { + .co = qemu_coroutine_self(), + .bs = bs, + .node = node, + }; + + node->status = NODE_STATUS_INFLIGHT; + qemu_iovec_init(&readahead_acb.qiov, 1); + qemu_iovec_add(&readahead_acb.qiov, node->data, node->common.bytes); + + pcache_node_ref(node); + + bdrv_aio_preadv(bs->file, node->common.offset, &readahead_acb.qiov, + node->common.bytes, pcache_aio_readahead_cb, + &readahead_acb); + qemu_coroutine_yield(); +out: + free(acb); +} + +static void pcache_readahead_request(PCacheAIOCB *acb) +{ + Coroutine *co = qemu_coroutine_create(pcache_co_readahead, acb); + qemu_coroutine_enter(co); +} + static coroutine_fn int pcache_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) @@ -105,14 +314,23 @@ static coroutine_fn int pcache_co_preadv(BlockDriverState *bs, uint64_t offset, BDRVPCacheState *s = bs->opaque; PCacheAIOCB acb = { .co = qemu_coroutine_self(), + .bs = bs, + .offset = offset, + .bytes = bytes, }; - if (s->max_aio_size >= bytes) { - update_req_stats(s->req_stats, offset, bytes); + if (bytes > s->max_aio_size) { + bdrv_aio_preadv(bs->file, offset, qiov, bytes, pcache_aio_cb, &acb); + goto out; } + update_req_stats(s->req_stats, offset, bytes); + bdrv_aio_preadv(bs->file, offset, qiov, bytes, pcache_aio_cb, &acb); + pcache_readahead_request(&acb); + +out: qemu_coroutine_yield(); return acb.ret; @@ -137,10 +355,16 @@ static void pcache_state_init(QemuOpts *opts, BDRVPCacheState *s) { uint64_t stats_size = qemu_opt_get_size(opts, PCACHE_OPT_STATS_SIZE, PCACHE_DEFAULT_STATS_SIZE); + uint64_t cache_size = qemu_opt_get_size(opts, PCACHE_OPT_CACHE_SIZE, + PCACHE_DEFAULT_CACHE_SIZE); s->req_stats = rbcache_create(NULL, NULL, stats_size, RBCACHE_FIFO, s); s->max_aio_size = qemu_opt_get_size(opts, PCACHE_OPT_MAX_AIO_SIZE, PCACHE_DEFAULT_MAX_AIO_SIZE); + s->cache = rbcache_create(pcache_node_alloc, pcache_node_free, cache_size, + RBCACHE_LRU, s); + s->readahead_size = qemu_opt_get_size(opts, PCACHE_OPT_READAHEAD_SIZE, + PCACHE_DEFAULT_READAHEAD_SIZE); } static int pcache_file_open(BlockDriverState *bs, QDict *options, int flags, @@ -177,6 +401,7 @@ static void pcache_close(BlockDriverState *bs) BDRVPCacheState *s = bs->opaque; rbcache_destroy(s->req_stats); + rbcache_destroy(s->cache); } static void pcache_parse_filename(const char *filename, QDict *options,