diff --git a/copper/Cargo.toml b/copper/Cargo.toml index 81f5ea273..802ea52c9 100644 --- a/copper/Cargo.toml +++ b/copper/Cargo.toml @@ -13,7 +13,8 @@ serde = { version = "1.0", features = ["derive"] } serde_derive = "1.0" uom = { version = "0.36.0", features = ["rational"] } ron = "0.8.1" - +copper-log = { path = "../copper_log" } +copper-log-runtime = { path = "../copper_log_runtime" } diff --git a/copper/build.rs b/copper/build.rs new file mode 100644 index 000000000..4416602e7 --- /dev/null +++ b/copper/build.rs @@ -0,0 +1,6 @@ +fn main() { + println!( + "cargo:rustc-env=OUT_DIR={}", + std::env::var("OUT_DIR").unwrap() + ); +} diff --git a/copper/src/monitoring.rs b/copper/src/monitoring.rs index 5474d9e81..4641ce8ba 100644 --- a/copper/src/monitoring.rs +++ b/copper/src/monitoring.rs @@ -4,7 +4,11 @@ use crate::CuResult; use std::alloc::{GlobalAlloc, Layout, System}; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Once; + +use copper_log::debug; + +#[global_allocator] +pub static GLOBAL: CountingAllocator = CountingAllocator::new(); pub struct CountingAllocator { allocated: AtomicUsize, @@ -48,8 +52,31 @@ unsafe impl GlobalAlloc for CountingAllocator { } } -#[global_allocator] -pub static GLOBAL: CountingAllocator = CountingAllocator::new(); +pub struct ScopedAllocCounter { + bf_allocated: usize, + bf_deallocated: usize, +} + +impl ScopedAllocCounter { + pub fn new() -> Self { + ScopedAllocCounter { + bf_allocated: GLOBAL.get_allocated(), + bf_deallocated: GLOBAL.get_deallocated(), + } + } +} + +impl Drop for ScopedAllocCounter { + fn drop(&mut self) { + let allocated = GLOBAL.get_allocated() - self.bf_allocated; + let deallocated = GLOBAL.get_deallocated() - self.bf_deallocated; + debug!( + "Allocations: +{}B -{}B", + allocated = allocated, + deallocated = deallocated, + ); + } +} pub struct MonitoringTask {} diff --git a/copper_log/build.rs b/copper_log/build.rs index ca3298467..4416602e7 100644 --- a/copper_log/build.rs +++ b/copper_log/build.rs @@ -1,7 +1,6 @@ -use std::env; - fn main() { - let out_dir = env::var("OUT_DIR").unwrap(); - println!("cargo:rustc-env=OUT_DIR={}", out_dir); - println!("cargo:rustc-env=RUSTFLAGS=--cfg procmacro2_semver_exempt"); + println!( + "cargo:rustc-env=OUT_DIR={}", + std::env::var("OUT_DIR").unwrap() + ); } diff --git a/copper_log/src/macros.rs b/copper_log/src/macros.rs index e6f288207..4b9a738aa 100644 --- a/copper_log/src/macros.rs +++ b/copper_log/src/macros.rs @@ -33,10 +33,8 @@ pub fn debug(input: TokenStream) -> TokenStream { use copper_log_runtime::ANONYMOUS; let msg = #msg; let index = #index; - println!("{} -> [{}]", index, msg); let mut params = Vec::::new(); let mut params_istring = Vec::::new(); - }; let mut unnamed_params = vec![]; @@ -69,9 +67,11 @@ pub fn debug(input: TokenStream) -> TokenStream { params.push(param); } }); - let postfix = quote! { - let packed_value = Value::Seq(vec![to_value(index).unwrap(), Value::Seq(params_istring), Value::Seq(params)]); + let vparams = Value::Seq(params); + // to do add conditional + println!("{} {}", msg, &vparams); + let packed_value = Value::Seq(vec![to_value(index).unwrap(), Value::Seq(params_istring), vparams]); copper_log_runtime::log(packed_value); }; diff --git a/copper_log_test/Cargo.toml b/copper_log_test/Cargo.toml index b45d7276b..9c1d3fd6c 100644 --- a/copper_log_test/Cargo.toml +++ b/copper_log_test/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +copper = { path = "../copper" } copper-log = { path = "../copper_log" } copper-log-runtime = { path = "../copper_log_runtime" } copper-value = { path = "../copper_value" } diff --git a/copper_log_test/src/main.rs b/copper_log_test/src/main.rs index d8be1c806..68adc8ac6 100644 --- a/copper_log_test/src/main.rs +++ b/copper_log_test/src/main.rs @@ -10,19 +10,18 @@ fn main() { a: i32, b: i32, } - let t = Test { a: 2, b: 3 }; - - let v = to_value(t).unwrap(); - - println!("{:?}", v); - let mytuple = (1, "toto", 3.34f64, true, 'a'); - let v = to_value(mytuple).unwrap(); - println!("{:?}", v); - debug!("Just a string"); - debug!("anonymous param constants {} {}", 3u16, 2u8); - debug!("named param constants {} {}", a = 3, b = 2); - debug!("mixed named param constants, {} {} {}", a = 3, 54, b = 2); - rt.close(); + { + let hop = copper::monitoring::ScopedAllocCounter::new(); + let gigantic_vec = vec![0u8; 1_000_000]; + debug!("Just a string"); + debug!("anonymous param constants {} {}", 3u16, 2u8); + debug!("named param constants {} {}", a = 3, b = 2); + debug!("mixed named param constants, {} {} {}", a = 3, 54, b = 2); + debug!("complex tuple", mytuple); + debug!("Struct", Test { a: 3, b: 4 }); + debug!("u8", gigantic_vec[999999]); + } debug!(" AFTER CLOSE {} ", "AFTER CLOSE"); + rt.close(); } diff --git a/copper_value/src/lib.rs b/copper_value/src/lib.rs index 6f788b97a..5fc995d9e 100644 --- a/copper_value/src/lib.rs +++ b/copper_value/src/lib.rs @@ -4,6 +4,7 @@ use ordered_float::OrderedFloat; use serde::Deserialize; use std::cmp::Ordering; use std::collections::BTreeMap; +use std::fmt::{Display, Formatter}; use std::hash::{Hash, Hasher}; pub use de::*; @@ -40,6 +41,61 @@ pub enum Value { Map(BTreeMap), Bytes(Vec), } +impl Display for Value { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Value::Bool(v) => write!(f, "{}", v), + Value::U8(v) => write!(f, "{}", v), + Value::U16(v) => write!(f, "{}", v), + Value::U32(v) => write!(f, "{}", v), + Value::U64(v) => write!(f, "{}", v), + Value::I8(v) => write!(f, "{}", v), + Value::I16(v) => write!(f, "{}", v), + Value::I32(v) => write!(f, "{}", v), + Value::I64(v) => write!(f, "{}", v), + Value::F32(v) => write!(f, "{}", v), + Value::F64(v) => write!(f, "{}", v), + Value::Char(v) => write!(f, "{}", v), + Value::String(v) => write!(f, "{}", v), + Value::Unit => write!(f, "()"), + Value::Option(v) => match v { + Some(v) => write!(f, "Some({})", v), + None => write!(f, "None"), + }, + Value::Newtype(v) => write!(f, "Newtype({})", v), + Value::Seq(v) => { + write!(f, "[")?; + for (i, v) in v.iter().enumerate() { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "{}", v)?; + } + write!(f, "]") + } + Value::Map(v) => { + write!(f, "{{")?; + for (i, (k, v)) in v.iter().enumerate() { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "{}: {}", k, v)?; + } + write!(f, "}}") + } + Value::Bytes(v) => { + write!(f, "[")?; + for (i, b) in v.iter().enumerate() { + if i > 0 { + write!(f, " ")?; + } + write!(f, "{:02x}", b)?; + } + write!(f, "]") + } + } + } +} impl Hash for Value { fn hash(&self, hasher: &mut H)