Skip to content

Commit dbac563

Browse files
authored
🖨️ Allocator breakdown fmt (#130)
* test debug_print_breakdown * remove copy-paste lines, code that actually compiles now * add d3d12 version, sort by size * remove unused self * move functions to allocator struct * modify function to return formatted result * replace fmt function with new version * writeln! * writeln! * apply suggestions * extend() * remove index * const * share const between backends
1 parent baba110 commit dbac563

File tree

4 files changed

+98
-26
lines changed

4 files changed

+98
-26
lines changed

src/allocator/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,21 @@ pub(crate) trait SubAllocator: SubAllocatorBase + std::fmt::Debug {
8686
self.allocated() == 0
8787
}
8888
}
89+
90+
pub(crate) const VISUALIZER_TABLE_MAX_ENTRY_NAME_LEN: usize = 40;
91+
92+
pub(crate) fn fmt_bytes(mut amount: u64) -> String {
93+
const SUFFIX: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
94+
95+
let mut idx = 0;
96+
let mut print_amount = amount as f64;
97+
loop {
98+
if amount < 1024 {
99+
return format!("{:.2} {}", print_amount, SUFFIX[idx]);
100+
}
101+
102+
print_amount = amount as f64 / 1024.0;
103+
amount /= 1024;
104+
idx += 1;
105+
}
106+
}

src/d3d12/mod.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![deny(clippy::unimplemented, clippy::unwrap_used, clippy::ok_expect)]
22

3+
use std::fmt;
4+
35
use log::{debug, Level};
46

57
use windows::Win32::{Foundation::E_OUTOFMEMORY, Graphics::Direct3D12::*};
@@ -72,7 +74,9 @@ pub use visualizer::AllocatorVisualizer;
7274
use super::allocator;
7375
use super::allocator::AllocationType;
7476

75-
use crate::{AllocationError, AllocatorDebugSettings, MemoryLocation, Result};
77+
use crate::{
78+
allocator::fmt_bytes, AllocationError, AllocatorDebugSettings, MemoryLocation, Result,
79+
};
7680

7781
/// [`ResourceCategory`] is used for supporting [`D3D12_RESOURCE_HEAP_TIER_1`].
7882
/// [`ResourceCategory`] will be ignored if device supports [`D3D12_RESOURCE_HEAP_TIER_2`].
@@ -536,7 +540,6 @@ impl MemoryType {
536540
}
537541
}
538542

539-
#[derive(Debug)]
540543
pub struct Allocator {
541544
device: ID3D12Device,
542545
debug_settings: AllocatorDebugSettings,
@@ -740,6 +743,44 @@ impl Allocator {
740743
}
741744
}
742745

746+
impl fmt::Debug for Allocator {
747+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
748+
let mut allocation_report = vec![];
749+
750+
for memory_type in &self.memory_types {
751+
for block in memory_type.memory_blocks.iter().flatten() {
752+
allocation_report.extend(block.sub_allocator.report_allocations())
753+
}
754+
}
755+
756+
let total_size_in_bytes = allocation_report.iter().map(|report| report.size).sum();
757+
758+
allocation_report.sort_by_key(|alloc| std::cmp::Reverse(alloc.size));
759+
760+
writeln!(
761+
f,
762+
"================================================================"
763+
)?;
764+
writeln!(
765+
f,
766+
"ALLOCATION BREAKDOWN ({})",
767+
fmt_bytes(total_size_in_bytes)
768+
)?;
769+
770+
for alloc in &allocation_report {
771+
writeln!(
772+
f,
773+
"{:max_len$.max_len$}\t- {}",
774+
alloc.name,
775+
fmt_bytes(alloc.size),
776+
max_len = allocator::VISUALIZER_TABLE_MAX_ENTRY_NAME_LEN,
777+
)?;
778+
}
779+
780+
Ok(())
781+
}
782+
}
783+
743784
impl Drop for Allocator {
744785
fn drop(&mut self) {
745786
if self.debug_settings.log_leaks_on_shutdown {

src/vulkan/mod.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use ash::vk;
1111
use log::{debug, warn, Level};
1212
use std::fmt;
1313

14-
use crate::{AllocationError, AllocatorDebugSettings, MemoryLocation, Result};
14+
use crate::{
15+
allocator::fmt_bytes, AllocationError, AllocatorDebugSettings, MemoryLocation, Result,
16+
};
1517

1618
#[derive(Clone, Debug)]
1719
pub struct AllocationCreateDesc<'a> {
@@ -453,12 +455,39 @@ pub struct Allocator {
453455

454456
impl fmt::Debug for Allocator {
455457
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
456-
f.debug_struct("Allocator")
457-
.field("memory_types", &self.memory_types)
458-
.field("memory_heaps", &self.memory_heaps)
459-
.field("buffer_image_granularity", &self.buffer_image_granularity)
460-
.field("debug_settings", &self.debug_settings)
461-
.finish()
458+
let mut allocation_report = vec![];
459+
460+
for memory_type in &self.memory_types {
461+
for block in memory_type.memory_blocks.iter().flatten() {
462+
allocation_report.extend(block.sub_allocator.report_allocations())
463+
}
464+
}
465+
466+
let total_size_in_bytes = allocation_report.iter().map(|report| report.size).sum();
467+
468+
allocation_report.sort_by_key(|alloc| std::cmp::Reverse(alloc.size));
469+
470+
writeln!(
471+
f,
472+
"================================================================"
473+
)?;
474+
writeln!(
475+
f,
476+
"ALLOCATION BREAKDOWN ({})",
477+
fmt_bytes(total_size_in_bytes)
478+
)?;
479+
480+
for alloc in &allocation_report {
481+
writeln!(
482+
f,
483+
"{:max_len$.max_len$}\t- {}",
484+
alloc.name,
485+
fmt_bytes(alloc.size),
486+
max_len = allocator::VISUALIZER_TABLE_MAX_ENTRY_NAME_LEN,
487+
)?;
488+
}
489+
490+
Ok(())
462491
}
463492
}
464493

src/vulkan/visualizer.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::new_without_default)]
22

33
use super::Allocator;
4-
use crate::allocator::resolve_backtrace;
4+
use crate::allocator::{fmt_bytes, resolve_backtrace};
55
use crate::visualizer::ColorScheme;
66
use log::error;
77

@@ -308,22 +308,6 @@ impl AllocatorVisualizer {
308308
total_size_in_bytes = allocation_report.iter().map(|report| report.size).sum();
309309
}
310310

311-
let suffix = ["B", "KB", "MB", "GB", "TB"];
312-
313-
let fmt_bytes = |mut amount: u64| -> String {
314-
let mut idx = 0;
315-
let mut print_amount = amount as f64;
316-
loop {
317-
if amount < 1024 {
318-
return format!("{:.2} {}", print_amount, suffix[idx]);
319-
}
320-
321-
print_amount = amount as f64 / 1024.0;
322-
amount /= 1024;
323-
idx += 1;
324-
}
325-
};
326-
327311
let mut window = imgui::Window::new(format!(
328312
"Allocation Breakdown ({})###allocation_breakdown_window",
329313
fmt_bytes(total_size_in_bytes)

0 commit comments

Comments
 (0)