SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::time::Stopwatch Struct Reference

Lightweight elapsed-cycle measurement utility. More...

#include <stk_time_util.h>

Public Member Functions

 Stopwatch ()
 Construct a Stopwatch.
template<typename Func>
void Start (Func now_func)
 Capture the start time.
template<typename Func>
Cycles Update (Func now_func)
 Return the number of cycles elapsed since the last Start() or Update().

Protected Attributes

Cycles m_prev
 Counter value captured at the last Start() or Update() call.

Static Private Attributes

static constexpr Cycles NOT_STARTED = 0ULL
 Value of m_prev meaning that stopwatch is not started.

Detailed Description

Lightweight elapsed-cycle measurement utility.

Measures the number of CPU cycles (or other monotonic counter units) that have elapsed between successive calls to Update(). The time source is supplied by the caller as a callable (function pointer, lambda, or functor), allowing the stopwatch to remain independent of any particular hardware counter.

Typical usage:

sw.Start([]() { return stk::hw::HiResClock::GetCycles(); });
// ... work to be measured ...
Cycles elapsed = sw.Update([]() { return stk::hw::HiResClock::GetCycles(); });
// elapsed holds the number of cycles since the last Start() or Update().
uint64_t Cycles
Cycles value.
Definition stk_common.h:168
static Cycles GetCycles()
Get number of clock cycles elapsed.
Lightweight elapsed-cycle measurement utility.
void Start(Func now_func)
Capture the start time.
Cycles Update(Func now_func)
Return the number of cycles elapsed since the last Start() or Update().
Note
Not thread-safe. Intended for use within a single task or ISR context.
If Update() is called before Start(), the first call is treated as a start point and returns 0 cycles elapsed.
Wrap-around of the underlying counter is handled naturally by unsigned subtraction, provided the elapsed time does not exceed one full counter period.

Definition at line 156 of file stk_time_util.h.

Constructor & Destructor Documentation

◆ Stopwatch()

stk::time::Stopwatch::Stopwatch ( )
inline

Construct a Stopwatch.

Note
The stopwatch is initially stopped. Call Start() before the first meaningful Update() call; otherwise the first Update() call will implicitly act as the start point and return 0.

Definition at line 166 of file stk_time_util.h.

167 {}
Cycles m_prev
Counter value captured at the last Start() or Update() call.
static constexpr Cycles NOT_STARTED
Value of m_prev meaning that stopwatch is not started.

References m_prev, and NOT_STARTED.

Member Function Documentation

◆ Start()

template<typename Func>
void stk::time::Stopwatch::Start ( Func now_func)
inline

Capture the start time.

Template Parameters
FuncCallable type returning a Cycles value.
Parameters
[in]now_funcCallable invoked with no arguments that returns the current time as a Cycles value (e.g. a CPU cycle counter read function or a lambda wrapping one).
Note
Resets the internal reference point to the current counter value. The next Update() call will measure elapsed cycles from this moment.

Definition at line 178 of file stk_time_util.h.

179 {
180 m_prev = static_cast<Cycles>(now_func());
181 }

References m_prev.

◆ Update()

template<typename Func>
Cycles stk::time::Stopwatch::Update ( Func now_func)
inline

Return the number of cycles elapsed since the last Start() or Update().

Template Parameters
FuncCallable type returning a Cycles value.
Parameters
[in]now_funcCallable invoked with no arguments that returns the current time as a Cycles value. Must be the same counter source used in the preceding Start() call.
Returns
Elapsed Cycles since the previous reference point. Returns 0 on the very first call when Start() was not called beforehand (the call itself becomes the new reference point).
Note
Updates the internal reference point to the current counter value, so each successive call measures the interval since the previous one.

Definition at line 195 of file stk_time_util.h.

196 {
197 Cycles now = static_cast<Cycles>(now_func());
198
200 {
201 m_prev = now;
202 }
203
204 Cycles diff = now - m_prev;
205 m_prev = now;
206
207 return diff;
208 }
#define STK_UNLIKELY(x)
Provides a compiler hint that the given expression is highly unlikely to evaluate to true (typically ...
Definition stk_defs.h:734

References m_prev, NOT_STARTED, and STK_UNLIKELY.

Member Data Documentation

◆ m_prev

Cycles stk::time::Stopwatch::m_prev
protected

Counter value captured at the last Start() or Update() call.

Definition at line 211 of file stk_time_util.h.

Referenced by Start(), Stopwatch(), and Update().

◆ NOT_STARTED

Cycles stk::time::Stopwatch::NOT_STARTED = 0ULL
staticconstexprprivate

Value of m_prev meaning that stopwatch is not started.

Definition at line 158 of file stk_time_util.h.

Referenced by Stopwatch(), and Update().


The documentation for this struct was generated from the following file: