All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Gomez <da.gomez@kernel.org>
To: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Daniel Gomez" <da.gomez@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: Julia Lawall <Julia.Lawall@inria.fr>,
	 Corinn Tiffany <corinn.tiffany@inria.fr>,
	 "Liam R. Howlett" <liam@infradead.org>,
	 Philipp Stanner <pstanner@redhat.com>,
	linux-kernel@vger.kernel.org,  rust-for-linux@vger.kernel.org,
	Samsung GOST <gost.dev@samsung.com>,
	 Daniel Gomez <da.gomez@samsung.com>
Subject: [PATCH 3/3] lib/xarray_benchmark_rust: add module
Date: Wed, 23 Sep 2026 23:00:29 +0200	[thread overview]
Message-ID: <20260923-rxarray-next-v1-3-92eedf185649@samsung.com> (raw)
In-Reply-To: <20260923-rxarray-next-v1-0-92eedf185649@samsung.com>

From: Daniel Gomez <da.gomez@samsung.com>

Benchmark XArray Rust APIs using `kernel::bench`.

Assisted-by: LLM
Signed-off-by: Daniel Gomez <da.gomez@samsung.com>
---
 MAINTAINERS                  |   1 +
 lib/Kconfig.debug            |  10 ++++
 lib/Makefile                 |   1 +
 lib/xarray_benchmark_rust.rs | 117 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 129 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index b8bdfe9e22226..f6e45a88475b0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -29690,6 +29690,7 @@ W:	https://rust-for-linux.com
 B:	https://github.com/Rust-for-Linux/linux/issues
 C:	https://rust-for-linux.zulipchat.com
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/da.gomez/linux.git rxarray-next
+F:	lib/xarray_benchmark_rust.rs
 F:	rust/kernel/bench.rs
 F:	rust/kernel/rxarray.rs
 
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 134b15a44625e..6e008f657cd4d 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2667,6 +2667,16 @@ config FIND_BIT_BENCHMARK_RUST
 
 	  If unsure, say N.
 
+config XARRAY_BENCHMARK_RUST
+	tristate "Benchmark the XArray Rust APIs"
+	depends on RUST
+	help
+	  This builds the "xarray_benchmark_rust" module. It runs the same
+	  workloads through the Rust abstraction of the C XArray (kernel::xarray),
+	  and through the Rust XArray implementation (kernel::rxarray).
+
+	  If unsure, say N.
+
 config TEST_FIRMWARE
 	tristate "Test firmware loading via userspace interface"
 	depends on FW_LOADER
diff --git a/lib/Makefile b/lib/Makefile
index dfab958327c5c..9401f5146592e 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -66,6 +66,7 @@ obj-y += kstrtox.o
 obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o
 obj-$(CONFIG_REGION_ALLOC_BENCHMARK) += region_alloc_benchmark.o
 obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o
+obj-$(CONFIG_XARRAY_BENCHMARK_RUST) += xarray_benchmark_rust.o
 obj-$(CONFIG_TEST_BPF) += test_bpf.o
 test_dhry-objs := dhry_1.o dhry_2.o dhry_run.o
 obj-$(CONFIG_TEST_DHRY) += test_dhry.o
