perfetto: Add helpers for passing VkDebugUtilsObjectNameInfoEXT to perfetto.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22350>
This commit is contained in:
Emma Anholt 2025-04-07 14:20:40 -07:00 committed by Marge Bot
parent 55d788f434
commit b47a6a5418

View file

@ -21,11 +21,14 @@
* DEALINGS IN THE SOFTWARE.
*/
#include "vulkan/runtime/vk_object.h"
#include "perfetto.h"
#include "util/hash_table.h"
#include "util/perf/u_trace.h"
#include "util/ralloc.h"
#include "util/set.h"
/**
* Struct tracking state during a perfetto packet sequence
@ -50,13 +53,19 @@ class MesaRenderpassIncrementalState {
{
debug_markers = _mesa_hash_table_create(NULL, _mesa_hash_string,
_mesa_key_string_equal);
named_objects = _mesa_pointer_set_create(NULL);
}
~MesaRenderpassIncrementalState() { ralloc_free(debug_markers); }
~MesaRenderpassIncrementalState()
{
ralloc_free(debug_markers);
ralloc_free(named_objects);
}
bool was_cleared = true;
struct hash_table *debug_markers;
struct set *named_objects;
};
using perfetto::DataSource;
@ -176,6 +185,55 @@ class MesaRenderpassDataSource
}
}
void EmitSetDebugUtilsObjectNameEXT(TraceContext &ctx,
const struct vk_object_base *object)
{
if (object->object_name) {
auto packet = ctx.NewTracePacket();
// NOTE: Perfetto sorts events (at least approximately) by timestamp in
// the process of parsing. The debug names will be getting tracked in
// perfetto's hash tables in wall time-ish order (from the CPU), while
// references to them from GPU render stages will be happening later,
// possibly after an object is renamed from the CPU's perspective.
// This appears to be a limitation of perfetto's GPU event protocols.
packet->set_timestamp(perfetto::base::GetBootTimeNs().count());
packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_BOOTTIME);
auto api = packet->set_vulkan_api_event();
auto object_name = api->set_vk_debug_utils_object_name();
object_name->set_vk_device((uint64_t) (uintptr_t) object->device);
object_name->set_object((uint64_t) (uintptr_t) object);
object_name->set_object_type(object->type);
object_name->set_object_name(object->object_name);
_mesa_set_add(ctx.GetIncrementalState()->named_objects, object);
}
}
/* Call this from your driver's vkSetDebugUtilsObjectNameEXT implementation
*/
void
SetDebugUtilsObjectNameEXT(TraceContext &ctx,
const VkDebugUtilsObjectNameInfoEXT *pNameInfo)
{
EmitSetDebugUtilsObjectNameEXT(
ctx, vk_object_base_from_u64_handle(pNameInfo->objectHandle,
pNameInfo->objectType));
}
/* You may call this on any Vulkan object before you emit a trace that would
* reference that object, so that the debug object name can be reassociated
* with it if we've lost incremental state (or tracing just started after
* application launch).
*/
void RefreshSetDebugUtilsObjectNameEXT(TraceContext &ctx,
const struct vk_object_base *object)
{
if (!_mesa_set_search(ctx.GetIncrementalState()->named_objects, object))
EmitSetDebugUtilsObjectNameEXT(ctx, object);
}
private:
/* Hash table of application generated events (string -> iid) (use
* tctx.GetDataSourceLocked()->debug_marker_stage() to get a stage iid)