6
6
#include < string>
7
7
#include < thread>
8
8
#include < utility>
9
- /* ! \file instrumentation.hpp
10
- \brief Define utilities for instrumentation of the code
11
9
12
- This is the home of anything that gets placed inside other classes or functions in order to instrument the code.
13
- e.g. for profiling or debugging. Everything should ideally be macro-fied so it can be included only for certain
14
- builds, or specified by build time options.
15
- */
10
+ /* !
11
+ * @file instrumentation.hpp
12
+ * @brief Define utilities for instrumentation of the code
13
+ *
14
+ * This is the home of anything that gets placed inside other classes or functions in order to instrument the code.
15
+ * e.g. for profiling or debugging. Everything should ideally be macro-fied so it can be included only for certain
16
+ * builds, or specified by build time options.
17
+ */
16
18
17
19
struct ProfileResult
18
20
{
21
+ // / @struct ProfileResult
19
22
// / @brief Hold the results of a profiled function to be written out.
20
23
21
24
std::string name;
@@ -26,21 +29,64 @@ struct ProfileResult
26
29
27
30
class ProfileWriter
28
31
{
29
- /* ! @class ProfileWriter
32
+ /* !
33
+ * @class ProfileWriter
30
34
* @brief Singleton class to collect timing information for functions and write out to a file that can be inspected
31
35
* later with visual profiling tool
32
36
*
33
37
* Writes out profiling information in a json format readable by chrome tracing
34
38
* (https://www.chromium.org/developers/how-tos/trace-event-profiling-tool/) Use the macros provided to instrument
35
- * the code like: \code{.cpp}
39
+ * the source code like:
40
+ *
41
+ * @code{.cpp}
42
+ * \\ header.hpp
43
+ *
44
+ * class ClassName
45
+ * {
46
+ * returnType func(args);
47
+ * }
48
+ *
49
+ *
50
+ * \\ implementation.cpp
51
+ *
52
+ * ClassName::func(args)
53
+ * {
54
+ * NT_PROFILE();
55
+ *
56
+ * \\ implementation code
57
+ *
58
+ * }
59
+ * @endcode
60
+ *
61
+ * In order to instantiate the ProfileWriter in an application you will then need to use NT_PROFILE_BEGINSESSION()
62
+ * and NT_PROFILE_ENDSESSION() like:
63
+ *
64
+ * @code{.cpp}
65
+ *
66
+ * \\ application.cpp
67
+ *
68
+ * void main()
69
+ * {
70
+ * NT_PROFILE_BEGINSESSION(sessionName);
36
71
*
37
- * \code{.cpp}
72
+ * \\ ... code ...
73
+ * ClassName instance;
38
74
*
39
- * Then open up your favourite chromium based browser and go to chrome://tracing. You can then just drag and drop
40
- * the profiling json file and should see a lovely display of the collected profile information.
75
+ * instance.func(args);
76
+ *
77
+ * \\ ... code ...
78
+ *
79
+ * NT_PROFILE_ENDSSION();
80
+ * }
81
+ *
82
+ * @endcode
83
+ *
84
+ * This will save a json file called <sessionName>-profile.json.
85
+ * Then you can open up your favourite chromium based browser and go to chrome://tracing. You can then just drag and
86
+ * drop the profiling json file and should see a lovely display of the collected profile information.
41
87
*/
42
88
43
- // / \ todo currently only suppor the format used by chrome tracing. Would be nice to support other formats too.
89
+ // / @ todo currently only suppor the format used by chrome tracing. Would be nice to support other formats too.
44
90
// / Should just be a case of adding additional option for writeProfile and header and footer
45
91
46
92
public:
@@ -120,18 +166,21 @@ class ProfileWriter
120
166
};
121
167
122
168
class InstrumentationTimer
123
- /* !
124
- * @class InstrumentationTimer
125
- * @brief Class to perform the actual timing
126
- *
127
- *
128
- *
129
- */
130
169
{
170
+ /* !
171
+ * @class InstrumentationTimer
172
+ * @brief Class to perform timing
173
+ *
174
+ * Gets created at the start of the scope to time then will be deleted when the scope ends.
175
+ * When deleted, will write out timing information to the output stream defined by ProfileWriter.
176
+ *
177
+ *
178
+ */
179
+
131
180
public:
132
181
// / @brief Construct an InstrumentationTimer object and start the clock
133
- // / @param[in] name The name of the profile. Typically use __FUNCSIG__ so it's clear which part of the code is being
134
- // / profiled.
182
+ // / @param[in] name The name of the profile. Typically use __PRETTY_FUNCTION__ so it's clear which part of the code
183
+ // / is being profiled.
135
184
InstrumentationTimer (std::string name) : _name(std::move(name)), _stopped(false )
136
185
{
137
186
_startTimepoint = std::chrono::high_resolution_clock::now ();
@@ -182,6 +231,7 @@ class InstrumentationTimer
182
231
#endif
183
232
184
233
// / @brief Profile the current scope
234
+ // / Shold always be used at the very start of the scope.
185
235
#ifdef USE_PROFILING
186
236
// NOLINTNEXTLINE
187
237
#define NT_PROFILE () InstrumentationTimer timer##__LINE__(std::string(__PRETTY_FUNCTION__))
@@ -190,6 +240,7 @@ class InstrumentationTimer
190
240
#endif
191
241
192
242
// / @brief End the profiling session
243
+ // / Should be used at the very end of an application, after all functions containing a NT_PROFILE() have been called
193
244
#ifdef USE_PROFILING
194
245
// NOLINTNEXTLINE
195
246
#define NT_PROFILE_ENDSESSION () ProfileWriter::get().endSession()
0 commit comments