Skip to main content

addr_symbolizer/
stats.rs

1// Axel '0vercl0k' Souchet - April 21 2024
2//! This module contains the [`Stats`] type that is used to keep track of
3//! various statistics when symbolizing.
4use std::collections::HashMap;
5use std::fmt::Debug;
6
7use crate::pe::{PdbId, PeId};
8
9/// Various statistics that the symbolizer keeps track of.
10#[derive(Default, Clone, Debug)]
11pub struct Stats {
12    /// The number of addresses symbolized.
13    pub n_addrs: u64,
14    /// The PDB identifiers that have been downloaded & the associated file size
15    /// in bytes.
16    pub pdb_downloaded: HashMap<PdbId, u64>,
17    /// The PE identifiers that have been downloaded & the associated file size
18    /// in bytes.
19    pub pe_downloaded: HashMap<PeId, u64>,
20    /// The number of time the address cache was a hit.
21    pub cache_hit: u64,
22}
23
24impl Stats {
25    #[must_use]
26    pub fn did_download_pdb(&self, pdb_id: &PdbId) -> bool {
27        self.pdb_downloaded.contains_key(pdb_id)
28    }
29
30    #[must_use]
31    pub fn did_download_pe(&self, pe_id: &PeId) -> bool {
32        self.pe_downloaded.contains_key(pe_id)
33    }
34
35    #[must_use]
36    pub fn amount_downloaded(&self) -> u64 {
37        0u64.saturating_add(self.pe_downloaded.values().sum())
38            .saturating_add(self.pdb_downloaded.values().sum())
39    }
40
41    #[must_use]
42    pub fn pdb_download_count(&self) -> usize {
43        self.pdb_downloaded.len()
44    }
45
46    #[must_use]
47    pub fn pe_download_count(&self) -> usize {
48        self.pe_downloaded.len()
49    }
50
51    pub fn downloaded_pdb(&mut self, pdb_id: PdbId, size: u64) {
52        assert!(self.pdb_downloaded.insert(pdb_id, size).is_none());
53    }
54
55    pub fn downloaded_pe(&mut self, pe_id: PeId, size: u64) {
56        assert!(self.pe_downloaded.insert(pe_id, size).is_none());
57    }
58
59    pub fn addr_symbolized(&mut self) {
60        self.n_addrs += 1;
61    }
62
63    pub fn cache_hit(&mut self) {
64        self.cache_hit += 1;
65    }
66}