sp_state_machine/
stats.rs1use core::cell::RefCell;
21#[cfg(feature = "std")]
22use std::time::{Duration, Instant};
23
24#[derive(Clone, Debug, Default)]
26pub struct UsageUnit {
27 pub ops: u64,
29 pub bytes: u64,
31}
32
33#[derive(Clone, Debug)]
35pub struct UsageInfo {
36 pub reads: UsageUnit,
38 pub writes: UsageUnit,
40 pub nodes_writes: UsageUnit,
42 pub overlay_writes: UsageUnit,
45 pub removed_nodes: UsageUnit,
47 pub cache_reads: UsageUnit,
49 pub modified_reads: UsageUnit,
51 pub memory: usize,
53
54 #[cfg(feature = "std")]
55 pub started: Instant,
57 #[cfg(feature = "std")]
58 pub span: Duration,
60}
61
62#[derive(Debug, Default, Clone)]
65pub struct StateMachineStats {
66 pub reads_modified: RefCell<u64>,
70 pub bytes_read_modified: RefCell<u64>,
73 pub writes_overlay: RefCell<u64>,
76 pub bytes_writes_overlay: RefCell<u64>,
79}
80
81impl StateMachineStats {
82 pub fn add(&self, other: &StateMachineStats) {
84 *self.reads_modified.borrow_mut() += *other.reads_modified.borrow();
85 *self.bytes_read_modified.borrow_mut() += *other.bytes_read_modified.borrow();
86 *self.writes_overlay.borrow_mut() += *other.writes_overlay.borrow();
87 *self.bytes_writes_overlay.borrow_mut() += *other.bytes_writes_overlay.borrow();
88 }
89}
90
91impl UsageInfo {
92 pub fn empty() -> Self {
96 Self {
97 reads: UsageUnit::default(),
98 writes: UsageUnit::default(),
99 overlay_writes: UsageUnit::default(),
100 nodes_writes: UsageUnit::default(),
101 removed_nodes: UsageUnit::default(),
102 cache_reads: UsageUnit::default(),
103 modified_reads: UsageUnit::default(),
104 memory: 0,
105 #[cfg(feature = "std")]
106 started: Instant::now(),
107 #[cfg(feature = "std")]
108 span: Default::default(),
109 }
110 }
111 pub fn include_state_machine_states(&mut self, count: &StateMachineStats) {
113 self.modified_reads.ops += *count.reads_modified.borrow();
114 self.modified_reads.bytes += *count.bytes_read_modified.borrow();
115 self.overlay_writes.ops += *count.writes_overlay.borrow();
116 self.overlay_writes.bytes += *count.bytes_writes_overlay.borrow();
117 }
118}
119
120impl StateMachineStats {
121 pub fn tally_read_modified(&self, data_bytes: u64) {
123 *self.reads_modified.borrow_mut() += 1;
124 *self.bytes_read_modified.borrow_mut() += data_bytes;
125 }
126 pub fn tally_write_overlay(&self, data_bytes: u64) {
128 *self.writes_overlay.borrow_mut() += 1;
129 *self.bytes_writes_overlay.borrow_mut() += data_bytes;
130 }
131}