From patchwork Tue Nov 15 06:37:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9429083 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 60E4360469 for ; Tue, 15 Nov 2016 07:12:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F434289F4 for ; Tue, 15 Nov 2016 07:12:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 33C6628A98; Tue, 15 Nov 2016 07:12:53 +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 4C47E289F4 for ; Tue, 15 Nov 2016 07:12:52 +0000 (UTC) Received: from localhost ([::1]:44614 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6XvH-0000NH-Ir for patchwork-qemu-devel@patchwork.kernel.org; Tue, 15 Nov 2016 02:12:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47503) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6Xuo-0000K4-Jh for qemu-devel@nongnu.org; Tue, 15 Nov 2016 02:12:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c6Xul-0000mg-BJ for qemu-devel@nongnu.org; Tue, 15 Nov 2016 02:12:22 -0500 Received: from mail-ve1eur01on0092.outbound.protection.outlook.com ([104.47.1.92]:25052 helo=EUR01-VE1-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 1c6Xuk-0000mC-R8; Tue, 15 Nov 2016 02:12:19 -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=HdNSvtbVh81sxahz1AO21HpWF0QLGUhCD1uXaVsIVi0=; b=HOqSKZygLEM74LgWm/Esl1V5DRk1f7UCgAmiacpRbV35aCipVSchGf+aOI1rSL+X9+o55mD/dwfvL4U8tvcgoxoU7r7tDjzE3SdnAaA3QKdVvrS1CY3AWyLnWg0+H6KjbBML5ZiXcDMw7iOdZcXC1felUdBPtUKezBSwLHmeLgc= 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:15 +0000 From: Pavel Butsykin To: , Date: Tue, 15 Nov 2016 09:37:01 +0300 Message-ID: <20161115063715.12561-5-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:ZytWC20she/of17ZTpjQO9jzyT2tazVAPrVFcjuQ/nt28WRJg6UFiBBvNVk1O9ZQN1KgY+onaiUvV5lpuNol5EZotr4z89vkvRC+MyXo9WAmMeXoUkzuTdiRcwRCi3W9v28xOUertdwSJtDdKL5j7HsnRMv7EJYMpyPXJJpICW4=; 3:EMg7hYRDxtJla9HaiRFeumJzUzFl/Qq1/aXFhG93tYnlTrCTtmQ7vyNlFU00Umuggt3qskVatNAsgkHKWcgMh3tt0DaTLSW8rnsFpm63Of9aTQKGRMlwmm2Ot/Em6cm70osZFGkM0Q3BiYOy05IerxNbq1cWKwR3GkXDL5Otqz8=; 25:2odakAYBqphTN9XwXYT8PuQCUVKJOpW4Ftyhwx+tKMXxUix3BZ7foIUUbMwPzx4V3LRg1UsD0DkqUjvng7aoGDBsSO61KUTVrbk25ATmTr4w+AsoxYm9FGDS2bUy09QzQxxhoMYX43aO/LkWfG/g6uXqh9r6zNk4reG9xjfNS5mfHJxuJ+XpEmZ4KB3qMg4ohV45z8Onmiijb1EexaEtHhbH3zB2LYZlJHiZELWsUCiYb3bCKt5u5ORsb76zatxMu2CGbyBhmzCXUqH3vGN39ZygDDsY5flascsUbrQQXSUahigTXVnDaS2qesxpqhHdvKa5YTJstNz7xuLQo2wa2geNnkaP2V6mpyVhbjD1pkJAmOmR+mmNwaYkKZZayjF273ONu1OdClBYOMKq7mhS+WEx/KFxZ4RWjw5wP0VKz0VqfxMnqqgqAhE0ozREASX1 X-MS-Office365-Filtering-Correlation-Id: 96c40a25-5b9f-4e98-3679-08d40d21fad5 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:VI1PR0802MB2558; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 31:QiuhUKCDkTcYjsXq3MH9QE9WaF4Lzvcu6EjjbySrvSi9N7HTxHeptkuc/f9pI0eYFTE1GHIxZXiiO2BpuZ1xbyHHmW1Y7N6FDZJAAgAbF7W8siqGfforPdjFGKHQToXf1b+DBbag8WNEfpzl3wuILgOlJe9cZ6r8sbzREMp62m+oEowUjiLWfnIFtUP6qTrRQvrkDE8lViCbrErBt4dzz3AJMQAvSxQ+z+e+Pq8YDQ/pTmmkJMJR4OIbzV8GS8cB; 20:+MJEpDGgowCQ9SILMVYkyzUFHnc71o8TUGrSAs+KSFnLWtIZY4w9ErfWWX51Wa0PVkod0d95/s5qtvrlGZpWYiBTpDbNYXuqdFSdXuUu1JnUAD2LmrqbHwboraidXWcl+pYqVW3w5qTL1Fo4p/lWZAGelDZE1eIb+6owurOIpt4=; 4:3oL3Vq0O/V6EjrCFqtknhEJ4ZyK0EAG4Xlewe/I40BsQAaIe1Y/E5cmB4Two2tukz9A47iSRlVycu9Sl5NDUvDg0PuEnr6OcYAeI1hWBiyyeYgEvo0WhbHDunP++Ml6Je2D7p/H+vXuajuqgGNu54pYcRxtQPV05EbzFNDb/oqdj3YGdrwTO0UTlVcapiMoLDsCSnhOcJ4+WkH2O+dAUFvrBVKddfmisuQH2/0YYY8F5KH3HfhNTG/5yk6n4Z9EntsSL38nSz4DuTTdDOl0niEaulzoBa1QWcQ2hJBLMduhJZmlKsVYxvP8qZ5VjuLzJ4Q8itI7EV0W8KzFmfbG9h/gni0XDE3q/nOVrE9vQfusoRS//2+qslR65VepMQRrWZeDwBZwXSBM/6Au+42IEKx6ivbFLOcO3SXj+7kYuWvAZrNMGtik+2saCSdLaAIXH2M6F2FcigUCnMnATPpKsf7poWfOFPcTEQK80+FFyPtQ= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(17755550239193); 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)(979002)(6009001)(7916002)(199003)(189002)(47776003)(105586002)(101416001)(1076002)(68736007)(106356001)(6666003)(575784001)(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)(969003)(989001)(999001)(1009001)(1019001); 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:mccgA5OFuQo/2ndkgWa4Efp5VA3fveEBTfp8f5C?= =?us-ascii?Q?/bhm1aWEoafC/jJXFLQsv15bjCOuEATUoqsmIVyovd4MwHUxL+4UkYzwanAn?= =?us-ascii?Q?4sflbQbIEpbTE/nzslQeOqFLojVWbQFLeamXYH0yjadEaTXZbV6/WRBcQV7g?= =?us-ascii?Q?BHN118vNszmoJu0e7soDKV5r64vbIce2KyhI2VgNn5/UOvgzVZpFbEoTg079?= =?us-ascii?Q?yO0+R0N9rcKxwg8+qRuxshGpijhppo09+CBcsoOZgqPIVUTXXhvxUCVMb093?= =?us-ascii?Q?v+0MFAHDaPYp0vmKvP+P0w98RlZ9vJBWKBf2w0e6e20QRsp1oJMWORXvtkZh?= =?us-ascii?Q?yn8w1p6g8S6FSkbjsc48+WZlyXlbp5PzuM7kDQUY2FtOPjVy9p2kqsYnYdz7?= =?us-ascii?Q?PpmQXaD+IBWFiTeP9skODy1L8rOtVXDpRzPtt64GGc720bbKzF6So7Bn4bxq?= =?us-ascii?Q?k0GfH/k7ZpRb0bw+rftQPmfud81EXJjivgZ4C0J0j7BI/GeMQka1E+dQv3bc?= =?us-ascii?Q?P8PJGa2kjrBFuvYDwVM00Vw3iGoHEgRxaywnNb2G1K+0CRyun0LBsbnnlJJo?= =?us-ascii?Q?vcorqnJ5UlR1Rmli1B8Scf2NMKe/4XEPK365HbLE7/kjWP6A6dYRht4/quq6?= =?us-ascii?Q?NnBfTqDq9e9POd58rI8yfelp6UTy2mvqPuCuvAl6HLZ+syeETEWPjyAM3zcc?= =?us-ascii?Q?xhewZWj0vrMFycSPOo2p53BK8HRnfRUzWJKk6wmZQztOBgAdotTiVLoezwUt?= =?us-ascii?Q?m1m33Xx/VfJwwTFmc98ZBzTDRLgKaTeY2QI7EQF2Zj7JvsPPfAvAujmmpeiW?= =?us-ascii?Q?y4Om2cI0oHfQDiEVFqOpgBHSvoipCmbiSJKXaU1UsP9fs91Sxdz0tnU+IXOG?= =?us-ascii?Q?kpp8+asbfJRqTzZ3LVLRdHDTTjVC2CRX+IXEk5O44SxFr88/4h5+ej696bHe?= =?us-ascii?Q?ul+bSf1oRt5ttR2t/o8yK4QyWvpP59zuazvbgJup/vcAfQ52dROVitcn4RO1?= =?us-ascii?Q?uJ3ukWz31wg8MNO+XqXkaETiJcq3Jb/oqnMh7foCMRmsN2wRBpWhjDZUCko0?= =?us-ascii?Q?yNLGYkuDKUDYbfmF/iVMf0efYNyJvWOo6dhcMmts0wniJKN0/1WIXNARToPn?= =?us-ascii?Q?dccRG7FKLpHmyIN8QQB5AW4D71dWSorq/yjQIc3kA0tvsiEIjSLw8u+fhg1+?= =?us-ascii?Q?dGWKm40y4SUHdPVfM6rxD5TnLOPKy8tEwJILC?= X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 6:5QIjlXleQB3XM9yZ9nTq2J5TfrEmQnEbVmEhqfLHkh454IhWCzliVn4YVlbxvDvVUo3q2btDhVMlGhoUq4Im5msRh799ZnBZ8mBVp+AvW2gN4hh8aK23jtxIzlC+mNHeAmTs6bFcct1u2szE507/gcHkhntc2S0KsApROEKSBTBe3rPNNCAcbvFxZzNzqOGD4YjkCRO5dmipNug3MUIehkk23yOm146+XYT6+53Wr8G7cnUCOfMjmBh87sRxjzk5wBRUpY2G5WeLRpuojLg0cRQdF6c+13h9WhEa9OeNSTutlBiODzd5Dz9wu4hmps+yzx/LgNLhLkffxdNvj/gXw4v6hU2YhF25MphQGdbCWxzvZVsETwYhsgw2UfxCQG47; 5:x6dHvdzcbvGmmWmqE4X+pizeZyNwuN23ISxM/Z/BFNKHj+BmM0DT6rmsqVVHgs1jFzlI96z7KsubWzf9HBNFhE4m9WA4xG6yXY4HSk91uitS6t8JEEk1n7Hvd+pMvMeyK4WRNIQLq3eHdPuYkHbKxwWGGI7M4KfnIT1ckwFyePw=; 24:BmIGW+ID2gABB23J2Cq4U3GFKHQYrt/+K1HG7qDGPVe1uDV77i9T7CUkwolMK6gksjU/2/1+WfdApuFNaWhtrrt9erCAAhwvzUPbuL+YNvU= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 7:mOqkvG/JhTi6pTZajRyof2xSR4X110lmuyeEQ+J+uitog4kNdlgB1NJGC1ZKMWi15R01Bc5jCLS7nTNAILO4h4VLx+jqobhof7MdylI0x0OuaNqph9vJUTk5OEdWLsAm8kEfj915dkTWIT1wYqSxrMp9fhHt5wzu0R2IGwAL1+827FzmHirddocCH8dW1H2vvEHtPFrQ0UY7NFGCQqW6y7cduCQ+LqW86k2YYUTHlNYc75TJXNjlv8N/cb8YmjnYSdnhmjyQw5eOkQZTHF/PL+F8vZL/epZowMg2W6ZMcZnMroxCH9DEcw+OTlXr3gpPYhr5tUMnpxOPXbE6X8XmrVdlxJw7L9uc27o8ajinCWE=; 20:2G7agIUxsSb3gXQFT3IZeIfTGjin1osPSD1qxb/ICzRZa3WDJyJaYiDzNUv0YaFUqc/uFFiqE2Je07WUrJMnKIzJBOmrsGmfyqJjLI1C0JuY8ZtuWWoC+apZWrVC8endXQb4Xf8doZ5sDByyRk3EZVuTdP87FqR0bZ/mk+5Y7gY= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2016 06:38:15.2155 (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.1.92 Subject: [Qemu-devel] [PATCH v1 04/18] util/rbcache: range-based cache core 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 RBCache provides functionality to cache the data from block devices (basically). The range here is used as the main key for searching and storing data. The cache is based on red-black trees, so basic operations search, insert, delete are performed for O(log n). It is important to note that QEMU usually does not require a data cache, but in reality, there are already some cases where a cache of small amounts can increase performance, so as the data structure was selected red-black trees, this is a fairly simple data structure and show high efficiency on a small number of elements. Therefore, when the minimum range is 512 bytes, the recommended size of the cache memory no more than 8-16mb. Also note that this cache implementation allows to store ranges of different lengths without alignment. Generic cache core can easily be used to implement different caching policies at the block level, such as read-ahed. Also it can be used in some special cases, for example for caching data in qcow2 when sequential allocating writes to image with backing file. Signed-off-by: Pavel Butsykin --- MAINTAINERS | 6 ++ include/qemu/rbcache.h | 105 +++++++++++++++++++++ util/Makefile.objs | 1 + util/rbcache.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 358 insertions(+) create mode 100644 include/qemu/rbcache.h create mode 100644 util/rbcache.c diff --git a/MAINTAINERS b/MAINTAINERS index ddf797b..cb74802 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1365,6 +1365,12 @@ F: include/qemu/rbtree.h F: include/qemu/rbtree_augmented.h F: util/rbtree.c +Range-Based Cache +M: Denis V. Lunev +S: Supported +F: include/qemu/rbcache.h +F: util/rbcache.c + UUID M: Fam Zheng S: Supported diff --git a/include/qemu/rbcache.h b/include/qemu/rbcache.h new file mode 100644 index 0000000..c8f0a9f --- /dev/null +++ b/include/qemu/rbcache.h @@ -0,0 +1,105 @@ +/* + * QEMU Range-Based Cache core + * + * Copyright (C) 2015-2016 Parallels IP Holdings GmbH. All rights reserved. + * + * Author: Pavel Butsykin + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#ifndef RBCACHE_H +#define RBCACHE_H + +#include "qemu/rbtree.h" +#include "qemu/queue.h" + +typedef struct RBCacheNode { + struct RbNode rb_node; + uint64_t offset; + uint64_t bytes; + QTAILQ_ENTRY(RBCacheNode) entry; +} RBCacheNode; + +typedef struct RBCache RBCache; + +typedef RBCacheNode *RBNodeAlloc(uint64_t offset, uint64_t bytes, void *opaque); +typedef void RBNodeFree(RBCacheNode *node, void *opaque); + + +enum eviction_type { + RBCACHE_FIFO, + RBCACHE_LRU, +}; + +/** + * rbcache_search: + * @rbcache: the cache object. + * @offset: the start of the range. + * @bytes: the size of the range. + * + * Returns the node corresponding to the range(offset, bytes), + * or NULL if the node was not found. + */ +void *rbcache_search(RBCache *rbcache, uint64_t offset, uint64_t bytes); + +/** + * rbcache_insert: + * @rbcache: the cache object. + * @node: a new node for the cache. + * + * Returns the new node, or old node if the node already exists. + */ +void *rbcache_insert(RBCache *rbcache, RBCacheNode *node); + +/** + * rbcache_search_and_insert: + * @rbcache: the cache object. + * @offset: the start of the range. + * @bytes: the size of the range. + * + * rbcache_search_and_insert() is like rbcache_insert(), except that a new node + * is allocated inside the function. Returns the new node, or old node if the + * node already exists. + */ +void *rbcache_search_and_insert(RBCache *rbcache, uint64_t offset, + uint64_t byte); + +/** + * rbcache_remove: + * @rbcache: the cache object. + * @node: the node to remove. + * + * Removes the cached range owned by the node. + */ +void rbcache_remove(RBCache *rbcache, RBCacheNode *node); + +RBCacheNode *rbcache_node_alloc(RBCache *rbcache, uint64_t offset, + uint64_t bytes); + +void rbcache_node_free(RBCache *rbcache, RBCacheNode *node); + +/** + * rbcache_create: + * @alloc: callback to allocation node, allows to upgrade allocate and expand + * the capabilities of the node. + * @free: callback to release node, must be used together with alloc callback. + * @limit_size: maximum cache size in bytes. + * @eviction_type: method of memory limitation + * @opaque: the opaque pointer to pass to the callback. + * + * Returns the cache object. + */ +RBCache *rbcache_create(RBNodeAlloc *alloc, RBNodeFree *free, + uint64_t limit_size, int eviction_type, void *opaque); + +/** + * rbcache_destroy: + * @rbcache: the cache object. + * + * Cleanup the cache object created with rbcache_create(). + */ +void rbcache_destroy(RBCache *rbcache); + +#endif /* RBCACHE_H */ diff --git a/util/Makefile.objs b/util/Makefile.objs index 727a567..9fb0de6 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -38,3 +38,4 @@ util-obj-y += qdist.o util-obj-y += qht.o util-obj-y += range.o util-obj-y += rbtree.o +util-obj-y += rbcache.o diff --git a/util/rbcache.c b/util/rbcache.c new file mode 100644 index 0000000..05cfa5a --- /dev/null +++ b/util/rbcache.c @@ -0,0 +1,246 @@ +/* + * QEMU Range-Based Cache core + * + * Copyright (C) 2015-2016 Parallels IP Holdings GmbH. All rights reserved. + * + * Author: Pavel Butsykin + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * later. See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qemu/rbcache.h" + +/* RBCache provides functionality to cache the data from block devices + * (basically). The range here is used as the main key for searching and storing + * data. The cache is based on red-black trees, so basic operations search, + * insert, delete are performed for O(log n). + * + * It is important to note that QEMU usually does not require a data cache, but + * in reality, there are already some cases where a cache of small amounts can + * increase performance, so as the data structure was selected red-black trees, + * this is a quite simple data structure and show high efficiency on a small + * number of elements. Therefore, when the minimum range is 512 bytes, the + * recommended size of the cache memory no more than 8-16mb. Also note that this + * cache implementation allows to store ranges of different lengths without + * alignment. + */ + +struct RBCache { + struct RbRoot root; + RBNodeAlloc *alloc; + RBNodeFree *free; + uint64_t limit_size; + uint64_t cur_size; + int eviction_type; + void *opaque; + + QTAILQ_HEAD(RBCacheNodeHead, RBCacheNode) queue; +}; + +static int node_key_cmp(const RBCacheNode *node1, const RBCacheNode *node2) +{ + assert(node1 != NULL); + assert(node2 != NULL); + + if (node1->offset >= node2->offset + node2->bytes) { + return 1; + } + if (node1->offset + node1->bytes <= node2->offset) { + return -1; + } + + return 0; +} + +static RBCacheNode *node_previous(RBCacheNode* node, uint64_t target_offset) +{ + while (node) { + struct RbNode *prev_rb_node = rb_prev(&node->rb_node); + RBCacheNode *prev_node; + if (prev_rb_node == NULL) { + break; + } + prev_node = container_of(prev_rb_node, RBCacheNode, rb_node); + if (prev_node->offset + prev_node->bytes <= target_offset) { + break; + } + node = prev_node; + } + + assert(node != NULL); + + return node; +} + +RBCacheNode *rbcache_node_alloc(RBCache *rbcache, uint64_t offset, + uint64_t bytes) +{ + RBCacheNode *node; + + if (rbcache->alloc) { + node = rbcache->alloc(offset, bytes, rbcache->opaque); + } else { + node = g_malloc(sizeof(*node)); + } + + node->offset = offset; + node->bytes = bytes; + + return node; +} + +void rbcache_node_free(RBCache *rbcache, RBCacheNode *node) +{ + if (rbcache->free) { + rbcache->free(node, rbcache->opaque); + } else { + g_free(node); + } +} + +static void rbcache_try_shrink(RBCache *rbcache) +{ + while (rbcache->cur_size > rbcache->limit_size) { + RBCacheNode *node; + assert(!QTAILQ_EMPTY(&rbcache->queue)); + + node = QTAILQ_LAST(&rbcache->queue, RBCacheNodeHead); + + rbcache_remove(rbcache, node); + } +} + +static inline void node_move_in_queue(RBCache *rbcache, RBCacheNode *node) +{ + if (rbcache->eviction_type == RBCACHE_LRU) { + QTAILQ_REMOVE(&rbcache->queue, node, entry); + QTAILQ_INSERT_HEAD(&rbcache->queue, node, entry); + } +} + +static RBCacheNode *node_insert(RBCache *rbcache, RBCacheNode *node, bool alloc) +{ + struct RbNode **new, *parent = NULL; + + assert(rbcache != NULL); + assert(node->bytes != 0); + + /* Figure out where to put new node */ + new = &(rbcache->root.rb_node); + while (*new) { + RBCacheNode *this = container_of(*new, RBCacheNode, rb_node); + int result = node_key_cmp(node, this); + if (result == 0) { + this = node_previous(this, node->offset); + node_move_in_queue(rbcache, this); + return this; + } + parent = *new; + new = result < 0 ? &((*new)->rb_left) : &((*new)->rb_right); + } + + if (alloc) { + node = rbcache_node_alloc(rbcache, node->offset, node->bytes); + } + /* Add new node and rebalance tree. */ + rb_link_node(&node->rb_node, parent, new); + rb_insert_color(&node->rb_node, &rbcache->root); + + rbcache->cur_size += node->bytes; + + rbcache_try_shrink(rbcache); + + QTAILQ_INSERT_HEAD(&rbcache->queue, node, entry); + + return node; +} + +void *rbcache_search(RBCache *rbcache, uint64_t offset, uint64_t bytes) +{ + struct RbNode *rb_node; + RBCacheNode node = { + .offset = offset, + .bytes = bytes, + }; + + assert(rbcache != NULL); + + rb_node = rbcache->root.rb_node; + while (rb_node) { + RBCacheNode *this = container_of(rb_node, RBCacheNode, rb_node); + int32_t result = node_key_cmp(&node, this); + if (result == 0) { + this = node_previous(this, offset); + node_move_in_queue(rbcache, this); + return this; + } + rb_node = result < 0 ? rb_node->rb_left : rb_node->rb_right; + } + return NULL; +} + +void *rbcache_insert(RBCache *rbcache, RBCacheNode *node) +{ + return node_insert(rbcache, node, false); +} + +void *rbcache_search_and_insert(RBCache *rbcache, uint64_t offset, + uint64_t bytes) +{ + RBCacheNode node = { + .offset = offset, + .bytes = bytes, + }; + + return node_insert(rbcache, &node, true); +} + +void rbcache_remove(RBCache *rbcache, RBCacheNode *node) +{ + assert(rbcache->cur_size >= node->bytes); + + rbcache->cur_size -= node->bytes; + rb_erase(&node->rb_node, &rbcache->root); + + QTAILQ_REMOVE(&rbcache->queue, node, entry); + + rbcache_node_free(rbcache, node); +} + +RBCache *rbcache_create(RBNodeAlloc *alloc, RBNodeFree *free, + uint64_t limit_size, int eviction_type, void *opaque) +{ + RBCache *rbcache = g_malloc(sizeof(*rbcache)); + + + /* We can't use only one callback, or both or neither */ + assert(!(!alloc ^ !free)); + + rbcache->root = RB_ROOT; + rbcache->alloc = alloc; + rbcache->free = free; + rbcache->opaque = opaque; + rbcache->limit_size = limit_size; + rbcache->cur_size = 0; + rbcache->eviction_type = eviction_type; + + QTAILQ_INIT(&rbcache->queue); + + return rbcache; +} + +void rbcache_destroy(RBCache *rbcache) +{ + RBCacheNode *node, *next; + + assert(rbcache != NULL); + + QTAILQ_FOREACH_SAFE(node, &rbcache->queue, entry, next) { + QTAILQ_REMOVE(&rbcache->queue, node, entry); + rbcache_node_free(rbcache, node); + } + + g_free(rbcache); +}