VLink  2.0.0
A high-performance communication middleware
cpu_profiler_guard.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 by Thun Lu. All rights reserved.
3  * Author: Thun Lu <thun.lu@zohomail.cn>
4  * Repo: https://github.com/thun-res/vlink
5  * _ __ __ _ __
6  * | | / / / / (_) ____ / /__
7  * | | / / / / / / / __ \ / //_/
8  * | |/ / / /___ / / / / / / / ,<
9  * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 /**
25  * @file cpu_profiler_guard.h
26  * @brief Stack-based RAII bracket that pairs every @c CpuProfiler::begin with a guaranteed end.
27  *
28  * @details
29  * @c CpuProfilerGuard ties the @c begin / @c end pair of a @c CpuProfiler to scope lifetime so
30  * the active interval closes even when the protected region exits through an exception.
31  *
32  * @par RAII pattern
33  *
34  * @verbatim
35  * scope entry +---------------------------+ scope exit
36  * ----------> | CpuProfilerGuard guard(p) | ---------->
37  * +-----------+---------------+
38  * | ctor: p->begin()
39  * v
40  * active interval
41  * |
42  * | dtor: p->end()
43  * v
44  * interval closed
45  * @endverbatim
46  *
47  * The guard accepts a @c nullptr profiler so call sites can be guarded uniformly while a
48  * higher-level switch (typically @c CpuProfiler::is_global_enabled) decides whether the work is
49  * worth measuring.
50  *
51  * @par Example
52  * @code
53  * vlink::CpuProfiler profiler;
54  *
55  * void process_frame() {
56  * vlink::CpuProfilerGuard guard(&profiler); // profiler.begin()
57  * do_frame_work();
58  * } // profiler.end() on scope exit
59  *
60  * const double utilisation = profiler.get();
61  * @endcode
62  *
63  * @note Non-copyable and non-movable; the guard must live on the stack of the protected region.
64  */
65 
66 #pragma once
67 
68 #include "./macros.h"
69 
70 namespace vlink {
71 
72 /**
73  * @class CpuProfilerGuard
74  * @brief Scope guard that opens and closes a @c CpuProfiler active interval.
75  *
76  * @details
77  * Calls @c CpuProfiler::begin in its constructor and @c CpuProfiler::end in its destructor.
78  * A null profiler pointer is accepted; both ends become no-ops in that case.
79  */
81  public:
82  /**
83  * @brief Constructs the guard and opens the active interval on @p profiler.
84  *
85  * @param profiler Target profiler; @c nullptr disables both ends.
86  */
87  explicit CpuProfilerGuard(class CpuProfiler* profiler) noexcept;
88 
89  /**
90  * @brief Destructor; closes the active interval on the bound profiler.
91  */
92  ~CpuProfilerGuard() noexcept;
93 
94  private:
95  class CpuProfiler* profiler_;
96 
98 };
99 
100 } // namespace vlink
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VLINK_EXPORT
Definition: macros.h:81
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174