From df7bd80d06587d173179df6e0e7c0bbe8bfcb726 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Tue, 21 Jun 2022 13:06:12 +0200 Subject: [PATCH] Start on the runtime console subscriber --- Cargo.toml | 2 +- runtime/console/Cargo.toml | 10 +++++ runtime/console/src/callsites.rs | 75 ++++++++++++++++++++++++++++++++ runtime/console/src/lib.rs | 10 +++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 runtime/console/Cargo.toml create mode 100644 runtime/console/src/callsites.rs create mode 100644 runtime/console/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index a50d54c..21b74fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -123,4 +123,4 @@ tempfile = "3.2" shadow-rs = "0.11" [workspace] -members = ["modules/*", "api"] +members = ["runtime/*", "modules/*", "api"] diff --git a/runtime/console/Cargo.toml b/runtime/console/Cargo.toml new file mode 100644 index 0000000..45d09e9 --- /dev/null +++ b/runtime/console/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "console" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +console-api = "0.3" +tracing = "0.1" \ No newline at end of file diff --git a/runtime/console/src/callsites.rs b/runtime/console/src/callsites.rs new file mode 100644 index 0000000..d10c395 --- /dev/null +++ b/runtime/console/src/callsites.rs @@ -0,0 +1,75 @@ +use std::fmt; +use std::fmt::Formatter; +use std::ptr; +use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; +use tracing::Metadata; + +pub(crate) struct Callsites { + array: [AtomicPtr>; MAX_CALLSITES], + len: AtomicUsize, +} + +impl Callsites { + pub(crate) fn insert(&self, callsite: &'static Metadata<'static>) { + if self.contains(callsite) { + return; + } + + let idx = self.len.fetch_add(1, Ordering::AcqRel); + if idx <= MAX_CALLSITES { + self.array[idx] + .compare_exchange( + ptr::null_mut(), + callsite as *const _ as *mut _, + Ordering::AcqRel, + Ordering::Acquire, + ) + .expect("would have clobbered callsite array"); + } else { + todo!("Need to spill callsite over into backup storage"); + } + } + + pub(crate) fn contains(&self, callsite: &'static Metadata<'static>) -> bool { + let mut idx = 0; + let mut end = self.len.load(Ordering::Acquire); + while { + for cs in &self.array[idx..end] { + let ptr = cs.load(Ordering::Acquire); + let meta = unsafe { ptr as *const _ as &'static Metadata<'static> }; + if meta.callsite() == callsite.callsite() { + return true; + } + } + + idx = end; + + // Check if new callsites were added since we iterated + end = self.len.load(Ordering::Acquire); + end > idx + } {} + + false + } +} + +impl Default for Callsites { + fn default() -> Self { + const NULLPTR: AtomicPtr<_> = AtomicPtr::new(ptr::null_mut()); + Self { + array: [NULLPTR; MAX_CALLSITES], + len: AtomicUsize::new(0), + } + } +} + +impl fmt::Debug for Callsites { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let len = self.len.load(Ordering::Acquire); + f.debug_struct("Callsites") + .field("MAX_CALLSITES", &MAX_CALLSITES) + .field("array", &&self.array[..len]) + .field("len", &len) + .finish() + } +} diff --git a/runtime/console/src/lib.rs b/runtime/console/src/lib.rs new file mode 100644 index 0000000..b02b962 --- /dev/null +++ b/runtime/console/src/lib.rs @@ -0,0 +1,10 @@ +mod callsites; + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +}