Files
a0_basic_app
a1_vehicle
a2_async_sim
ab_glyph
ab_glyph_rasterizer
adler
adler32
agents
aho_corasick
anyhow
approx
aquamarine
ash
atty
bitflags
bytemuck
byteorder
cache_padded
cfg_if
chrono
color_quant
crc32fast
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_utils
deflate
draw2d
either
flexi_logger
generic_array
gif
glfw
glfw_sys
glob
image
indoc
itertools
jpeg_decoder
lazy_static
libc
libloading
log
matrixmultiply
memchr
memoffset
miniz_oxide
nalgebra
base
geometry
linalg
third_party
num_complex
num_cpus
num_integer
num_iter
num_rational
num_traits
owned_ttf_parser
paste
png
proc_macro2
proc_macro_error
proc_macro_error_attr
quote
raw_window_handle
rawpointer
rayon
rayon_core
regex
regex_syntax
scoped_threadpool
scopeguard
semver
semver_parser
serde
serde_derive
simba
smawk
spin_sleep
syn
terminal_size
textwrap
thiserror
thiserror_impl
tiff
time
triple_buffer
ttf_parser
typenum
unicode_width
unicode_xid
unindent
vk_sys
weezl
yansi
 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
    }
}