From patchwork Tue Nov 15 06:37:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9429275 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 04FFE6047D for ; Tue, 15 Nov 2016 10:12:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E2A1E286AB for ; Tue, 15 Nov 2016 10:12:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D73F2286EF; Tue, 15 Nov 2016 10:12:38 +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 9E270286B7 for ; Tue, 15 Nov 2016 10:12:37 +0000 (UTC) Received: from localhost ([::1]:45230 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6ajE-0004AC-UC for patchwork-qemu-devel@patchwork.kernel.org; Tue, 15 Nov 2016 05:12:36 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57845) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6ai5-0003WM-Ln for qemu-devel@nongnu.org; Tue, 15 Nov 2016 05:11:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c6ai2-0003c0-GN for qemu-devel@nongnu.org; Tue, 15 Nov 2016 05:11:25 -0500 Received: from mail-he1eur01on0101.outbound.protection.outlook.com ([104.47.0.101]:45385 helo=EUR01-HE1-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 1c6ai1-0003bc-MP; Tue, 15 Nov 2016 05:11:22 -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=O/UXDczX6vvBn5hoXO55v+Zp3FqgT8/5Cg5JchFDxso=; b=N/39fYkO6B8Kpt5pQ/JxTKBymrGrxWMVmwb3RewibUeVMPbUEykVUsAppq4lNBj6NfXwuYyz5edLkXMlCHVJr9qHuY7pZyRR02Vrovl3Y5+439IcSYne4KEBHraZ2/gVPqpz3qI83JcVh1obBfGXl9xfGwEWGQh9hJFItVaHGXY= 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:16 +0000 From: Pavel Butsykin To: , Date: Tue, 15 Nov 2016 09:37:02 +0300 Message-ID: <20161115063715.12561-6-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:/Dk7NfF7dvlDXCm6PXt/FhJypS13kVzPOlN6nxYaQK8KqSN+HwvU2P8dyjj1PoM47I1vCKjzsO+fiABtTjOdsxolEfG3TPccbqAUUDyx1+dBjPOAVdwG36tWo1Rs//uCz6JXENNfFk77UNJPeqIoEY9fMYwuTkQp4OYDDZMGLms=; 3:RdaS/nu1Uy8DspoNVBcG68kXyt1PfUZ3i1mGk8djQ3pc42QpgDapp7mUe0hW175NNdsH+RVki/nFXi6YQqH0q0cNfksPAekdR71PB3pSviDCeSShSP3hNxEj+QeCzfBA8pIssT2zAxTf+kBcUnbmkZZpLMnrxUYJ4bspNGoLHz0= X-MS-Office365-Filtering-Correlation-Id: b81f43ac-2f86-43a2-423b-08d40d21fb75 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:VI1PR0802MB2558; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2558; 25:CmX07x0wlGozSgQYJMIWxsUxsFOGuWvn4jTh+au78ixtyPaAGF5WlNHSzBMjPV3Nx3HyxHghOkuG6R6Aangf1hPJjDDcayxEVoTqAxQosRbOFAS05XT+r6njUGJ5+IL4ZSiA8BdktRYkEWYp2NFcJbsMBA8fPCoWk1f3du4gAMc4pHbI1enldCNijm/0OC3Y5GGSyjciZWI1J3iglzIIcWhKUng/mqR77JGlBl6xA0UY89sQUdwtereQnJSpILyqwJEKcNeIJsTV/cHNkcmGQMG6G8OVxuxKHhJUukVclgcOyVGQYjUdsG6XKviMOUcWbsDB0Ce9m/vK5v+Q/A/A04KwbgBYkUw7nUukZNIhiyuWMeHjuovU6LLpd0bXv+crdrolGM1yJYbcpmOU+Hu4DrOSB6VspPG4tn/zEnYuoAA8AWaTQnX6vQodecL35cHciTh8+Qqortc12OAtBbjQZ/JNZgdeZEuCZk1Vb9nOVdsDSo6oL2cbyIt2COq8k4bEMwKm5oVTO1sSdM61Fh4YCKqqRh9fPofeNFgTTDuW7cS2zZt2eWsr68zICCqYgUHMRlbRt35u82w+Dz2EQuflng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bk0bU3UCA9H9i+TXYRSSjN2Lfpz3dud0= 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)(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:jp/je1vSHsCHwKL9ylf+Tn0dT9MbaIgie7nOnTf?= =?us-ascii?Q?ln/42N8Gfc8MXOQUFP5mUutWSoMKc/QNayWD4ZiVlGIzI4EJvcXCqQ7FF7CS?= =?us-ascii?Q?6TRYzE/j7Zcxh6d899SUI6K4mFOgXbu6V/XsXxnvCbkfTSLiwVd79fOCAhoH?= =?us-ascii?Q?HvSGid1OavN8Fx1tTCRwvx90GdM6YQTccZaWTSXTTH6CBpWtFqvN4FoDF9ur?= =?us-ascii?Q?WIiZ5hz7vlYyJjWHiMK0mbwxMH+SRpV7rvrqb0nP254PxT6AqlzqKeoQzDHr?= =?us-ascii?Q?0Dc85sC5hUT7Xjn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jjskCGcJtSfm0I7Ui2ZaT4b/gT47PC9yJFD1uo9+oHR+z9UISsCDy+74I8/y7rN+ItBtYwOXWmimfalNY8/JHSCsv6WVgD3uG1ZZuFpGyAKuaXJ4CLruakmSXlUZ80g0/eIhHV6Som5/ZofTSctRvumUsOxJgo= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2016 06:38:16.3034 (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.0.101 Subject: [Qemu-devel] [PATCH v1 05/18] tests/test-rbcache: add test cases 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 Signed-off-by: Pavel Butsykin --- tests/Makefile.include | 3 + tests/test-rbcache.c | 308 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 311 insertions(+) create mode 100644 tests/test-rbcache.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 8162f6f..36bb472 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -54,6 +54,8 @@ check-unit-y += tests/test-hbitmap$(EXESUF) gcov-files-test-hbitmap-y = blockjob.c check-unit-y += tests/test-blockjob$(EXESUF) check-unit-y += tests/test-blockjob-txn$(EXESUF) +gcov-files-test-rbcache-y = util/rbcache.c +check-unit-y += tests/test-rbcache$(EXESUF) check-unit-y += tests/test-x86-cpuid$(EXESUF) # all code tested by test-x86-cpuid is inside topology.h gcov-files-test-x86-cpuid-y = @@ -481,6 +483,7 @@ tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y) tests/test-iov$(EXESUF): tests/test-iov.o $(test-util-obj-y) tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o $(test-util-obj-y) +tests/test-rbcache$(EXESUF): tests/test-rbcache.o $(test-util-obj-y) tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o tests/test-xbzrle$(EXESUF): tests/test-xbzrle.o migration/xbzrle.o page_cache.o $(test-util-obj-y) tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o diff --git a/tests/test-rbcache.c b/tests/test-rbcache.c new file mode 100644 index 0000000..1c72bfa --- /dev/null +++ b/tests/test-rbcache.c @@ -0,0 +1,308 @@ +/* + * 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" + +typedef struct TestRBCacheData { + RBCache *cache; +} TestRBCacheData; + +typedef struct TestRBCacheConfig { + uint64_t limit_size; + int eviction_type; + RBNodeAlloc *alloc; + RBNodeFree *free; + void *opaque; +} TestRBCacheConfig; + +#define KB(_n) ((_n) << 10) +#define MB(_n) ((_n) << 20) + +#define OFFSET1 0 +#define SIZE1 KB(1) + +#define OFFSET2 KB(1) +#define SIZE2 KB(2) + +#define OFFSET3 KB(15) +#define SIZE3 KB(1) + +#define OFFSET4 KB(7) +#define SIZE4 KB(7) + +#define OFFSET5 KB(2) +#define SIZE5 KB(8) + + +static void test_rbcache_init(TestRBCacheData *data, const void *ctx) +{ + g_assert_nonnull(data->cache); +} + +static void test_rbcache_insert(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node1 = rbcache_node_alloc(data->cache, OFFSET1, SIZE1); + RBCacheNode *node2 = rbcache_node_alloc(data->cache, OFFSET2, SIZE2); + RBCacheNode *node3 = rbcache_node_alloc(data->cache, OFFSET3, SIZE3); + RBCacheNode *node4 = rbcache_node_alloc(data->cache, OFFSET4, SIZE4); + RBCacheNode *node5 = rbcache_node_alloc(data->cache, OFFSET5, SIZE5); + RBCacheNode *node; + + node = rbcache_insert(data->cache, node1); + g_assert_true(node == node1); + + node = rbcache_insert(data->cache, node2); + g_assert_true(node == node2); + + node = rbcache_insert(data->cache, node3); + g_assert_true(node == node3); + + node = rbcache_insert(data->cache, node4); + g_assert_true(node == node4); + + node = rbcache_insert(data->cache, node5); + g_assert_true(node != node5); + + rbcache_node_free(data->cache, node5); +} + +static void test_rbcache_search(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + test_rbcache_insert(data, ctx); + + node = rbcache_search(data->cache, OFFSET1, SIZE1); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET1); + g_assert_cmpuint(node->bytes, ==, SIZE1); + + node = rbcache_search(data->cache, OFFSET2 + KB(1), SIZE1); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); + + node = rbcache_search(data->cache, OFFSET5, SIZE5); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); + + node = rbcache_search(data->cache, OFFSET5 + KB(2), SIZE5); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET4); + g_assert_cmpuint(node->bytes, ==, SIZE4); + + node = rbcache_search(data->cache, OFFSET3 + SIZE3, SIZE3); + g_assert_null(node); +} + +static void test_rbcache_search_and_insert(TestRBCacheData *data, + const void *ctx) +{ + RBCacheNode *node; + + node = rbcache_search_and_insert(data->cache, OFFSET1, SIZE1); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET1); + g_assert_cmpuint(node->bytes, ==, SIZE1); + + node = rbcache_search_and_insert(data->cache, OFFSET2, SIZE2); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, OFFSET3, SIZE3); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET3); + g_assert_cmpuint(node->bytes, ==, SIZE3); + + node = rbcache_search_and_insert(data->cache, OFFSET4, SIZE4); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET4); + g_assert_cmpuint(node->bytes, ==, SIZE4); + + node = rbcache_search_and_insert(data->cache, OFFSET5, SIZE5); + g_assert_nonnull(node); + g_assert_cmpuint(node->offset, ==, OFFSET2); + g_assert_cmpuint(node->bytes, ==, SIZE2); +} + +static void test_rbcache_remove(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + test_rbcache_search_and_insert(data, ctx); + + node = rbcache_search(data->cache, OFFSET1, SIZE1); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET1, SIZE1); + g_assert_null(node); + + node = rbcache_search(data->cache, OFFSET3, SIZE3); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET3, SIZE3); + g_assert_null(node); + + node = rbcache_search(data->cache, OFFSET4, SIZE4); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET4, SIZE4); + g_assert_null(node); + + node = rbcache_search(data->cache, OFFSET2, SIZE2); + g_assert_nonnull(node); + rbcache_remove(data->cache, node); + node = rbcache_search(data->cache, OFFSET2, SIZE2); + g_assert_null(node); +} + +static void test_rbcache_shrink(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + node = rbcache_search_and_insert(data->cache, 0, MB(2)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(2), MB(3)); + g_assert_nonnull(node); + + node = rbcache_search(data->cache, 0, MB(2)); + g_assert_null(node); + + node = rbcache_search(data->cache, MB(2), MB(3)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, 0, MB(2)); + g_assert_nonnull(node); + + node = rbcache_search(data->cache, 0, MB(2)); + g_assert_nonnull(node); + + node = rbcache_search(data->cache, MB(2), MB(3)); + g_assert_null(node); +} + +static void test_rbcache_shrink_fifo(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + rbcache_search_and_insert(data->cache, 0, MB(1)); + rbcache_search_and_insert(data->cache, MB(1), MB(1)); + rbcache_search_and_insert(data->cache, MB(2), MB(1)); + rbcache_search_and_insert(data->cache, MB(3), MB(1)); + + node = rbcache_search_and_insert(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + + node = rbcache_search_and_insert(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_null(node); + + node = rbcache_search_and_insert(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_null(node); + + node = rbcache_search_and_insert(data->cache, MB(7), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_null(node); +} + +static void test_rbcache_shrink_lru(TestRBCacheData *data, const void *ctx) +{ + RBCacheNode *node; + + rbcache_search_and_insert(data->cache, 0, MB(1)); + rbcache_search_and_insert(data->cache, MB(1), MB(1)); + rbcache_search_and_insert(data->cache, MB(2), MB(1)); + rbcache_search_and_insert(data->cache, MB(3), MB(1)); + + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_nonnull(node); + + node = rbcache_search_and_insert(data->cache, MB(4), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, 0, MB(1)); + g_assert_null(node); + + node = rbcache_search_and_insert(data->cache, MB(5), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(3), MB(1)); + g_assert_null(node); + + node = rbcache_search_and_insert(data->cache, MB(6), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(2), MB(1)); + g_assert_null(node); + + node = rbcache_search_and_insert(data->cache, MB(7), MB(1)); + g_assert_nonnull(node); + node = rbcache_search(data->cache, MB(1), MB(1)); + g_assert_null(node); +} + +static void test_rbcache_setup(TestRBCacheData *data, const void *ctx) +{ + const TestRBCacheConfig *config = ctx; + + data->cache = + rbcache_create(config->alloc, config->free, config->limit_size, + config->eviction_type, config->opaque); +} + +static void test_rbcache_teardown(TestRBCacheData *data, const void *ctx) +{ + rbcache_destroy(data->cache); +} + +static void rbcache_test_add(const char *testpath, + void (*test_func)(TestRBCacheData *data, + const void *user_data), void *ctx) +{ + g_test_add(testpath, TestRBCacheData, ctx, test_rbcache_setup, test_func, + test_rbcache_teardown); +} + +int main(int argc, char **argv) +{ + TestRBCacheConfig config = { + .limit_size = MB(4), + .eviction_type = RBCACHE_FIFO, + }; + + g_test_init(&argc, &argv, NULL); + + rbcache_test_add("/rbcache/init", test_rbcache_init, &config); + rbcache_test_add("/rbcache/insert", test_rbcache_insert, &config); + rbcache_test_add("/rbcache/search", test_rbcache_search, &config); + rbcache_test_add("/rbcache/search_and_insert", + test_rbcache_search_and_insert, &config); + rbcache_test_add("/rbcache/rbcache_remove", test_rbcache_remove, &config); + rbcache_test_add("/rbcache/shrink", test_rbcache_shrink, &config); + rbcache_test_add("/rbcache/shrink/fifo", test_rbcache_shrink_fifo, &config); + + config.eviction_type = RBCACHE_LRU; + rbcache_test_add("/rbcache/shrink/lru", test_rbcache_shrink_lru, &config); + + g_test_run(); + + return 0; +}