use core::cell::RefCell;
#[cfg(feature = "std")]
use std::time::{Duration, Instant};
#[derive(Clone, Debug, Default)]
pub struct UsageUnit {
pub ops: u64,
pub bytes: u64,
}
#[derive(Clone, Debug)]
pub struct UsageInfo {
pub reads: UsageUnit,
pub writes: UsageUnit,
pub nodes_writes: UsageUnit,
pub overlay_writes: UsageUnit,
pub removed_nodes: UsageUnit,
pub cache_reads: UsageUnit,
pub modified_reads: UsageUnit,
pub memory: usize,
#[cfg(feature = "std")]
pub started: Instant,
#[cfg(feature = "std")]
pub span: Duration,
}
#[derive(Debug, Default, Clone)]
pub struct StateMachineStats {
pub reads_modified: RefCell<u64>,
pub bytes_read_modified: RefCell<u64>,
pub writes_overlay: RefCell<u64>,
pub bytes_writes_overlay: RefCell<u64>,
}
impl StateMachineStats {
pub fn add(&self, other: &StateMachineStats) {
*self.reads_modified.borrow_mut() += *other.reads_modified.borrow();
*self.bytes_read_modified.borrow_mut() += *other.bytes_read_modified.borrow();
*self.writes_overlay.borrow_mut() += *other.writes_overlay.borrow();
*self.bytes_writes_overlay.borrow_mut() += *other.bytes_writes_overlay.borrow();
}
}
impl UsageInfo {
pub fn empty() -> Self {
Self {
reads: UsageUnit::default(),
writes: UsageUnit::default(),
overlay_writes: UsageUnit::default(),
nodes_writes: UsageUnit::default(),
removed_nodes: UsageUnit::default(),
cache_reads: UsageUnit::default(),
modified_reads: UsageUnit::default(),
memory: 0,
#[cfg(feature = "std")]
started: Instant::now(),
#[cfg(feature = "std")]
span: Default::default(),
}
}
pub fn include_state_machine_states(&mut self, count: &StateMachineStats) {
self.modified_reads.ops += *count.reads_modified.borrow();
self.modified_reads.bytes += *count.bytes_read_modified.borrow();
self.overlay_writes.ops += *count.writes_overlay.borrow();
self.overlay_writes.bytes += *count.bytes_writes_overlay.borrow();
}
}
impl StateMachineStats {
pub fn tally_read_modified(&self, data_bytes: u64) {
*self.reads_modified.borrow_mut() += 1;
*self.bytes_read_modified.borrow_mut() += data_bytes;
}
pub fn tally_write_overlay(&self, data_bytes: u64) {
*self.writes_overlay.borrow_mut() += 1;
*self.bytes_writes_overlay.borrow_mut() += data_bytes;
}
}