|
VLink
2.3.0
A high-performance communication middleware
|
Atomic high-resolution timer with selectable clock source and unit. More...
#include <elapsed_timer.h>
Public Types | |
| enum | Method : uint8_t { kCpuTimestamp = 0 , kCpuActiveTime = 1 } |
| Clock source selector. More... | |
| enum | Accuracy : uint8_t { kMilli = 0 , kMicro = 1 , kNano = 2 } |
| Precision selector for stored and returned time values. More... | |
Public Member Functions | |
| ElapsedTimer () noexcept | |
Constructs a timer with default method (kCpuTimestamp) and accuracy (kMilli). More... | |
| ElapsedTimer (Method method) noexcept | |
| Constructs a timer with the given clock source and default millisecond accuracy. More... | |
| ElapsedTimer (Accuracy accuracy) noexcept | |
| Constructs a timer with the default clock source and the given accuracy. More... | |
| ElapsedTimer (Method method, Accuracy accuracy) noexcept | |
| Constructs a timer with the given clock source and accuracy. More... | |
| ElapsedTimer (const ElapsedTimer &target) noexcept | |
| Copy constructor; copies the snapshot value and configuration. More... | |
| ElapsedTimer (ElapsedTimer &&target) noexcept | |
| Move constructor; equivalent to copy for this snapshot-valued type. More... | |
| ~ElapsedTimer () noexcept | |
| Destructor. More... | |
| ElapsedTimer & | operator= (const ElapsedTimer &target) noexcept |
| Copy assignment; copies the snapshot value and configuration. More... | |
| ElapsedTimer & | operator= (ElapsedTimer &&) noexcept |
| Move assignment; equivalent to copy assignment for this snapshot-valued type. More... | |
| Method | get_method () const noexcept |
| Returns the clock source this timer was configured with. More... | |
| Accuracy | get_accuracy () const noexcept |
| Returns the precision this timer was configured with. More... | |
| void | start () noexcept |
| Records the current time as the start reference if the timer is inactive. More... | |
| void | stop () noexcept |
Marks the timer inactive by writing -1 into the start reference. More... | |
| int64_t | restart () noexcept |
| Atomically reads the elapsed time and starts a new measurement from now. More... | |
| bool | is_active () const noexcept |
| Reports whether the timer is currently measuring. More... | |
| int64_t | get () const noexcept |
Returns the elapsed time since start without altering state. More... | |
Static Public Member Functions | |
| static uint64_t | get_sys_timestamp (Accuracy accuracy=kMilli, bool high_resolution=true) noexcept |
| Returns the current wall-clock timestamp. More... | |
| static uint64_t | get_cpu_timestamp (Accuracy accuracy=kMilli, bool high_resolution=true) noexcept |
| Returns the current monotonic CPU timestamp. More... | |
| static uint64_t | get_cpu_active_time (Accuracy accuracy=kMilli) noexcept |
| Returns the cumulative CPU time consumed by the current process. More... | |
Atomic high-resolution timer with selectable clock source and unit.
Records the elapsed time between start and get / restart. Marked final. Copy and move constructors copy the snapshot value (not exclusive ownership), allowing trivial propagation through wrapper structures.
| enum vlink::ElapsedTimer::Accuracy : uint8_t |
| enum vlink::ElapsedTimer::Method : uint8_t |
Clock source selector.
| Enumerator | |
|---|---|
| kCpuTimestamp | Monotonic wall clock ( |
| kCpuActiveTime | Process CPU time (user + kernel via |
|
noexcept |
Constructs a timer with default method (kCpuTimestamp) and accuracy (kMilli).
The timer is inactive on construction; call start to begin measuring.
|
explicitnoexcept |
Constructs a timer with the given clock source and default millisecond accuracy.
| method | Clock source. |
|
explicitnoexcept |
Constructs a timer with the default clock source and the given accuracy.
| accuracy | Precision of returned values. |
Constructs a timer with the given clock source and accuracy.
| method | Clock source. |
| accuracy | Precision of returned values. |
|
noexcept |
Copy constructor; copies the snapshot value and configuration.
| target | Source timer. |
|
noexcept |
Move constructor; equivalent to copy for this snapshot-valued type.
| target | Source timer. |
|
noexcept |
Destructor.
|
noexcept |
Returns the elapsed time since start without altering state.
-1 when inactive.
|
noexcept |
Returns the precision this timer was configured with.
|
staticnoexcept |
Returns the cumulative CPU time consumed by the current process.
On POSIX uses getrusage(RUSAGE_SELF); on Windows uses GetProcessTimes. The value is cumulative; subtract two samples to obtain a delta.
| accuracy | Desired precision. Default: kMilli. |
|
staticnoexcept |
Returns the current monotonic CPU timestamp.
On Linux uses CLOCK_MONOTONIC_RAW (immune to NTP) when high_resolution is true; otherwise uses std::chrono::steady_clock. Windows always uses steady_clock.
| accuracy | Desired precision. Default: kMilli. |
| high_resolution | Linux-only switch for clock_gettime. Default: true. |
|
noexcept |
Returns the clock source this timer was configured with.
|
staticnoexcept |
Returns the current wall-clock timestamp.
On Linux uses clock_gettime with CLOCK_REALTIME when high_resolution is true, falling back to std::chrono::system_clock otherwise. On Windows always uses std::chrono::system_clock.
| accuracy | Desired precision. Default: kMilli. |
| high_resolution | Linux-only switch for clock_gettime. Default: true. |
|
noexcept |
Reports whether the timer is currently measuring.
true when start has produced a non-sentinel reference.
|
noexcept |
Copy assignment; copies the snapshot value and configuration.
| target | Source timer. |
*this.
|
noexcept |
Move assignment; equivalent to copy assignment for this snapshot-valued type.
*this.
|
noexcept |
Atomically reads the elapsed time and starts a new measurement from now.
Performs a single atomic exchange. When the timer was already active the returned value is the elapsed time since the last start; when it was inactive the return is negative (the raw sentinel) and the timer is left active starting from this call. Overlapping restarts whose clock samples arrive out of order return zero elapsed time.
|
noexcept |
Records the current time as the start reference if the timer is inactive.
Uses a compare-and-swap on the internal atomic so concurrent calls compete and only one wins. Already-active timers are unchanged; call stop first to rearm.
|
noexcept |
Marks the timer inactive by writing -1 into the start reference.
After stop the timer reports is_active() == false and get() returns -1.