GCC Code Coverage Report


Directory: ./
File: include/sot/core/stop-watch.hh
Date: 2024-12-13 12:22:33
Exec Total Coverage
Lines: 8 9 88.9%
Branches: 0 0 -%

Line Branch Exec Source
1 /*
2 Copyright (c) 2010-2013 Tommaso Urli
3
4 Tommaso Urli tommaso.urli@uniud.it University of Udine
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 */
26
27 #ifndef __sot_core_stopwatch_H__
28 #define __sot_core_stopwatch_H__
29
30 #include <ctime>
31 #include <iostream>
32 #include <map>
33 #include <sstream>
34
35 #ifndef WIN32
36 /* The classes below are exported */
37 #pragma GCC visibility push(default)
38 #endif
39
40 // Generic stopwatch exception class
41 struct StopwatchException {
42 public:
43 StopwatchException(std::string error) : error(error) {}
44 std::string error;
45 };
46
47 enum StopwatchMode {
48 NONE = 0, // Clock is not initialized
49 CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
50 REAL_TIME = 2 // Clock calculates time by asking the operating system how
51 // much real time passed
52 };
53
54 #define STOP_WATCH_MAX_NAME_LENGTH 80
55
56 /**
57 @brief A class representing a stopwatch.
58
59 @code
60 Stopwatch swatch();
61 @endcode
62
63 The Stopwatch class can be used to measure execution time of code,
64 algorithms, etc., // TODO: he Stopwatch can be initialized in two
65 time-taking modes, CPU time and real time:
66
67 @code
68 swatch.set_mode(REAL_TIME);
69 @endcode
70
71 CPU time is the time spent by the processor on a certain piece of code,
72 while real time is the real amount of time taken by a certain piece of
73 code to execute (i.e. in general if you are doing hard work such as
74 image or video editing on a different process the measured time will
75 probably increase).
76
77 How does it work? Basically, one wraps the code to be measured with the
78 following method calls:
79
80 @code
81 swatch.start("My astounding algorithm");
82 // Hic est code
83 swatch.stop("My astounding algorithm");
84 @endcode
85
86 A string representing the code ID is provided so that nested portions of
87 code can be profiled separately:
88
89 @code
90 swatch.start("My astounding algorithm");
91
92 swatch.start("My astounding algorithm - Super smart init");
93 // Initialization
94 swatch.stop("My astounding algorithm - Super smart init");
95
96 swatch.start("My astounding algorithm - Main loop");
97 // Loop
98 swatch.stop("My astounding algorithm - Main loop");
99
100 swatch.stop("My astounding algorithm");
101 @endcode
102
103 Note: ID strings can be whatever you like, in the previous example I have
104 used "My astounding algorithm - *" only to enforce the fact that the
105 measured code portions are part of My astounding algorithm, but there's no
106 connection between the three measurements.
107
108 If the code for a certain task is scattered through different files or
109 portions of the same file one can use the start-pause-stop method:
110
111 @code
112 swatch.start("Setup");
113 // First part of setup
114 swatch.pause("Setup");
115
116 swatch.start("Main logic");
117 // Main logic
118 swatch.stop("Main logic");
119
120 swatch.start("Setup");
121 // Cleanup (part of the setup)
122 swatch.stop("Setup");
123 @endcode
124
125 Finally, to report the results of the measurements just run:
126
127 @code
128 swatch.report("Code ID");
129 @endcode
130
131 Thou can also provide an additional std::ostream& parameter to report() to
132 redirect the logging on a different output. Also, you can use the
133 get_total/min/max/average_time() methods to get the individual numeric data,
134 without all the details of the logging. You can also extend Stopwatch to
135 implement your own logging syntax.
136
137 To report all the measurements:
138
139 @code
140 swatch.report_all();
141 @endcode
142
143 Same as above, you can redirect the output by providing a std::ostream&
144 parameter.
145
146 */
147 class Stopwatch {
148 public:
149 /** Constructor */
150 Stopwatch(StopwatchMode _mode = NONE);
151
152 /** Destructor */
153 ~Stopwatch();
154
155 /** Tells if a performance with a certain ID exists */
156 bool performance_exists(std::string perf_name);
157
158 /** Initialize stopwatch to use a certain time taking mode */
159 void set_mode(StopwatchMode mode);
160
161 /** Start the stopwatch related to a certain piece of code */
162 void start(std::string perf_name);
163
164 /** Stops the stopwatch related to a certain piece of code */
165 void stop(std::string perf_name);
166
167 /** Stops the stopwatch related to a certain piece of code */
168 void pause(std::string perf_name);
169
170 /** Reset a certain performance record */
171 void reset(std::string perf_name);
172
173 /** Resets all the performance records */
174 void reset_all();
175
176 /** Dump the data of a certain performance record */
177 void report(std::string perf_name, int precision = 2,
178 std::ostream &output = std::cout);
179
180 /** Dump the data of all the performance records */
181 void report_all(int precision = 2, std::ostream &output = std::cout);
182
183 /** Returns total execution time of a certain performance */
184 long double get_total_time(std::string perf_name);
185
186 /** Returns average execution time of a certain performance */
187 long double get_average_time(std::string perf_name);
188
189 /** Returns minimum execution time of a certain performance */
190 long double get_min_time(std::string perf_name);
191
192 /** Returns maximum execution time of a certain performance */
193 long double get_max_time(std::string perf_name);
194
195 /** Return last measurement of a certain performance */
196 long double get_last_time(std::string perf_name);
197
198 /** Return the time since the start of the last measurement of a given
199 performance. */
200 long double get_time_so_far(std::string perf_name);
201
202 /** Turn off clock, all the Stopwatch::* methods return without doing
203 anything after this method is called. */
204 void turn_off();
205
206 /** Turn on clock, restore clock operativity after a turn_off(). */
207 void turn_on();
208
209 /** Take time, depends on mode */
210 long double take_time();
211
212 protected:
213 /** Struct to hold the performance data */
214 struct PerformanceData {
215 4 PerformanceData()
216 4 : clock_start(0),
217 4 total_time(0),
218 4 min_time(0),
219 4 max_time(0),
220 4 last_time(0),
221 4 paused(false),
222 4 stops(0) {}
223
224 /** Start time */
225 long double clock_start;
226
227 /** Cumulative total time */
228 long double total_time;
229
230 /** Minimum time */
231 long double min_time;
232
233 /** Maximum time */
234 long double max_time;
235
236 /** Last time */
237 long double last_time;
238
239 /** Tells if this performance has been paused, only for internal use */
240 bool paused;
241
242 /** How many cycles have been this stopwatch executed? */
243 int stops;
244 };
245
246 /** Flag to hold the clock's status */
247 bool active;
248
249 /** Time taking mode */
250 StopwatchMode mode;
251
252 /** Pointer to the dynamic structure which holds the collection of performance
253 data */
254 std::map<std::string, PerformanceData> *records_of;
255 };
256
257 Stopwatch &getProfiler();
258
259 #ifndef WIN32
260 #pragma GCC visibility pop
261 #endif
262
263 #endif
264