VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
elapsed_timer.h File Reference

High-resolution elapsed-time measurement with configurable clock source and precision. More...

#include <atomic>
#include <cstdint>
#include "./macros.h"
Include dependency graph for elapsed_timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

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

Namespaces

Detailed Description

High-resolution elapsed-time measurement with configurable clock source and precision.

ElapsedTimer measures how much time (wall-clock or CPU-active time) has elapsed since start() was called. It is the lowest-level timing primitive in VLink and is used internally by DeadlineTimer, CpuProfiler, and the message-loop task latency tracking.

Two independent axes of configuration are available:

Clock source (Method)
Value Clock source Notes
kCpuTimestamp CLOCK_MONOTONIC_RAW / steady_clock Monotonic wall time, never jumps
kCpuActiveTime getrusage / GetProcessTimes CPU user+kernel time consumed
Precision (Accuracy)
Value Unit Range (64-bit)
kMilli milliseconds ~292 million years
kMicro microseconds ~292 thousand years
kNano nanoseconds ~292 years
Note
  • The timer is not started on construction; call start() explicitly.
  • get() returns -1 when the timer has not been started or has been stopped.
  • The internal start_time_ is stored as a std::atomic<int64_t>, making concurrent reads from multiple threads safe, though concurrent start()/stop() calls from different threads lead to a data race on the "is started" semantic.
  • On Linux, get_sys_timestamp() uses CLOCK_REALTIME via clock_gettime for nanosecond resolution; on Windows it falls back to std::chrono::system_clock.
  • get_cpu_timestamp() uses CLOCK_MONOTONIC_RAW on Linux (immune to NTP adjustments) and std::chrono::steady_clock on other platforms.
Example
t.start();
do_work();
int64_t us = t.get(); // microseconds elapsed; -1 if not started
t.stop();
// One-liner: restart returns elapsed and resets the timer atomically
int64_t delta = t.restart();