Skip to content

Commit

Permalink
vkd3d: Add queue profile for PSO compilation.
Browse files Browse the repository at this point in the history
Makes it easier to correlate stutters.

Signed-off-by: Hans-Kristian Arntzen <[email protected]>
  • Loading branch information
HansKristian-Work committed Feb 3, 2025
1 parent c6dd992 commit 8a7035a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
28 changes: 28 additions & 0 deletions libs/vkd3d/queue_timeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ void vkd3d_queue_timeline_trace_complete_present_wait(struct vkd3d_queue_timelin
vkd3d_queue_timeline_trace_free_index(trace, cookie.index);
}

void vkd3d_queue_timeline_trace_complete_pso_compile(struct vkd3d_queue_timeline_trace *trace,
struct vkd3d_queue_timeline_trace_cookie cookie, uint64_t pso_hash, const char *completion_kind)
{
const struct vkd3d_queue_timeline_trace_state *state;
double end_ts, start_ts;
unsigned int tid;

if (!trace->active || cookie.index == 0)
return;

state = &trace->state[cookie.index];
end_ts = (double)(vkd3d_get_current_time_ns() - trace->base_ts) * 1e-3;
start_ts = (double)(state->start_ts - trace->base_ts) * 1e-3;

tid = vkd3d_get_current_thread_id();
fprintf(trace->file, "{ \"name\": \"%016"PRIx64" %s\", \"ph\": \"X\", \"tid\": \"%u\", \"pid\": \"pso\", \"ts\": %f, \"dur\": %f },\n",
pso_hash, completion_kind, tid, start_ts, end_ts - start_ts);

vkd3d_queue_timeline_trace_free_index(trace, cookie.index);
}

