|
1 |
| -use std::{backtrace::Backtrace, sync::Arc}; |
| 1 | +use std::{backtrace::Backtrace, fmt, ops::Range, sync::Arc}; |
2 | 2 |
|
3 | 3 | use log::*;
|
4 | 4 |
|
@@ -29,20 +29,83 @@ impl AllocationType {
|
29 | 29 | }
|
30 | 30 | }
|
31 | 31 |
|
| 32 | +/// Describes an allocation in the [`AllocatorReport`]. |
32 | 33 | #[derive(Clone)]
|
33 |
| -pub(crate) struct AllocationReport { |
34 |
| - pub(crate) name: String, |
35 |
| - pub(crate) size: u64, |
| 34 | +pub struct AllocationReport { |
| 35 | + /// The name provided to the `allocate()` function. |
| 36 | + pub name: String, |
| 37 | + /// The offset in bytes of the allocation in its memory block. |
| 38 | + pub offset: u64, |
| 39 | + /// The size in bytes of the allocation. |
| 40 | + pub size: u64, |
36 | 41 | #[cfg(feature = "visualizer")]
|
37 | 42 | pub(crate) backtrace: Arc<Backtrace>,
|
38 | 43 | }
|
39 | 44 |
|
| 45 | +/// Describes a memory block in the [`AllocatorReport`]. |
| 46 | +#[derive(Clone)] |
| 47 | +pub struct MemoryBlockReport { |
| 48 | + /// The size in bytes of this memory block. |
| 49 | + pub size: u64, |
| 50 | + /// The range of allocations in [`AllocatorReport::allocations`] that are associated |
| 51 | + /// to this memory block. |
| 52 | + pub allocations: Range<usize>, |
| 53 | +} |
| 54 | + |
| 55 | +/// A report that can be generated for informational purposes using `Allocator::generate_report()`. |
| 56 | +#[derive(Clone)] |
| 57 | +pub struct AllocatorReport { |
| 58 | + /// All live allocations, sub-allocated from memory blocks. |
| 59 | + pub allocations: Vec<AllocationReport>, |
| 60 | + /// All memory blocks. |
| 61 | + pub blocks: Vec<MemoryBlockReport>, |
| 62 | + /// Sum of the memory used by all allocations, in bytes. |
| 63 | + pub total_allocated_bytes: u64, |
| 64 | + /// Sum of the memory reserved by all memory blocks including unallocated regions, in bytes. |
| 65 | + pub total_reserved_bytes: u64, |
| 66 | +} |
| 67 | + |
| 68 | +impl fmt::Debug for AllocationReport { |
| 69 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 70 | + let name = if !self.name.is_empty() { |
| 71 | + self.name.as_str() |
| 72 | + } else { |
| 73 | + "--" |
| 74 | + }; |
| 75 | + write!(f, "{name:?}: {}", fmt_bytes(self.size)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl fmt::Debug for AllocatorReport { |
| 80 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 81 | + let mut allocations = self.allocations.clone(); |
| 82 | + allocations.sort_by_key(|alloc| std::cmp::Reverse(alloc.size)); |
| 83 | + |
| 84 | + let max_num_allocations_to_print = f.precision().unwrap_or(usize::MAX); |
| 85 | + allocations.truncate(max_num_allocations_to_print); |
| 86 | + |
| 87 | + f.debug_struct("AllocatorReport") |
| 88 | + .field( |
| 89 | + "summary", |
| 90 | + &std::format_args!( |
| 91 | + "{} / {}", |
| 92 | + fmt_bytes(self.total_allocated_bytes), |
| 93 | + fmt_bytes(self.total_reserved_bytes) |
| 94 | + ), |
| 95 | + ) |
| 96 | + .field("blocks", &self.blocks.len()) |
| 97 | + .field("allocations", &self.allocations.len()) |
| 98 | + .field("largest", &allocations.as_slice()) |
| 99 | + .finish() |
| 100 | + } |
| 101 | +} |
| 102 | + |
40 | 103 | #[cfg(feature = "visualizer")]
|
41 | 104 | pub(crate) trait SubAllocatorBase: crate::visualizer::SubAllocatorVisualizer {}
|
42 | 105 | #[cfg(not(feature = "visualizer"))]
|
43 | 106 | pub(crate) trait SubAllocatorBase {}
|
44 | 107 |
|
45 |
| -pub(crate) trait SubAllocator: SubAllocatorBase + std::fmt::Debug + Sync + Send { |
| 108 | +pub(crate) trait SubAllocator: SubAllocatorBase + fmt::Debug + Sync + Send { |
46 | 109 | fn allocate(
|
47 | 110 | &mut self,
|
48 | 111 | size: u64,
|
@@ -82,8 +145,6 @@ pub(crate) trait SubAllocator: SubAllocatorBase + std::fmt::Debug + Sync + Send
|
82 | 145 | }
|
83 | 146 | }
|
84 | 147 |
|
85 |
| -pub(crate) const VISUALIZER_TABLE_MAX_ENTRY_NAME_LEN: usize = 40; |
86 |
| - |
87 | 148 | pub(crate) fn fmt_bytes(mut amount: u64) -> String {
|
88 | 149 | const SUFFIX: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
|
89 | 150 |
|
|
0 commit comments