util/perf: simplify u_trace_instrument
authorChia-I Wu <olvaffe@gmail.com>
Tue, 30 Aug 2022 00:25:15 +0000 (17:25 -0700)
committerMarge Bot <emma+marge@anholt.net>
Fri, 2 Sep 2022 19:46:50 +0000 (19:46 +0000)
When ut_perfetto_enabled changes, update _u_trace_instrument as well.
This allows u_trace_instrument to check just a single variable.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18309>

src/util/perf/u_trace.c
src/util/perf/u_trace.h

index 2bd80d9..4e56531 100644 (file)
@@ -38,7 +38,7 @@
 #define TIMESTAMP_BUF_SIZE 0x1000
 #define TRACES_PER_CHUNK   (TIMESTAMP_BUF_SIZE / sizeof(uint64_t))
 
-bool _u_trace_instrument;
+int _u_trace_instrument;
 
 #ifdef HAVE_PERFETTO
 int ut_perfetto_enabled;
@@ -369,7 +369,8 @@ get_tracefile(void)
          tracefile = stdout;
       }
 
-      _u_trace_instrument = tracefile || debug_get_option_trace_instrument();
+      if (tracefile || debug_get_option_trace_instrument())
+         p_atomic_inc(&_u_trace_instrument);
 
       firsttime = false;
    }
@@ -465,14 +466,16 @@ u_trace_perfetto_start(void)
 {
    list_for_each_entry (struct u_trace_context, utctx, &ctx_list, node)
       queue_init(utctx);
-   ut_perfetto_enabled++;
+   if (p_atomic_inc_return(&ut_perfetto_enabled) == 1)
+      p_atomic_inc(&_u_trace_instrument);
 }
 
 void
 u_trace_perfetto_stop(void)
 {
    assert(ut_perfetto_enabled > 0);
-   ut_perfetto_enabled--;
+   if (p_atomic_dec_return(&ut_perfetto_enabled) == 0)
+      p_atomic_dec(&_u_trace_instrument);
 }
 #endif
 
index fcee749..9d3acfc 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdint.h>
 #include <stdio.h>
 
+#include "util/u_atomic.h"
 #include "util/u_queue.h"
 
 #ifdef __cplusplus
@@ -286,8 +287,8 @@ void u_trace_perfetto_stop(void);
 static inline bool
 u_trace_instrument(void)
 {
-   extern bool _u_trace_instrument;
-   return _u_trace_instrument || ut_perfetto_enabled;
+   extern int _u_trace_instrument;
+   return p_atomic_read_relaxed(&_u_trace_instrument);
 }
 
 static inline bool