diff --git a/lib/xarray_benchmark_rust.rs b/lib/xarray_benchmark_rust.rs
new file mode 100644
index 0000000000000..fa69b975d8d9f
--- /dev/null
+++ b/lib/xarray_benchmark_rust.rs
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+//! Benchmark for the XArray Rust APIs.
+
+use kernel::{
+    bench::{self, Bencher},
+    prelude::*,
+    rxarray::{self, XArray4, XArray6},
+    time::{Delta, Instant, Monotonic},
+    xarray::{self, AllocKind}, //
+};
+
+/// Stores integer `entries` at `0..entries` in the empty `xa` and returns the time taken.
+fn store_int<const SHIFT: usize, const SIZE: usize>(
+    mut xa: rxarray::XArray<KBox<u64>, SHIFT, SIZE>,
+    entries: usize,
+) -> Delta {
+    let time = Instant::<Monotonic>::now();
+    for i in 0..entries {
+        let entry = rxarray::Entry::try_int(i * 10).expect("the value fits in an integer entry");
+        xa.store(i, entry, GFP_KERNEL).expect("store");
+    }
+    time.elapsed()
+}
+
+/// Stores pointer `entries` at `0..entries` in the empty `xa` and returns the time taken.
+fn store_ptr<const SHIFT: usize, const SIZE: usize>(
+    mut xa: rxarray::XArray<KBox<u64>, SHIFT, SIZE>,
+    entries: usize,
+) -> Delta {
+    let time = Instant::<Monotonic>::now();
+    for i in 0..entries {
+        let entry = rxarray::Entry::Pointer(KBox::new(i as u64, GFP_KERNEL).expect("allocation"));
+        xa.store(i, entry, GFP_KERNEL).expect("store");
+    }
+    time.elapsed()
+}
+
+/// Allocates the empty C XArray that [`store_ptr_xarray`] stores into.
+fn new_ptr_xarray() -> Pin<KBox<xarray::XArray<KBox<u64>>>> {
+    KBox::pin_init(xarray::XArray::new(AllocKind::Alloc), GFP_KERNEL).expect("allocation")
+}
+
+/// Stores pointer `entries` at `0..entries` in the empty C XArray `xa` and returns the
+/// time taken.
+fn store_ptr_xarray(xa: Pin<KBox<xarray::XArray<KBox<u64>>>>, entries: usize) -> Delta {
+    let time = Instant::<Monotonic>::now();
+    for i in 0..entries {
+        let value = KBox::new(i as u64, GFP_KERNEL).expect("allocation");
+        xa.lock()
+            .store(i, value, GFP_KERNEL)
+            .map_err(|e| e.error)
+            .expect("store");
+    }
+    time.elapsed()
+}
+
+/// Runs every benchmark.
+fn benchmark(samples: usize, entries: usize) -> Result {
+    let mut bench = Bencher::new(samples, entries)?;
+
+    pr_info!("{samples} samples x {entries} entries, ns per sample:\n");
+    pr_info!("{}\n", bench::Heading);
+    pr_info!(
+        "{}\n",
+        bench.run("store_int_rxarray4", XArray4::<KBox<u64>>::new, store_int)
+    );
+    pr_info!(
+        "{}\n",
+        bench.run("store_int_rxarray6", XArray6::<KBox<u64>>::new, store_int)
+    );
+    pr_info!(
+        "{}\n",
+        bench.run("store_ptr_rxarray4", XArray4::<KBox<u64>>::new, store_ptr)
+    );
+    pr_info!(
+        "{}\n",
+        bench.run("store_ptr_rxarray6", XArray6::<KBox<u64>>::new, store_ptr)
+    );
+    pr_info!(
+        "{}\n",
+        bench.run("store_ptr_xarray", new_ptr_xarray, store_ptr_xarray)
+    );
+    pr_info!("total runtime {}\n", bench.runtime());
+    Ok(())
+}
+
+/// The benchmark module.
+struct Benchmark;
+
+impl kernel::Module for Benchmark {
+    fn init(_module: &'static ThisModule) -> Result<Self> {
+        let samples = module_parameters::samples.value();
+        let entries = module_parameters::entries.value();
+        benchmark(samples, entries)?;
+
+        Ok(Benchmark)
+    }
+}
+
+module! {
+    type: Benchmark,
+    name: "xarray_benchmark_rust",
+    authors: ["Daniel Gomez <da.gomez@samsung.com>"],
+    description: "Benchmark: XArray",
+    license: "GPL v2",
+    params: {
+        samples: usize {
+            default: 100,
+            description: "Timed runs per benchmark",
+        },
+        entries: usize {
+            default: 100_000,
+            description: "Entries stored per run",
+        },
+    },
+}

-- 
2.55.0


  parent reply	other threads:[~2026-09-23 21:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-23 21:00 [PATCH 0/3] Rust XArray Daniel Gomez
2026-09-23 21:00 ` [PATCH 1/3] rust: rxarray: add rust xarray support Daniel Gomez
2026-09-23 21:00 ` [PATCH 2/3] rust: kernel: add bench Daniel Gomez
2026-09-23 21:00 ` Daniel Gomez [this message]
2026-09-23 21:04 ` [PATCH 0/3] Rust XArray Daniel Gomez
2026-09-23 21:08 ` Daniel Almeida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260923-rxarray-next-v1-3-92eedf185649@samsung.com \
    --to=da.gomez@kernel.org \
    --cc=Julia.Lawall@inria.fr \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=corinn.tiffany@inria.fr \
    --cc=da.gomez@samsung.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=gost.dev@samsung.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=pstanner@redhat.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=willy@infradead.org \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.