Skip to content

Commit

Permalink
Add a public API to expose allocation reports
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed May 15, 2024
1 parent cbf5299 commit df10424
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/allocator/dedicated_block_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl SubAllocator for DedicatedBlockAllocator {
.name
.clone()
.unwrap_or_else(|| "<Unnamed Dedicated allocation>".to_owned()),
offset: 0,
size: self.size,
#[cfg(feature = "visualizer")]
backtrace: self.backtrace.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/allocator/free_list_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ impl SubAllocator for FreeListAllocator {
.name
.clone()
.unwrap_or_else(|| "<Unnamed FreeList allocation>".to_owned()),
offset: chunk.offset,
size: chunk.size,
#[cfg(feature = "visualizer")]
backtrace: chunk.backtrace.clone(),
Expand Down
23 changes: 19 additions & 4 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{backtrace::Backtrace, sync::Arc};
use std::{backtrace::Backtrace, ops::Range, sync::Arc};

use log::*;

Expand Down Expand Up @@ -30,13 +30,28 @@ impl AllocationType {
}

#[derive(Clone)]
pub(crate) struct AllocationReport {
pub(crate) name: String,
pub(crate) size: u64,
pub struct AllocationReport {
pub name: String,
pub offset: u64,
pub size: u64,
#[cfg(feature = "visualizer")]
pub(crate) backtrace: Arc<Backtrace>,
}

#[derive(Clone)]
pub struct MemoryBlockReport {
pub size: u64,
pub allocations: Range<usize>,
}

#[derive(Clone)]
pub struct AllocatorReport {
pub allocations: Vec<AllocationReport>,
pub blocks: Vec<MemoryBlockReport>,
pub total_allocated_bytes: u64,
pub total_reserved_bytes: u64,
}

#[cfg(feature = "visualizer")]
pub(crate) trait SubAllocatorBase: crate::visualizer::SubAllocatorVisualizer {}
#[cfg(not(feature = "visualizer"))]
Expand Down
28 changes: 28 additions & 0 deletions src/d3d12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{backtrace::Backtrace, fmt, sync::Arc};

use log::{debug, warn, Level};

use crate::allocator::AllocatorReport;
use windows::Win32::{
Foundation::E_OUTOFMEMORY,
Graphics::{Direct3D12::*, Dxgi::Common::DXGI_FORMAT},
Expand Down Expand Up @@ -1099,6 +1100,33 @@ impl Allocator {
Ok(())
}
}

pub fn genetate_report(&self) -> AllocatorReport {
let mut allocations = vec![];
let mut blocks = vec![];
let mut total_reserved_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_reserved_bytes += block.size;
let first_allocation = allocations.len();
allocations.extend(block.sub_allocator.report_allocations());
blocks.push(MemoryBlockReport {
size: block.size,
allocations: first_allocation..allocations.len(),
});
}
}

let total_allocated_bytes = allocations.iter().map(|report| report.size).sum();

AllocatorReport {
allocations,
blocks,
total_allocated_bytes,
total_reserved_bytes,
}
}
}

impl fmt::Debug for Allocator {
Expand Down
1 change: 1 addition & 0 deletions src/visualizer/allocation_reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub(crate) fn render_allocation_reports_ui(
name,
size,
backtrace,
..
} = alloc;

row.col(|ui| {
Expand Down
31 changes: 29 additions & 2 deletions src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use log::{debug, Level};

use super::allocator;
use crate::{
allocator::fmt_bytes, AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation,
Result,
allocator::{fmt_bytes, AllocatorReport, MemoryBlockReport},
AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -972,6 +972,33 @@ impl Allocator {
})
.map(|memory_type| memory_type.memory_type_index as _)
}

pub fn generate_report(&self) -> AllocatorReport {
let mut allocations = vec![];
let mut blocks = vec![];
let mut total_reserved_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_reserved_bytes += block.size;
let first_allocation = allocations.len();
allocations.extend(block.sub_allocator.report_allocations());
blocks.push(MemoryBlockReport {
size: block.size,
allocations: first_allocation..allocations.len(),
});
}
}

let total_allocated_bytes = allocations.iter().map(|report| report.size).sum();

AllocatorReport {
allocations,
blocks,
total_allocated_bytes,
total_reserved_bytes,
}
}
}

impl Drop for Allocator {
Expand Down

0 comments on commit df10424

Please sign in to comment.