pbrt_r3/core/stats/
stat_reporter.rs

1#[cfg(feature = "stats")]
2mod _impl {
3    use super::super::stats_accumlator::*;
4    use crate::core::options::PbrtOptions;
5
6    use std::sync::Arc;
7    use std::sync::Mutex;
8    use std::sync::RwLock;
9
10    pub trait StatReporter: Send + Sync {
11        fn report(&self, accum: &mut StatsAccumulator);
12        fn clear(&mut self);
13    }
14
15    pub type StatReporterRef = Arc<RwLock<dyn StatReporter>>;
16
17    static REGISTER_REPORTERS: Mutex<Vec<StatReporterRef>> = Mutex::new(Vec::new());
18
19    pub fn init_stats() {}
20
21    pub fn register_stat_reporter(reporter: StatReporterRef) {
22        let options = PbrtOptions::get();
23        if !options.stats {
24            return;
25        }
26        let mut reporters = REGISTER_REPORTERS.lock().unwrap();
27        reporters.push(reporter);
28    }
29
30    pub fn print_stats() {
31        let options = PbrtOptions::get();
32        if !options.stats {
33            return;
34        }
35        let mut accum = StatsAccumulator::new();
36        let reporters = REGISTER_REPORTERS.lock().unwrap();
37        for reporter in reporters.iter() {
38            let reporter = reporter.read().unwrap();
39            reporter.report(&mut accum);
40        }
41        println!("{}", accum);
42    }
43
44    pub fn clear_stats() {
45        let reporters = REGISTER_REPORTERS.lock().unwrap();
46        for reporter in reporters.iter() {
47            let mut reporter = reporter.write().unwrap();
48            reporter.clear();
49        }
50    }
51}
52
53#[cfg(not(feature = "stats"))]
54mod _impl {
55    pub fn init_stats() {
56        log::warn!("Stats is not enabled.");
57    }
58    pub fn print_stats() {}
59    pub fn clear_stats() {}
60}
61
62pub use _impl::*;