radv/rgp: Use common trace trigger
authorKonstantin Seurer <konstantin.seurer@gmail.com>
Sun, 8 Jan 2023 20:29:33 +0000 (21:29 +0100)
committerMarge Bot <emma+marge@anholt.net>
Tue, 27 Jun 2023 06:25:56 +0000 (06:25 +0000)
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20573>

src/amd/vulkan/layers/radv_sqtt_layer.c
src/amd/vulkan/radv_device.c
src/amd/vulkan/radv_instance.c
src/amd/vulkan/radv_physical_device.c
src/amd/vulkan/radv_private.h
src/amd/vulkan/radv_sqtt.c

index 7ea88f3..66af62d 100644 (file)
@@ -521,20 +521,19 @@ radv_describe_pipeline_bind(struct radv_cmd_buffer *cmd_buffer, VkPipelineBindPo
    radv_emit_sqtt_userdata(cmd_buffer, &marker, sizeof(marker) / 4);
 }
 
-/* TODO: Improve the way to trigger capture (overlay, etc). */
 static void
 radv_handle_sqtt(VkQueue _queue)
 {
    RADV_FROM_HANDLE(radv_queue, queue, _queue);
-   static bool sqtt_enabled = false;
-   static uint64_t num_frames = 0;
-   bool resize_trigger = false;
 
-   if (sqtt_enabled) {
+   bool trigger = queue->device->sqtt_triggered;
+   queue->device->sqtt_triggered = false;
+
+   if (queue->device->sqtt_enabled) {
       struct ac_sqtt_trace sqtt_trace = {0};
 
       radv_end_sqtt(queue);
-      sqtt_enabled = false;
+      queue->device->sqtt_enabled = false;
 
       /* TODO: Do something better than this whole sync. */
       queue->device->vk.dispatch_table.QueueWaitIdle(_queue);
@@ -551,48 +550,26 @@ radv_handle_sqtt(VkQueue _queue)
          /* Trigger a new capture if the driver failed to get
           * the trace because the buffer was too small.
           */
-         resize_trigger = true;
+         trigger = true;
       }
 
       /* Clear resources used for this capture. */
       radv_reset_sqtt_trace(queue->device);
    }
 
-   if (!sqtt_enabled) {
-      bool frame_trigger = num_frames == queue->device->sqtt.start_frame;
-      bool file_trigger = false;
-#ifndef _WIN32
-      if (queue->device->sqtt.trigger_file && access(queue->device->sqtt.trigger_file, W_OK) == 0) {
-         if (unlink(queue->device->sqtt.trigger_file) == 0) {
-            file_trigger = true;
-         } else {
-            /* Do not enable tracing if we cannot remove the file,
-             * because by then we'll trace every frame ... */
-            fprintf(stderr, "RADV: could not remove thread trace trigger file, ignoring\n");
-         }
+   if (trigger) {
+      if (ac_check_profile_state(&queue->device->physical_device->rad_info)) {
+         fprintf(stderr, "radv: Canceling RGP trace request as a hang condition has been "
+                         "detected. Force the GPU into a profiling mode with e.g. "
+                         "\"echo profile_peak  > "
+                         "/sys/class/drm/card0/device/power_dpm_force_performance_level\"\n");
+         return;
       }
-#endif
-
-      if (frame_trigger || file_trigger || resize_trigger) {
-         if (ac_check_profile_state(&queue->device->physical_device->rad_info)) {
-            fprintf(stderr, "radv: Canceling RGP trace request as a hang condition has been "
-                            "detected. Force the GPU into a profiling mode with e.g. "
-                            "\"echo profile_peak  > "
-                            "/sys/class/drm/card0/device/power_dpm_force_performance_level\"\n");
-            return;
-         }
 
-         /* Sample CPU/GPU clocks before starting the trace. */
-         if (!radv_sqtt_sample_clocks(queue->device)) {
-            fprintf(stderr, "radv: Failed to sample clocks\n");
-         }
-
-         radv_begin_sqtt(queue);
-         assert(!sqtt_enabled);
-         sqtt_enabled = true;
-      }
+      radv_begin_sqtt(queue);
+      assert(!queue->device->sqtt_enabled);
+      queue->device->sqtt_enabled = true;
    }
-   num_frames++;
 }
 
 VKAPI_ATTR VkResult VKAPI_CALL
index 14db8d6..4922d4d 100644 (file)
@@ -83,9 +83,10 @@ typedef void *drmDevicePtr;
 #endif
 
 static bool
-radv_spm_trace_enabled()
+radv_spm_trace_enabled(struct radv_instance *instance)
 {
-   return radv_sqtt_enabled() && debug_get_bool_option("RADV_THREAD_TRACE_CACHE_COUNTERS", true);
+   return (instance->vk.trace_mode == RADV_TRACE_MODE_RGP) &&
+          debug_get_bool_option("RADV_THREAD_TRACE_CACHE_COUNTERS", true);
 }
 
 VKAPI_ATTR VkResult VKAPI_CALL
@@ -553,7 +554,7 @@ init_dispatch_tables(struct radv_device *device, struct radv_physical_device *ph
       add_entrypoints(&b, &rage2_device_entrypoints, RADV_APP_DISPATCH_TABLE);
    }
 
-   if (radv_sqtt_enabled())
+   if (physical_device->instance->vk.trace_mode & RADV_TRACE_MODE_RGP)
       add_entrypoints(&b, &sqtt_device_entrypoints, RADV_RGP_DISPATCH_TABLE);
 
    if ((physical_device->instance->vk.trace_mode & RADV_TRACE_MODE_RRA) && radv_enable_rt(physical_device, false))
@@ -625,6 +626,9 @@ capture_trace(VkQueue _queue)
       }
    }
 
