lib/find_bit_benchmark_rust.rs

Source file repositories/reference/linux-study-clean/lib/find_bit_benchmark_rust.rs

File Facts

System
Linux kernel
Corpus path
lib/find_bit_benchmark_rust.rs
Extension
.rs
Size
2864 bytes
Lines
105
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

fn test_next_bit(bitmap: &BitmapVec) {
    let time = Instant::<Monotonic>::now();
    let mut cnt = 0;
    let mut i = 0;

    while let Some(index) = bitmap.next_bit(i) {
        cnt += 1;
        i = index + 1;
        // CONFIG_RUST_BITMAP_HARDENED enforces strict bounds.
        if i == BITMAP_LEN {
            break;
        }
    }

    let delta = time.elapsed();
    pr_cont!(
        "\nnext_bit:           {:18} ns, {:6} iterations",
        delta.as_nanos(),
        cnt
    );
}

fn test_next_zero_bit(bitmap: &BitmapVec) {
    let time = Instant::<Monotonic>::now();
    let mut cnt = 0;
    let mut i = 0;

    while let Some(index) = bitmap.next_zero_bit(i) {
        cnt += 1;
        i = index + 1;
        // CONFIG_RUST_BITMAP_HARDENED enforces strict bounds.
        if i == BITMAP_LEN {
            break;
        }
    }

    let delta = time.elapsed();
    pr_cont!(
        "\nnext_zero_bit:      {:18} ns, {:6} iterations",
        delta.as_nanos(),
        cnt
    );
}

fn find_bit_test() {
    pr_err!("Benchmark");
    pr_cont!("\nStart testing find_bit() Rust with random-filled bitmap");

    let mut bitmap = BitmapVec::new(BITMAP_LEN, GFP_KERNEL).expect("alloc bitmap failed");
    bitmap.fill_random();

    test_next_bit(&bitmap);
    test_next_zero_bit(&bitmap);

    pr_cont!("\nStart testing find_bit() Rust with sparse bitmap");

    let mut bitmap = BitmapVec::new(BITMAP_LEN, GFP_KERNEL).expect("alloc sparse bitmap failed");
    let nbits = BITMAP_LEN / SPARSENESS;
    for _i in 0..nbits {
        // SAFETY: __get_random_u32_below is safe to call with any u32 argument.
        let bit =
            unsafe { bindings::__get_random_u32_below(BITMAP_LEN.try_into().unwrap()) as usize };
        bitmap.set_bit(bit);
    }

    test_next_bit(&bitmap);
    test_next_zero_bit(&bitmap);
    pr_cont!("\n");
}

impl kernel::Module for Benchmark {
    fn init(_module: &'static ThisModule) -> Result<Self> {
        find_bit_test();
        // Return error so test module can be inserted again without rmmod.
        Err(code::EINVAL)
    }
}

module! {
    type: Benchmark,
    name: "find_bit_benchmark_rust",
    authors: ["Burak Emir <bqe@google.com>"],
    description: "Module with benchmark for bitmap Rust API",
    license: "GPL v2",
}

Annotation

Implementation Notes