wavelength 1.0.0
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5#include "os/cycles.h"
6
16class Timer {
17public:
20 mRunning = 0;
21 mCycles = 0;
22 mLastMs = 0.0f;
23 }
24
28 void Reset() {
29 const float fMs = static_cast<float>(mCycles) * gSystemCycles2Ms;
30 mRunning = 0;
31 mCycles = 0;
32 mLastMs = fMs;
33 }
34
39 void Pause() {
40 if (mRunning > 0) {
41 mRunning = -mRunning;
42 mCycles += ReadCycleCount() - mStart;
43 }
44 }
45
49 void Resume() {
50 if (mRunning < 0) {
51 mRunning = -mRunning;
52 mStart = ReadCycleCount();
53 }
54 }
55
62 void Split() {
63 if (mRunning == 1) {
64 const unsigned nCount = ReadCycleCount();
65 mCycles = nCount - mStart;
66 mStart = nCount;
67 }
68 }
69
73 void Start() {
74 if (mRunning++ == 0) {
75 mStart = ReadCycleCount();
76 }
77 }
78
82 void Stop() {
83 if (--mRunning == 0) {
84 mCycles += ReadCycleCount() - mStart;
85 }
86 }
87
96 static Timer *Find(const char *pszName);
97
103 static std::vector<Timer> sTimers;
104
106 unsigned GetCycles() const {
107 return mCycles;
108 }
109
111 void SetCycles(unsigned nCycles) {
112 mCycles = nCycles;
113 }
114
116 float GetLastMs() const {
117 return mLastMs;
118 }
119
121 void SetLastMs(float fLastMs) {
122 mLastMs = fLastMs;
123 }
124
126 const char *GetName() const {
127 return mName;
128 }
129
131 void SetName(const char *pszName) {
132 mName = pszName;
133 }
134
136 void SetRunning(int nRunning) {
137 mRunning = nRunning;
138 }
139
140private:
141 unsigned mStart;
142 unsigned mCycles;
143 float mLastMs;
144 const char *mName;
145 int mRunning;
146};
147
155void TimerSleep(int nMs);
156
164
172
void SetLastMs(float fLastMs)
Definition timer.h:121
void SetName(const char *pszName)
Definition timer.h:131
void Split()
Restart the measurement at the current counter reading.
Definition timer.h:62
void Start()
Raise the start count, and take the counter reading when the count leaves zero.
Definition timer.h:73
void SetRunning(int nRunning)
Definition timer.h:136
static Timer * Find(const char *pszName)
Find a timer of the registry of named timers.
const char * GetName() const
Definition timer.h:126
static std::vector< Timer > sTimers
The registry of named timers.
Definition timer.h:103
Timer()
Construct a stopped timer with no measurement.
Definition timer.h:19
void Stop()
Lower the start count, and add the cycles since the start to mCycles when it arrives at zero.
Definition timer.h:82
void Pause()
Suspend a running timer, adding the cycles since the start to mCycles.
Definition timer.h:39
float GetLastMs() const
Definition timer.h:116
void Resume()
Continue a timer Pause() suspended from the current counter reading.
Definition timer.h:49
void SetCycles(unsigned nCycles)
Definition timer.h:111
void Reset()
Stop the timer and record the cycles measured so far in milliseconds in mLastMs.
Definition timer.h:28
unsigned GetCycles() const
Definition timer.h:106
unsigned ReadCycleCount()
Read the free-running cycle counter.
float gSystemCycles2Ms
Milliseconds per EE cycle.
void TimerSleep(int nMs)
Busy-wait on the system clock.
void TimerTerminate()
Shut the timers down.
void TimerInit()
Set the cycle conversion and restart the system clock from zero.
void TimerLoadNames()
Build the named timers, one for each symbol of the timer block of the system configuration.