VLink  2.0.0
A high-performance communication middleware
vlink::ElapsedTimer类 参考final

Atomic high-resolution timer with selectable clock source and unit. 更多...

#include <elapsed_timer.h>

vlink::ElapsedTimer 的协作图:

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. 更多...
 
ElapsedTimeroperator= (const ElapsedTimer &target) noexcept
 Copy assignment; copies the snapshot value and configuration. 更多...
 
ElapsedTimeroperator= (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.

成员枚举类型说明

◆ Accuracy

Precision selector for stored and returned time values.

枚举值
kMilli 

Millisecond precision.

kMicro 

Microsecond precision.

kNano 

Nanosecond precision.

◆ Method

Clock source selector.

枚举值
kCpuTimestamp 

Monotonic wall clock (steady_clock for instance timing).

kCpuActiveTime 

Process CPU time (user + kernel via getrusage / GetProcessTimes).

构造及析构函数说明

◆ ElapsedTimer() [1/6]

vlink::ElapsedTimer::ElapsedTimer ( )
noexcept

Constructs a timer with default method (kCpuTimestamp) and accuracy (kMilli).

The timer is inactive on construction; call start to begin measuring.

◆ ElapsedTimer() [2/6]

vlink::ElapsedTimer::ElapsedTimer ( Method  method)
explicitnoexcept

Constructs a timer with the given clock source and default millisecond accuracy.

参数
methodClock source.

◆ ElapsedTimer() [3/6]

vlink::ElapsedTimer::ElapsedTimer ( Accuracy  accuracy)
explicitnoexcept

Constructs a timer with the default clock source and the given accuracy.

参数
accuracyPrecision of returned values.

◆ ElapsedTimer() [4/6]

vlink::ElapsedTimer::ElapsedTimer ( Method  method,
Accuracy  accuracy 
)
explicitnoexcept

Constructs a timer with the given clock source and accuracy.

参数
methodClock source.
accuracyPrecision of returned values.

◆ ElapsedTimer() [5/6]

vlink::ElapsedTimer::ElapsedTimer ( const ElapsedTimer target)
noexcept

Copy constructor; copies the snapshot value and configuration.

参数
targetSource timer.

◆ ElapsedTimer() [6/6]

vlink::ElapsedTimer::ElapsedTimer ( ElapsedTimer &&  target)
noexcept

Move constructor; equivalent to copy for this snapshot-valued type.

参数
targetSource timer.

◆ ~ElapsedTimer()

vlink::ElapsedTimer::~ElapsedTimer ( )
noexcept

Destructor.

成员函数说明

◆ get()

int64_t vlink::ElapsedTimer::get ( ) const
noexcept

Returns the elapsed time since start without altering state.

返回
Elapsed value in the configured unit, or -1 when inactive.

◆ get_accuracy()

Accuracy vlink::ElapsedTimer::get_accuracy ( ) const
noexcept

Returns the precision this timer was configured with.

返回
Configured precision.

◆ get_cpu_active_time()

static uint64_t vlink::ElapsedTimer::get_cpu_active_time ( Accuracy  accuracy = kMilli)
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.

参数
accuracyDesired precision. Default: kMilli.
返回
Process CPU time in the requested unit.

◆ get_cpu_timestamp()

static uint64_t vlink::ElapsedTimer::get_cpu_timestamp ( Accuracy  accuracy = kMilli,
bool  high_resolution = true 
)
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.

参数
accuracyDesired precision. Default: kMilli.
high_resolutionLinux-only switch for clock_gettime. Default: true.
返回
Monotonic timestamp in the requested unit.

◆ get_method()

Method vlink::ElapsedTimer::get_method ( ) const
noexcept

Returns the clock source this timer was configured with.

返回
Configured clock source.

◆ get_sys_timestamp()

static uint64_t vlink::ElapsedTimer::get_sys_timestamp ( Accuracy  accuracy = kMilli,
bool  high_resolution = true 
)
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.

参数
accuracyDesired precision. Default: kMilli.
high_resolutionLinux-only switch for clock_gettime. Default: true.
返回
Wall-clock timestamp in the requested unit.

◆ is_active()

bool vlink::ElapsedTimer::is_active ( ) const
noexcept

Reports whether the timer is currently measuring.

返回
true when start has produced a non-sentinel reference.

◆ operator=() [1/2]

ElapsedTimer& vlink::ElapsedTimer::operator= ( const ElapsedTimer target)
noexcept

Copy assignment; copies the snapshot value and configuration.

参数
targetSource timer.
返回
Reference to *this.

◆ operator=() [2/2]

ElapsedTimer& vlink::ElapsedTimer::operator= ( ElapsedTimer &&  )
noexcept

Move assignment; equivalent to copy assignment for this snapshot-valued type.

返回
Reference to *this.

◆ restart()

int64_t vlink::ElapsedTimer::restart ( )
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.

返回
Elapsed time in the configured unit, or a negative value if the timer was inactive.

◆ start()

void vlink::ElapsedTimer::start ( )
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.

◆ stop()

void vlink::ElapsedTimer::stop ( )
noexcept

Marks the timer inactive by writing -1 into the start reference.

After stop the timer reports is_active() == false and get() returns -1.


该类的文档由以下文件生成: