1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#[cfg(feature = "std")]
use std::time::{Instant, Duration};
use sp_std::cell::RefCell;
#[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;
}
}