Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rust: add find_bit_benchmark_rust module.

Microbenchmark protected by a config FIND_BIT_BENCHMARK_RUST,
following `find_bit_benchmark.c` but testing the Rust Bitmap API.

We add a fill_random() method protected by the config in order to
maintain the abstraction.

The sample output from the benchmark, both C and Rust version:

find_bit_benchmark.c output:
```
Start testing find_bit() with random-filled bitmap
[ 438.101937] find_next_bit: 860188 ns, 163419 iterations
[ 438.109471] find_next_zero_bit: 912342 ns, 164262 iterations
[ 438.116820] find_last_bit: 726003 ns, 163419 iterations
[ 438.130509] find_nth_bit: 7056993 ns, 16269 iterations
[ 438.139099] find_first_bit: 1963272 ns, 16270 iterations
[ 438.173043] find_first_and_bit: 27314224 ns, 32654 iterations
[ 438.180065] find_next_and_bit: 398752 ns, 73705 iterations
[ 438.186689]
Start testing find_bit() with sparse bitmap
[ 438.193375] find_next_bit: 9675 ns, 656 iterations
[ 438.201765] find_next_zero_bit: 1766136 ns, 327025 iterations
[ 438.208429] find_last_bit: 9017 ns, 656 iterations
[ 438.217816] find_nth_bit: 2749742 ns, 655 iterations
[ 438.225168] find_first_bit: 721799 ns, 656 iterations
[ 438.231797] find_first_and_bit: 2819 ns, 1 iterations
[ 438.238441] find_next_and_bit: 3159 ns, 1 iterations
```

find_bit_benchmark_rust.rs output:
```
[ 451.182459] find_bit_benchmark_rust:
[ 451.186688] Start testing find_bit() Rust with random-filled bitmap
[ 451.194450] next_bit: 777950 ns, 163644 iterations
[ 451.201997] next_zero_bit: 918889 ns, 164036 iterations
[ 451.208642] Start testing find_bit() Rust with sparse bitmap
[ 451.214300] next_bit: 9181 ns, 654 iterations
[ 451.222806] next_zero_bit: 1855504 ns, 327026 iterations
```

Here are the results from 32 samples, with 95% confidence interval.
The microbenchmark was built with RUST_BITMAP_HARDENED=n and run on a
machine that did not execute other processes.

Random-filled bitmap:
+-----------+-------+-----------+--------------+-----------+-----------+
| Benchmark | Lang | Mean (ms) | Std Dev (ms) | 95% CI Lo | 95% CI Hi |
+-----------+-------+-----------+--------------+-----------+-----------+
| find_bit/ | C | 825.07 | 53.89 | 806.40 | 843.74 |
| next_bit | Rust | 870.91 | 46.29 | 854.88 | 886.95 |
+-----------+-------+-----------+--------------+-----------+-----------+
| find_zero/| C | 933.56 | 56.34 | 914.04 | 953.08 |
| next_zero | Rust | 945.85 | 60.44 | 924.91 | 966.79 |
+-----------+-------+-----------+--------------+-----------+-----------+

Rust appears 5.5% slower for next_bit, 1.3% slower for next_zero.

Sparse bitmap:
+-----------+-------+-----------+--------------+-----------+-----------+
| Benchmark | Lang | Mean (ms) | Std Dev (ms) | 95% CI Lo | 95% CI Hi |
+-----------+-------+-----------+--------------+-----------+-----------+
| find_bit/ | C | 13.17 | 6.21 | 11.01 | 15.32 |
| next_bit | Rust | 14.30 | 8.27 | 11.43 | 17.17 |
+-----------+-------+-----------+--------------+-----------+-----------+
| find_zero/| C | 1859.31 | 82.30 | 1830.80 | 1887.83 |
| next_zero | Rust | 1908.09 | 139.82 | 1859.65 | 1956.54 |
+-----------+-------+-----------+--------------+-----------+-----------+

Rust appears 8.5% slower for next_bit, 2.6% slower for next_zero.

In summary, taking the arithmetic mean of all slow-downs, we can say
the Rust API has a 4.5% slowdown.

Suggested-by: Alice Ryhl <aliceryhl@google.com>
Suggested-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
Reviewed-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Burak Emir <bqe@google.com>
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>

authored by

Burak Emir and committed by
Yury Norov (NVIDIA)
38cc91db 11eca92a

