Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751266AbdJ1Maf (ORCPT ); Sat, 28 Oct 2017 08:30:35 -0400 Received: from mail-bl2nam02on0059.outbound.protection.outlook.com ([104.47.38.59]:25312 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751000AbdJ1Mad (ORCPT ); Sat, 28 Oct 2017 08:30:33 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Yuri.Norov@cavium.com; From: Yury Norov To: linux-kernel@vger.kernel.org Cc: Yury Norov , Alexey Dobriyan , Andrew Morton , Clement Courbet , Matthew Wilcox , Rasmus Villemoes Subject: [PATCH] lib: hint GCC to inlilne _find_next_bit() helper Date: Sat, 28 Oct 2017 15:30:12 +0300 Message-Id: <20171028123012.14187-1-ynorov@caviumnetworks.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [50.233.148.156] X-ClientProxiedBy: VI1PR0801CA0087.eurprd08.prod.outlook.com (2603:10a6:800:7d::31) To CY4PR0701MB3828.namprd07.prod.outlook.com (2603:10b6:910:94::34) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e42be2aa-d744-428e-514f-08d51dffacef X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(4534020)(4602075)(2017052603199);SRVR:CY4PR0701MB3828; X-Microsoft-Exchange-Diagnostics: 1;CY4PR0701MB3828;3:vfxYS6+USRLIJXzW73/TeI9EOfRDXy2ecLJt1/PXLrZl/KLSwEBBdLJ2uxo7Vi664QVZM0h6d9sUGUDdgAqiJIiTsqCfGyoY/fqgpU28oDUAjaf+p3jnkIxVbm7BjL00OlRe9gBMHuX3rfOT292FI5UHgAQMGTDpYcJIQ50OpzbKo8yjK30XhwWy83C8kSDe2FoQM1mbloq1ZSeWkN91NY7OjX2lxetNHLpFJCe3J6pFCeJ8R+gs/0BSZ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l/iClHsLfXqWAIz+9Beh7z9EuyVDE6NThJWwGE312cCbaWU/5H8J3fgJOZXnvzv/Ld1T2TEZlp0++xYsJhHSo6JPCIugLcWP+Ah8TdFlI0Mz/KGdOPrMOxx/Hh2wFXKM6iXmiuyXhpDnMqP3nHw55fUD+FpkehBdPeEPu7j7kbA2tqQZiX8zfjzePFx9MuKXkWsgkVuPVHqlXo5Czrn1mYmT1vH8E= X-Exchange-Antispam-Report-Test: UriScan:(89211679590171)(211936372134217)(153496737603132); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(8121501046)(5005006)(93006095)(100000703101)(100105400095)(3231020)(3002001)(10201501046)(920507026)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123564025)(20161123560025)(20161123555025)(20161123558100)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:CY4PR0701MB3828;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:CY4PR0701MB3828; X-Microsoft-Exchange-Diagnostics: 1;CY4PR0701MB3828;4:egefVxIRO9km3UBu6H2NFVitLmJh2h13p0jwN3ruAbnc4R7v3FwWjmbH8cUxQz93VLCjYY4/2nflIELUAUOQnPFNzwv9MIbyshSYQK4PZrPmq3wyabazL7Ku/WA+h1q0UeZLEM4iM52KBhPPSGto8xofmV4DGJkUD0XJeXRlkc2SyMkCs5F1OqQh7Yh1i6f2tV1Q0jnaqYABFzpIOOJSGsc3bF8hDBe3pLmjHQfcDxKxV7AyB1RXD22qeP7RzpHPMjX6yCldXJYkKFuwuHp6CWupc5NRSSMMqTyYKwIEbN07C42kgDo9sOvSUluLK9oVny+Ql3W8Bu6gbGUhMklvtHshML2Aqst4+XIDaDYcAYPjMfzTG0kV7ESZgENxjSPL X-Forefront-PRVS: 04740D25F1 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6009001)(6069001)(346002)(376002)(53754006)(199003)(189002)(54906003)(66066001)(53386004)(6496005)(47776003)(42882006)(6916009)(6666003)(316002)(53936002)(16586007)(6306002)(575784001)(50226002)(76506005)(36756003)(68736007)(48376002)(50466002)(33646002)(106356001)(5660300001)(105586002)(2361001)(2351001)(101416001)(50986999)(6486002)(8666007)(8936002)(81166006)(81156014)(8676002)(25786009)(5003940100001)(1076002)(3846002)(6116002)(189998001)(2906002)(478600001)(305945005)(16526018)(45080400002)(39060400002)(34040400001)(72206003)(4326008)(97736004)(966005)(7736002)(2004002);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR0701MB3828;H:localhost;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY4PR0701MB3828;23:KVuFBM1gM4CDoXBgoxa1EKN9T+DPjoeW8CKvEVX?= =?us-ascii?Q?2rXQg6dC1Gd8gnbGBdpjJUbl+PbC6kIOwu7+MAgaQqmiTpoWE9OMffkNJ8K/?= =?us-ascii?Q?eN7e5qOsnJuZD2ZA+UWnVM9fuRFzGLPThOskD5+Eji3c1kVutdxlz60RM628?= =?us-ascii?Q?F0NG+5meAMBssTVpWWyJ1gTCd6lIUKnhcZxrMnbfWvg8Ef2SZEhhTHoq8qTs?= =?us-ascii?Q?63EI0x3LVC8fQjWqmPU8VXWQlxpGaMdg2KqpZVKbsjm2cU9p7fj4pzKNe27X?= =?us-ascii?Q?D33YNaA59Beujjq9yGlltRWKfU6T/pbpuEfQEhiFGVu6LXsiw68sl/XaABir?= =?us-ascii?Q?adoro67piPI8Xm4kyGD/xyoq3Puw5C5uCRuq721K87QcOfMTCs0k0Fl685Zw?= =?us-ascii?Q?PYuAiMsbV1lRKr2HDgOYc0MBAZmIWrIhVgc0h9RA/ReI1u5Vyqq+h1+DM9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Ve1MEIu14of7r4ppnqW10?= =?us-ascii?Q?NNZ5eeQ=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1;CY4PR0701MB3828;6:0pPg+YHvabCIbUD4f9RwmiC6h5NR9whRFA//OyUEixnIQ8r84wknJVXaXYyVBLI5dGivcz3ZFsHYsekR0W02doceUIP923inf1PbjrN8Y0PRq0uASeqos8WrSiY5uzv12kTbb2r6iY8y44J5ixdA5VIdwkKR+ZncrC8viEy84ePQmbzYXKzkzMLIYxydI2EU/P23DsAEJ+En26EJCKPynD1pNOTRLye3ezxr8gRS3a35wBkGjEFpbfCOAFkkqODkhiEU1ClSlYDe1fV+CVMx+0RxA2+58zZoH2l8As84TFjFj/hujeeuVeJ5yhHNhIVrm97l/7e24KaCcOuPiR1Lq+THFpm6D0qZu10oyaKRvO4=;5:6zIcxk60fVnv2cS/1UIlaMKV/Ky6Ls7UsDMZ3WUOmg4vlB0FIlca33U6D4VqiqpNk1e1LfZE8rJHbbP3UZepGaPDHZCd7odrcHTw+EO6JOqH+LFeHtouStycoruIxwLEardsySCs4uNiHVRqr8N/ZjLXwHw88stUMSIuakcIrsc=;24:4xSAJnatSOc+mD+s6nRhXWGFNhpGxIA92Q4o6LgVD6gBIbUR261/elegTxV/IK5yc0VJhIXuSfGIap2GfvDDjACjCm7RInJd29qzUoA+8FU=;7:yOYClfDXal159tMfwMDCENAuOfWyQuZBtjqRYxrAlbt5NVyAlTdmO1hGxjaSbgYVwGpM6Qv8qJal40hqmFL2WaQvHizf/3xs5zXFtA5ewhzud4iEq8FzjO/jPC5gZDy1UhcDJEa4GKPT9B69NDOxZJYWAC3CzYKsF+46pDPYZDKCQuKt2wswqjesyouRTNqmvN1GKq5+3VI6WHeRKcHpKkMJbEKKjiEIop7bkWTUfrjXBGLTapDftEZN1VeGXdFp SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Oct 2017 12:30:28.8492 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e42be2aa-d744-428e-514f-08d51dffacef X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR0701MB3828 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi all, It seems that inlining the _find_next_bit() helper makes find_next_bit() and find_next_zero_bit() 2 times faster at the scenario of finding all set/cleared bits of randomly initialised bitmap. For another typical scenario of traversing sparse bitmap there is also measurable improvement observed, about 15%. The increasing of text size of find_bit.o module is 40 bytes for arm64 - from 252 to 292 bytes - is looking acceptable. This patch also contains test module. Measured on ThunderX machine. Tests for other architectures are very appreciated. Before: [ 96.856195] Start testing find_bit() with random-filled bitmap [ 96.868322] find_next_bit: 34529 cycles, 16304 iterations [ 96.879525] find_next_zero_bit: 35771 cycles, 16465 iterations [ 96.891409] find_last_bit: 17444 cycles, 16304 iterations [ 96.914445] find_first_bit: 1219671 cycles, 16305 iterations [ 96.925802] Start testing find_bit() with sparse bitmap [ 96.936308] find_next_bit: 301 cycles, 66 iterations [ 96.946981] find_next_zero_bit: 70897 cycles, 32703 iterations [ 96.958694] find_last_bit: 286 cycles, 66 iterations [ 96.968710] find_first_bit: 5260 cycles, 66 iterations After: [ 169.464229] Start testing find_bit() with random-filled bitmap [ 169.476191] find_next_bit: 17520 cycles, 16336 iterations [ 169.487210] find_next_zero_bit: 17622 cycles, 16433 iterations [ 169.499111] find_last_bit: 19272 cycles, 16335 iterations [ 169.519735] find_first_bit: 978657 cycles, 16337 iterations [ 169.530912] Start testing find_bit() with sparse bitmap [ 169.541414] find_next_bit: 252 cycles, 66 iterations [ 169.551726] find_next_zero_bit: 34554 cycles, 32703 iterations [ 169.563436] find_last_bit: 294 cycles, 66 iterations [ 169.573439] find_first_bit: 3964 cycles, 66 iterations CC: Alexey Dobriyan CC: Andrew Morton CC: Clement Courbet CC: Matthew Wilcox CC: Rasmus Villemoes Signed-off-by: Yury Norov --- lib/Kconfig.debug | 9 ++++ lib/Makefile | 1 + lib/find_bit.c | 2 +- lib/test_find_bit.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/find_bit.c | 2 +- 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 lib/test_find_bit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index dfdad67d8f6c..138034cc68a3 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1838,6 +1838,15 @@ config TEST_BPF If unsure, say N. +config TEST_FIND_BIT + tristate "Test find_bit functions" + default n + help + This builds the "test_find_bit" module that measure find_*_bit() + functions performance. + + If unsure, say N. + config TEST_FIRMWARE tristate "Test firmware loading via userspace interface" default n diff --git a/lib/Makefile b/lib/Makefile index dafa79613fb4..edb792b42c86 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -45,6 +45,7 @@ obj-y += hexdump.o obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o obj-y += kstrtox.o obj-$(CONFIG_TEST_BPF) += test_bpf.o +obj-$(CONFIG_TEST_FIND_BIT) += test_find_bit.o obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o diff --git a/lib/find_bit.c b/lib/find_bit.c index 6ed74f78380c..9b0c89f3fd3a 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -28,7 +28,7 @@ * find_next_zero_bit. The difference is the "invert" argument, which * is XORed with each fetched word before searching it for one bits. */ -static unsigned long _find_next_bit(const unsigned long *addr, +static inline unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start, unsigned long invert) { unsigned long tmp; diff --git a/lib/test_find_bit.c b/lib/test_find_bit.c new file mode 100644 index 000000000000..8eaf10cae214 --- /dev/null +++ b/lib/test_find_bit.c @@ -0,0 +1,141 @@ +/* + * Test for find_*_bit functions. + * + * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + */ + +/* + * find_bit functions are widely used in kernel, so the successful boot + * is good enough test for correctness. + * + * This test is focused on performance of traversing bitmaps. Two typical + * scenarios are reproduced: + * - randomly filled bitmap with approximately equal number of set and + * cleared bits; + * - sparse bitmap with few set bits at random positions. + */ + +#include +#include +#include +#include +#include +#include + +#define BITMAP_LEN (4096UL * 8) +#define SPARSE (500) + +static DECLARE_BITMAP(bitmap, BITMAP_LEN) __initdata; + +/* + * This is Schlemiel the Painter's algorithm. + * Should be called after all other tests for the same data + * because it sets all bits of bitmap to 1. + */ +static int __init test_find_first_bit(void *bitmap, unsigned long len) +{ + unsigned long i, cnt; + cycles_t cycles; + + cycles = get_cycles(); + for (cnt = i = 0; i < len; cnt++) { + i = find_first_bit(bitmap, len); + __clear_bit(i, bitmap); + } + cycles = get_cycles() - cycles; + pr_err("find_first_bit: %ld cycles, %ld iterations\n", cycles, cnt); + + return 0; +} + +static int __init test_find_next_bit(const void *bitmap, unsigned long len) +{ + unsigned long i, cnt; + cycles_t cycles; + + cycles = get_cycles(); + for (cnt = i = 0; i < BITMAP_LEN; cnt++) + i = find_next_bit(bitmap, BITMAP_LEN, i) + 1; + cycles = get_cycles() - cycles; + pr_err("find_next_bit: %ld cycles, %ld iterations\n", cycles, cnt); + + return 0; +} + +static int __init test_find_next_zero_bit(const void *bitmap, unsigned long len) +{ + unsigned long i, cnt; + cycles_t cycles; + + cycles = get_cycles(); + for (cnt = i = 0; i < BITMAP_LEN; cnt++) + i = find_next_zero_bit(bitmap, len, i) + 1; + cycles = get_cycles() - cycles; + pr_err("find_next_zero_bit: %ld cycles, %ld iterations\n", cycles, cnt); + + return 0; +} + +static int __init test_find_last_bit(const void *bitmap, unsigned long len) +{ + unsigned long l, cnt = 0; + cycles_t cycles; + + cycles = get_cycles(); + do { + cnt++; + l = find_last_bit(bitmap, len); + if (l >= len) + break; + len = l; + } while (len); + cycles = get_cycles() - cycles; + pr_err("find_last_bit: %ld cycles, %ld iterations\n", cycles, cnt); + + return 0; +} + +static int __init find_bit_test(void) +{ + unsigned long nbits = BITMAP_LEN / SPARSE; + + pr_err("Start testing find_bit() with random-filled bitmap\n"); + + get_random_bytes(bitmap, sizeof(bitmap)); + + test_find_next_bit(bitmap, BITMAP_LEN); + test_find_next_zero_bit(bitmap, BITMAP_LEN); + test_find_last_bit(bitmap, BITMAP_LEN); + test_find_first_bit(bitmap, BITMAP_LEN); + + pr_err("Start testing find_bit() with sparse bitmap\n"); + + bitmap_zero(bitmap, BITMAP_LEN); + + while (nbits--) + __set_bit(prandom_u32() % BITMAP_LEN, bitmap); + + test_find_next_bit(bitmap, BITMAP_LEN); + test_find_next_zero_bit(bitmap, BITMAP_LEN); + test_find_last_bit(bitmap, BITMAP_LEN); + test_find_first_bit(bitmap, BITMAP_LEN); + + return 0; +} +module_init(find_bit_test); + +static void __exit test_find_bit_cleanup(void) +{ +} +module_exit(test_find_bit_cleanup); + +MODULE_LICENSE("GPL"); diff --git a/tools/lib/find_bit.c b/tools/lib/find_bit.c index 42c15f906aac..6873b5fa221b 100644 --- a/tools/lib/find_bit.c +++ b/tools/lib/find_bit.c @@ -29,7 +29,7 @@ * find_next_zero_bit. The difference is the "invert" argument, which * is XORed with each fetched word before searching it for one bits. */ -static unsigned long _find_next_bit(const unsigned long *addr, +static inline unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start, unsigned long invert) { unsigned long tmp; -- 2.11.0