Skip to content

Commit

Permalink
logging value strs, alloc scoped tool
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 26, 2024
1 parent e5c9000 commit ae87365
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 26 deletions.
3 changes: 2 additions & 1 deletion copper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }



6 changes: 6 additions & 0 deletions copper/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
println!(
"cargo:rustc-env=OUT_DIR={}",
std::env::var("OUT_DIR").unwrap()
);
}
33 changes: 30 additions & 3 deletions copper/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {}

Expand Down
9 changes: 4 additions & 5 deletions copper_log/build.rs
Original file line number Diff line number Diff line change
@@ -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()
);
}
8 changes: 4 additions & 4 deletions copper_log/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Value>::new();
let mut params_istring = Vec::<Value>::new();

};

let mut unnamed_params = vec![];
Expand Down Expand Up @@ -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);
};

Expand Down
1 change: 1 addition & 0 deletions copper_log_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
25 changes: 12 additions & 13 deletions copper_log_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
56 changes: 56 additions & 0 deletions copper_value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -40,6 +41,61 @@ pub enum Value {
Map(BTreeMap<Value, Value>),
Bytes(Vec<u8>),
}
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<H>(&self, hasher: &mut H)
Expand Down

0 comments on commit ae87365

Please sign in to comment.