+135
+1
MAINTAINERS
··· 4307 4307 M: Burak Emir <bqe@google.com> 4308 4308 R: Yury Norov <yury.norov@gmail.com> 4309 4309 S: Maintained 4310 + F: lib/find_bit_benchmark_rust.rs 4310 4311 F: rust/kernel/bitmap.rs 4311 4312 4312 4313 BITOPS API
+13
lib/Kconfig.debug
··· 2607 2607 2608 2608 If unsure, say N. 2609 2609 2610 + config FIND_BIT_BENCHMARK_RUST 2611 + tristate "Test find_bit functions in Rust" 2612 + depends on RUST 2613 + help 2614 + This builds the "find_bit_benchmark_rust" module. It is a micro 2615 + benchmark that measures the performance of Rust functions that 2616 + correspond to the find_*_bit() operations in C. It follows the 2617 + FIND_BIT_BENCHMARK closely but will in general not yield same 2618 + numbers due to extra bounds checks and overhead of foreign 2619 + function calls. 2620 + 2621 + If unsure, say N. 2622 + 2610 2623 config TEST_FIRMWARE 2611 2624 tristate "Test firmware loading via userspace interface" 2612 2625 depends on FW_LOADER
+1
lib/Makefile
··· 62 62 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o 63 63 obj-y += kstrtox.o 64 64 obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o 65 + obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o 65 66 obj-$(CONFIG_TEST_BPF) += test_bpf.o 66 67 test_dhry-objs := dhry_1.o dhry_2.o dhry_run.o 67 68 obj-$(CONFIG_TEST_DHRY) += test_dhry.o
+104
lib/find_bit_benchmark_rust.rs
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + //! Benchmark for find_bit-like methods in Bitmap Rust API. 3 + 4 + use kernel::alloc::flags::GFP_KERNEL; 5 + use kernel::bindings; 6 + use kernel::bitmap::BitmapVec; 7 + use kernel::error::{code, Result}; 8 + use kernel::prelude::module; 9 + use kernel::time::{Instant, Monotonic}; 10 + use kernel::ThisModule; 11 + use kernel::{pr_cont, pr_err}; 12 + 13 + const BITMAP_LEN: usize = 4096 * 8 * 10; 14 + // Reciprocal of the fraction of bits that are set in sparse bitmap. 15 + const SPARSENESS: usize = 500; 16 + 17 + /// Test module that benchmarks performance of traversing bitmaps. 18 + struct Benchmark(); 19 + 20 + fn test_next_bit(bitmap: &BitmapVec) { 21 + let time = Instant::<Monotonic>::now(); 22 + let mut cnt = 0; 23 + let mut i = 0; 24 + 25 + while let Some(index) = bitmap.next_bit(i) { 26 + cnt += 1; 27 + i = index + 1; 28 + // CONFIG_RUST_BITMAP_HARDENED enforces strict bounds. 29 + if i == BITMAP_LEN { 30 + break; 31 + } 32 + } 33 + 34 + let delta = time.elapsed(); 35 + pr_cont!( 36 + "\nnext_bit: {:18} ns, {:6} iterations", 37 + delta.as_nanos(), 38 + cnt 39 + ); 40 + } 41 + 42 + fn test_next_zero_bit(bitmap: &BitmapVec) { 43 + let time = Instant::<Monotonic>::now(); 44 + let mut cnt = 0; 45 + let mut i = 0; 46 + 47 + while let Some(index) = bitmap.next_zero_bit(i) { 48 + cnt += 1; 49 + i = index + 1; 50 + // CONFIG_RUST_BITMAP_HARDENED enforces strict bounds. 51 + if i == BITMAP_LEN { 52 + break; 53 + } 54 + } 55 + 56 + let delta = time.elapsed(); 57 + pr_cont!( 58 + "\nnext_zero_bit: {:18} ns, {:6} iterations", 59 + delta.as_nanos(), 60 + cnt 61 + ); 62 + } 63 + 64 + fn find_bit_test() { 65 + pr_err!("Benchmark"); 66 + pr_cont!("\nStart testing find_bit() Rust with random-filled bitmap"); 67 + 68 + let mut bitmap = BitmapVec::new(BITMAP_LEN, GFP_KERNEL).expect("alloc bitmap failed"); 69 + bitmap.fill_random(); 70 + 71 + test_next_bit(&bitmap); 72 + test_next_zero_bit(&bitmap); 73 + 74 + pr_cont!("\nStart testing find_bit() Rust with sparse bitmap"); 75 + 76 + let mut bitmap = BitmapVec::new(BITMAP_LEN, GFP_KERNEL).expect("alloc sparse bitmap failed"); 77 + let nbits = BITMAP_LEN / SPARSENESS; 78 + for _i in 0..nbits { 79 + // SAFETY: __get_random_u32_below is safe to call with any u32 argument. 80 + let bit = 81 + unsafe { bindings::__get_random_u32_below(BITMAP_LEN.try_into().unwrap()) as usize }; 82 + bitmap.set_bit(bit); 83 + } 84 + 85 + test_next_bit(&bitmap); 86 + test_next_zero_bit(&bitmap); 87 + pr_cont!("\n"); 88 + } 89 + 90 + impl kernel::Module for Benchmark { 91 + fn init(_module: &'static ThisModule) -> Result<Self> { 92 + find_bit_test(); 93 + // Return error so test module can be inserted again without rmmod. 94 + Err(code::EINVAL) 95 + } 96 + } 97 + 98 + module! { 99 + type: Benchmark, 100 + name: "find_bit_benchmark_rust", 101 + authors: ["Burak Emir <bqe@google.com>"], 102 + description: "Module with benchmark for bitmap Rust API", 103 + license: "GPL v2", 104 + }
+1
rust/bindings/bindings_helper.h
··· 67 67 #include <linux/pm_opp.h> 68 68 #include <linux/poll.h> 69 69 #include <linux/property.h> 70 + #include <linux/random.h> 70 71 #include <linux/refcount.h> 71 72 #include <linux/regulator/consumer.h> 72 73 #include <linux/sched.h>
+15
rust/kernel/bitmap.rs
··· 252 252 pub fn len(&self) -> usize { 253 253 self.nbits 254 254 } 255 + 256 + /// Fills this `Bitmap` with random bits. 257 + #[cfg(CONFIG_FIND_BIT_BENCHMARK_RUST)] 258 + pub fn fill_random(&mut self) { 259 + // SAFETY: `self.as_mut_ptr` points to either an array of the 260 + // appropriate length or one usize. 261 + unsafe { 262 + bindings::get_random_bytes( 263 + self.as_mut_ptr().cast::<ffi::c_void>(), 264 + usize::div_ceil(self.nbits, bindings::BITS_PER_LONG as usize) 265 + * bindings::BITS_PER_LONG as usize 266 + / 8, 267 + ); 268 + } 269 + } 255 270 } 256 271 257 272 impl Bitmap {