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
//! The `ledger_metric_report_service` periodically reports ledger store metrics.

use {
    solana_ledger::blockstore::Blockstore,
    std::{
        string::ToString,
        sync::{
            atomic::{AtomicBool, Ordering},
            Arc,
        },
        thread::{self, Builder, JoinHandle},
        time::Duration,
    },
};

// Determines how often we report blockstore metrics under
// LedgerMetricReportService.  Note that there're other blockstore
// metrics that are reported outside LedgerMetricReportService.
const BLOCKSTORE_METRICS_REPORT_PERIOD_MILLIS: u64 = 10000;

pub struct LedgerMetricReportService {
    t_cf_metric: JoinHandle<()>,
}

impl LedgerMetricReportService {
    pub fn new(blockstore: Arc<Blockstore>, exit: &Arc<AtomicBool>) -> Self {
        let exit_signal = exit.clone();
        let t_cf_metric = Builder::new()
            .name("solRocksCfMtrcs".to_string())
            .spawn(move || loop {
                if exit_signal.load(Ordering::Relaxed) {
                    break;
                }
                thread::sleep(Duration::from_millis(
                    BLOCKSTORE_METRICS_REPORT_PERIOD_MILLIS,
                ));
                blockstore.submit_rocksdb_cf_metrics_for_all_cfs();
            })
            .unwrap();
        Self { t_cf_metric }
    }

    pub fn join(self) -> thread::Result<()> {
        self.t_cf_metric.join()
    }
}