Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
profiler.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/export.hpp>
5
6#include <chrono>
7#include <cstddef>
8#include <cstdint>
9#include <filesystem>
10#include <iosfwd>
11#include <span>
12#include <string>
13#include <string_view>
14#include <vector>
15
16namespace aegis2d::core {
17
21 std::string name{};
23 std::size_t depth = 0;
25 double ms = 0.0;
28 std::uint32_t calls = 0;
29};
30
32struct TraceEvent {
34 std::string name{};
37 double start_us = 0.0;
39 double duration_us = 0.0;
40};
41
45 std::string name{};
47 std::span<const TraceEvent> events{};
48};
49
50class Profiler;
51
53class AEGIS2D_CORE_EXPORT ProfileScope {
54public:
57 ProfileScope(const ProfileScope&) = delete;
61
62private:
63 friend class Profiler;
64 explicit ProfileScope(Profiler* profiler) noexcept;
65
66 Profiler* profiler_;
67};
68
92class AEGIS2D_CORE_EXPORT Profiler {
93public:
95 using Clock = std::chrono::steady_clock;
96
98 Profiler() = default;
99
102 void set_enabled(bool enabled) noexcept;
103
106 [[nodiscard]] bool enabled() const noexcept;
107
109 void begin_frame();
110
113 void end_frame();
114
119 [[nodiscard]] ProfileScope scope(std::string_view name);
120
123 void begin(std::string_view name);
124
126 void end();
127
130 [[nodiscard]] std::span<const ProfileEntry> last_frame() const noexcept;
131
134 [[nodiscard]] double last_frame_ms() const noexcept;
135
141 void start_trace(std::size_t capacity = 1'000'000);
142
144 void stop_trace() noexcept;
145
148 [[nodiscard]] bool tracing() const noexcept;
149
152 [[nodiscard]] std::span<const TraceEvent> trace() const noexcept;
153
156 [[nodiscard]] std::size_t dropped() const noexcept;
157
158private:
159 void record(std::string_view name, Clock::time_point start, Clock::time_point end);
160
161 struct Open {
162 std::size_t entry = 0;
163 Clock::time_point start;
164 };
165
166 std::vector<ProfileEntry> frame_;
167 // Parent of each entry of frame_, or none for a phase.
168 std::vector<std::size_t> parents_;
169 std::vector<Open> open_;
170 std::vector<ProfileEntry> last_;
171 std::vector<TraceEvent> trace_;
172 std::size_t trace_capacity_ = 0;
173 std::size_t dropped_ = 0;
174 bool tracing_ = false;
175 Clock::time_point frame_start_;
176 double last_ms_ = 0.0;
177 bool in_frame_ = false;
178 bool enabled_ = true;
179};
180
188AEGIS2D_CORE_EXPORT void write_chrome_trace(std::ostream& out, std::span<const TraceThread> threads,
189 std::string_view process = "aegis2d");
190
196AEGIS2D_CORE_EXPORT void save_chrome_trace(const std::filesystem::path& file,
197 std::span<const TraceThread> threads,
198 std::string_view process = "aegis2d");
199
205[[nodiscard]] AEGIS2D_CORE_EXPORT std::vector<std::string> profile_lines(const Profiler& profiler,
206 std::size_t most = 12);
207
208} // namespace aegis2d::core
Times a scope while it lives: from Profiler::scope() to its destruction.
Definition profiler.hpp:53
ProfileScope(const ProfileScope &)=delete
~ProfileScope()
Ends the scope.
ProfileScope & operator=(const ProfileScope &)=delete
ProfileScope & operator=(ProfileScope &&)=delete
ProfileScope(ProfileScope &&)=delete
Timers around the parts of a frame (its phases, each system of a ecs::Scheduler......
Definition profiler.hpp:92
bool enabled() const noexcept
Tells whether the timers are on.
Profiler()=default
Profiler on, with no frame yet.
std::chrono::steady_clock Clock
Clock of the timers.
Definition profiler.hpp:95
void set_enabled(bool enabled) noexcept
Turns the timers on or off.
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
std::vector< std::string > profile_lines(const Profiler &profiler, std::size_t most=12)
The breakdown of the last frame as lines of the metrics overlay.
void save_chrome_trace(const std::filesystem::path &file, std::span< const TraceThread > threads, std::string_view process="aegis2d")
Writes traces to a file in the Chrome trace format (write_chrome_trace()).
void write_chrome_trace(std::ostream &out, std::span< const TraceThread > threads, std::string_view process="aegis2d")
Writes traces in the Chrome trace format, which Perfetto (ui.perfetto.dev) and chrome://tracing open.
Definition endpoint.hpp:85
Time a named part of a frame took, as Profiler::last_frame() gives it.
Definition profiler.hpp:19
double ms
Time spent in it during the frame, every call added, in milliseconds.
Definition profiler.hpp:25
std::size_t depth
How deep it is: 0 for a phase of the frame, 1 for a scope inside one...
Definition profiler.hpp:23
std::uint32_t calls
How many times it was entered during the frame (the fixed steps of a frame run the same systems sever...
Definition profiler.hpp:28
std::string name
Name of the scope, such as "update" or a system's name.
Definition profiler.hpp:21
A scope timed while tracing (Profiler::start_trace()), for write_chrome_trace().
Definition profiler.hpp:32
std::string name
Name of the scope, or "frame" for a whole frame.
Definition profiler.hpp:34
double start_us
When it started, in microseconds of the profiler's clock (the same for every profiler of the process,...
Definition profiler.hpp:37
double duration_us
How long it took, in microseconds.
Definition profiler.hpp:39
The trace of one thread, as write_chrome_trace() writes it.
Definition profiler.hpp:43
std::span< const TraceEvent > events
Its events, from Profiler::trace().
Definition profiler.hpp:47
std::string name
Name of the thread in the viewer, such as "main" or "room 2".
Definition profiler.hpp:45