Skip to content

Commit 32364ea

Browse files
committed
Updated profiler
1 parent e4ddd4e commit 32364ea

File tree

3 files changed

+1414
-1
lines changed

3 files changed

+1414
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ message(STATUS " CMakeLists: Zest")
66
option(ZING_BUILD_TESTS "Build Tests" ON)
77

88
# Global Settings
9-
set(CMAKE_CXX_STANDARD 20)
9+
set(CMAKE_CXX_STANDARD 23)
1010
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1111
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1212
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

include/zest/time/pico_profiler.h

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#pragma once
2+
3+
#include <thread>
4+
#include <zest/time/timer.h>
5+
#include <zest/math/math.h>
6+
#include <zest/math/math_utils.h>
7+
8+
namespace Zest
9+
{
10+
11+
namespace Profiler
12+
{
13+
14+
struct ProfilerEntry
15+
{
16+
// static infos
17+
const char* szSection;
18+
const char* szFile;
19+
int line;
20+
unsigned int color;
21+
// infos used for rendering infos
22+
int level = 0;
23+
int64_t startTime;
24+
int64_t endTime;
25+
uint32_t parent;
26+
};
27+
28+
struct FrameThreadInfo
29+
{
30+
uint32_t threadIndex;
31+
uint32_t activeEntry;
32+
};
33+
34+
struct Region
35+
{
36+
std::string name;
37+
int64_t startTime;
38+
int64_t endTime;
39+
};
40+
41+
struct Frame : Region
42+
{
43+
uint32_t frameThreadCount = 0;
44+
std::vector<FrameThreadInfo> frameThreads;
45+
};
46+
47+
struct ThreadData
48+
{
49+
bool initialized;
50+
uint32_t callStackDepth = 0;
51+
uint32_t maxLevel = 0;
52+
int64_t minTime;
53+
int64_t maxTime;
54+
uint32_t currentEntry = 0;
55+
bool hidden = false;
56+
std::string name;
57+
std::vector<ProfilerEntry> entries;
58+
std::vector<uint32_t> entryStack;
59+
};
60+
61+
struct ProfileSettings
62+
{
63+
uint32_t MaxThreads = 120;
64+
uint32_t MaxCallStack = 20;
65+
uint32_t MaxEntriesPerThread = 100000;
66+
uint32_t MaxFrames = 10000;
67+
uint32_t MaxRegions = 10000;
68+
};
69+
70+
void SetProfileSettings(const ProfileSettings& settings);
71+
void Init();
72+
void NewFrame();
73+
void NameThread(const char* pszName);
74+
void SetPaused(bool pause);
75+
void BeginRegion();
76+
void EndRegion();
77+
void SetRegionLimit(uint64_t maxTimeNs);
78+
void PushSectionBase(const char*, uint32_t, const char*, int);
79+
void PopSection();
80+
void ShowProfile();
81+
void HideThread();
82+
void Finish();
83+
84+
struct ProfileScope
85+
{
86+
ProfileScope(const char* szSection, uint32_t color, const char* szFile, int line)
87+
{
88+
PushSectionBase(szSection, color, szFile, line);
89+
}
90+
~ProfileScope()
91+
{
92+
PopSection();
93+
}
94+
};
95+
96+
struct RegionScope
97+
{
98+
RegionScope()
99+
{
100+
BeginRegion();
101+
}
102+
~RegionScope()
103+
{
104+
EndRegion();
105+
}
106+
};
107+
#define PROFILE_COL_LOCK 0xFF0000FF
108+
109+
template <class _Mutex>
110+
class profile_lock_guard { // class with destructor that unlocks a mutex
111+
public:
112+
using mutex_type = _Mutex;
113+
114+
explicit profile_lock_guard(_Mutex& _Mtx, const char* name = "Mutex", const char* szFile = nullptr, int line = 0) : _MyMutex(_Mtx) { // construct and lock
115+
PushSectionBase(name, PROFILE_COL_LOCK, szFile, line);
116+
_MyMutex.lock();
117+
PopSection();
118+
}
119+
120+
profile_lock_guard(_Mutex& _Mtx, std::adopt_lock_t) : _MyMutex(_Mtx) {} // construct but don't lock
121+
122+
~profile_lock_guard() noexcept {
123+
_MyMutex.unlock();
124+
}
125+
126+
profile_lock_guard(const profile_lock_guard&) = delete;
127+
profile_lock_guard& operator=(const profile_lock_guard&) = delete;
128+
129+
private:
130+
_Mutex& _MyMutex;
131+
};
132+
133+
#define LOCK_GUARD(var, name) \
134+
::Zest::Profiler::profile_lock_guard name##_lock(var, #name, __FILE__, __LINE__)
135+
136+
const glm::vec4& ColorFromName(const char* pszName, const uint32_t len);
137+
138+
} // namespace Profiler
139+
} // namespace Zest
140+
141+
// PROFILE_SCOPE(MyNameWithoutQuotes)
142+
#define PROFILE_SCOPE(name) \
143+
static const uint32_t name##_color = Zest::ToPackedARGB(Zest::Profiler::ColorFromName(#name, uint32_t(strlen(#name)))); \
144+
Zest::Profiler::ProfileScope name##_scope(#name, name##_color, __FILE__, __LINE__);
145+
146+
// PROFILE_SCOPE(char*, ImColor32 bit value)
147+
#define PROFILE_SCOPE_STR(str, col) \
148+
Zest::Profiler::ProfileScope name##_scope(str, col, __FILE__, __LINE__);
149+
150+
// Mark one extra region
151+
#define PROFILE_REGION(name) \
152+
Zest::Profiler::RegionScope name##_region;
153+
154+
// Give a thread a name.
155+
#define PROFILE_NAME_THREAD(name) \
156+
Zest::Profiler::NameThread(#name);
157+
158+
// Hide a thread. Not sure this is tested or works....
159+
#define PROFILE_HIDE_THREAD() \
160+
Zest::Profiler::HideThread();
161+
162+
163+

0 commit comments

Comments
 (0)