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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::graphics::vulkan::device_allocator::Allocation;

#[derive(Debug, Copy, Clone)]
pub struct Metrics {
    pub total_allocations: u32,
    pub max_concurrent_allocations: u32,
    pub current_allocations: u32,
    pub mean_allocation_byte_size: u64,
    pub biggest_allocation: u64,
    pub smallest_allocation: u64,
}

impl Default for Metrics {
    /// Create an empty metrics object with all counters set to zero or their
    /// reasonable defaults.
    fn default() -> Self {
        Self {
            total_allocations: 0,
            max_concurrent_allocations: 0,
            current_allocations: 0,
            mean_allocation_byte_size: 0,
            biggest_allocation: 0,
            smallest_allocation: u64::MAX,
        }
    }
}

impl Metrics {
    /// Update counters within the metrics data structure in response to a new
    /// allocation being acquired.
    pub fn measure_alloctaion(&mut self, allocation: &Allocation) {
        self.current_allocations += 1;

        // weighted average of allocation sizes
        self.mean_allocation_byte_size = ((self.mean_allocation_byte_size
            * self.total_allocations as u64)
            + allocation.byte_size)
            / (self.total_allocations + 1) as u64;

        self.total_allocations += 1;

        self.max_concurrent_allocations = self
            .max_concurrent_allocations
            .max(self.current_allocations);
        self.biggest_allocation =
            self.biggest_allocation.max(allocation.byte_size);
        self.smallest_allocation =
            self.smallest_allocation.min(allocation.byte_size);
    }

    /// Update counters within the metrics data structure in response to an
    /// allocation being freed.
    pub fn measure_free(&mut self) {
        self.current_allocations -= 1;
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_mean_allocation_size() {
        let mut metrics = Metrics::default();
        metrics.measure_alloctaion(&allocation_with_size(100));
        metrics.measure_alloctaion(&allocation_with_size(50));
        metrics.measure_alloctaion(&allocation_with_size(75));

        assert_eq!(metrics.mean_allocation_byte_size, 75);
    }

    #[test]
    fn test_allocation_counters() {
        let mut metrics = Metrics::default();
        metrics.measure_alloctaion(&allocation_with_size(100));
        metrics.measure_alloctaion(&allocation_with_size(50));
        metrics.measure_alloctaion(&allocation_with_size(75));
        metrics.measure_free();

        assert_eq!(metrics.current_allocations, 2);
        assert_eq!(metrics.smallest_allocation, 50);
        assert_eq!(metrics.biggest_allocation, 100);
        assert_eq!(metrics.max_concurrent_allocations, 3);
    }

    fn allocation_with_size(size: u64) -> Allocation {
        let mut allocation = Allocation::null();
        allocation.byte_size = size;
        allocation
    }
}