|
VLink 2.0.0
A high-performance communication middleware
|
Atomic, high-resolution elapsed-time timer. 更多...
#include <elapsed_timer.h>
Public 类型 | |
| enum | Method : uint8_t { kCpuTimestamp = 0 , kCpuActiveTime = 1 } |
| Selects the underlying clock source for time measurement. 更多... | |
| enum | Accuracy : uint8_t { kMilli = 0 , kMicro = 1 , kNano = 2 } |
| Selects the precision (resolution) of the timer values returned. 更多... | |
Public 成员函数 | |
| ElapsedTimer () noexcept | |
Constructs an ElapsedTimer with default method (kCpuTimestamp) and default accuracy (kMilli). | |
| ElapsedTimer (Method method) noexcept | |
Constructs an ElapsedTimer with the specified clock source. | |
| ElapsedTimer (Accuracy accuracy) noexcept | |
Constructs an ElapsedTimer with the specified precision. | |
| ElapsedTimer (Method method, Accuracy accuracy) noexcept | |
Constructs an ElapsedTimer with the specified clock source and precision. | |
| ElapsedTimer (const ElapsedTimer &target) noexcept | |
| Copy constructor. Copies the current start-time snapshot, method, and accuracy. | |
| ElapsedTimer (ElapsedTimer &&target) noexcept | |
| Move constructor. Behaves identically to the copy constructor. | |
| ~ElapsedTimer () noexcept | |
| Destructor. | |
| ElapsedTimer & | operator= (const ElapsedTimer &target) noexcept |
| Copy-assignment operator. | |
| ElapsedTimer & | operator= (ElapsedTimer &&) noexcept |
| Move-assignment operator. | |
| Method | get_method () const noexcept |
| Returns the clock source configured for this timer. | |
| Accuracy | get_accuracy () const noexcept |
| Returns the precision configured for this timer. | |
| void | start () noexcept |
| Starts the timer if it is not already active. | |
| void | stop () noexcept |
Stops the timer, setting the internal start time to -1. | |
| int64_t | restart () noexcept |
Atomically resets the start time to now and returns the elapsed time since the previous start() / restart() call. | |
| bool | is_active () const noexcept |
Returns true when the timer has been started and not yet stopped. | |
| int64_t | get () const noexcept |
Returns the elapsed time since start() was called. | |
静态 Public 成员函数 | |
| static uint64_t | get_sys_timestamp (Accuracy accuracy=kMilli, bool high_resolution=true) noexcept |
| Returns the current wall-clock (system) 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 accumulated CPU time (user + kernel) consumed by the current process. | |
Atomic, high-resolution elapsed-time timer.
Measures elapsed time since start() using either a monotonic wall-clock (kCpuTimestamp) or the process CPU-active time (kCpuActiveTime). The class is final and not copyable (copy/move constructors are provided but only copy the snapshot value, not ownership semantics).
| enum vlink::ElapsedTimer::Accuracy : uint8_t |
| enum vlink::ElapsedTimer::Method : uint8_t |
Selects the underlying clock source for time measurement.
| 枚举值 | |
|---|---|
| kCpuTimestamp | Monotonic wall-clock (CLOCK_MONOTONIC_RAW on Linux) |
| kCpuActiveTime | Process CPU time (user + kernel, via getrusage) |
|
noexcept |
Constructs an ElapsedTimer with default method (kCpuTimestamp) and default accuracy (kMilli).
The timer is not started after construction.
|
explicitnoexcept |
Constructs an ElapsedTimer with the specified clock source.
| method | The clock source to use (kCpuTimestamp or kCpuActiveTime). |
|
explicitnoexcept |
Constructs an ElapsedTimer with the specified precision.
| accuracy | The precision of time values (kMilli, kMicro, or kNano). |
Constructs an ElapsedTimer with the specified clock source and precision.
| method | The clock source to use. |
| accuracy | The precision of time values. |
|
noexcept |
Copy constructor. Copies the current start-time snapshot, method, and accuracy.
| target | The source timer. |
|
noexcept |
Move constructor. Behaves identically to the copy constructor.
| target | The source timer. |
|
noexcept |
Destructor.
|
nodiscardnoexcept |
Returns the elapsed time since start() was called.
The value is computed as (current_time - start_time_) in the configured precision units. Returns -1 if the timer has not been started (or has been stopped).
-1 when the timer is not active.
|
nodiscardnoexcept |
Returns the precision configured for this timer.
Accuracy value set at construction.
|
staticnodiscardnoexcept |
Returns the accumulated CPU time (user + kernel) consumed by the current process.
Uses getrusage(RUSAGE_SELF) on POSIX or GetProcessTimes on Windows. The returned value is the total CPU time used so far in the process lifetime, not a delta. Use ElapsedTimer with kCpuActiveTime to measure deltas.
| accuracy | Desired precision (default: kMilli). |
|
staticnodiscardnoexcept |
Returns the current monotonic CPU timestamp.
On Linux uses CLOCK_MONOTONIC_RAW (unaffected by NTP) when high_resolution is true; otherwise uses std::chrono::steady_clock. On Windows always uses std::chrono::steady_clock.
| accuracy | Desired precision (default: kMilli). |
| high_resolution | Whether to use clock_gettime (Linux only, default: true). |
|
nodiscardnoexcept |
Returns the clock source configured for this timer.
Method value set at construction.
|
staticnodiscardnoexcept |
Returns the current wall-clock (system) timestamp.
On Linux uses CLOCK_REALTIME via clock_gettime for maximum precision when high_resolution is true; otherwise uses std::chrono::system_clock. On Windows always uses std::chrono::system_clock.
| accuracy | Desired precision (default: kMilli). |
| high_resolution | Whether to use clock_gettime (Linux only, default: true). |
|
nodiscardnoexcept |
Returns true when the timer has been started and not yet stopped.
true if active (start_time_ >= 0), false otherwise.
|
noexcept |
Copy-assignment operator.
| target | The source timer. |
*this.
|
noexcept |
Move-assignment operator.
| target | The source timer (moved from). |
*this.
|
noexcept |
Atomically resets the start time to now and returns the elapsed time since the previous start() / restart() call.
This is equivalent to get() followed by start(), but performed atomically using exchange. If the timer was not active, returns a negative value (the raw start_time_ value, which is -1 when stopped).
|
noexcept |
|
noexcept |
Stops the timer, setting the internal start time to -1.
After stop(), is_active() returns false and get() returns -1.