From da21313122de3a431ea716e7f42818febcb99453 Mon Sep 17 00:00:00 2001 From: Lucas Farias Date: Tue, 20 May 2025 11:20:20 -0300 Subject: [PATCH] Add more metrics to `MeshAllocatorDiagnosticPlugin` --- .../diagnostic/mesh_allocator_diagnostic_plugin.rs | 12 ++++++++++++ crates/bevy_render/src/mesh/allocator.rs | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/crates/bevy_render/src/diagnostic/mesh_allocator_diagnostic_plugin.rs b/crates/bevy_render/src/diagnostic/mesh_allocator_diagnostic_plugin.rs index 7a190b8e44..f8ebae1414 100644 --- a/crates/bevy_render/src/diagnostic/mesh_allocator_diagnostic_plugin.rs +++ b/crates/bevy_render/src/diagnostic/mesh_allocator_diagnostic_plugin.rs @@ -12,12 +12,17 @@ const MESH_ALLOCATOR_SLABS: DiagnosticPath = DiagnosticPath::const_new("mesh_all const MESH_ALLOCATOR_SLABS_SIZE: DiagnosticPath = DiagnosticPath::const_new("mesh_allocator_slabs_size"); +/// Number of meshes allocated into slabs +const MESH_ALLOCATOR_ALLOCATIONS: DiagnosticPath = + DiagnosticPath::const_new("mesh_allocator_allocations"); + pub struct MeshAllocatorDiagnosticPlugin; impl Plugin for MeshAllocatorDiagnosticPlugin { fn build(&self, app: &mut bevy_app::App) { app.register_diagnostic(Diagnostic::new(MESH_ALLOCATOR_SLABS).with_suffix(" slabs")) .register_diagnostic(Diagnostic::new(MESH_ALLOCATOR_SLABS_SIZE).with_suffix(" bytes")) + .register_diagnostic(Diagnostic::new(MESH_ALLOCATOR_ALLOCATIONS).with_suffix(" meshes")) .init_resource::() .add_systems(PreUpdate, add_mesh_allocator_measurement); @@ -31,6 +36,7 @@ impl Plugin for MeshAllocatorDiagnosticPlugin { struct MeshAllocatorMeasurements { slabs: AtomicUsize, slabs_size: AtomicU64, + allocations: AtomicUsize, } fn add_mesh_allocator_measurement( @@ -43,6 +49,9 @@ fn add_mesh_allocator_measurement( diagnostics.add_measurement(&MESH_ALLOCATOR_SLABS_SIZE, || { measurements.slabs_size.load(Ordering::Relaxed) as f64 }); + diagnostics.add_measurement(&MESH_ALLOCATOR_ALLOCATIONS, || { + measurements.allocations.load(Ordering::Relaxed) as f64 + }); } fn measure_allocator( @@ -55,4 +64,7 @@ fn measure_allocator( measurements .slabs_size .store(allocator.slabs_size(), Ordering::Relaxed); + measurements + .allocations + .store(allocator.allocations(), Ordering::Relaxed); } diff --git a/crates/bevy_render/src/mesh/allocator.rs b/crates/bevy_render/src/mesh/allocator.rs index 3a4f684e72..66ad6eda99 100644 --- a/crates/bevy_render/src/mesh/allocator.rs +++ b/crates/bevy_render/src/mesh/allocator.rs @@ -424,6 +424,10 @@ impl MeshAllocator { self.slabs.iter().map(|slab| slab.1.buffer_size()).sum() } + pub fn allocations(&self) -> usize { + self.mesh_id_to_index_slab.len() + } + /// Given a slab and a mesh with data located with it, returns the buffer /// and range of that mesh data within the slab. fn mesh_slice_in_slab(