void vkd3d_queue_timeline_trace_complete_blocking(struct vkd3d_queue_timeline_trace *trace,
struct vkd3d_queue_timeline_trace_cookie cookie, const char *pid)
{
Expand Down Expand Up @@ -443,6 +464,13 @@ vkd3d_queue_timeline_trace_register_present_wait(struct vkd3d_queue_timeline_tra
return vkd3d_queue_timeline_trace_register_generic_op(trace, VKD3D_QUEUE_TIMELINE_TRACE_STATE_TYPE_PRESENT_WAIT, str);
}

struct vkd3d_queue_timeline_trace_cookie
vkd3d_queue_timeline_trace_register_pso_compile(struct vkd3d_queue_timeline_trace *trace)
{
/* Details are filled in later. */
return vkd3d_queue_timeline_trace_register_generic_op(trace, VKD3D_QUEUE_TIMELINE_TRACE_STATE_TYPE_PSO_COMPILATION, "");
}

struct vkd3d_queue_timeline_trace_cookie
vkd3d_queue_timeline_trace_register_generic_region(struct vkd3d_queue_timeline_trace *trace, const char *tag)
{
Expand Down
74 changes: 74 additions & 0 deletions libs/vkd3d/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,7 @@ static HRESULT vkd3d_create_compute_pipeline(struct d3d12_pipeline_state *state,
VkPipelineCreationFeedbackCreateInfo feedback_info;
struct vkd3d_shader_debug_ring_spec_info spec_info;
struct vkd3d_shader_code_debug *spirv_code_debug;
struct vkd3d_queue_timeline_trace_cookie cookie;
VkPipelineCreationFeedbackEXT feedbacks[1];
VkComputePipelineCreateInfo pipeline_info;
VkPipelineCreationFeedbackEXT feedback;
Expand Down Expand Up @@ -3016,9 +3017,31 @@ static HRESULT vkd3d_create_compute_pipeline(struct d3d12_pipeline_state *state,
if (state->compute.code.meta.flags & VKD3D_SHADER_META_FLAG_DISABLE_OPTIMIZATIONS)
pipeline_info.flags |= VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT;

cookie = vkd3d_queue_timeline_trace_register_pso_compile(&device->queue_timeline_trace);

vr = VK_CALL(vkCreateComputePipelines(device->vk_device,
vk_cache, 1, &pipeline_info, NULL, &state->compute.vk_pipeline));

if (vkd3d_queue_timeline_trace_cookie_is_valid(cookie))
{
const char *kind;

if (vr == VK_SUCCESS)
{
if (pipeline_info.flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)
kind = "COMP IDENT OK";
else
kind = "COMP OK";
}
else if (vr == VK_PIPELINE_COMPILE_REQUIRED)
kind = "COMP MISS";
else
kind = "COMP ERR";

vkd3d_queue_timeline_trace_complete_pso_compile(&state->device->queue_timeline_trace,
cookie, vkd3d_pipeline_cache_compatibility_condense(&state->pipeline_cache_compat), kind);
}

if (vkd3d_config_flags & VKD3D_CONFIG_FLAG_PIPELINE_LIBRARY_LOG)
{
if (pipeline_info.flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)
Expand Down Expand Up @@ -3052,8 +3075,17 @@ static HRESULT vkd3d_create_compute_pipeline(struct d3d12_pipeline_state *state,
vk_remove_struct(&pipeline_info.stage,
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_MODULE_IDENTIFIER_CREATE_INFO_EXT);

cookie = vkd3d_queue_timeline_trace_register_pso_compile(&device->queue_timeline_trace);

vr = VK_CALL(vkCreateComputePipelines(device->vk_device,
vk_cache, 1, &pipeline_info, NULL, &state->compute.vk_pipeline));

if (vkd3d_queue_timeline_trace_cookie_is_valid(cookie))
{
const char *kind = vr == VK_SUCCESS ? "FALLBACK OK" : "FALLBACK ERR";
vkd3d_queue_timeline_trace_complete_pso_compile(&state->device->queue_timeline_trace,
cookie, vkd3d_pipeline_cache_compatibility_condense(&state->pipeline_cache_compat), kind);
}
}

TRACE("Called vkCreateComputePipelines.\n");
Expand Down Expand Up @@ -5777,6 +5809,7 @@ static VkResult d3d12_pipeline_state_link_pipeline_variant(struct d3d12_pipeline
struct d3d12_graphics_pipeline_state *graphics = &state->graphics;
struct vkd3d_fragment_output_pipeline_desc fragment_output_desc;
struct vkd3d_vertex_input_pipeline_desc vertex_input_desc;
struct vkd3d_queue_timeline_trace_cookie cookie;
VkPipelineLibraryCreateInfoKHR library_info;
VkGraphicsPipelineCreateInfo create_info;
VkPipeline vk_libraries[3];
Expand Down Expand Up @@ -5822,9 +5855,18 @@ static VkResult d3d12_pipeline_state_link_pipeline_variant(struct d3d12_pipeline
if (!key)
create_info.flags |= VK_PIPELINE_CREATE_LINK_TIME_OPTIMIZATION_BIT_EXT;

cookie = vkd3d_queue_timeline_trace_register_pso_compile(&state->device->queue_timeline_trace);

vr = VK_CALL(vkCreateGraphicsPipelines(state->device->vk_device,
vk_cache, 1, &create_info, NULL, vk_pipeline));

if (vkd3d_queue_timeline_trace_cookie_is_valid(cookie))
{
const char *kind = vr == VK_SUCCESS ? "LINK OK" : "LINK ERR";
vkd3d_queue_timeline_trace_complete_pso_compile(&state->device->queue_timeline_trace,
cookie, vkd3d_pipeline_cache_compatibility_condense(&state->pipeline_cache_compat), kind);
}

if (vr != VK_SUCCESS && vr != VK_PIPELINE_COMPILE_REQUIRED)
{
ERR("Failed to link pipeline variant, vr %d.\n", vr);
Expand All @@ -5851,6 +5893,7 @@ VkPipeline d3d12_pipeline_state_create_pipeline_variant(struct d3d12_pipeline_st
VkPipelineCreationFeedbackCreateInfoEXT feedback_info;
VkPipelineMultisampleStateCreateInfo multisample_info;
VkPipelineDynamicStateCreateInfo dynamic_create_info;
struct vkd3d_queue_timeline_trace_cookie cookie;
struct d3d12_device *device = state->device;
VkGraphicsPipelineCreateInfo pipeline_desc;
VkPipelineViewportStateCreateInfo vp_desc;
Expand Down Expand Up @@ -6021,8 +6064,30 @@ VkPipeline d3d12_pipeline_state_create_pipeline_variant(struct d3d12_pipeline_st
else
feedback_info.pipelineStageCreationFeedbackCount = 0;

cookie = vkd3d_queue_timeline_trace_register_pso_compile(&device->queue_timeline_trace);

vr = VK_CALL(vkCreateGraphicsPipelines(device->vk_device, vk_cache, 1, &pipeline_desc, NULL, &vk_pipeline));

if (vkd3d_queue_timeline_trace_cookie_is_valid(cookie))
{
const char *kind;

if (vr == VK_SUCCESS)
{
if (pipeline_desc.flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)
kind = "GFX IDENT OK";
else
kind = "GFX OK";
}
else if (vr == VK_PIPELINE_COMPILE_REQUIRED)
kind = "GFX MISS";
else
kind = "GFX ERR";

vkd3d_queue_timeline_trace_complete_pso_compile(&state->device->queue_timeline_trace,
cookie, vkd3d_pipeline_cache_compatibility_condense(&state->pipeline_cache_compat), kind);
}

if (vkd3d_config_flags & VKD3D_CONFIG_FLAG_PIPELINE_LIBRARY_LOG)
{
if (pipeline_desc.flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)
Expand All @@ -6048,7 +6113,16 @@ VkPipeline d3d12_pipeline_state_create_pipeline_variant(struct d3d12_pipeline_st
pipeline_desc.flags &= ~VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT;
/* Internal modules are known to be non-null now. */
pipeline_desc.pStages = state->graphics.stages;

cookie = vkd3d_queue_timeline_trace_register_pso_compile(&device->queue_timeline_trace);
vr = VK_CALL(vkCreateGraphicsPipelines(device->vk_device, vk_cache, 1, &pipeline_desc, NULL, &vk_pipeline));

if (vkd3d_queue_timeline_trace_cookie_is_valid(cookie))
{
const char *kind = vr == VK_SUCCESS ? "FALLBACK OK" : "FALLBACK ERR";
vkd3d_queue_timeline_trace_complete_pso_compile(&state->device->queue_timeline_trace,
cookie, vkd3d_pipeline_cache_compatibility_condense(&state->pipeline_cache_compat), kind);
}
}

TRACE("Completed vkCreateGraphicsPipelines.\n");
Expand Down
7 changes: 7 additions & 0 deletions libs/vkd3d/vkd3d_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -4880,6 +4880,9 @@ enum vkd3d_queue_timeline_trace_state_type
/* Time spent blocking in LowLatencySleep in user thread. */
VKD3D_QUEUE_TIMELINE_TRACE_STATE_TYPE_LOW_LATENCY_SLEEP,

/* PSO compilation */
VKD3D_QUEUE_TIMELINE_TRACE_STATE_TYPE_PSO_COMPILATION,

/* Reset() and Close() are useful instant events to see when command recording is happening and
* which threads do so. */
VKD3D_QUEUE_TIMELINE_TRACE_STATE_TYPE_COMMAND_LIST,
Expand Down Expand Up @@ -4956,6 +4959,8 @@ struct vkd3d_queue_timeline_trace_cookie
vkd3d_queue_timeline_trace_register_low_latency_sleep(struct vkd3d_queue_timeline_trace *trace,
uint64_t present_id);
struct vkd3d_queue_timeline_trace_cookie
vkd3d_queue_timeline_trace_register_pso_compile(struct vkd3d_queue_timeline_trace *trace);
struct vkd3d_queue_timeline_trace_cookie
vkd3d_queue_timeline_trace_register_sparse(struct vkd3d_queue_timeline_trace *trace, uint32_t num_tiles);
struct vkd3d_queue_timeline_trace_cookie
vkd3d_queue_timeline_trace_register_execute(struct vkd3d_queue_timeline_trace *trace,
Expand Down Expand Up @@ -4989,6 +4994,8 @@ void vkd3d_queue_timeline_trace_begin_execute_overhead(struct vkd3d_queue_timeli
struct vkd3d_queue_timeline_trace_cookie cookie);
void vkd3d_queue_timeline_trace_end_execute_overhead(struct vkd3d_queue_timeline_trace *trace,
struct vkd3d_queue_timeline_trace_cookie cookie);
void vkd3d_queue_timeline_trace_complete_pso_compile(struct vkd3d_queue_timeline_trace *trace,
struct vkd3d_queue_timeline_trace_cookie cookie, uint64_t pso_hash, const char *completion_kind);

struct vkd3d_address_binding_report_buffer_info
{
Expand Down

0 comments on commit 8a7035a

Please sign in to comment.