From ffa145bd70670ec6a569cf64088af67b3c207754 Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Fri, 15 Sep 2023 16:41:49 +0200 Subject: [PATCH 1/5] TRAINING: add MonitoredBuffer --- samples/rust/rust_counting.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/samples/rust/rust_counting.rs b/samples/rust/rust_counting.rs index 5477e37cf109f9..9ede169a6af9b3 100644 --- a/samples/rust/rust_counting.rs +++ b/samples/rust/rust_counting.rs @@ -4,7 +4,7 @@ //! Rust counting example for Kangrejos -use kernel::{new_mutex, prelude::*, sync::Mutex}; +use kernel::{init, new_mutex, prelude::*, sync::Mutex}; module! { type: RustCounting, @@ -46,6 +46,35 @@ impl NamedCounter { } } +#[pin_data] +struct MonitoredBuffer { + #[pin] + read: NamedCounter, + #[pin] + write: NamedCounter, + buf: Box<[u8; 100_000]>, +} + +impl MonitoredBuffer { + fn new() -> impl PinInit { + pin_init!(Self { + read <- NamedCounter::new("Read Counter", 0, i32::MAX), + write <- NamedCounter::new("Write Counter", 0, i32::MAX), + buf: Box::new([0; 100_000]), + }) + } + + fn set(&mut self, idx: usize, val: u8) { + self.write.increment(); + self.buf[idx] = val; + } + + fn get(&self, idx: usize) -> u8 { + self.read.increment(); + self.buf[idx] + } +} + impl kernel::Module for RustCounting { fn init(_module: &'static ThisModule) -> Result { Ok(Self) From 159fe8962fe4be65f135cd0c6e6a2d65a716aa84 Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Fri, 15 Sep 2023 16:43:05 +0200 Subject: [PATCH 2/5] TRAINING: make MonitoredBuffer Fallible --- samples/rust/rust_counting.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/rust/rust_counting.rs b/samples/rust/rust_counting.rs index 9ede169a6af9b3..296df8d2fe12dd 100644 --- a/samples/rust/rust_counting.rs +++ b/samples/rust/rust_counting.rs @@ -56,11 +56,11 @@ struct MonitoredBuffer { } impl MonitoredBuffer { - fn new() -> impl PinInit { - pin_init!(Self { + fn new() -> impl PinInit { + try_pin_init!(Self { read <- NamedCounter::new("Read Counter", 0, i32::MAX), write <- NamedCounter::new("Write Counter", 0, i32::MAX), - buf: Box::new([0; 100_000]), + buf: Box::try_new([0; 100_000])?, }) } From 01098491a1b7532400fbd994fa7e784385630c3c Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Fri, 15 Sep 2023 16:44:46 +0200 Subject: [PATCH 3/5] TRAINING: use Zeroable --- samples/rust/rust_counting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/rust/rust_counting.rs b/samples/rust/rust_counting.rs index 296df8d2fe12dd..97132773cf6685 100644 --- a/samples/rust/rust_counting.rs +++ b/samples/rust/rust_counting.rs @@ -60,7 +60,7 @@ impl MonitoredBuffer { try_pin_init!(Self { read <- NamedCounter::new("Read Counter", 0, i32::MAX), write <- NamedCounter::new("Write Counter", 0, i32::MAX), - buf: Box::try_new([0; 100_000])?, + buf: Box::init(init::zeroed())?, }) } From f011cc0651add228d25b303179746693066a5815 Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Fri, 15 Sep 2023 16:56:35 +0200 Subject: [PATCH 4/5] TRAINING: Zeroable derive --- samples/rust/rust_counting.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/samples/rust/rust_counting.rs b/samples/rust/rust_counting.rs index 97132773cf6685..4f6155e2a08bc8 100644 --- a/samples/rust/rust_counting.rs +++ b/samples/rust/rust_counting.rs @@ -75,8 +75,23 @@ impl MonitoredBuffer { } } +#[derive(Zeroable, Debug)] +struct LotsOfData { + a_buf: [u8; 128], + b_buf: [u8; 256], + mode: i32, + count: usize, + data: *mut u8, + len: usize, +} + impl kernel::Module for RustCounting { fn init(_module: &'static ThisModule) -> Result { + let data = Box::init(init!(LotsOfData { + mode: 8, + ..Zeroable::zeroed() + })); + pr_info!("{data:?}"); Ok(Self) } } From aaef0ce983a57ab3b4a19f5a8faf4909871bd38e Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Fri, 15 Sep 2023 06:05:10 +0200 Subject: [PATCH 5/5] TRAINING: add PinnedDrop --- samples/rust/rust_counting.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/samples/rust/rust_counting.rs b/samples/rust/rust_counting.rs index 4f6155e2a08bc8..93a0524420f4b0 100644 --- a/samples/rust/rust_counting.rs +++ b/samples/rust/rust_counting.rs @@ -4,7 +4,12 @@ //! Rust counting example for Kangrejos -use kernel::{init, new_mutex, prelude::*, sync::Mutex}; +use kernel::{ + init::{self, PinnedDrop}, + new_mutex, + prelude::*, + sync::Mutex, +}; module! { type: RustCounting, @@ -46,7 +51,7 @@ impl NamedCounter { } } -#[pin_data] +#[pin_data(PinnedDrop)] struct MonitoredBuffer { #[pin] read: NamedCounter, @@ -75,6 +80,17 @@ impl MonitoredBuffer { } } +#[pinned_drop] +impl PinnedDrop for MonitoredBuffer { + fn drop(self: Pin<&mut Self>) { + pr_info!( + "Monitored Buffer was read {} times and written to {} times.", + self.read.value(), + self.write.value() + ) + } +} + #[derive(Zeroable, Debug)] struct LotsOfData { a_buf: [u8; 128],