-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfiler.cpp
More file actions
105 lines (85 loc) · 2.52 KB
/
Profiler.cpp
File metadata and controls
105 lines (85 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Profiler.hpp"
#include <cstring>
#include <algorithm>
namespace Caffeine::Debug {
Profiler& Profiler::instance() {
static Profiler s;
return s;
}
void Profiler::beginScope(const char* name) {
if (!m_enabled) return;
InternalScopeData* scope = findOrCreateScope(name);
if (!scope) return;
scope->activeTimer.reset();
scope->activeTimer.start();
}
void Profiler::endScope(const char* name) {
if (!m_enabled) return;
InternalScopeData* scope = findScope(name);
if (!scope) return;
scope->activeTimer.stop();
f64 ms = scope->activeTimer.elapsed().millis();
scope->callCount++;
scope->totalMs += ms;
if (ms < scope->minMs) scope->minMs = ms;
if (ms > scope->maxMs) scope->maxMs = ms;
}
void Profiler::report(Vector<ScopeStats>& out) const {
out.clear();
for (usize i = 0; i < m_scopeCount; ++i) {
const auto& s = m_scopes[i];
ScopeStats stats;
stats.name = s.name;
stats.callCount = s.callCount;
stats.totalMs = s.totalMs;
stats.avgMs = (s.callCount > 0) ? s.totalMs / static_cast<f64>(s.callCount) : 0.0;
stats.minMs = s.minMs;
stats.maxMs = s.maxMs;
out.pushBack(stats);
}
}
void Profiler::reset() {
m_scopeCount = 0;
for (usize i = 0; i < MAX_SCOPES; ++i) {
m_scopes[i] = InternalScopeData{};
}
}
usize Profiler::scopeCount() const {
return m_scopeCount;
}
Profiler::InternalScopeData* Profiler::findScope(const char* name) {
for (usize i = 0; i < m_scopeCount; ++i) {
if (strcmp(m_scopes[i].name, name) == 0) {
return &m_scopes[i];
}
}
return nullptr;
}
const Profiler::InternalScopeData* Profiler::findScope(const char* name) const {
for (usize i = 0; i < m_scopeCount; ++i) {
if (strcmp(m_scopes[i].name, name) == 0) {
return &m_scopes[i];
}
}
return nullptr;
}
Profiler::InternalScopeData* Profiler::findOrCreateScope(const char* name) {
InternalScopeData* existing = findScope(name);
if (existing) return existing;
if (m_scopeCount >= MAX_SCOPES) return nullptr;
auto& scope = m_scopes[m_scopeCount];
scope.name = name;
scope.callCount = 0;
scope.totalMs = 0.0;
scope.minMs = 1e18;
scope.maxMs = 0.0;
++m_scopeCount;
return &scope;
}
ProfileScope::ProfileScope(const char* name) : m_name(name) {
Profiler::instance().beginScope(m_name);
}
ProfileScope::~ProfileScope() {
Profiler::instance().endScope(m_name);
}
} // namespace Caffeine::Debug