Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add infrastructure for counting and reporting internal resources #5708

Merged
merged 7 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ ignored = ["cfg_aliases"]
[lib]

[features]
## Internally count resources and events for debugging purposes. If the counters
## feature is disabled, the counting infrastructure is removed from the build and
## the exposed counters always return 0.
counters = ["wgt/counters"]
nical marked this conversation as resolved.
Show resolved Hide resolved

## Log all API entry points at info instead of trace level.
api_log_info = []

Expand Down
15 changes: 15 additions & 0 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,21 @@ impl Global {
}
}

pub fn device_get_internal_counters<A: HalApi>(
&self,
device_id: DeviceId,
) -> wgt::InternalCounters {
let hub = A::hub(self);
if let Ok(device) = hub.devices.get(device_id) {
wgt::InternalCounters {
hal: device.get_hal_counters(),
core: wgt::CoreCounters {},
}
} else {
Default::default()
}
}

pub fn queue_drop<A: HalApi>(&self, queue_id: QueueId) {
profiling::scope!("Queue::drop");
api_log!("Queue::drop {queue_id:?}");
Expand Down
8 changes: 8 additions & 0 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ impl<A: HalApi> Device<A> {
snatch_guard: SnatchGuard,
) -> Result<(UserClosures, bool), WaitIdleError> {
profiling::scope!("Device::maintain");

let fence = fence_guard.as_ref().unwrap();
let last_done_index = if maintain.is_wait() {
let index_to_wait_for = match maintain {
Expand Down Expand Up @@ -3641,6 +3642,13 @@ impl<A: HalApi> Device<A> {
pub(crate) fn new_usage_scope(&self) -> UsageScope<'_, A> {
UsageScope::new_pooled(&self.usage_scopes, &self.tracker_indices)
}

pub fn get_hal_counters(&self) -> wgt::HalCounters {
self.raw
.as_ref()
.map(|raw| raw.get_internal_counters())
.unwrap_or_default()
}
}

impl<A: HalApi> Device<A> {
Expand Down
80 changes: 73 additions & 7 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl super::Device {
null_rtv_handle,
mem_allocator,
dxc_container,
counters: Default::default(),
})
}

Expand Down Expand Up @@ -377,6 +378,8 @@ impl crate::Device for super::Device {
unsafe { resource.SetName(cwstr.as_ptr()) };
}

self.counters.buffers.add(1);

Ok(super::Buffer {
resource,
size,
Expand All @@ -388,11 +391,14 @@ impl crate::Device for super::Device {
// Only happens when it's using the windows_rs feature and there's an allocation
if let Some(alloc) = buffer.allocation.take() {
super::suballocation::free_buffer_allocation(
self,
alloc,
// SAFETY: for allocations to exist, the allocator must exist
unsafe { self.mem_allocator.as_ref().unwrap_unchecked() },
);
}

self.counters.buffers.sub(1);
}

unsafe fn map_buffer(
Expand Down Expand Up @@ -459,6 +465,8 @@ impl crate::Device for super::Device {
unsafe { resource.SetName(cwstr.as_ptr()) };
}

self.counters.textures.add(1);

Ok(super::Texture {
resource,
format: desc.format,
Expand All @@ -473,11 +481,14 @@ impl crate::Device for super::Device {
unsafe fn destroy_texture(&self, mut texture: super::Texture) {
if let Some(alloc) = texture.allocation.take() {
super::suballocation::free_texture_allocation(
self,
alloc,
// SAFETY: for allocations to exist, the allocator must exist
unsafe { self.mem_allocator.as_ref().unwrap_unchecked() },
);
}

self.counters.textures.sub(1);
}

unsafe fn create_texture_view(
Expand All @@ -487,6 +498,8 @@ impl crate::Device for super::Device {
) -> Result<super::TextureView, DeviceError> {
let view_desc = desc.to_internal(texture);

self.counters.texture_views.add(1);

Ok(super::TextureView {
raw_format: view_desc.rtv_dsv_format,
aspects: view_desc.aspects,
Expand Down Expand Up @@ -583,6 +596,7 @@ impl crate::Device for super::Device {
},
})
}

unsafe fn destroy_texture_view(&self, view: super::TextureView) {
if view.handle_srv.is_some() || view.handle_uav.is_some() {
let mut pool = self.srv_uav_pool.lock();
Expand All @@ -605,6 +619,8 @@ impl crate::Device for super::Device {
pool.free_handle(handle);
}
}

self.counters.texture_views.sub(1);
}

unsafe fn create_sampler(
Expand Down Expand Up @@ -643,10 +659,14 @@ impl crate::Device for super::Device {
desc.lod_clamp.clone(),
);

self.counters.samplers.add(1);

Ok(super::Sampler { handle })
}

unsafe fn destroy_sampler(&self, sampler: super::Sampler) {
self.sampler_pool.lock().free_handle(sampler.handle);
self.counters.samplers.sub(1);
}

unsafe fn create_command_encoder(
Expand All @@ -663,6 +683,8 @@ impl crate::Device for super::Device {
unsafe { allocator.SetName(cwstr.as_ptr()) };
}

self.counters.command_encoders.add(1);

Ok(super::CommandEncoder {
allocator,
device: self.raw.clone(),
Expand All @@ -675,7 +697,10 @@ impl crate::Device for super::Device {
end_of_pass_timer_query: None,
})
}
unsafe fn destroy_command_encoder(&self, _encoder: super::CommandEncoder) {}

unsafe fn destroy_command_encoder(&self, _encoder: super::CommandEncoder) {
self.counters.command_encoders.sub(1);
}

unsafe fn create_bind_group_layout(
&self,
Expand All @@ -698,6 +723,8 @@ impl crate::Device for super::Device {
}
}

self.counters.bind_group_layouts.add(1);

let num_views = num_buffer_views + num_texture_views;
Ok(super::BindGroupLayout {
entries: desc.entries.to_vec(),
Expand All @@ -724,7 +751,10 @@ impl crate::Device for super::Device {
copy_counts: vec![1; num_views.max(num_samplers) as usize],
})
}
unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) {}

unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) {
self.counters.bind_group_layouts.sub(1);
}

unsafe fn create_pipeline_layout(
&self,
Expand Down Expand Up @@ -1063,6 +1093,8 @@ impl crate::Device for super::Device {
unsafe { raw.SetName(cwstr.as_ptr()) };
}

self.counters.pipeline_layouts.add(1);

Ok(super::PipelineLayout {
shared: super::PipelineLayoutShared {
signature: raw,
Expand All @@ -1081,7 +1113,10 @@ impl crate::Device for super::Device {
},
})
}
unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) {}

unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) {
self.counters.pipeline_layouts.sub(1);
}

unsafe fn create_bind_group(
&self,
Expand Down Expand Up @@ -1253,26 +1288,33 @@ impl crate::Device for super::Device {
None => None,
};

self.counters.bind_groups.add(1);

Ok(super::BindGroup {
handle_views,
handle_samplers,
dynamic_buffers,
})
}

unsafe fn destroy_bind_group(&self, group: super::BindGroup) {
if let Some(dual) = group.handle_views {
self.shared.heap_views.free_slice(dual);
}
if let Some(dual) = group.handle_samplers {
self.shared.heap_samplers.free_slice(dual);
}

self.counters.bind_groups.sub(1);
}

unsafe fn create_shader_module(
&self,
desc: &crate::ShaderModuleDescriptor,
shader: crate::ShaderInput,
) -> Result<super::ShaderModule, crate::ShaderError> {
self.counters.shader_modules.add(1);

let raw_name = desc.label.and_then(|label| ffi::CString::new(label).ok());
match shader {
crate::ShaderInput::Naga(naga) => Ok(super::ShaderModule { naga, raw_name }),
Expand All @@ -1282,6 +1324,7 @@ impl crate::Device for super::Device {
}
}
unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) {
self.counters.shader_modules.sub(1);
// just drop
}

Expand Down Expand Up @@ -1463,14 +1506,18 @@ impl crate::Device for super::Device {
unsafe { raw.SetName(cwstr.as_ptr()) };
}

self.counters.render_pipelines.add(1);

Ok(super::RenderPipeline {
raw,
layout: desc.layout.shared.clone(),
topology,
vertex_strides,
})
}
unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) {}
unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) {
self.counters.render_pipelines.sub(1);
}

unsafe fn create_compute_pipeline(
&self,
Expand Down Expand Up @@ -1502,12 +1549,17 @@ impl crate::Device for super::Device {
unsafe { raw.SetName(cwstr.as_ptr()) };
}

self.counters.compute_pipelines.add(1);

Ok(super::ComputePipeline {
raw,
layout: desc.layout.shared.clone(),
})
}
unsafe fn destroy_compute_pipeline(&self, _pipeline: super::ComputePipeline) {}

unsafe fn destroy_compute_pipeline(&self, _pipeline: super::ComputePipeline) {
self.counters.compute_pipelines.sub(1);
}

unsafe fn create_pipeline_cache(
&self,
Expand Down Expand Up @@ -1548,9 +1600,14 @@ impl crate::Device for super::Device {
unsafe { raw.SetName(cwstr.as_ptr()) };
}

self.counters.query_sets.add(1);

Ok(super::QuerySet { raw, raw_ty })
}
unsafe fn destroy_query_set(&self, _set: super::QuerySet) {}

unsafe fn destroy_query_set(&self, _set: super::QuerySet) {
self.counters.query_sets.sub(1);
}

unsafe fn create_fence(&self) -> Result<super::Fence, DeviceError> {
let mut raw = d3d12::Fence::null();
Expand All @@ -1565,9 +1622,14 @@ impl crate::Device for super::Device {
hr.into_device_result("Fence creation")?;
null_comptr_check(&raw)?;

self.counters.fences.add(1);

Ok(super::Fence { raw })
}
unsafe fn destroy_fence(&self, _fence: super::Fence) {}
unsafe fn destroy_fence(&self, _fence: super::Fence) {
self.counters.fences.sub(1);
}

unsafe fn get_fence_value(
&self,
fence: &super::Fence,
Expand Down Expand Up @@ -1711,4 +1773,8 @@ impl crate::Device for super::Device {
// Destroy a D3D12 resource as per-usual.
todo!()
}

fn get_internal_counters(&self) -> wgt::HalCounters {
self.counters.clone()
}
}
1 change: 1 addition & 0 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pub struct Device {
null_rtv_handle: descriptor::Handle,
mem_allocator: Option<Mutex<suballocation::GpuAllocatorWrapper>>,
dxc_container: Option<Arc<shader_compilation::DxcContainer>>,
counters: wgt::HalCounters,
}

unsafe impl Send for Device {}
Expand Down
Loading
Loading