VLink  2.0.0
A high-performance communication middleware
vlink::CpuProfiler Class Referencefinal

Tracks active CPU time as a percentage of wall-clock time using a SpinLock guard. More...

#include <cpu_profiler.h>

Collaboration diagram for vlink::CpuProfiler:

Public Member Functions

 CpuProfiler () noexcept
 Constructs a profiler with zeroed accumulators and no active interval. More...
 
 ~CpuProfiler () noexcept
 Destructor. More...
 
void begin () noexcept
 Opens a new active interval. More...
 
void end () noexcept
 Closes the current active interval and accrues its elapsed time. More...
 
double get () const noexcept
 Returns the current CPU utilisation ratio as a percentage. More...
 
double restart () noexcept
 Returns the current utilisation and atomically zeroes all accumulators. More...
 

Static Public Member Functions

static bool is_global_enabled () noexcept
 Reports whether profiling is enabled via the VLINK_PROFILER_ENABLE environment variable. More...
 

Detailed Description

Tracks active CPU time as a percentage of wall-clock time using a SpinLock guard.

Each begin / end pair contributes one active interval to the running total. get returns the ratio of active to elapsed time; restart returns the same value and clears the accumulators atomically. Non-copyable; each profiler tracks a single logical stream.

Constructor & Destructor Documentation

◆ CpuProfiler()

vlink::CpuProfiler::CpuProfiler ( )
noexcept

Constructs a profiler with zeroed accumulators and no active interval.

◆ ~CpuProfiler()

vlink::CpuProfiler::~CpuProfiler ( )
noexcept

Destructor.

Member Function Documentation

◆ begin()

void vlink::CpuProfiler::begin ( )
noexcept

Opens a new active interval.

Restarts the internal active timer and, on the first call, also starts the wall-clock timer used as the denominator in get. Acquires the internal SpinLock briefly.

◆ end()

void vlink::CpuProfiler::end ( )
noexcept

Closes the current active interval and accrues its elapsed time.

Reads the active timer's elapsed value and adds non-negative deltas to total_active_. Acquires the internal SpinLock briefly.

◆ get()

double vlink::CpuProfiler::get ( ) const
noexcept

Returns the current CPU utilisation ratio as a percentage.

Computed as (total_active_ns / total_elapsed_ns) * 100.0. Returns 0.0 when no time has elapsed yet. Values can exceed 100 on multi-core systems where the sum of active intervals outruns wall-clock time on one core.

Returns
Utilisation percentage; 0.0 when no data has accrued.

◆ is_global_enabled()

static bool vlink::CpuProfiler::is_global_enabled ( )
staticnoexcept

Reports whether profiling is enabled via the VLINK_PROFILER_ENABLE environment variable.

The environment is sampled lazily on the first call and cached for the process lifetime. Use the return value to gate expensive sampling around begin / end pairs.

Returns
true when VLINK_PROFILER_ENABLE is set to "1".

◆ restart()

double vlink::CpuProfiler::restart ( )
noexcept

Returns the current utilisation and atomically zeroes all accumulators.

Equivalent to reading get and then resetting both timers. Acquires the internal SpinLock briefly.

Returns
Utilisation percentage accrued since construction or the last restart.

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