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

Tracks CPU active time as a percentage of total elapsed wall-clock time. More...

#include <cpu_profiler.h>

Collaboration diagram for vlink::CpuProfiler:

Public Member Functions

 CpuProfiler () noexcept
 Constructs a profiler with all accumulators initialised to zero.
 ~CpuProfiler () noexcept
 Destructor.
void begin () noexcept
 Marks the start of an active CPU work section.
void end () noexcept
 Marks the end of an active CPU work section and accumulates the elapsed time.
double get () const noexcept
 Returns the current CPU utilisation ratio as a percentage.
double restart () noexcept
 Returns the current utilisation and resets all accumulators to zero.

Static Public Member Functions

static bool is_global_enabled () noexcept
 Returns whether CPU profiling is globally enabled via environment variable.

Detailed Description

Tracks CPU active time as a percentage of total elapsed wall-clock time.

Each begin() / end() pair contributes one active interval to the running total. get() returns the cumulative utilisation ratio; restart() returns the ratio and resets all accumulators.

Note
Copy and assignment are disabled. Instances should be owned by a single component.

Constructor & Destructor Documentation

◆ CpuProfiler()

vlink::CpuProfiler::CpuProfiler ( )
noexcept

Constructs a profiler with all accumulators initialised to zero.

Here is the caller graph for this function:

◆ ~CpuProfiler()

vlink::CpuProfiler::~CpuProfiler ( )
noexcept

Destructor.

Here is the call graph for this function:

Member Function Documentation

◆ begin()

void vlink::CpuProfiler::begin ( )
noexcept

Marks the start of an active CPU work section.

Restarts the active-time timer. If this is the very first call, also starts the wall-clock timestamp timer used as the denominator in get(). Safe to call multiple times; each call resets the active-timer baseline.

Note
This method acquires an internal SpinLock briefly.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ end()

void vlink::CpuProfiler::end ( )
noexcept

Marks the end of an active CPU work section and accumulates the elapsed time.

Reads the active-time elapsed since the last begin() and adds it to the running total (total_active_). Negative elapsed values (e.g., clock skew) are ignored.

Note
This method acquires an internal SpinLock briefly.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get()

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

Returns the current CPU utilisation ratio as a percentage.

Computes: (total_active_ns / total_elapsed_ns) * 100.0 Returns 0.0 if no time has elapsed since profiling started.

Returns
CPU utilisation in the range [0.0, 100.0] (may exceed 100 on multi-core).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_global_enabled()

bool vlink::CpuProfiler::is_global_enabled ( )
staticnodiscardnoexcept

Returns whether CPU profiling is globally enabled via environment variable.

Reads VLINK_PROFILER_ENABLE on the first call and caches the result. Returns true if the variable is set to "1", false otherwise.

Returns
true if profiling is enabled globally.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ restart()

double vlink::CpuProfiler::restart ( )
noexcept

Returns the current utilisation and resets all accumulators to zero.

The return value is identical to calling get() before the reset. After this call, get() returns 0.0 until the next begin() / end() pair.

Note
This method acquires an internal SpinLock briefly.
Returns
CPU utilisation percentage accumulated since construction or the last restart().
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: