Nebula
Loading...
Searching...
No Matches
debugtimer.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/refcounted.h"
13#include "timing/timer.h"
14#include "util/stringatom.h"
15#include "util/ringbuffer.h"
17
18//------------------------------------------------------------------------------
19#if NEBULA_ENABLE_PROFILING
20#define _declare_timer(timer) Ptr<Debug::DebugTimer> timer;
21#define _declare_static_timer(timer) static Ptr<Debug::DebugTimer> timer;
22#define _setup_timer(timer) {timer = Debug::DebugTimer::Create(); timer->Setup(Util::StringAtom(#timer));}
23#define _setup_grouped_timer(timer, group) {timer = Debug::DebugTimer::Create(); timer->Setup(Util::StringAtom(#timer), group);}
24#define _setup_timer_singleton(timer) {timer = Debug::DebugTimer::CreateAsSingleton(Util::StringAtom(#timer));}
25#define _setup_timer_singleton_name(timer, timerName) {timer = Debug::DebugTimer::CreateAsSingleton(Util::StringAtom(#timerName));}
26#define _discard_timer_singleton(timer) {Debug::DebugTimer::DestroySingleton(Util::StringAtom(#timer));}
27#define _discard_timer(timer) timer->Discard(); timer = nullptr;
28#define _start_timer(timer) timer->Start();
29#define _pause_timer(timer) timer->Pause();
30#define _stop_timer(timer) timer->Stop();
31#define _start_accum_timer(timer) timer->StartAccum();
32#define _stop_accum_timer(timer) timer->StopAccum();
33#define _reset_accum_timer(timer) timer->ResetAccum();
34#else
35#define _declare_timer(timer)
36#define _setup_timer(timer)
37#define _setup_grouped_timer(timer, group)
38#define _discard_timer(timer)
39#define _start_timer(timer)
40#define _pause_timer(timer)
41#define _stop_timer(timer)
42#endif
43
44//------------------------------------------------------------------------------
45namespace Debug
46{
48{
50public:
52 DebugTimer();
54 virtual ~DebugTimer();
55
57 void Setup(const Util::StringAtom& timerName, const Util::StringAtom& group = Util::StringAtom("Ungrouped"));
59 void Discard();
61 bool IsValid() const;
62
64 void Start();
66 void Pause();
68 void Stop();
69
71 void StartAccum();
73 void StopAccum();
75 void ResetAccum();
76
78 const Util::StringAtom& GetName() const;
80 const Util::StringAtom& GetGroup() const;
82 Timing::Time GetSample() const;
85
87 static Ptr<DebugTimer> CreateAsSingleton(const Util::StringAtom& timerName);
89 static void DestroySingleton(const Util::StringAtom& timerName);
90
91private:
100};
101
102//------------------------------------------------------------------------------
105inline bool
107{
108 return this->name.IsValid();
109}
110
111//------------------------------------------------------------------------------
114inline void
116{
117 this->timer.Start();
118}
119
120//------------------------------------------------------------------------------
123inline void
125{
126 this->timer.Stop();
127}
128
129//------------------------------------------------------------------------------
132inline void
134{
135 this->timer.Stop();
136 this->critSect.Enter();
137 this->history.Add(this->timer.GetTime() * 1000.0f);
138 this->critSect.Leave();
139 this->timer.Reset();
140}
141
142//------------------------------------------------------------------------------
145inline void
147{
148 this->timer.Start();
149 this->startTime = this->timer.GetTime();
150}
151
152//------------------------------------------------------------------------------
155inline void
157{
158 Timing::Time stop = this->timer.GetTime();
159 Timing::Time diff = stop - this->startTime;
160 this->accumTime += diff;
161 this->timer.Stop();
162}
163
164//------------------------------------------------------------------------------
167inline void
169{
170 if (this->timer.Running())
171 {
172 this->timer.Stop();
173 }
174 if (this->accumTime != 0.0)
175 {
176 this->resultTime = this->accumTime;
177 this->critSect.Enter();
178 this->history.Add(this->resultTime * 1000.0f);
179 this->critSect.Leave();
180 this->timer.Reset();
181 this->accumTime = 0.0;
182 }
183}
184
185//------------------------------------------------------------------------------
188inline const Util::StringAtom&
190{
191 return this->name;
192}
193
194//------------------------------------------------------------------------------
197inline const Util::StringAtom&
199{
200 return this->group;
201}
202
203} // namespace Debug
204//------------------------------------------------------------------------------
The common base class of Nebula.
Definition refcounted.h:38
A debug timer for measuring time spent in code blocks.
Definition debugtimer.h:48
Timing::Time startTime
Definition debugtimer.h:97
Timing::Timer timer
Definition debugtimer.h:95
__DeclareClass(DebugTimer)
Util::Array< Timing::Time > GetHistory() const
get the timer's history
Definition debugtimer.cc:94
virtual ~DebugTimer()
destructor
Definition debugtimer.cc:31
void Stop()
stop the timer, writes sample to history
Definition debugtimer.h:133
void Discard()
discard the timer
Definition debugtimer.cc:62
Timing::Time accumTime
Definition debugtimer.h:96
const Util::StringAtom & GetGroup() const
get the timer group
Definition debugtimer.h:198
void Start()
start or continue the timer
Definition debugtimer.h:115
Util::StringAtom group
Definition debugtimer.h:94
Timing::Time resultTime
Definition debugtimer.h:98
Timing::Time GetSample() const
get the most current sample
Definition debugtimer.cc:78
static void DestroySingleton(const Util::StringAtom &timerName)
create as singleton
Definition debugtimer.cc:121
Threading::CriticalSection critSect
Definition debugtimer.h:92
void StopAccum()
stop the timer, writes sample to history
Definition debugtimer.h:156
void Setup(const Util::StringAtom &timerName, const Util::StringAtom &group=Util::StringAtom("Ungrouped"))
setup the timer
Definition debugtimer.cc:43
const Util::StringAtom & GetName() const
get the timer name
Definition debugtimer.h:189
bool IsValid() const
return true if this timer has been setup
Definition debugtimer.h:106
Util::StringAtom name
Definition debugtimer.h:93
DebugTimer()
constructor
Definition debugtimer.cc:19
Util::RingBuffer< Timing::Time > history
Definition debugtimer.h:99
void Pause()
pause the timer
Definition debugtimer.h:124
void StartAccum()
start or continue the timer
Definition debugtimer.h:146
static Ptr< DebugTimer > CreateAsSingleton(const Util::StringAtom &timerName)
create as singleton
Definition debugtimer.cc:106
void ResetAccum()
stop the timer, writes sample to history
Definition debugtimer.h:168
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
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 ring buffer stores up to a maximum number of elements in a circular fashion.
Definition ringbuffer.h:21
void Add(const TYPE &elm)
add an element to the ring buffer
Definition ringbuffer.h:316
A StringAtom.
Definition stringatom.h:22
bool IsValid() const
return true if valid (contains a non-empty string)
Definition stringatom.h:353
Definition corepagehandler.cc:13
double Time
the time datatype
Definition time.h:18