Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
tracy.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2
4
6
7#ifndef TRACY_CALLSTACK
8#define TRACY_CALLSTACK 0
9#endif
10
11#include <client/TracyProfiler.hpp>
12#include <client/TracyScoped.hpp>
13#include <client/TracyThread.hpp>
14
15#include <cstddef>
16#include <cstdint>
17#include <cstring>
18#include <memory>
19#include <new>
20#include <source_location>
21#include <string_view>
22
23namespace helios::profile {
24
25namespace {
26
27constexpr size_t kTracyZoneStorageSize = sizeof(tracy::ScopedZone);
28
30 "Tracy ScopedZone exceeds HELIOS_PROFILE_ZONE_STORAGE_BYTES");
31
32[[nodiscard]] constexpr tracy::PlotFormatType ToTracyPlotFormat(
33 PlotFormat format) noexcept {
34 switch (format) {
36 return tracy::PlotFormatType::Number;
38 return tracy::PlotFormatType::Memory;
40 return tracy::PlotFormatType::Percentage;
42 return tracy::PlotFormatType::Number;
43 }
44 return tracy::PlotFormatType::Number;
45}
46
47[[nodiscard]] tracy::ScopedZone* AsTracyZone(
48 std::span<std::byte> storage) noexcept {
49 return std::launder(reinterpret_cast<tracy::ScopedZone*>(storage.data()));
50}
51
52} // namespace
53
54size_t TracyBackend::ZoneStorageSize() const noexcept {
55 return kTracyZoneStorageSize;
56}
57
59 std::span<std::byte> storage) noexcept {
60 const char* const name_ptr = spec.name.empty() ? nullptr : spec.name.data();
61 const size_t name_size = spec.name.size();
62 const char* const function = spec.loc.function_name();
63 const char* const file = spec.loc.file_name();
64 const auto line = spec.loc.line();
65
66 auto* zone = AsTracyZone(storage);
67 std::construct_at(zone, line, file, std::strlen(file), function,
68 std::strlen(function), name_ptr, name_size, spec.color,
69 spec.callstack_depth, spec.active);
70}
71
72void TracyBackend::EndZone(std::span<std::byte> storage) noexcept {
73 std::destroy_at(AsTracyZone(storage));
74}
75
76void TracyBackend::ZoneText(std::span<std::byte> storage,
77 std::string_view text) noexcept {
78 AsTracyZone(storage)->Text(text.data(), text.size());
79}
80
81void TracyBackend::ZoneValue(std::span<std::byte> storage,
82 uint64_t value) noexcept {
83 AsTracyZone(storage)->Value(value);
84}
85
86void TracyBackend::ZoneName(std::span<std::byte> storage,
87 std::string_view name) noexcept {
88 AsTracyZone(storage)->Name(name.data(), name.size());
89}
90
91void TracyBackend::FrameMark() noexcept {
92 tracy::Profiler::SendFrameMark(nullptr);
93}
94
96 tracy::Profiler::SendFrameMark(name.CStr());
97}
98
100 tracy::Profiler::SendFrameMark(name.CStr(),
101 tracy::QueueType::FrameMarkMsgStart);
102}
103
105 tracy::Profiler::SendFrameMark(name.CStr(),
106 tracy::QueueType::FrameMarkMsgEnd);
107}
108
109void TracyBackend::Message(std::string_view text, uint32_t color) noexcept {
110 if (color == 0) {
111 tracy::Profiler::Message(text.data(), text.size(), TRACY_CALLSTACK);
112 } else {
113 tracy::Profiler::MessageColor(text.data(), text.size(), color,
115 }
116}
117
119 tracy::SetThreadName(name.CStr());
120}
121
122void TracyBackend::Plot(CStringView name, double value) noexcept {
123 tracy::Profiler::PlotData(name.CStr(), value);
124}
125
127 bool fill, uint32_t color) noexcept {
128 tracy::Profiler::ConfigurePlot(name.CStr(), ToTracyPlotFormat(type), step,
129 fill, color);
130}
131
132void TracyBackend::Alloc(const void* ptr, size_t size,
133 std::optional<CStringView> name, int depth,
134 std::source_location /*loc*/) noexcept {
135 if (!name.has_value()) {
136 if (depth > 0) {
137 tracy::Profiler::MemAllocCallstack(ptr, size, depth, false);
138 } else {
139 tracy::Profiler::MemAllocCallstack(ptr, size, TRACY_CALLSTACK, false);
140 }
141 return;
142 }
143 if (depth > 0) {
144 tracy::Profiler::MemAllocCallstackNamed(ptr, size, depth, false,
145 name->CStr());
146 } else if (TRACY_CALLSTACK > 0) {
147 tracy::Profiler::MemAllocCallstackNamed(ptr, size, TRACY_CALLSTACK, false,
148 name->CStr());
149 } else {
150 tracy::Profiler::MemAllocNamed(ptr, size, false, name->CStr());
151 }
152}
153
154void TracyBackend::Free(const void* ptr, std::optional<CStringView> name,
155 int depth, std::source_location /*loc*/) noexcept {
156 if (!name.has_value()) {
157 if (depth > 0) {
158 tracy::Profiler::MemFreeCallstack(ptr, depth, false);
159 } else {
160 tracy::Profiler::MemFreeCallstack(ptr, TRACY_CALLSTACK, false);
161 }
162 return;
163 }
164 if (depth > 0) {
165 tracy::Profiler::MemFreeCallstackNamed(ptr, depth, false, name->CStr());
166 } else if (TRACY_CALLSTACK > 0) {
167 tracy::Profiler::MemFreeCallstackNamed(ptr, TRACY_CALLSTACK, false,
168 name->CStr());
169 } else {
170 tracy::Profiler::MemFreeNamed(ptr, false, name->CStr());
171 }
172}
173
175 if (TRACY_CALLSTACK > 0) {
176 tracy::Profiler::MemDiscardCallstack(name.CStr(), false, TRACY_CALLSTACK);
177 } else {
178 tracy::Profiler::MemDiscard(name.CStr(), false);
179 }
180}
181
182void TracyBackend::MemoryDiscard(CStringView name, int depth) noexcept {
183 tracy::Profiler::MemDiscardCallstack(name.CStr(), false, depth);
184}
185
186} // namespace helios::profile
void FrameMark() noexcept override
Marks the end of the current frame.
Definition tracy.cpp:91
void Free(const void *ptr, std::optional< CStringView > name, int depth, std::source_location loc) noexcept override
Records a memory deallocation.
Definition tracy.cpp:154
size_t ZoneStorageSize() const noexcept override
Returns the per-zone storage required by this backend.
Definition tracy.cpp:54
void Plot(CStringView name, double value) noexcept override
Records a plot sample.
Definition tracy.cpp:122
void SetThreadName(CStringView name) noexcept override
Sets the current thread name.
Definition tracy.cpp:118
void Alloc(const void *ptr, size_t size, std::optional< CStringView > name, int depth, std::source_location loc) noexcept override
Records a memory allocation.
Definition tracy.cpp:132
void FrameMarkEnd(CStringView name) noexcept override
Marks the end of a named frame region.
Definition tracy.cpp:104
void ZoneValue(std::span< std::byte > storage, uint64_t value) noexcept override
Attaches a numeric value to the active zone.
Definition tracy.cpp:81
void EndZone(std::span< std::byte > storage) noexcept override
Ends a profiling zone.
Definition tracy.cpp:72
void FrameMarkStart(CStringView name) noexcept override
Marks the start of a named frame region.
Definition tracy.cpp:99
void PlotConfig(CStringView name, PlotFormat type, bool step, bool fill, uint32_t color) noexcept override
Configures a plot channel.
Definition tracy.cpp:126
void Message(std::string_view text, uint32_t color) noexcept override
Emits a timeline message.
Definition tracy.cpp:109
void ZoneText(std::span< std::byte > storage, std::string_view text) noexcept override
Attaches text to the active zone.
Definition tracy.cpp:76
void BeginZone(const ZoneSpec &spec, std::span< std::byte > storage) noexcept override
Begins a profiling zone.
Definition tracy.cpp:58
void MemoryDiscard(CStringView name) noexcept override
Discards tracked memory for a named pool.
Definition tracy.cpp:174
void ZoneName(std::span< std::byte > storage, std::string_view name) noexcept override
Renames the active zone.
Definition tracy.cpp:86
constexpr tracy::PlotFormatType ToTracyPlotFormat(PlotFormat format) noexcept
Definition tracy.cpp:32
tracy::ScopedZone * AsTracyZone(std::span< std::byte > storage) noexcept
Definition tracy.cpp:47
PlotFormat
Plot value format for timeline plots.
Definition common.hpp:10
constexpr size_t kZoneStorageBytes
Definition config.hpp:12
BasicCStringView< char > CStringView
A view of a null-terminated C string.
Immutable zone description for a single instrumentation site.
Definition common.hpp:22
#define TRACY_CALLSTACK
Definition tracy.cpp:8