perfetto: Make a MesaRenderpassDataSource with common setup/start/stop.
authorEmma Anholt <emma@anholt.net>
Thu, 16 Mar 2023 18:44:27 +0000 (11:44 -0700)
committerMarge Bot <emma+marge@anholt.net>
Thu, 30 Mar 2023 02:19:34 +0000 (02:19 +0000)
Deduplicates some code from intel/tu/freedreno, and will be a common place
to put other shared code.

The downside I can see is this logging:

[013.129]      tu_perfetto.cc:122 Tracing started
[013.129]  intel_driver_ds.cc:133 Tracing started

("oh, huh, apparently data sources for both drivers are registered?  wild")

becomes:

[142.906] erfetto_renderpass.h:50 Tracing started
[142.907] erfetto_renderpass.h:50 Tracing started

("huh, why is my driver's data source being started twice?").
Unfortunately we can't easily get a string for the data source type due to
not having rtti.

Reviewed-by: Rob Clark <robdclark@chromium.org>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22157>

src/freedreno/vulkan/tu_perfetto.cc
src/gallium/drivers/freedreno/freedreno_perfetto.cc
src/intel/ds/intel_driver_ds.cc
src/util/perf/u_perfetto_renderpass.h [new file with mode: 0644]

index 45df6f9..53e5149 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "util/hash_table.h"
 #include "util/perf/u_perfetto.h"
+#include "util/perf/u_perfetto_renderpass.h"
 
 #include "tu_tracepoints.h"
 #include "tu_tracepoints_perfetto.h"
@@ -104,21 +105,11 @@ struct TuRenderpassTraits : public perfetto::DefaultDataSourceTraits {
    using IncrementalStateType = TuRenderpassIncrementalState;
 };
 
