Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::Profiler Class Reference

Timers around the parts of a frame (its phases, each system of a ecs::Scheduler...), and the breakdown of the last frame for the metrics overlay. More...

#include <aegis2d/core/profiler.hpp>

Public Types

using Clock = std::chrono::steady_clock
 Clock of the timers.
 

Public Member Functions

 Profiler ()=default
 Profiler on, with no frame yet.
 
void set_enabled (bool enabled) noexcept
 Turns the timers on or off.
 
bool enabled () const noexcept
 Tells whether the timers are on.
 
void begin_frame ()
 Starts a frame, ending the one in progress if end_frame() was not called.
 
void end_frame ()
 Ends the frame: its breakdown becomes last_frame().
 
ProfileScope scope (std::string_view name)
 Times a scope until the returned object is destroyed.
 
void begin (std::string_view name)
 Starts timing a scope, as scope() does; end it with end().
 
void end ()
 Ends the innermost scope started; does nothing if none is open.
 
std::span< const ProfileEntry > last_frame () const noexcept
 The breakdown of the last frame ended.
 
double last_frame_ms () const noexcept
 Duration of the last frame ended, from begin_frame() to end_frame().
 
void start_trace (std::size_t capacity=1 '000 '000)
 Starts recording every scope timed, and every frame, for a trace.
 
void stop_trace () noexcept
 Stops recording; the events recorded are kept.
 
bool tracing () const noexcept
 Tells whether scopes are recorded.
 
std::span< const TraceEvent > trace () const noexcept
 The events recorded, each scope when it ended.
 
std::size_t dropped () const noexcept
 Events not recorded because the trace was full.
 

Detailed Description

Timers around the parts of a frame (its phases, each system of a ecs::Scheduler...), and the breakdown of the last frame for the metrics overlay.

A frame goes from begin_frame() to end_frame(); inside it, each scope() times the part of the code it lives in, and scopes nest. A scope entered several times in a frame at the same place, as a system at each fixed step, adds up into one entry. Off (set_enabled(false)), the scopes do nothing but check it. Not thread-safe: one profiler per thread.

core::Profiler profiler;
systems.set_profiler(&profiler); // a scope per system of the scheduler
// Each frame:
profiler.begin_frame();
{
const auto update = profiler.scope("update");
systems.update(registry, step);
}
{
const auto render = profiler.scope("render");
...
overlay.draw(renderer, font, metrics, registry.size(), core::profile_lines(profiler));
}
profiler.end_frame();
Timers around the parts of a frame (its phases, each system of a ecs::Scheduler......
Definition profiler.hpp:92
void end_frame()
Ends the frame: its breakdown becomes last_frame().
void begin_frame()
Starts a frame, ending the one in progress if end_frame() was not called.
ProfileScope scope(std::string_view name)
Times a scope until the returned object is destroyed.
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.

Member Typedef Documentation

◆ Clock

using aegis2d::core::Profiler::Clock = std::chrono::steady_clock

Clock of the timers.

Constructor & Destructor Documentation

◆ Profiler()

aegis2d::core::Profiler::Profiler ( )
default

Profiler on, with no frame yet.

Member Function Documentation

◆ begin()

void aegis2d::core::Profiler::begin ( std::string_view  name)

Starts timing a scope, as scope() does; end it with end().

Parameters
nameName of the scope.

◆ begin_frame()

void aegis2d::core::Profiler::begin_frame ( )

Starts a frame, ending the one in progress if end_frame() was not called.

◆ dropped()

std::size_t aegis2d::core::Profiler::dropped ( ) const
noexcept

Events not recorded because the trace was full.

Returns
How many, since start_trace().

◆ enabled()

bool aegis2d::core::Profiler::enabled ( ) const
noexcept

Tells whether the timers are on.

Returns
true by default.

◆ end()

void aegis2d::core::Profiler::end ( )

Ends the innermost scope started; does nothing if none is open.

◆ end_frame()

void aegis2d::core::Profiler::end_frame ( )

Ends the frame: its breakdown becomes last_frame().

The scopes still open are ended here first.

◆ last_frame()

std::span< const ProfileEntry > aegis2d::core::Profiler::last_frame ( ) const
noexcept

The breakdown of the last frame ended.

Returns
Its scopes, in the order they were first entered, each after its parent.

◆ last_frame_ms()

double aegis2d::core::Profiler::last_frame_ms ( ) const
noexcept

Duration of the last frame ended, from begin_frame() to end_frame().

Returns
In milliseconds, 0 before the first one.

◆ scope()

ProfileScope aegis2d::core::Profiler::scope ( std::string_view  name)

Times a scope until the returned object is destroyed.

Outside a frame, or off, the scope is not timed.

Parameters
nameName of the scope, as the breakdown shows it.
Returns
The scope: keep it in a variable (const auto timer = profiler.scope("x");).

◆ set_enabled()

void aegis2d::core::Profiler::set_enabled ( bool  enabled)
noexcept

Turns the timers on or off.

Parameters
enabledfalse to make scopes do nothing; the last frame is kept.

◆ start_trace()

void aegis2d::core::Profiler::start_trace ( std::size_t  capacity = 1 '000 '000)

Starts recording every scope timed, and every frame, for a trace.

The events recorded before are forgotten. Recording stops by itself after capacity events (dropped() counts the next ones), so that a forgotten trace does not take all the memory: about 50 bytes per event.

Parameters
capacityMost events kept.

◆ stop_trace()

void aegis2d::core::Profiler::stop_trace ( )
noexcept

Stops recording; the events recorded are kept.

◆ trace()

std::span< const TraceEvent > aegis2d::core::Profiler::trace ( ) const
noexcept

The events recorded, each scope when it ended.

Returns
The events, in the order they ended.

◆ tracing()

bool aegis2d::core::Profiler::tracing ( ) const
noexcept

Tells whether scopes are recorded.

Returns
true between start_trace() and stop_trace().

The documentation for this class was generated from the following file: