cfxcore/statistics/
mod.rs

1use crate::{consensus::ConsensusGraphStatistics, sync::SyncGraphStatistics};
2use metrics::{Gauge, GaugeUsize};
3use parking_lot::RwLock;
4use std::sync::Arc;
5lazy_static! {
6    static ref SYNC_INSERTED_HEADER_COUNT: Arc<dyn Gauge<usize>> =
7        GaugeUsize::register_with_group(
8            "graph_statistic",
9            "sync_graph_inserted_header_count"
10        );
11    static ref SYNC_INSERTED_BLOCK_COUNT: Arc<dyn Gauge<usize>> =
12        GaugeUsize::register_with_group(
13            "graph_statistic",
14            "sync_graph_inserted_block_count"
15        );
16    static ref CONSENSUS_INSERTED_BLOCK_COUNT: Arc<dyn Gauge<usize>> =
17        GaugeUsize::register_with_group(
18            "graph_statistic",
19            "consensus_graph_inserted_header_count"
20        );
21}
22
23pub type SharedStatistics = Arc<Statistics>;
24
25#[derive(Debug)]
26pub struct StatisticsInner {
27    pub sync_graph: SyncGraphStatistics,
28    pub consensus_graph: ConsensusGraphStatistics,
29}
30
31impl StatisticsInner {
32    pub fn new() -> Self {
33        StatisticsInner {
34            sync_graph: SyncGraphStatistics::new(),
35            consensus_graph: ConsensusGraphStatistics::new(),
36        }
37    }
38}
39
40pub struct Statistics {
41    pub inner: RwLock<StatisticsInner>,
42}
43
44impl Statistics {
45    pub fn new() -> Self {
46        Statistics {
47            inner: RwLock::new(StatisticsInner::new()),
48        }
49    }
50
51    pub fn inc_sync_graph_inserted_block_count(&self) {
52        let mut inner = self.inner.write();
53        inner.sync_graph.inserted_block_count += 1;
54        SYNC_INSERTED_BLOCK_COUNT.update(inner.sync_graph.inserted_block_count)
55    }
56
57    pub fn inc_sync_graph_inserted_header_count(&self) {
58        let mut inner = self.inner.write();
59        inner.sync_graph.inserted_header_count += 1;
60        SYNC_INSERTED_HEADER_COUNT
61            .update(inner.sync_graph.inserted_header_count)
62    }
63
64    pub fn inc_consensus_graph_processed_block_count(&self) {
65        self.inner.write().consensus_graph.processed_block_count += 1;
66    }
67
68    pub fn inc_consensus_graph_activated_block_count(&self) {
69        self.inner.write().consensus_graph.activated_block_count += 1;
70    }
71
72    pub fn set_consensus_graph_inserted_block_count(&self, count: usize) {
73        let mut inner = self.inner.write();
74        inner.consensus_graph.inserted_block_count = count;
75        CONSENSUS_INSERTED_BLOCK_COUNT
76            .update(inner.consensus_graph.inserted_block_count)
77    }
78
79    pub fn get_consensus_graph_processed_block_count(&self) -> usize {
80        self.inner.read().consensus_graph.processed_block_count
81    }
82
83    pub fn clear_sync_and_consensus_graph_statistics(&self) {
84        let mut inner = self.inner.write();
85        inner.sync_graph.clear();
86        inner.consensus_graph.clear();
87    }
88
89    pub fn log_statistics(&self) {
90        let inner = self.inner.read();
91        info!("Statistics: {:?}", *inner);
92    }
93}