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

Stack-based RAII bracket that pairs every CpuProfiler::begin with a guaranteed end. More...

#include "./macros.h"
Include dependency graph for cpu_profiler_guard.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  vlink::CpuProfilerGuard
 Scope guard that opens and closes a CpuProfiler active interval. More...
 

Namespaces

 

Detailed Description

Stack-based RAII bracket that pairs every CpuProfiler::begin with a guaranteed end.

CpuProfilerGuard ties the begin / end pair of a CpuProfiler to scope lifetime so the active interval closes even when the protected region exits through an exception.

RAII pattern
*   scope entry +---------------------------+ scope exit
*   ----------> | CpuProfilerGuard guard(p) | ---------->
*               +-----------+---------------+
*                           | ctor: p->begin()
*                           v
*                       active interval
*                           |
*                           | dtor: p->end()
*                           v
*                       interval closed
* 

The guard accepts a nullptr profiler so call sites can be guarded uniformly while a higher-level switch (typically CpuProfiler::is_global_enabled) decides whether the work is worth measuring.

Example
void process_frame() {
vlink::CpuProfilerGuard guard(&profiler); // profiler.begin()
do_frame_work();
} // profiler.end() on scope exit
const double utilisation = profiler.get();
Note
Non-copyable and non-movable; the guard must live on the stack of the protected region.