-class TuRenderpassDataSource : public perfetto::DataSource<TuRenderpassDataSource, TuRenderpassTraits> {
-public:
-   void OnSetup(const SetupArgs &) override
+class TuRenderpassDataSource : public MesaRenderpassDataSource<TuRenderpassDataSource,
+                                                               TuRenderpassTraits> {
+   void OnStart(const StartArgs &args) override
    {
-      // Use this callback to apply any custom configuration to your data source
-      // based on the TraceConfig in SetupArgs.
-   }
-
-   void OnStart(const StartArgs &) override
-   {
-      // This notification can be used to initialize the GPU driver, enable
-      // counters, etc. StartArgs will contains the DataSourceDescriptor,
-      // which can be extended.
-      u_trace_perfetto_start();
-      PERFETTO_LOG("Tracing started");
+      MesaRenderpassDataSource<TuRenderpassDataSource, TuRenderpassTraits>::OnStart(args);
 
       /* Note: clock_id's below 128 are reserved.. for custom clock sources,
        * using the hash of a namespaced string is the recommended approach.
@@ -131,21 +122,6 @@ public:
       gpu_max_timestamp = 0;
       last_suspend_count = 0;
    }
-
-   void OnStop(const StopArgs &) override
-   {
-      PERFETTO_LOG("Tracing stopped");
-
-      // Undo any initialization done in OnStart.
-      u_trace_perfetto_stop();
-      // TODO we should perhaps block until queued traces are flushed?
-
-      Trace([](TuRenderpassDataSource::TraceContext ctx) {
-         auto packet = ctx.NewTracePacket();
-         packet->Finalize();
-         ctx.Flush();
-      });
-   }
 };
 
 PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(TuRenderpassDataSource);
index d76dabe..046fd58 100644 (file)
@@ -24,6 +24,7 @@
 #include <perfetto.h>
 
 #include "util/perf/u_perfetto.h"
+#include "util/perf/u_perfetto_renderpass.h"
 
 #include "freedreno_tracepoints.h"
 
@@ -47,21 +48,12 @@ struct FdRenderpassTraits : public perfetto::DefaultDataSourceTraits {
    using IncrementalStateType = FdRenderpassIncrementalState;
 };
 
-class FdRenderpassDataSource : public perfetto::DataSource<FdRenderpassDataSource, FdRenderpassTraits> {
+class FdRenderpassDataSource : public MesaRenderpassDataSource<FdRenderpassDataSource, FdRenderpassTraits> {
 public:
-   void OnSetup(const SetupArgs &) override
-   {
-      // Use this callback to apply any custom configuration to your data source
-      // based on the TraceConfig in SetupArgs.
-   }
 
-   void OnStart(const StartArgs &) override
+   void OnStart(const StartArgs &args) override
    {
-      // This notification can be used to initialize the GPU driver, enable
-      // counters, etc. StartArgs will contains the DataSourceDescriptor,
-      // which can be extended.
-      u_trace_perfetto_start();
-      PERFETTO_LOG("Tracing started");
+      MesaRenderpassDataSource<FdRenderpassDataSource, FdRenderpassTraits>::OnStart(args);
 
       /* Note: clock_id's below 128 are reserved.. for custom clock sources,
        * using the hash of a namespaced string is the recommended approach.
@@ -70,21 +62,6 @@ public:
       gpu_clock_id =
          _mesa_hash_string("org.freedesktop.mesa.freedreno") | 0x80000000;
    }
-
-   void OnStop(const StopArgs &) override
-   {
-      PERFETTO_LOG("Tracing stopped");
-
-      // Undo any initialization done in OnStart.
-      u_trace_perfetto_stop();
-      // TODO we should perhaps block until queued traces are flushed?
-
-      Trace([](FdRenderpassDataSource::TraceContext ctx) {
-         auto packet = ctx.NewTracePacket();
-         packet->Finalize();
-         ctx.Flush();
-      });
-   }
 };
 
 PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(FdRenderpassDataSource);
index 42b0ac8..d719515 100644 (file)
@@ -37,6 +37,7 @@
 #ifdef HAVE_PERFETTO
 
 #include "util/perf/u_perfetto.h"
+#include "util/perf/u_perfetto_renderpass.h"
 
 #include "intel_tracepoints_perfetto.h"
 
@@ -100,38 +101,8 @@ struct IntelRenderpassTraits : public perfetto::DefaultDataSourceTraits {
    using IncrementalStateType = IntelRenderpassIncrementalState;
 };
 
-class IntelRenderpassDataSource : public perfetto::DataSource<IntelRenderpassDataSource,
-                                                            IntelRenderpassTraits> {
-public:
-   void OnSetup(const SetupArgs &) override
-   {
-      // Use this callback to apply any custom configuration to your data source
-      // based on the TraceConfig in SetupArgs.
-   }
-
-   void OnStart(const StartArgs &) override
-   {
-      // This notification can be used to initialize the GPU driver, enable
-      // counters, etc. StartArgs will contains the DataSourceDescriptor,
-      // which can be extended.
-      u_trace_perfetto_start();
-      PERFETTO_LOG("Tracing started");
-   }
-
-   void OnStop(const StopArgs &) override
-   {
-      PERFETTO_LOG("Tracing stopped");
-
-      // Undo any initialization done in OnStart.
-      u_trace_perfetto_stop();
-      // TODO we should perhaps block until queued traces are flushed?
-
-      Trace([](IntelRenderpassDataSource::TraceContext ctx) {
-         auto packet = ctx.NewTracePacket();
-         packet->Finalize();
-         ctx.Flush();
-      });
-   }
+class IntelRenderpassDataSource : public MesaRenderpassDataSource<IntelRenderpassDataSource,
+                                                                  IntelRenderpassTraits> {
 };
 
 PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(IntelRenderpassDataSource);
diff --git a/src/util/perf/u_perfetto_renderpass.h b/src/util/perf/u_perfetto_renderpass.h
new file mode 100644 (file)
index 0000000..0491d8c
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright © 2023 Google LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "perfetto.h"
+
+#include "util/perf/u_trace.h"
+
+using perfetto::DataSource;
+template <typename DataSourceType, typename DataSourceTraits>
+class MesaRenderpassDataSource
+    : public perfetto::DataSource<DataSourceType, DataSourceTraits> {
+ public:
+   void OnSetup(const perfetto::DataSourceBase::SetupArgs &) override
+   {
+      // Use this callback to apply any custom configuration to your data
+      // source based on the TraceConfig in SetupArgs.
+   }
+
+   void OnStart(const perfetto::DataSourceBase::StartArgs &) override
+   {
+      // This notification can be used to initialize the GPU driver, enable
+      // counters, etc. StartArgs will contains the DataSourceDescriptor,
+      // which can be extended.
+      u_trace_perfetto_start();
+      PERFETTO_LOG("Tracing started");
+   }
+
+   void OnStop(const perfetto::DataSourceBase::StopArgs &) override
+   {
+      PERFETTO_LOG("Tracing stopped");
+
+      // Undo any initialization done in OnStart.
+      u_trace_perfetto_stop();
+      // TODO we should perhaps block until queued traces are flushed?
+
+      static_cast<DataSourceType *>(this)->Trace([](auto ctx) {
+         auto packet = ctx.NewTracePacket();
+         packet->Finalize();
+         ctx.Flush();
+      });
+   }
+};
+
+/* Begin the C API section. */