Skip to content

Commit 38cc91d

Browse files
Burak EmirYuryNorov
authored andcommitted
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>
1 parent 11eca92 commit 38cc91d

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4307,6 +4307,7 @@ M: Alice Ryhl <aliceryhl@google.com>
43074307
M: Burak Emir <bqe@google.com>
43084308
R: Yury Norov <yury.norov@gmail.com>
43094309
S: Maintained
4310+
F: lib/find_bit_benchmark_rust.rs
43104311
F: rust/kernel/bitmap.rs
43114312

43124313
BITOPS API

lib/Kconfig.debug

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,19 @@ config FIND_BIT_BENCHMARK
26072607

26082608
If unsure, say N.
26092609

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+
26102623
config TEST_FIRMWARE
26112624
tristate "Test firmware loading via userspace interface"
26122625
depends on FW_LOADER

lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ obj-y += hexdump.o
6262
obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
6363
obj-y += kstrtox.o
6464
obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o
65+
obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o
6566
obj-$(CONFIG_TEST_BPF) += test_bpf.o
6667
test_dhry-objs := dhry_1.o dhry_2.o dhry_run.o
6768
obj-$(CONFIG_TEST_DHRY) += test_dhry.o

lib/find_bit_benchmark_rust.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include <linux/pm_opp.h>
6868
#include <linux/poll.h>
6969
#include <linux/property.h>
70+
#include <linux/random.h>
7071
#include <linux/refcount.h>
7172
#include <linux/regulator/consumer.h>
7273
#include <linux/sched.h>

rust/kernel/bitmap.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ impl BitmapVec {
252252
pub fn len(&self) -> usize {
253253
self.nbits
254254
}
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+
}
255270
}
256271

257272
impl Bitmap {

0 commit comments

Comments
 (0)