Nebula
Loading...
Searching...
No Matches
profiling.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include "timing/time.h"
11#include "timing/timer.h"
12#include "util/stack.h"
13#include "util/dictionary.h"
14#include "util/stringatom.h"
15#include "util/tupleutility.h"
16#include "threading/thread.h"
18#include <atomic>
19
20//------------------------------------------------------------------------------
26//------------------------------------------------------------------------------
27
28// use these macros to insert markers
29#if NEBULA_ENABLE_PROFILING
30#define N_SCOPE(name, cat) Profiling::ProfilingScopeLock __##name##cat##scope__(#name, #cat, __FILE__, __LINE__);
31#define N_SCOPE_DYN(str, cat) Profiling::ProfilingScopeLock __dynscope##cat##__(str, #cat, __FILE__, __LINE__);
32#define N_SCOPE_ACCUM(name, cat) Profiling::ProfilingScopeLock __##name##cat##scope__(#name, #cat, __FILE__, __LINE__, true);
33#define N_SCOPE_DYN_ACCUM(name, cat) Profiling::ProfilingScopeLock __##name##cat##scope__(#name, #cat, __FILE__, __LINE__, true);
34#define N_MARKER_BEGIN(name, cat) { Profiling::ProfilingScope __##name##cat##scope__ = {#name, #cat, __FILE__, __LINE__, false}; Profiling::ProfilingPushScope(__##name##cat##scope__); }
35#define N_MARKER_DYN_BEGIN(str, cat) { Profiling::ProfilingScope __dynmarker##cat##__ = {str, #cat, __FILE__, __LINE__, false}; Profiling::ProfilingPushScope(__dynmarker##cat##__); }
36#define N_MARKER_END() { Profiling::ProfilingPopScope(); }
37#define N_COUNTER_INCR(name, value) Profiling::ProfilingIncreaseCounter(name, value);
38#define N_COUNTER_DECR(name, value) Profiling::ProfilingDecreaseCounter(name, value);
39#define N_BUDGET_COUNTER_SETUP(name, budget) Profiling::ProfilingSetupBudgetCounter(name, budget);
40#define N_BUDGET_COUNTER_INCR(name, value) Profiling::ProfilingBudgetIncreaseCounter(name, value);
41#define N_BUDGET_COUNTER_DECR(name, value) Profiling::ProfilingBudgetDecreaseCounter(name, value);
42#define N_BUDGET_COUNTER_RESET(name) Profiling::ProfilingBudgetResetCounter(name);
43#define N_DECLARE_COUNTER(name, label) static const char* name = #label;
44#else
45#define N_SCOPE(name, cat)
46#define N_SCOPE_DYN(str, cat)
47#define N_SCOPE_ACCUM(name, cat)
48#define N_SCOPE_DYN_ACCUM(name, cat)
49#define N_MARKER_BEGIN(name, cat)
50#define N_MARKER_END()
51#define N_COUNTER_INCR(name, value)
52#define N_COUNTER_DECR(name, value)
53#define N_DECLARE_COUNTER(name, label)
54#endif
55
56namespace Profiling
57{
58
59struct ProfilingScope;
60struct ProfilingContext;
61
63void ProfilingPushScope(const ProfilingScope& scope);
70
73
79void ProfilingClear();
80
85
88
90void ProfilingIncreaseCounter(const char* id, uint64 value);
92void ProfilingDecreaseCounter(const char* id, uint64 value);
95
97void ProfilingSetupBudgetCounter(const char* id, uint64 budget);
99void ProfilingBudgetIncreaseCounter(const char* id, uint64 value);
101void ProfilingBudgetDecreaseCounter(const char* id, uint64 value);
103void ProfilingBudgetResetCounter(const char* id);
106
110
112{
115 : name(nullptr)
116 , category(nullptr)
117 , file(nullptr)
118 , line(-1)
119 , accum(false)
120 , start(0)
121 , duration(0)
122 {};
123
125 ProfilingScope(const char* name, Util::StringAtom category, const char* file, int line, bool accum)
126 : name(name)
128 , file(file)
129 , line(line)
130 , accum(accum)
131 , start(0)
132 , duration(0)
133 {};
134
135 const char* name;
137 const char* file;
138 int line;
139 bool accum;
140
144};
145
148{
150 ProfilingScopeLock(const char* name, Util::StringAtom category, const char* file, int line, bool accum = false)
151 {
152 scope.name = name;
153 scope.category = category;
154 scope.file = file;
155 scope.line = line;
156 scope.accum = accum;
158 }
159
166};
167
182
183} // namespace Profiling
Critical section objects are used to protect a portion of code from parallel execution.
A timer object is the most basic object for time measurement.
Nebula's dynamic array class.
Definition array.h:60
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
Nebula's stack class (a FILO container).
Definition stack.h:19
A StringAtom.
Definition stringatom.h:22
Definition profiling.cc:9
const Util::Dictionary< const char *, Util::Pair< uint64, uint64 > > & ProfilingGetBudgetCounters()
Return set of budget counters.
Definition profiling.cc:257
void ProfilingSetupBudgetCounter(const char *id, uint64 budget)
Setup a profiling budget counter.
Definition profiling.cc:206
void ProfilingNewFrame()
pushes an 'end of frame' marker, only available on the main thread
Definition profiling.cc:84
void ProfilingClear()
clear all scopes
Definition profiling.cc:150
const Util::Dictionary< const char *, uint64 > & ProfilingGetCounters()
return table of counters
Definition profiling.cc:197
void ProfilingIncreaseCounter(const char *id, uint64 value)
increment profiling counter
Definition profiling.cc:167
void ProfilingBudgetIncreaseCounter(const char *id, uint64 value)
Increment budget counter.
Definition profiling.cc:222
Threading::AtomicCounter ProfilingContextCounter
atomic counter used to give each thread a unique id
Definition profiling.cc:15
void ProfilingPopScope()
pop scope from scope stack
Definition profiling.cc:38
Util::Dictionary< const char *, uint64 > counters
Definition profiling.cc:160
const Util::Array< ProfilingContext > ProfilingGetContexts()
get all profiling contexts
Definition profiling.cc:141
void ProfilingBudgetResetCounter(const char *id)
Reset budget counter.
Definition profiling.cc:244
Threading::CriticalSection counterLock
Definition profiling.cc:159
const Util::Array< ProfilingScope > & ProfilingGetScopes(Threading::ThreadId thread)
get all top level scopes based on thread, only run when you know the thread is finished
Definition profiling.cc:128
void ProfilingDecreaseCounter(const char *id, uint64 value)
decrement profiling counter
Definition profiling.cc:184
Util::Array< Threading::CriticalSection * > contextMutexes
Definition profiling.cc:12
Util::Dictionary< const char *, Util::Pair< uint64, uint64 > > budgetCounters
Definition profiling.cc:161
Util::Dictionary< Util::StringAtom, Util::Array< ProfilingScope > > scopesByCategory
Definition profiling.cc:13
void ProfilingBudgetDecreaseCounter(const char *id, uint64 value)
Decrement budget counter.
Definition profiling.cc:233
Threading::CriticalSection categoryLock
Definition profiling.cc:14
void ProfilingRegisterThread()
register a new thread for the profiling
Definition profiling.cc:114
void ProfilingPushScope(const ProfilingScope &scope)
push scope to scope stack
Definition profiling.cc:22
Util::Array< ProfilingContext > profilingContexts
Definition profiling.cc:11
Timing::Time ProfilingGetTime()
get current frametime
Definition profiling.cc:103
The Jobs2 system provides a set of threads and a pool of jobs from which threads can pickup work.
Definition jobs2.h:16
pthread_t ThreadId
Definition linuxthreadid.h:15
volatile int AtomicCounter
Definition interlocked.h:19
double Time
the time datatype
Definition time.h:18
thread context of profiling
Definition profiling.h:170
Timing::Timer timer
Definition profiling.h:178
Util::Stack< ProfilingScope > scopes
Definition profiling.h:175
Util::StringAtom threadName
Definition profiling.h:179
ProfilingContext()
Definition profiling.h:171
Threading::ThreadId threadId
Definition profiling.h:180
Util::Array< ProfilingScope > topLevelScopes
Definition profiling.h:176
Definition profiling.h:112
Util::StringAtom category
Definition profiling.h:136
const char * file
Definition profiling.h:137
bool accum
Definition profiling.h:139
ProfilingScope()
default constructor
Definition profiling.h:114
const char * name
Definition profiling.h:135
ProfilingScope(const char *name, Util::StringAtom category, const char *file, int line, bool accum)
constructor
Definition profiling.h:125
Timing::Time duration
Definition profiling.h:142
int line
Definition profiling.h:138
Timing::Time start
Definition profiling.h:141
Util::Array< ProfilingScope > children
Definition profiling.h:143
convenience class used to automatically push and pop scopes
Definition profiling.h:148
ProfilingScopeLock(const char *name, Util::StringAtom category, const char *file, int line, bool accum=false)
constructor
Definition profiling.h:150
~ProfilingScopeLock()
destructor
Definition profiling.h:161
ProfilingScope scope
Definition profiling.h:165
Typedefs for the Timing subsystem.
uint64_t uint64
Definition types.h:36