diff --git a/meson.build b/meson.build index 1ed9c8242e9..f4fd28eb7d5 100644 --- a/meson.build +++ b/meson.build @@ -2210,6 +2210,12 @@ if with_gpuvis pre_args += '-DHAVE_GPUVIS' endif +with_sysprof = get_option('sysprof') +if with_sysprof + dep_sysprof = dependency('sysprof-capture-4', version: '>= 3.38.0') + pre_args += '-DHAVE_SYSPROF' +endif + add_project_arguments(pre_args, language : ['c', 'cpp']) add_project_arguments(c_cpp_args, language : ['c', 'cpp']) diff --git a/meson.options b/meson.options index a56bcef6e2c..d3e81d03022 100644 --- a/meson.options +++ b/meson.options @@ -638,6 +638,13 @@ option( description : 'Enable tracing markers for gpuvis. Default: false' ) +option( + 'sysprof', + type : 'boolean', + value : false, + description : 'Enable tracing markers for sysprof. Default: false' +) + option( 'custom-shader-replacement', type : 'string', diff --git a/src/util/meson.build b/src/util/meson.build index db2292df7da..0c774e6aeec 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -277,6 +277,14 @@ if with_gpuvis ) endif +if with_sysprof + files_mesa_util += files( + 'perf/u_sysprof.c', + 'perf/u_sysprof.h', + ) + deps_for_libmesa_util += dep_sysprof +endif + u_trace_py = files('perf/u_trace.py') libmesa_util_simd = static_library( diff --git a/src/util/perf/cpu_trace.h b/src/util/perf/cpu_trace.h index d975f059517..7c7637a4d18 100644 --- a/src/util/perf/cpu_trace.h +++ b/src/util/perf/cpu_trace.h @@ -8,6 +8,7 @@ #include "u_perfetto.h" #include "u_gpuvis.h" +#include "u_sysprof.h" #include "util/detect_os.h" #include "util/macros.h" @@ -93,6 +94,18 @@ #endif /* HAVE_GPUVIS */ +#if defined(HAVE_SYSPROF) + +#define _MESA_SYSPROF_TRACE_BEGIN(name) util_sysprof_begin(name) +#define _MESA_SYSPROF_TRACE_END(scope) util_sysprof_end(scope) + +#else + +#define _MESA_SYSPROF_TRACE_BEGIN(name) NULL +#define _MESA_SYSPROF_TRACE_END(scope) + +#endif /* HAVE_SYSPROF */ + #if __has_attribute(cleanup) && __has_attribute(unused) #include @@ -126,6 +139,7 @@ static inline void * _mesa_trace_scope_begin(const char *format, ...) { char name[_MESA_TRACE_SCOPE_MAX_NAME_LENGTH]; + void *scope = NULL; va_list args; va_start(args, format); @@ -136,17 +150,21 @@ _mesa_trace_scope_begin(const char *format, ...) _MESA_TRACE_BEGIN(name); _MESA_GPUVIS_TRACE_BEGIN(name); - return NULL; + scope = _MESA_SYSPROF_TRACE_BEGIN(name); + return scope; } static inline void * _mesa_trace_scope_flow_begin(const char *name, uint64_t *id) { + void *scope = NULL; + if (*id == 0) *id = util_perfetto_next_id(); _MESA_TRACE_FLOW_BEGIN(name, *id); _MESA_GPUVIS_TRACE_BEGIN(name); - return NULL; + scope = _MESA_SYSPROF_TRACE_BEGIN(name); + return scope; } static inline void @@ -154,6 +172,7 @@ _mesa_trace_scope_end(UNUSED void **scope) { _MESA_GPUVIS_TRACE_END(); _MESA_TRACE_END(); + _MESA_SYSPROF_TRACE_END(scope); } #else diff --git a/src/util/perf/u_sysprof.c b/src/util/perf/u_sysprof.c new file mode 100644 index 00000000000..cc2f3bc0caf --- /dev/null +++ b/src/util/perf/u_sysprof.c @@ -0,0 +1,40 @@ +/* + * Copyright 2025 Igalia S.L. + * SPDX-License-Identifier: MIT + */ + +#include "u_sysprof.h" + +#include /* needed for sysprof-collector.h */ +#include +#include +#include + +struct perf_sysprof_entry { + SysprofTimeStamp begin; + /* SysprofCaptureMark itself limits it to 40 characters */ + char name[40]; +}; + +void * +util_sysprof_begin(const char *name) +{ + struct perf_sysprof_entry *trace = + malloc(sizeof(struct perf_sysprof_entry)); + + trace->begin = SYSPROF_CAPTURE_CURRENT_TIME; + snprintf(trace->name, sizeof(trace->name), "%s", name); + + return trace; +} + +void +util_sysprof_end(void **scope) +{ + struct perf_sysprof_entry *trace = (struct perf_sysprof_entry *) *scope; + + sysprof_collector_mark(trace->begin, + SYSPROF_CAPTURE_CURRENT_TIME - trace->begin, "Mesa", + trace->name, NULL); + free(trace); +} diff --git a/src/util/perf/u_sysprof.h b/src/util/perf/u_sysprof.h new file mode 100644 index 00000000000..eb6a4fb8eec --- /dev/null +++ b/src/util/perf/u_sysprof.h @@ -0,0 +1,17 @@ +/* + * Copyright 2025 Igalia S.L. + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void *util_sysprof_begin(const char *name); +void util_sysprof_end(void **scope); + +#ifdef __cplusplus +} +#endif