From patchwork Thu Aug 25 13:44: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: 9299411 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 69ED260757 for ; Thu, 25 Aug 2016 13:57:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B7AD29354 for ; Thu, 25 Aug 2016 13:57:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5007B29356; Thu, 25 Aug 2016 13:57:28 +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 9CB3C29354 for ; Thu, 25 Aug 2016 13:57:27 +0000 (UTC) Received: from localhost ([::1]:56417 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bcv9q-0006PR-12 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 25 Aug 2016 09:57:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47074) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bcuyY-0004HC-Ou for qemu-devel@nongnu.org; Thu, 25 Aug 2016 09:45:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bcuyT-0002D8-3y for qemu-devel@nongnu.org; Thu, 25 Aug 2016 09:45:45 -0400 Received: from mail-db5eur01on0139.outbound.protection.outlook.com ([104.47.2.139]:49482 helo=EUR01-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bcuyN-0002AT-V5; Thu, 25 Aug 2016 09:45:36 -0400 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=D92OGGPdxuGUANo6vq8CIcitX0tIPe7qcpNxU7FzOI8=; b=hNrPv98QSjtct+qTQ6rszmoiFlduviuRkb6oZwMbwGFsturwiekULxom+cgIINpnMumLf505us8+94vHzo6dY+iCye+MG6uCtKU6u4OleAgJYXuw6h0+jIomQss+7H5A06KKwWuvlXnjxgjJfAzVdp3quI2RHUfjqPaCj8htGDM= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.10) by DB6PR0802MB2549.eurprd08.prod.outlook.com (10.172.251.147) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.587.9; Thu, 25 Aug 2016 13:45:33 +0000 From: Pavel Butsykin To: , Date: Thu, 25 Aug 2016 16:44:06 +0300 Message-ID: <20160825134421.20231-8-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.8.3 In-Reply-To: <20160825134421.20231-1-pbutsykin@virtuozzo.com> References: <20160825134421.20231-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.10] X-ClientProxiedBy: AMSPR02CA0013.eurprd02.prod.outlook.com (10.242.225.141) To DB6PR0802MB2549.eurprd08.prod.outlook.com (10.172.251.147) X-MS-Office365-Filtering-Correlation-Id: b21d799f-c79c-45a0-de68-08d3ccee165f X-Microsoft-Exchange-Diagnostics: 1; DB6PR0802MB2549; 2:aB2bDGpR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6z+kV4vdgI8IBniqMs970oCD0inHD4Jmj+9CzJGtZeeTxBaX0ll0dNRJhZlJXg==; 31:Wec36j/qgL0yrkV9+zFZU9+92vS9UlcqJoTS6qXbLRCWYhgnoE5y9agxG4gbGAPOuxzWxq1lX/jHEA7wrdrWDlljuhgy88DEshPEu8B3CesRtOa4lsc9713QNvih5GCJorkIz5/fEBBlhVdrGzTmVameEE/Q+XjVKTpT9V0u6IgQXUpumLSHEojTfiPlS1OlZm+d66acnxdvqU41QDV2NcV6g0/1D546ctUcIKtk+r8= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6043046)(6042046); SRVR:DB6PR0802MB2549; BCL:0; PCL:0; RULEID:; SRVR:DB6PR0802MB2549; X-Microsoft-Exchange-Diagnostics: 1; DB6PR0802MB2549; 4:uLz1bK6I91fr3AnSsF/Fx5+53Fkrdjl9FkuMD5ADIsGZTD8CNKSEFyCmiRkP2lBZ5w7eFBVwBdCjFKg35Ccj/tt7VD2bSRfsg3iRBsBJhd3prLe5SWMHOFkbwLraa5EjusCIt8FsfkhtH9S5zkdeA9DDJOarEEyRWwiR7CXyKN+/4iCeycvT8F44+XyrPKp/IuTV51v/iPehzdaVb6Jy/SriNA0MMJ4GmS+Ged1pBwlnMCEuXhEgp3Z2u5uFKQZ2HWEbVR/R/CnkOenNwLkHXxyUTXd/erIRS9yi2TJOvQ5RIeCUJOcdDCbh0itJxSlyJmbEV62lEUta7tFHNKrClIYAtXi21OsmoasPuAnXiwtcIweds+iiGhEkyCL3NTIKSrGfObOKfR4s+OwPtjKSx3azMXG9n++ZXJVUdFvMKjA= X-Forefront-PRVS: 0045236D47 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(7916002)(189002)(199003)(86362001)(33646002)(1076002)(69596002)(5003940100001)(42186005)(68736007)(189998001)(92566002)(66066001)(106356001)(47776003)(6116002)(3846002)(586003)(36756003)(5001770100001)(97736004)(2906002)(4326007)(2950100001)(5660300001)(76176999)(48376002)(53416004)(105586002)(50466002)(81156014)(8676002)(50226002)(77096005)(81166006)(19580405001)(50986999)(229853001)(101416001)(7846002)(7736002)(305945005)(19580395003)(217873001); DIR:OUT; SFP:1102; SCL:1; SRVR:DB6PR0802MB2549; H:pavelb-Z68P-DS3.sw.ru; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A: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; DB6PR0802MB2549; 23:W4/YgwW5XDX0lYFQr14UkjI+GXkWBiD4QzHg8Ge?= =?us-ascii?Q?cf3Mr2kb+DPEq3kV5nC8YRHODhaF1XcdO+haW27nb28nbg15hizP7QZVSgKA?= =?us-ascii?Q?UBlKoRmImFz99E3TJYcsLBYZQfAorN8Ea7O/vTy4OPA3OyqO41EZWcxj1AEb?= =?us-ascii?Q?0+iEyZDrZUadgddNMgry7Fph9sTkGWflgjraudXs+n5hboHTLxP+z+ULghSG?= =?us-ascii?Q?utlgljMJJslSPH09F1XMsXYmbJKR+7GoLwKFCrZEOP/H3GJKwpyz2gV8nVCX?= =?us-ascii?Q?YaF077jVeJnJE1mLTf1LN/s0TNUovx7q9BkDRy7KJ7R24+pi4hecWfvY/VC0?= =?us-ascii?Q?bCyJNrbIrerUWTctKFzYF/AwbfRBcFjRwdukkLo0XLdEsHKTO2YmYAqZzVOu?= =?us-ascii?Q?Y+ttuBzVNnLo2nKcodmNHcscrahsERhlyEKeFEMm5ePtLJD5Zrjz9ET41v6i?= =?us-ascii?Q?AG4M5S2nvRkC1EPmLIo46EiZaHtVzKjm61CG7ITj5kdnruDr7P2SX0gcqjhc?= =?us-ascii?Q?KGi6bgBPODmzVIYAJqii22C0E1qbDKUN0rWpBH7SXquEhFJyXL/z3dPh9Yvw?= =?us-ascii?Q?1b8X2gzOo90yCd5eeNSabbECJZsDfShsTQOkjqd9rGS8jdpQrHKLqtC3XueT?= =?us-ascii?Q?nScsvH6fn78C4gHAytPqtcQaEc6HHxeEqADUNDBlzeu4iGTMP6H20Vui0Xgz?= =?us-ascii?Q?hyEHyZdPif8LsVi3XflYsmxbmeUepEGK7t6IFXW80f3jHDGbHLXTurPmf5Ob?= =?us-ascii?Q?nKoll9Kw/FhBHX9IITYCoPUvKqODffYR8aFl2/9W1Amba225VBtqrDmcAa7b?= =?us-ascii?Q?0/4dJQNHckPw2f68zrq1OS6kKdRkPrl54r57c2vAF8X9indEXqrVu/ZmnwpE?= =?us-ascii?Q?ZzdAkdBx+xU6VfdJgZAlBUalZVwCSQLBlyXynpYnkWamRwE9s1ynWBwSCwZ0?= =?us-ascii?Q?EtUoUZU7nY8RUhxBTFGTBTHYZQejI2xxUQbWclL01FfCyAgGFBCSr14aH8Fk?= =?us-ascii?Q?ohz6dMLZK640AwNpbmHxwvsC7tq+JYSLFdXkioMFds1OWmL5NDr5KgOT+Oz0?= =?us-ascii?Q?7jb0SvIOaSz/8ikdoqxQW15i9ylgheR17tWN6uMQtiyWy0gt1SWvfss2MQVN?= =?us-ascii?Q?YTv7jqVbfwFi524YRyOkF2Ns5NyjgzXGc7znMlyICEvcyS6SThXYQiw=3D?= =?us-ascii?Q?=3D?= X-Microsoft-Exchange-Diagnostics: 1; DB6PR0802MB2549; 6:qlodyub60Y8tvdJfUQF3DSlqYC/y687sFPCSFrTwlruJYMZUV5XkSIGhJHGSjOP/pMYsffgH7VC+Bn2hlOoHa9CDUa+eU5hm35wTrVyABV74Z6nRXrJZFyAOmmZYUCHoxzC9w0LQs7RcGrG2I8FzIVzfyoG7iMRX1Pb6sMrPa2qMFUB6uRVS5OfaA1zr04hjIzasLbHDFuTOwQoizjBeVKDV4P4FouEvF4kRRTKzHz5ExoSvif8XARA/YVxwgFW+TRE7VP7d1F391oper7TtKoI9tFrwgAm9DetwdpoziuoGtThszwqtmTXXpexBMuUe; 5:cXxHVvgyKuc4gKI1oPapCDs8xwEC7bDPCLi0U03rkPSnrudZTBsIsJEG8vYzX4Y0kSYN7L6AqZ4uMbhYJV07whd5pwwf29VSwIoSTG294Jid+V4Cg+Y1zmkVhWdPtDy8sQK9FU8juSKgSVdcdtUGlw==; 24:rLbCnQb1Zs9tV1k8QDyM5rLOrBHu86lGbvZmN/U8V4dndOw30IPYSuvnjfhmrWtxs8Fdcu3ArR8L4f8TLmdEIYw3PqXZKF3FII8p5AGyUoo=; 7:qa7L10u6Swfz6yT8zyLZ5+9qc1oteDNOwBfP6fFd0J7VpIX/QI9QjBPi5KqjZyG/SReAlnk4yWeW3IyGSIm8EBZlXOv5dWaQvgirrt2MXCJp3CJO0cHBMhXswgSILh4M9vz2//lJFqxhDkeH+K4lKh5+rOtSYuTiAcdVe9T8h4btrgEdaZ/dQVhh4XOcuZaQUUaA8V4emQqTv3o0/+Vebm2rkXJpO9uAxO7v2BfXRmQ2tXLiJRj4Uvh17x4k5hHp SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DB6PR0802MB2549; 20:bB3r5CoxCKM/PNXN2QmoaGC1cNUnxurlqhBG+saaVvulIVmniUfE9ClnZdlhUkh6U11wkK1yodWaGelfM6KDmY0uwZ8f8pR2nlimM+M4mYCEjpKl5sT41CX5BDue1Cjc1OwiE19UdLvDDaQtYiaC36BiHf6F9E2DhsRKy7BWhpo= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Aug 2016 13:45:33.2120 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0802MB2549 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.139 Subject: [Qemu-devel] [PATCH RFC 07/22] block/pcache: introduce LRU as method of memory 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, den@openvz.org, jsnow@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This is a simple solution to the problem of displacement of cache memory. The LRU can be useful to avoid the displacement of the nodes, which have been partially read. Signed-off-by: Pavel Butsykin --- block/pcache.c | 74 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/block/pcache.c b/block/pcache.c index 54d4526..7504db8 100644 --- a/block/pcache.c +++ b/block/pcache.c @@ -67,6 +67,11 @@ typedef struct ReqStor { CoMutex lock; } tree; + struct { + QTAILQ_HEAD(lru_head, BlockNode) list; + CoMutex lock; + } lru; + uint32_t curr_size; } ReqStor; @@ -75,12 +80,11 @@ typedef struct BDRVPCacheState { ReqStor pcache; - struct { - QTAILQ_HEAD(pcache_head, BlockNode) head; - CoMutex lock; - } list; - uint32_t cfg_cache_size; + +#ifdef PCACHE_DEBUG + uint64_t shrink_cnt_node; +#endif } BDRVPCacheState; typedef struct PrefCacheAIOCB { @@ -182,6 +186,44 @@ static inline void *pcache_node_alloc(RbNodeKey* key) return node; } +static void pcache_node_drop(BDRVPCacheState *s, PCNode *node) +{ + atomic_sub(&s->pcache.curr_size, node->cm.nb_sectors); + + qemu_co_mutex_lock(&s->pcache.lru.lock); + QTAILQ_REMOVE(&s->pcache.lru.list, &node->cm, entry); + qemu_co_mutex_unlock(&s->pcache.lru.lock); + + qemu_co_mutex_lock(&s->pcache.tree.lock); + rb_erase(&node->cm.rb_node, &s->pcache.tree.root); + qemu_co_mutex_unlock(&s->pcache.tree.lock); + + pcache_node_free(node); +} + +static void pcache_try_shrink(BDRVPCacheState *s) +{ + while (s->pcache.curr_size > s->cfg_cache_size) { + qemu_co_mutex_lock(&s->pcache.lru.lock); + assert(!QTAILQ_EMPTY(&s->pcache.lru.list)); + PCNode *rmv_node = PCNODE(QTAILQ_LAST(&s->pcache.lru.list, lru_head)); + qemu_co_mutex_unlock(&s->pcache.lru.lock); + + pcache_node_drop(s, rmv_node); +#ifdef PCACHE_DEBUG + atomic_inc(&s->shrink_cnt_node); +#endif + } +} + +static inline void pcache_lru_node_up(BDRVPCacheState *s, PCNode *node) +{ + qemu_co_mutex_lock(&s->pcache.lru.lock); + QTAILQ_REMOVE(&s->pcache.lru.list, &node->cm, entry); + QTAILQ_INSERT_HEAD(&s->pcache.lru.list, &node->cm, entry); + qemu_co_mutex_unlock(&s->pcache.lru.lock); +} + static bool pcache_node_find_and_create(PrefCacheAIOCB *acb, RbNodeKey *key, PCNode **out_node) { @@ -194,14 +236,17 @@ static bool pcache_node_find_and_create(PrefCacheAIOCB *acb, RbNodeKey *key, qemu_co_mutex_unlock(&s->pcache.tree.lock); if (found != new_node) { pcache_node_free(new_node); + pcache_lru_node_up(s, found); *out_node = found; return false; } atomic_add(&s->pcache.curr_size, new_node->cm.nb_sectors); - qemu_co_mutex_lock(&s->list.lock); - QTAILQ_INSERT_HEAD(&s->list.head, &new_node->cm, entry); - qemu_co_mutex_unlock(&s->list.lock); + qemu_co_mutex_lock(&s->pcache.lru.lock); + QTAILQ_INSERT_HEAD(&s->pcache.lru.list, &new_node->cm, entry); + qemu_co_mutex_unlock(&s->pcache.lru.lock); + + pcache_try_shrink(s); *out_node = new_node; return true; @@ -275,10 +320,7 @@ static BlockAIOCB *pcache_aio_readv(BlockDriverState *bs, { PrefCacheAIOCB *acb = pcache_aio_get(bs, sector_num, qiov, nb_sectors, cb, opaque, QEMU_AIO_READ); - - if (acb->s->pcache.curr_size < acb->s->cfg_cache_size) { - pcache_prefetch(acb); - } + pcache_prefetch(acb); bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, pcache_aio_cb, acb); @@ -309,8 +351,8 @@ static void pcache_state_init(QemuOpts *opts, BDRVPCacheState *s) s->pcache.tree.root = RB_ROOT; qemu_co_mutex_init(&s->pcache.tree.lock); - QTAILQ_INIT(&s->list.head); - qemu_co_mutex_init(&s->list.lock); + QTAILQ_INIT(&s->pcache.lru.list); + qemu_co_mutex_init(&s->pcache.lru.lock); s->pcache.curr_size = 0; s->cfg_cache_size = cache_size >> BDRV_SECTOR_BITS; @@ -350,8 +392,8 @@ static void pcache_close(BlockDriverState *bs) uint32_t cnt = 0; BDRVPCacheState *s = bs->opaque; BlockNode *node, *next; - QTAILQ_FOREACH_SAFE(node, &s->list.head, entry, next) { - QTAILQ_REMOVE(&s->list.head, node, entry); + QTAILQ_FOREACH_SAFE(node, &s->pcache.lru.list, entry, next) { + QTAILQ_REMOVE(&s->pcache.lru.list, node, entry); pcache_node_free(PCNODE(node)); cnt++; }