VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
vlink::ElapsedTimer类 参考final

Atomic, high-resolution elapsed-time timer. 更多...

#include <elapsed_timer.h>

vlink::ElapsedTimer 的协作图:

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.
ElapsedTimeroperator= (const ElapsedTimer &target) noexcept
 Copy-assignment operator.
ElapsedTimeroperator= (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).

成员枚举类型说明

◆ Accuracy

Selects the precision (resolution) of the timer values returned.

枚举值
kMilli 

Millisecond precision

kMicro 

Microsecond precision

kNano 

Nanosecond precision

◆ Method

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)

构造及析构函数说明

◆ ElapsedTimer() [1/6]

vlink::ElapsedTimer::ElapsedTimer ( )
noexcept

Constructs an ElapsedTimer with default method (kCpuTimestamp) and default accuracy (kMilli).

The timer is not started after construction.

这是这个函数的调用关系图:

◆ ElapsedTimer() [2/6]

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

Constructs an ElapsedTimer with the specified clock source.

参数
methodThe clock source to use (kCpuTimestamp or kCpuActiveTime).
函数调用图:

◆ ElapsedTimer() [3/6]

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

Constructs an ElapsedTimer with the specified precision.

参数
accuracyThe precision of time values (kMilli, kMicro, or kNano).
函数调用图:

◆ ElapsedTimer() [4/6]

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

Constructs an ElapsedTimer with the specified clock source and precision.

参数
methodThe clock source to use.
accuracyThe precision of time values.
函数调用图:

◆ ElapsedTimer() [5/6]

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

Copy constructor. Copies the current start-time snapshot, method, and accuracy.

参数
targetThe source timer.
函数调用图:

◆ ElapsedTimer() [6/6]

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

Move constructor. Behaves identically to the copy constructor.

参数
targetThe source timer.
函数调用图:

◆ ~ElapsedTimer()

vlink::ElapsedTimer::~ElapsedTimer ( )
noexcept

Destructor.

函数调用图:

成员函数说明

◆ get()

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

返回
  • Elapsed time in the configured units (>= 0) when active.
  • -1 when the timer is not active.
函数调用图:
这是这个函数的调用关系图:

◆ get_accuracy()

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

Returns the precision configured for this timer.

返回
The Accuracy value set at construction.
函数调用图:
这是这个函数的调用关系图:

◆ get_cpu_active_time()

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

参数
accuracyDesired precision (default: kMilli).
返回
Cumulative process CPU time in the requested unit.
函数调用图:
这是这个函数的调用关系图:

◆ get_cpu_timestamp()

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

参数
accuracyDesired precision (default: kMilli).
high_resolutionWhether to use clock_gettime (Linux only, default: true).
返回
Current monotonic timestamp in the requested unit.
函数调用图:
这是这个函数的调用关系图:

◆ get_method()

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

Returns the clock source configured for this timer.

返回
The Method value set at construction.
函数调用图:
这是这个函数的调用关系图:

◆ get_sys_timestamp()

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

参数
accuracyDesired precision (default: kMilli).
high_resolutionWhether to use clock_gettime (Linux only, default: true).
返回
Current system timestamp in the requested unit.
函数调用图:
这是这个函数的调用关系图:

◆ is_active()

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

Returns true when the timer has been started and not yet stopped.

返回
true if active (start_time_ >= 0), false otherwise.
函数调用图:
这是这个函数的调用关系图:

◆ operator=() [1/2]

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

Copy-assignment operator.

参数
targetThe source timer.
返回
A reference to *this.
函数调用图:

◆ operator=() [2/2]

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

Move-assignment operator.

参数
targetThe source timer (moved from).
返回
A reference to *this.
函数调用图:

◆ restart()

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

返回
  • Elapsed time in the configured units since the last start/restart, or
  • A negative value if the timer was not started before this call.
函数调用图:
这是这个函数的调用关系图:

◆ start()

void vlink::ElapsedTimer::start ( )
noexcept

Starts the timer if it is not already active.

Records the current time as the start reference using a CAS on the internal atomic. If the timer is already active this call is a no-op. Call stop() first to reset and then start() again.

函数调用图:
这是这个函数的调用关系图:

◆ stop()

void vlink::ElapsedTimer::stop ( )
noexcept

Stops the timer, setting the internal start time to -1.

After stop(), is_active() returns false and get() returns -1.

函数调用图:
这是这个函数的调用关系图:

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