+   if (queue->device->instance->vk.trace_mode & RADV_TRACE_MODE_RGP)
+      queue->device->sqtt_triggered = true;
+
    return result;
 }
 
@@ -925,7 +929,7 @@ radv_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCr
       radv_dump_enabled_options(device, stderr);
    }
 
-   if (radv_sqtt_enabled()) {
+   if (device->instance->vk.trace_mode & RADV_TRACE_MODE_RGP) {
       if (device->physical_device->rad_info.gfx_level < GFX8 || device->physical_device->rad_info.gfx_level > GFX11) {
          fprintf(stderr, "GPU hardware not supported: refer to "
                          "the RGP documentation for the list of "
@@ -942,9 +946,9 @@ radv_CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCr
               "radv: Thread trace support is enabled (initial buffer size: %u MiB, "
               "instruction timing: %s, cache counters: %s).\n",
               device->sqtt.buffer_size / (1024 * 1024), radv_is_instruction_timing_enabled() ? "enabled" : "disabled",
-              radv_spm_trace_enabled() ? "enabled" : "disabled");
+              radv_spm_trace_enabled(device->instance) ? "enabled" : "disabled");
 
-      if (radv_spm_trace_enabled()) {
+      if (radv_spm_trace_enabled(device->instance)) {
          /* TODO: add SPM counters for GFX11. */
          if (device->physical_device->rad_info.gfx_level == GFX10 ||
              device->physical_device->rad_info.gfx_level == GFX10_3) {
index 9a3efb9..b51072c 100644 (file)
@@ -247,6 +247,17 @@ static const struct vk_instance_extension_table radv_instance_extensions_support
 #endif
 };
 
+static void
+radv_handle_legacy_sqtt_trigger(struct vk_instance *instance)
+{
+   char *trigger_file = getenv("RADV_THREAD_TRACE_TRIGGER");
+   if (trigger_file) {
+      instance->trace_trigger_file = trigger_file;
+      instance->trace_mode |= RADV_TRACE_MODE_RGP;
+      fprintf(stderr, "WARNING: RADV_THREAD_TRACE_TRIGGER is deprecated, please use MESA_VK_TRACE_TRIGGER instead.\n");
+   }
+}
+
 VKAPI_ATTR VkResult VKAPI_CALL
 radv_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
                     VkInstance *pInstance)
@@ -273,6 +284,7 @@ radv_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationC
    }
 
    vk_instance_add_driver_trace_modes(&instance->vk, trace_options);
+   radv_handle_legacy_sqtt_trigger(&instance->vk);
 
    instance->debug_flags = parse_debug_string(getenv("RADV_DEBUG"), radv_debug_options);
    instance->perftest_flags = parse_debug_string(getenv("RADV_PERFTEST"), radv_perftest_options);
index 2b2f536..43ab4a3 100644 (file)
@@ -53,18 +53,12 @@ typedef void *drmDevicePtr;
 #include "ac_llvm_util.h"
 #endif
 
-bool
-radv_sqtt_enabled(void)
-{
-   return debug_get_num_option("RADV_THREAD_TRACE", -1) >= 0 || getenv("RADV_THREAD_TRACE_TRIGGER");
-}
-
 static bool
 radv_perf_query_supported(const struct radv_physical_device *pdev)
 {
    /* SQTT / SPM interfere with the register states for perf counters, and
     * the code has only been tested on GFX10.3 */
-   return pdev->rad_info.gfx_level == GFX10_3 && !radv_sqtt_enabled();
+   return pdev->rad_info.gfx_level == GFX10_3 && !(pdev->instance->vk.trace_mode & RADV_TRACE_MODE_RGP);
 }
 
 static bool
@@ -477,7 +471,7 @@ radv_physical_device_get_supported_extensions(const struct radv_physical_device
       .EXT_conditional_rendering = true,
       .EXT_conservative_rasterization = device->rad_info.gfx_level >= GFX9,
       .EXT_custom_border_color = true,
-      .EXT_debug_marker = radv_sqtt_enabled(),
+      .EXT_debug_marker = device->instance->vk.trace_mode & RADV_TRACE_MODE_RGP,
       .EXT_depth_bias_control = true,
       .EXT_depth_clip_control = true,
       .EXT_depth_clip_enable = true,
@@ -1894,7 +1888,7 @@ radv_physical_device_try_create(struct radv_instance *instance, drmDevicePtr drm
    device->ws = radv_null_winsys_create();
 #else
    if (drm_device) {
-      bool reserve_vmid = radv_sqtt_enabled();
+      bool reserve_vmid = instance->vk.trace_mode & RADV_TRACE_MODE_RGP;
 
       device->ws = radv_amdgpu_winsys_create(fd, instance->debug_flags, instance->perftest_flags, reserve_vmid);
    } else {
@@ -2491,7 +2485,7 @@ radv_GetPhysicalDeviceToolProperties(VkPhysicalDevice physicalDevice, uint32_t *
    uint32_t tool_count = 0;
 
    /* RGP */
-   rgp_enabled = radv_sqtt_enabled();
+   rgp_enabled = pdevice->instance->vk.trace_mode & RADV_TRACE_MODE_RGP;
    if (rgp_enabled)
       tool_count++;
 
index 3ce207e..a20629d 100644 (file)
@@ -388,8 +388,6 @@ VkResult create_drm_physical_device(struct vk_instance *vk_instance, struct _drm
 
 void radv_physical_device_destroy(struct vk_physical_device *vk_device);
 
-bool radv_sqtt_enabled(void);
-
 enum radv_trace_mode {
    /** Radeon GPU Profiler */
    RADV_TRACE_MODE_RGP = 1 << VK_TRACE_MODE_COUNT,
@@ -1043,6 +1041,8 @@ struct radv_device {
 
    /* Thread trace. */
    struct ac_sqtt sqtt;
+   bool sqtt_enabled;
+   bool sqtt_triggered;
 
    /* Memory trace. */
    struct radv_memory_trace_data memory_trace;
index 0fde0f8..37bc4fb 100644 (file)
@@ -574,11 +574,6 @@ radv_sqtt_init(struct radv_device *device)
 
    /* Default buffer size set to 32MB per SE. */
    device->sqtt.buffer_size = (uint32_t)debug_get_num_option("RADV_THREAD_TRACE_BUFFER_SIZE", 32 * 1024 * 1024);
-   device->sqtt.start_frame = (int)debug_get_num_option("RADV_THREAD_TRACE", -1);
-
-   const char *trigger_file = getenv("RADV_THREAD_TRACE_TRIGGER");
-   if (trigger_file)
-      device->sqtt.trigger_file = strdup(trigger_file);
 
    if (!radv_sqtt_init_bo(device))
       return false;
@@ -599,8 +594,6 @@ radv_sqtt_finish(struct radv_device *device)
    struct ac_sqtt *sqtt = &device->sqtt;
    struct radeon_winsys *ws = device->ws;
 
-   free(device->sqtt.trigger_file);
-
    radv_sqtt_finish_bo(device);
 
    for (unsigned i = 0; i < 2; i++) {