Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
backend.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <concepts>
8#include <cstddef>
9#include <cstdint>
10#include <optional>
11#include <source_location>
12#include <span>
13#include <string_view>
14#include <type_traits>
15
16namespace helios::profile {
17
18/**
19 * @brief Abstract profiler backend interface.
20 * @details Each backend receives a slice of `ScopedZone` storage sized by
21 * `ZoneStorageSize()`. Backends adapt Helios profiling concepts (zones,
22 * frames, counters, memory events) to their native representation.
23 *
24 * Thread-safety contract:
25 * - `Startup()` and `Shutdown()` are called only during single-threaded
26 * lifecycle phases.
27 * - All other dispatch hooks must be safe to call concurrently from multiple
28 * threads after `Profiler::Finalize()`.
29 */
30class Backend {
31public:
32 virtual ~Backend() noexcept = default;
33
34 /// @brief Called once after finalization (single-threaded).
35 virtual void Startup() noexcept {}
36
37 /// @brief Called during shutdown
38 /// (single-threaded).
39 virtual void Shutdown() noexcept {}
40
41 /**
42 * @brief Returns the per-zone storage required by this backend.
43 * @return Storage size in bytes
44 */
45 [[nodiscard]] virtual size_t ZoneStorageSize() const noexcept = 0;
46
47 /**
48 * @brief Begins a profiling zone.
49 * @param spec Zone specification
50 * @param storage Backend-specific storage slice
51 */
52 virtual void BeginZone(const ZoneSpec& spec,
53 std::span<std::byte> storage) noexcept = 0;
54
55 /**
56 * @brief Ends a profiling zone.
57 * @param storage Backend-specific storage slice
58 */
59 virtual void EndZone(std::span<std::byte> storage) noexcept = 0;
60
61 /**
62 * @brief Attaches text to the active zone.
63 * @param storage Backend-specific storage slice
64 * @param text Text to attach
65 */
66 virtual void ZoneText(std::span<std::byte> storage,
67 std::string_view text) noexcept = 0;
68
69 /**
70 * @brief Attaches a numeric value to the active zone.
71 * @param storage Backend-specific storage slice
72 * @param value Value to attach
73 */
74 virtual void ZoneValue(std::span<std::byte> storage,
75 uint64_t value) noexcept = 0;
76
77 /**
78 * @brief Renames the active zone.
79 * @param storage Backend-specific storage slice
80 * @param name New zone name
81 */
82 virtual void ZoneName(std::span<std::byte> storage,
83 std::string_view name) noexcept = 0;
84
85 /// @brief Marks the end of the current frame.
86 virtual void FrameMark() noexcept = 0;
87
88 /**
89 * @brief Marks the end of a named frame.
90 * @param name Frame name
91 */
92 virtual void FrameMark(CStringView name) noexcept = 0;
93
94 /**
95 * @brief Marks the start of a named frame region.
96 * @param name Frame region name
97 */
98 virtual void FrameMarkStart(CStringView name) noexcept = 0;
99
100 /**
101 * @brief Marks the end of a named frame region.
102 * @param name Frame region name
103 */
104 virtual void FrameMarkEnd(CStringView name) noexcept = 0;
105
106 /**
107 * @brief Emits a timeline message.
108 * @param text Message text
109 * @param color Optional message color
110 */
111 virtual void Message(std::string_view text, uint32_t color) noexcept = 0;
112
113 /**
114 * @brief Sets the current thread name.
115 * @param name Thread name
116 */
117 virtual void SetThreadName(CStringView name) noexcept = 0;
118
119 /**
120 * @brief Records a plot sample.
121 * @param name Plot channel name
122 * @param value Sample value
123 */
124 virtual void Plot(CStringView name, double value) noexcept = 0;
125
126 /**
127 * @brief Configures a plot channel.
128 * @param name Plot channel name
129 * @param type Plot value format
130 * @param step Whether the plot is step-wise
131 * @param fill Whether the plot is filled
132 * @param color Plot color
133 */
134 virtual void PlotConfig(CStringView name, PlotFormat type, bool step,
135 bool fill, uint32_t color) noexcept = 0;
136
137 /**
138 * @brief Records a memory allocation.
139 * @param ptr Allocated pointer
140 * @param size Allocation size
141 * @param name Optional allocation pool name
142 * @param depth Callstack depth
143 * @param loc Source location
144 */
145 virtual void Alloc(const void* ptr, size_t size,
146 std::optional<CStringView> name, int depth,
147 std::source_location loc) noexcept = 0;
148
149 /**
150 * @brief Records a memory deallocation.
151 * @param ptr Deallocated pointer
152 * @param name Optional allocation pool name
153 * @param depth Callstack depth
154 * @param loc Source location
155 */
156 virtual void Free(const void* ptr, std::optional<CStringView> name, int depth,
157 std::source_location loc) noexcept = 0;
158
159 /**
160 * @brief Discards tracked memory for a named pool.
161 * @param name Pool name
162 */
163 virtual void MemoryDiscard(CStringView name) noexcept = 0;
164
165 /**
166 * @brief Discards tracked memory for a named pool with callstack depth.
167 * @param name Pool name
168 * @param depth Callstack depth
169 */
170 virtual void MemoryDiscard(CStringView name, int depth) noexcept = 0;
171
172 /**
173 * @brief Returns the backend name.
174 * @return Backend identifier
175 */
176 [[nodiscard]] virtual std::string_view Name() const noexcept = 0;
177};
178
179/// @brief Type alias for profiler backend type IDs.
180using BackendTypeId = utils::TypeId;
181
182/// @brief Type alias for profiler backend type indices.
183using BackendTypeIndex = utils::TypeIndex;
184
185/**
186 * @brief Trait to identify concrete profiler backend types.
187 * @tparam T Type to check
188 */
189template <typename T>
191 std::derived_from<std::remove_cvref_t<T>, Backend> &&
192 !std::is_abstract_v<std::remove_cvref_t<T>>;
193
194} // namespace helios::profile
Abstract profiler backend interface.
Definition backend.hpp:30
virtual void BeginZone(const ZoneSpec &spec, std::span< std::byte > storage) noexcept=0
Begins a profiling zone.
virtual std::string_view Name() const noexcept=0
Returns the backend name.
virtual void ZoneName(std::span< std::byte > storage, std::string_view name) noexcept=0
Renames the active zone.
virtual void EndZone(std::span< std::byte > storage) noexcept=0
Ends a profiling zone.
virtual void PlotConfig(CStringView name, PlotFormat type, bool step, bool fill, uint32_t color) noexcept=0
Configures a plot channel.
virtual void Alloc(const void *ptr, size_t size, std::optional< CStringView > name, int depth, std::source_location loc) noexcept=0
Records a memory allocation.
virtual void FrameMark() noexcept=0
Marks the end of the current frame.
virtual void MemoryDiscard(CStringView name) noexcept=0
Discards tracked memory for a named pool.
virtual void Free(const void *ptr, std::optional< CStringView > name, int depth, std::source_location loc) noexcept=0
Records a memory deallocation.
virtual void FrameMarkStart(CStringView name) noexcept=0
Marks the start of a named frame region.
virtual void Shutdown() noexcept
Called during shutdown (single-threaded).
Definition backend.hpp:39
virtual void ZoneValue(std::span< std::byte > storage, uint64_t value) noexcept=0
Attaches a numeric value to the active zone.
virtual size_t ZoneStorageSize() const noexcept=0
Returns the per-zone storage required by this backend.
virtual void ZoneText(std::span< std::byte > storage, std::string_view text) noexcept=0
Attaches text to the active zone.
virtual void Message(std::string_view text, uint32_t color) noexcept=0
Emits a timeline message.
virtual void Plot(CStringView name, double value) noexcept=0
Records a plot sample.
virtual void Startup() noexcept
Called once after finalization (single-threaded).
Definition backend.hpp:35
virtual void SetThreadName(CStringView name) noexcept=0
Sets the current thread name.
virtual void FrameMarkEnd(CStringView name) noexcept=0
Marks the end of a named frame region.
virtual ~Backend() noexcept=default
Trait to identify concrete profiler backend types.
Definition backend.hpp:190
utils::TypeIndex BackendTypeIndex
Type alias for profiler backend type indices.
Definition backend.hpp:183
utils::TypeId BackendTypeId
Type alias for profiler backend type IDs.
Definition backend.hpp:180
PlotFormat
Plot value format for timeline plots.
Definition common.hpp:10
BasicCStringView< char > CStringView
A view of a null-terminated C string.
STL namespace.
Immutable zone description for a single instrumentation site.
Definition common.hpp:22