VLink  2.0.0
A high-performance communication middleware
cpu_profiler.h File Reference

Per-instance CPU utilisation sampler with an env-driven global enable gate. More...

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

Go to the source code of this file.

Classes

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

Namespaces

 

Detailed Description

Per-instance CPU utilisation sampler with an env-driven global enable gate.

CpuProfiler measures the fraction of wall-clock time spent inside paired begin / end intervals. Two ElapsedTimer instances back the computation: one accrues active intervals; the other captures the total wall-clock span since the first begin. The reported utilisation is therefore the ratio of accumulated active time to elapsed wall time.

Phases and events
Phase / event Source Effect on counters
First begin active timer + timestamp timer Both timers start
Subsequent begin active timer restart Resets the active baseline
end active timer elapsed Adds positive delta to total_active_
get read-only Returns (active / elapsed) * 100
restart read + reset Returns the value then zeroes counters
RAII guard pattern
*   vlink::CpuProfiler profiler;
*   --------------------------------------------------------
*   {
*     vlink::CpuProfilerGuard guard(&profiler);  // begin()
*     do_work();
*   }                                            // end()
*   const double percent = profiler.get();
*   --------------------------------------------------------
* 
Global enable gate
VLINK_PROFILER_ENABLE is read on the first call to is_global_enabled and cached for the remainder of the process lifetime. Set it to "1" to enable; any other value disables.
Example
for (auto& item : work_items) {
profiler.begin();
process(item);
profiler.end();
}
const double pct = profiler.restart();
publish(pct);
}