VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
vlink::ElapsedTimer Class Referencefinal

Atomic, high-resolution elapsed-time timer. More...

#include <elapsed_timer.h>

Collaboration diagram for vlink::ElapsedTimer:

Public Types

enum  Method : uint8_t { kCpuTimestamp = 0 , kCpuActiveTime = 1 }
 Selects the underlying clock source for time measurement. More...
enum  Accuracy : uint8_t { kMilli = 0 , kMicro = 1 , kNano = 2 }
 Selects the precision (resolution) of the timer values returned. More...

Public Member Functions

 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.

Static Public Member Functions

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.

Detailed Description

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).

Member Enumeration Documentation

◆ Accuracy

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

Enumerator
kMilli 

Millisecond precision.

kMicro 

Microsecond precision.

kNano 

Nanosecond precision.

◆ Method

Selects the underlying clock source for time measurement.

Enumerator
kCpuTimestamp 

Monotonic wall-clock (CLOCK_MONOTONIC_RAW on Linux).

kCpuActiveTime 

Process CPU time (user + kernel, via getrusage).

Constructor & Destructor Documentation

◆ 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.

Here is the caller graph for this function:

◆ ElapsedTimer() [2/6]

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

Constructs an ElapsedTimer with the specified clock source.

Parameters
methodThe clock source to use (kCpuTimestamp or kCpuActiveTime).
Here is the call graph for this function:

◆ ElapsedTimer() [3/6]

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

Constructs an ElapsedTimer with the specified precision.

Parameters
accuracyThe precision of time values (kMilli, kMicro, or kNano).
Here is the call graph for this function:

◆ ElapsedTimer() [4/6]

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

Constructs an ElapsedTimer with the specified clock source and precision.

Parameters
methodThe clock source to use.
accuracyThe precision of time values.
Here is the call graph for this function:

◆ ElapsedTimer() [5/6]

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

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

Parameters
targetThe source timer.
Here is the call graph for this function:

◆ ElapsedTimer() [6/6]

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

Move constructor. Behaves identically to the copy constructor.

Parameters
targetThe source timer.
Here is the call graph for this function:

◆ ~ElapsedTimer()

vlink::ElapsedTimer::~ElapsedTimer ( )
noexcept

Destructor.

Here is the call graph for this function:

Member Function Documentation

◆ 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).

Returns
  • Elapsed time in the configured units (>= 0) when active.
  • -1 when the timer is not active.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_accuracy()

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

Returns the precision configured for this timer.

Returns
The Accuracy value set at construction.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

Parameters
accuracyDesired precision (default: kMilli).
Returns
Cumulative process CPU time in the requested unit.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

Parameters
accuracyDesired precision (default: kMilli).
high_resolutionWhether to use clock_gettime (Linux only, default: true).
Returns
Current monotonic timestamp in the requested unit.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_method()

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

Returns the clock source configured for this timer.

Returns
The Method value set at construction.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

Parameters
accuracyDesired precision (default: kMilli).
high_resolutionWhether to use clock_gettime (Linux only, default: true).
Returns
Current system timestamp in the requested unit.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_active()

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

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

Returns
true if active (start_time_ >= 0), false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

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

Copy-assignment operator.

Parameters
targetThe source timer.
Returns
A reference to *this.
Here is the call graph for this function:

◆ operator=() [2/2]

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

Move-assignment operator.

Parameters
targetThe source timer (moved from).
Returns
A reference to *this.
Here is the call graph for this function:

◆ 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).

Returns
  • Elapsed time in the configured units since the last start/restart, or
  • A negative value if the timer was not started before this call.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: