Skip to content

Commit 04bc282

Browse files
committed
Merge pull request #104851 from Ivorforce/tracy
Add `profiler` option to `SCons` builds, with support for `tracy` and `perfetto`.
2 parents c9ef313 + c374788 commit 04bc282

25 files changed

+375
-1
lines changed

SConstruct

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ opts.Add(BoolVariable("use_volk", "Use the volk library to load the Vulkan loade
202202
opts.Add(BoolVariable("accesskit", "Use AccessKit C SDK", True))
203203
opts.Add(("accesskit_sdk_path", "Path to the AccessKit C SDK", ""))
204204
opts.Add(BoolVariable("sdl", "Enable the SDL3 input driver", True))
205+
opts.Add(("profiler_path", "Path to the Profiler framework. Only tracy and perfetto are supported at the moment.", ""))
206+
opts.Add(
207+
BoolVariable(
208+
"profiler_sample_callstack",
209+
"Profile random samples application-wide using a callstack based sampler.",
210+
False,
211+
)
212+
)
205213

206214
# Advanced options
207215
opts.Add(

core/SCsub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ env.CommandNoCache(
217217
)
218218

219219
# Chain load SCsubs
220+
SConscript("profiling/SCsub")
220221
SConscript("os/SCsub")
221222
SConscript("math/SCsub")
222223
SConscript("crypto/SCsub")

core/profiling/SCsub

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
from misc.utility.scons_hints import *
3+
4+
import pathlib
5+
from typing import Tuple
6+
7+
import profiling_builders
8+
9+
Import("env")
10+
11+
env.add_source_files(env.core_sources, "*.cpp")
12+
13+
14+
def get_profiler_and_path_from_path(path: pathlib.Path) -> Tuple[str, pathlib.Path]:
15+
if not path.is_dir():
16+
print("profiler_path must be empty or point to a directory.")
17+
Exit(255)
18+
19+
if (path / "sdk" / "perfetto.cc").is_file():
20+
# perfetto root directory.
21+
return "perfetto", path / "sdk"
22+
if (path / "perfetto.cc").is_file():
23+
# perfetto sdk directory.
24+
return "perfetto", path
25+
26+
if (path / "public" / "TracyClient.cpp").is_file():
27+
# tracy root directory
28+
return "tracy", path / "public"
29+
if (path / "TracyClient.cpp").is_file():
30+
# tracy public directory
31+
return "tracy", path
32+
33+
print("Unrecognized profiler_path option. Please set a path to either tracy or perfetto.")
34+
Exit(255)
35+
36+
37+
env["profiler"] = None
38+
if env["profiler_path"]:
39+
profiler_name, profiler_path = get_profiler_and_path_from_path(pathlib.Path(env["profiler_path"]))
40+
env["profiler"] = profiler_name
41+
42+
if profiler_name == "tracy":
43+
env.Prepend(CPPPATH=[str(profiler_path.absolute())])
44+
45+
env_tracy = env.Clone()
46+
env_tracy.Append(CPPDEFINES=["TRACY_ENABLE"])
47+
if env["profiler_sample_callstack"]:
48+
if env["platform"] not in ("windows", "linux", "android"):
49+
# Reference the feature matrix in the tracy documentation.
50+
print("Tracy does not support call stack sampling on this platform. Aborting.")
51+
Exit(255)
52+
53+
# 62 is the maximum supported callstack depth reported by the tracy docs.
54+
env_tracy.Append(CPPDEFINES=[("TRACY_CALLSTACK", 62)])
55+
env_tracy.disable_warnings()
56+
env_tracy.add_source_files(env.core_sources, str((profiler_path / "TracyClient.cpp").absolute()))
57+
elif profiler_name == "perfetto":
58+
env.Prepend(CPPPATH=[str(profiler_path.absolute())])
59+
60+
env_perfetto = env.Clone()
61+
if env["profiler_sample_callstack"]:
62+
print("Perfetto does not support call stack sampling. Aborting.")
63+
Exit(255)
64+
env_perfetto.disable_warnings()
65+
env_perfetto.Prepend(CPPPATH=[str(profiler_path.absolute())])
66+
env_perfetto.add_source_files(env.core_sources, str((profiler_path / "perfetto.cc").absolute()))
67+
68+
69+
env.CommandNoCache("profiling.gen.h", [env.Value(env["profiler"])], env.Run(profiling_builders.profiler_gen_builder))

core/profiling/profiling.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**************************************************************************/
2+
/* profiling.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "profiling.h"
32+
33+
#if defined(GODOT_USE_TRACY)
34+
void godot_init_profiler() {
35+
// Send our first event to tracy; otherwise it doesn't start collecting data.
36+
// FrameMark is kind of fitting because it communicates "this is where we started tracing".
37+
FrameMark;
38+
}
39+
#elif defined(GODOT_USE_PERFETTO)
40+
PERFETTO_TRACK_EVENT_STATIC_STORAGE();
41+
42+
void godot_init_profiler() {
43+
perfetto::TracingInitArgs args;
44+
45+
args.backends |= perfetto::kSystemBackend;
46+
47+
perfetto::Tracing::Initialize(args);
48+
perfetto::TrackEvent::Register();
49+
}
50+
#else
51+
void godot_init_profiler() {
52+
// Stub
53+
}
54+
#endif

core/profiling/profiling.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**************************************************************************/
2+
/* profiling.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "core/typedefs.h"
34+
#include "profiling.gen.h"
35+
36+
#if defined(GODOT_USE_TRACY)
37+
// Use the tracy profiler.
38+
39+
#define TRACY_ENABLE
40+
#include <tracy/Tracy.hpp>
41+
42+
#ifndef TRACY_CALLSTACK
43+
#define TRACY_CALLSTACK 0
44+
#endif
45+
46+
// Define tracing macros.
47+
#define GodotProfileFrameMark FrameMark
48+
#define GodotProfileZone(m_zone_name) ZoneScopedN(m_zone_name)
49+
#define GodotProfileZoneGroupedFirst(m_group_name, m_zone_name) ZoneNamedN(__godot_tracy_zone_##m_group_name, m_zone_name, true)
50+
#define GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name) __godot_tracy_zone_##m_group_name.~ScopedZone();
51+
#define GodotProfileZoneGrouped(m_group_name, m_zone_name) \
52+
GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name); \
53+
static constexpr tracy::SourceLocationData TracyConcat(__tracy_source_location, TracyLine){ m_zone_name, TracyFunction, TracyFile, (uint32_t)TracyLine, 0 }; \
54+
new (&__godot_tracy_zone_##m_group_name) tracy::ScopedZone(&TracyConcat(__tracy_source_location, TracyLine), TRACY_CALLSTACK, true)
55+
56+
void godot_init_profiler();
57+
58+
#elif defined(GODOT_USE_PERFETTO)
59+
// Use the perfetto profiler.
60+
61+
#include <perfetto.h>
62+
63+
PERFETTO_DEFINE_CATEGORIES(
64+
perfetto::Category("godot")
65+
.SetDescription("All Godot Events"), );
66+
67+
// See PERFETTO_INTERNAL_SCOPED_EVENT_FINALIZER
68+
struct PerfettoGroupedEventEnder {
69+
_FORCE_INLINE_ void _end_now() {
70+
TRACE_EVENT_END("godot");
71+
}
72+
73+
_FORCE_INLINE_ ~PerfettoGroupedEventEnder() {
74+
_end_now();
75+
}
76+
};
77+
78+
#define GodotProfileFrameMark // TODO
79+
#define GodotProfileZone(m_zone_name) TRACE_EVENT("godot", m_zone_name);
80+
#define GodotProfileZoneGroupedFirst(m_group_name, m_zone_name) \
81+
TRACE_EVENT_BEGIN("godot", m_zone_name); \
82+
PerfettoGroupedEventEnder __godot_perfetto_zone_##m_group_name
83+
#define GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name) __godot_perfetto_zone_##m_group_name.~PerfettoGroupedEventEnder()
84+
#define GodotProfileZoneGrouped(m_group_name, m_zone_name) \
85+
__godot_perfetto_zone_##m_group_name._end_now(); \
86+
TRACE_EVENT_BEGIN("godot", m_zone_name);
87+
88+
void godot_init_profiler();
89+
90+
#else
91+
// No profiling; all macros are stubs.
92+
93+
void godot_init_profiler();
94+
95+
#define GodotProfileFrameMark
96+
#define GodotProfileZone(m_zone_name)
97+
#define GodotProfileZoneGroupedFirst(m_group_name, m_zone_name)
98+
#define GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name)
99+
#define GodotProfileZoneGrouped(m_group_name, m_zone_name)
100+
101+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Functions used to generate source files during build time"""
2+
3+
import methods
4+
5+
6+
def profiler_gen_builder(target, source, env):
7+
with methods.generated_wrapper(str(target[0])) as file:
8+
if env["profiler"] == "tracy":
9+
file.write("#define GODOT_USE_TRACY\n")
10+
if env["profiler_sample_callstack"]:
11+
file.write("#define TRACY_CALLSTACK 62\n")
12+
if env["profiler"] == "perfetto":
13+
file.write("#define GODOT_USE_PERFETTO\n")

drivers/apple_embedded/os_apple_embedded.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "core/io/dir_access.h"
4242
#include "core/io/file_access.h"
4343
#include "core/os/main_loop.h"
44+
#include "core/profiling/profiling.h"
4445
#import "drivers/apple/os_log_logger.h"
4546
#include "main/main.h"
4647

@@ -205,6 +206,9 @@ Rect2 fit_keep_aspect_covered(const Vector2 &p_container, const Vector2 &p_rect)
205206
return true;
206207
}
207208

209+
GodotProfileFrameMark;
210+
GodotProfileZone("OS_AppleEmbedded::iterate");
211+
208212
if (DisplayServer::get_singleton()) {
209213
DisplayServer::get_singleton()->process_events();
210214
}

0 commit comments

Comments
 (0)