|
VLink
2.0.0
A high-performance communication middleware
|
Atomic high-resolution timer with selectable clock source and unit. 更多...
#include <elapsed_timer.h>
Public 类型 | |
| enum | Method : uint8_t { kCpuTimestamp = 0 , kCpuActiveTime = 1 } |
| Clock source selector. 更多... | |
| enum | Accuracy : uint8_t { kMilli = 0 , kMicro = 1 , kNano = 2 } |
| Precision selector for stored and returned time values. 更多... | |
Public 成员函数 | |
| ElapsedTimer () noexcept | |
Constructs a timer with default method (kCpuTimestamp) and accuracy (kMilli). 更多... | |
| ElapsedTimer (Method method) noexcept | |
| Constructs a timer with the given clock source and default millisecond accuracy. 更多... | |
| ElapsedTimer (Accuracy accuracy) noexcept | |
| Constructs a timer with the default clock source and the given accuracy. 更多... | |
| ElapsedTimer (Method method, Accuracy accuracy) noexcept | |
| Constructs a timer with the given clock source and accuracy. 更多... | |
| ElapsedTimer (const ElapsedTimer &target) noexcept | |
| Copy constructor; copies the snapshot value and configuration. 更多... | |
| ElapsedTimer (ElapsedTimer &&target) noexcept | |
| Move constructor; equivalent to copy for this snapshot-valued type. 更多... | |
| ~ElapsedTimer () noexcept | |
| Destructor. 更多... | |
| ElapsedTimer & | operator= (const ElapsedTimer &target) noexcept |
| Copy assignment; copies the snapshot value and configuration. 更多... | |
| ElapsedTimer & | operator= (ElapsedTimer &&) noexcept |
| Move assignment; equivalent to copy assignment for this snapshot-valued type. 更多... | |
| Method | get_method () const noexcept |
| Returns the clock source this timer was configured with. 更多... | |
| Accuracy | get_accuracy () const noexcept |
| Returns the precision this timer was configured with. 更多... | |
| void | start () noexcept |
| Records the current time as the start reference if the timer is inactive. 更多... | |
| void | stop () noexcept |
Marks the timer inactive by writing -1 into the start reference. 更多... | |
| int64_t | restart () noexcept |
| Atomically reads the elapsed time and starts a new measurement from now. 更多... | |
| bool | is_active () const noexcept |
| Reports whether the timer is currently measuring. 更多... | |
| int64_t | get () const noexcept |
Returns the elapsed time since start without altering state. 更多... | |
静态 Public 成员函数 | |
| static uint64_t | get_sys_timestamp (Accuracy accuracy=kMilli, bool high_resolution=true) noexcept |
| Returns the current wall-clock timestamp. 更多... | |
| static uint64_t | get_cpu_timestamp (Accuracy accuracy=kMilli, bool high_resolution=true) noexcept |
| Returns the current monotonic CPU timestamp. 更多... | |
| static uint64_t | get_cpu_active_time (Accuracy accuracy=kMilli) noexcept |
| Returns the cumulative CPU time consumed by the current process. 更多... | |
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.
| 枚举值 | |
|---|---|
| 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.
|
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.