TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_profiler.h
Go to the documentation of this file.
1#pragma once
2
3// Hierarchical CPU timing: scoped zones accumulated into a per-frame tree, keyed by
4// the hash of their call path (parent path + name) so nodes are stable across frames.
5//
6// Hot path: each TACHYON_PROFILE_SCOPE site caches its node index, and timestamps are raw
7// ticks (rdtsc on x86) converted to ms only at frame end.
8//
9// See engine design doc 28 (Diagnostics).
10
11#include <stdint.h>
12#include <span>
13
14#include <gui_core/gui_base_types.h> // SteadyClockNowNanos
15#include <gui_core/gui_hash.h> // Hash::CalculateConst / Combine
16
18{
21
24 class ProfilerCollector;
25
26 ProfilerCollector& MainThreadCollector();
27
29 void SetProfilingEnabled(bool enabled);
30
33 void BeginFrame(ProfilerCollector& collector);
34 void EndFrame (ProfilerCollector& collector);
36
38 void Reset(ProfilerCollector& collector);
39
41 void SetSmoothingWindow(ProfilerCollector& collector, int64_t nanoseconds);
42
47 {
48 uint64_t nameHash = 0;
49 const char* name = "";
50 const char* file = "";
51 int line = 0;
52 bool isWait = false;
53
54 uint64_t cachedKey = 0;
55 int32_t cachedIndex = -1;
56 uint32_t cachedGeneration = 0;
57 };
58
62 void EnterScope(ProfilerCollector& collector, ScopeLocation& loc) noexcept;
63 void ExitScope (ProfilerCollector& collector) noexcept;
65
67 void EnterScope(ProfilerCollector& collector, uint64_t nameHash, const char* name,
68 bool isWait = false, const char* file = "", int line = 0) noexcept;
69
72 {
73 public:
74 ScopedTimer(ProfilerCollector& collector, ScopeLocation& loc) noexcept
75 : m_collector(collector), m_armed(IsProfilingEnabled())
76 {
77 if (m_armed) EnterScope(m_collector, loc);
78 }
79 ~ScopedTimer() noexcept { if (m_armed) ExitScope(m_collector); }
80
81 ScopedTimer(const ScopedTimer&) = delete;
83
84 private:
85 ProfilerCollector& m_collector;
86 bool m_armed;
87 };
88
91 {
92 uint64_t key = 0;
93 uint64_t parentKey = 0;
94 const char* name = "";
95 const char* file = "";
96 int32_t line = 0;
97 int32_t depth = 0;
98 int32_t callCount = 0;
99 double totalMs = 0.0;
100 double selfMs = 0.0;
101 double minMs = 0.0;
102 double maxMs = 0.0;
104 double frameTotalMs = 0.0;
105 double frameSelfMs = 0.0;
106 bool isWait = false;
107 };
108
110 std::span<const ProfileSample> LastFrameSamples(const ProfilerCollector& collector);
111
113 double LastFrameTotalMs(const ProfilerCollector& collector);
115}
116
117// Define TACHYON_PROFILE_ENABLED=0 to compile every macro out.
118#ifndef TACHYON_PROFILE_ENABLED
119# define TACHYON_PROFILE_ENABLED 1
120#endif
121
122#if TACHYON_PROFILE_ENABLED
123# define TACHYON_PROFILE_CONCAT2(a, b) a##b
124# define TACHYON_PROFILE_CONCAT(a, b) TACHYON_PROFILE_CONCAT2(a, b)
125
126 // name must be a string literal so the hash folds at compile time.
127# define TACHYON_PROFILE_SCOPE(name) \
128 static ::tachyon::Diagnostics::ScopeLocation TACHYON_PROFILE_CONCAT(s_profLoc_, __LINE__) \
129 { ::tachyon::Hash::CalculateConst(name), name, __FILE__, __LINE__, false }; \
130 ::tachyon::Diagnostics::ScopedTimer TACHYON_PROFILE_CONCAT(profTimer_, __LINE__)( \
131 ::tachyon::Diagnostics::MainThreadCollector(), TACHYON_PROFILE_CONCAT(s_profLoc_, __LINE__))
132
133# define TACHYON_PROFILE_SCOPE_WAIT(name) \
134 static ::tachyon::Diagnostics::ScopeLocation TACHYON_PROFILE_CONCAT(s_profLoc_, __LINE__) \
135 { ::tachyon::Hash::CalculateConst(name), name, __FILE__, __LINE__, true }; \
136 ::tachyon::Diagnostics::ScopedTimer TACHYON_PROFILE_CONCAT(profTimer_, __LINE__)( \
137 ::tachyon::Diagnostics::MainThreadCollector(), TACHYON_PROFILE_CONCAT(s_profLoc_, __LINE__))
138
139 // Times a scope from a caller-owned ScopeLocation (e.g. an entry of a per-stage table).
140# define TACHYON_PROFILE_SCOPE_REF(locationLValue) \
141 ::tachyon::Diagnostics::ScopedTimer TACHYON_PROFILE_CONCAT(profTimer_, __LINE__)( \
142 ::tachyon::Diagnostics::MainThreadCollector(), (locationLValue))
143
144 // Root frame scope plus collector frame boundaries; wrap the whole loop iteration.
145# define TACHYON_PROFILE_FRAME_BEGIN(name) \
146 do { \
147 static ::tachyon::Diagnostics::ScopeLocation TACHYON_PROFILE_CONCAT(s_frameLoc_, __LINE__) \
148 { ::tachyon::Hash::CalculateConst(name), name, __FILE__, __LINE__, false }; \
149 ::tachyon::Diagnostics::BeginFrame(::tachyon::Diagnostics::MainThreadCollector()); \
150 ::tachyon::Diagnostics::EnterScope(::tachyon::Diagnostics::MainThreadCollector(), \
151 TACHYON_PROFILE_CONCAT(s_frameLoc_, __LINE__)); \
152 } while (0)
153# define TACHYON_PROFILE_FRAME_END() \
154 do { \
155 ::tachyon::Diagnostics::ExitScope(::tachyon::Diagnostics::MainThreadCollector()); \
156 ::tachyon::Diagnostics::EndFrame(::tachyon::Diagnostics::MainThreadCollector()); \
157 } while (0)
158#else
159# define TACHYON_PROFILE_SCOPE(name) ((void)0)
160# define TACHYON_PROFILE_SCOPE_WAIT(name) ((void)0)
161# define TACHYON_PROFILE_SCOPE_REF(locationLValue) ((void)0)
162# define TACHYON_PROFILE_FRAME_BEGIN(name) ((void)0)
163# define TACHYON_PROFILE_FRAME_END() ((void)0)
164#endif
ScopedTimer(const ScopedTimer &)=delete
~ScopedTimer() noexcept
Definition gui_profiler.h:79
ScopedTimer & operator=(const ScopedTimer &)=delete
ScopedTimer(ProfilerCollector &collector, ScopeLocation &loc) noexcept
Definition gui_profiler.h:74
void Reset(ProfilerCollector &collector)
Discards all nodes and history; cached scope sites re-resolve.
void BeginFrame(ProfilerCollector &collector)
Frame boundaries: call once each, around all scopes for the frame.
double LastFrameTotalMs(const ProfilerCollector &collector)
Smoothed total of the root scope(s): the frame total, in milliseconds.
ProfilerCollector & MainThreadCollector()
void EnterScope(ProfilerCollector &collector, ScopeLocation &loc) noexcept
Cached scope entry / exit, as the macros use.
void ExitScope(ProfilerCollector &collector) noexcept
Cached scope entry / exit, as the macros use.
void SetProfilingEnabled(bool enabled)
std::span< const ProfileSample > LastFrameSamples(const ProfilerCollector &collector)
The nodes of the last finalized frame, depth-first. Valid until the next EndFrame.
void SetSmoothingWindow(ProfilerCollector &collector, int64_t nanoseconds)
How long the smoothed durations average over: 250 ms by default, as the FPS readout.
void EndFrame(ProfilerCollector &collector)
Frame boundaries: call once each, around all scopes for the frame.
Definition gui_logging.h:24
One finalized node, depth-first. Durations are windowed averages (~4 updates/s).
Definition gui_profiler.h:91
const char * name
Definition gui_profiler.h:94
double minMs
Definition gui_profiler.h:101
int32_t depth
Definition gui_profiler.h:97
uint64_t parentKey
Definition gui_profiler.h:93
bool isWait
Definition gui_profiler.h:106
double selfMs
Definition gui_profiler.h:100
double totalMs
Definition gui_profiler.h:99
double frameTotalMs
This frame alone, not smoothed: for measuring a fixed run of frames.
Definition gui_profiler.h:104
const char * file
Definition gui_profiler.h:95
int32_t callCount
Definition gui_profiler.h:98
double frameSelfMs
Definition gui_profiler.h:105
int32_t line
Definition gui_profiler.h:96
double maxMs
Definition gui_profiler.h:102
uint64_t key
Definition gui_profiler.h:92
Per-call-site descriptor, static at each TACHYON_PROFILE_SCOPE site.
Definition gui_profiler.h:47
uint32_t cachedGeneration
Definition gui_profiler.h:56
uint64_t cachedKey
Definition gui_profiler.h:54
int line
Definition gui_profiler.h:51
const char * name
Definition gui_profiler.h:49
int32_t cachedIndex
Definition gui_profiler.h:55
uint64_t nameHash
Definition gui_profiler.h:48
const char * file
Definition gui_profiler.h:50
bool isWait
Definition gui_profiler.h:52