referrerpolicy=no-referrer-when-downgrade

sp_state_machine/
stats.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Usage statistics for state db
19
20use core::cell::RefCell;
21#[cfg(feature = "std")]
22use std::time::{Duration, Instant};
23
24/// Measured count of operations and total bytes.
25#[derive(Clone, Debug, Default)]
26pub struct UsageUnit {
27	/// Number of operations.
28	pub ops: u64,
29	/// Number of bytes.
30	pub bytes: u64,
31}
32
33/// Usage statistics for state backend.
34#[derive(Clone, Debug)]
35pub struct UsageInfo {
36	/// Read statistics (total).
37	pub reads: UsageUnit,
38	/// Write statistics (total).
39	pub writes: UsageUnit,
40	/// Write trie nodes statistics.
41	pub nodes_writes: UsageUnit,
42	/// Write into cached state machine
43	/// change overlay.
44	pub overlay_writes: UsageUnit,
45	/// Removed trie nodes statistics.
46	pub removed_nodes: UsageUnit,
47	/// Cache read statistics.
48	pub cache_reads: UsageUnit,
49	/// Modified value read statistics.
50	pub modified_reads: UsageUnit,
51	/// Memory used.
52	pub memory: usize,
53
54	#[cfg(feature = "std")]
55	/// Moment at which current statistics has been started being collected.
56	pub started: Instant,
57	#[cfg(feature = "std")]
58	/// Timespan of the statistics.
59	pub span: Duration,
60}
61
62/// Accumulated usage statistics specific to state machine
63/// crate.
64#[derive(Debug, Default, Clone)]
65pub struct StateMachineStats {
66	/// Number of read query from runtime
67	/// that hit a modified value (in state
68	/// machine overlay).
69	pub reads_modified: RefCell<u64>,
70	/// Size in byte of read queries that
71	/// hit a modified value.
72	pub bytes_read_modified: RefCell<u64>,
73	/// Number of time a write operation
74	/// occurs into the state machine overlay.
75	pub writes_overlay: RefCell<u64>,
76	/// Size in bytes of the writes overlay
77	/// operation.
78	pub bytes_writes_overlay: RefCell<u64>,
79}
80
81impl StateMachineStats {
82	/// Accumulates some registered stats.
83	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	/// Empty statistics.
93	///
94	/// Means no data was collected.
95	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	/// Add collected state machine to this state.
112	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	/// Tally one read modified operation, of some length.
122	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	/// Tally one write overlay operation, of some length.
127	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}