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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use super::{Metrics, MetricsReport};
use std::collections::HashMap;
use ash::vk;
pub struct ConsoleMarkdownReport {
ash_instance: ash::Instance,
physical_device: vk::PhysicalDevice,
}
impl ConsoleMarkdownReport {
const BASE: u64 = 1024;
const UNITS: [&'static str; 4] = ["B", "KiB", "MiB", "GiB"];
pub fn new(
ash_instance: ash::Instance,
physical_device: vk::PhysicalDevice,
) -> Self {
Self {
ash_instance,
physical_device,
}
}
fn formatted_metrics_list(metrics: &Metrics) -> String {
indoc::formatdoc!(
"
{indent}| Metric Name | {header:<12} |
{indent}| -------------------------- | {underline:<12} |
{indent}| max concurrent allocations | {:<12} |
{indent}| total allocation count | {:<12} |
{indent}| leaked allocation count | {:<12} |
{indent}| mean allocation size | {:<12} |
{indent}| biggest allocation | {:<12} |
{indent}| smallest allocation | {:<12} |
",
metrics.max_concurrent_allocations,
metrics.total_allocations,
metrics.current_allocations,
Self::pretty_print_bytes(metrics.mean_allocation_byte_size),
Self::pretty_print_bytes(metrics.biggest_allocation),
Self::pretty_print_bytes(metrics.smallest_allocation),
header = "Value",
underline = "------------",
indent = " ",
)
}
fn heap_description(&self, memory_type_index: u32) -> String {
use ash::version::InstanceV1_0;
let properties = unsafe {
self.ash_instance
.get_physical_device_memory_properties(self.physical_device)
};
let memory_properties =
properties.memory_types[memory_type_index as usize];
indoc::formatdoc!(
"
### Heap {heap} - Memory Type Index {type_index}
Properties: {properties:?}
",
heap = memory_properties.heap_index,
type_index = memory_type_index,
properties = memory_properties.property_flags
)
}
fn pretty_print_bytes(byte_size: u64) -> String {
let order_of_magnitude = Self::order_of_magnitude(byte_size);
let divisor = Self::BASE.pow(order_of_magnitude as u32);
let full_print = format!("{:.3}", byte_size as f32 / divisor as f32);
let truncated: f32 = full_print.parse().unwrap();
format!(
"{size} {unit}",
size = truncated,
unit = Self::UNITS[order_of_magnitude]
)
}
fn order_of_magnitude(byte_size: u64) -> usize {
let mut order_of_magnitude = 0;
let mut remaining_precision = byte_size as f64;
while remaining_precision >= 1.0 {
remaining_precision /= 1024.0;
order_of_magnitude += 1;
}
if order_of_magnitude > 0 {
order_of_magnitude -= 1;
}
order_of_magnitude.min(Self::UNITS.len())
}
}
impl MetricsReport for ConsoleMarkdownReport {
fn render(
&self,
name: &str,
total: &Metrics,
metrics_by_type: &HashMap<u32, Metrics>,
) {
let mut report = indoc::formatdoc!(
"
# {name} - Memory Report Totals
{totals}
## Metrics By Memory Type Index
",
name = name,
totals = Self::formatted_metrics_list(total)
);
for (memory_type_index, metrics) in metrics_by_type {
report += indoc::formatdoc!(
"
{heap_description}
{metrics}
",
heap_description = self.heap_description(*memory_type_index),
metrics = Self::formatted_metrics_list(&metrics),
)
.as_ref();
}
log::info!("{}", report);
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
pub fn test_pretty_print_bytes() {
use ConsoleMarkdownReport as CMR;
assert_eq!(CMR::pretty_print_bytes(1), "1 B");
assert_eq!(CMR::pretty_print_bytes(1024), "1 KiB");
assert_eq!(CMR::pretty_print_bytes(1024 * 1024), "1 MiB");
assert_eq!(CMR::pretty_print_bytes(1024 * 1024 * 1024), "1 GiB");
assert_eq!(CMR::pretty_print_bytes(1000), "1000 B");
assert_eq!(CMR::pretty_print_bytes(1034), "1.01 KiB");
assert_eq!(CMR::pretty_print_bytes(2048), "2 KiB");
assert_eq!(CMR::pretty_print_bytes(4096), "4 KiB");
assert_eq!(CMR::pretty_print_bytes(34235), "33.433 KiB");
}
}