Crocoddyl
 
Loading...
Searching...
No Matches
stop-watch.hpp
1
2// Copyright (c) 2010-2013 Tommaso Urli (tommaso.urli@uniud.it; University of
3// Udine)
4//
5// Permission is hereby granted, free of charge, to any person obtaining
6// a copy of this software and associated documentation files (the
7// "Software"), to deal in the Software without restriction, including
8// without limitation the rights to use, copy, modify, merge, publish,
9// distribute, sublicense, and/or sell copies of the Software, and to
10// permit persons to whom the Software is furnished to do so, subject to
11// the following conditions:
12//
13// The above copyright notice and this permission notice shall be
14// included in all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25#ifndef CROCODDYL_CORE_UTILS_STOPWATCH_H_
26#define CROCODDYL_CORE_UTILS_STOPWATCH_H_
27
28#include <ctime>
29#include <iostream>
30#include <limits>
31#include <map>
32#include <sstream>
33
34#ifndef WIN32
35/* The classes below are exported */
36#pragma GCC visibility push(default)
37#endif
38
39#define START_PROFILER(name) \
40 getProfiler().profiler_status() ? getProfiler().start(name) : void()
41#define STOP_PROFILER(name) \
42 getProfiler().profiler_status() ? getProfiler().stop(name) : void()
43
44#define STOP_WATCH_MAX_NAME_LENGTH 60
45#define STOP_WATCH_TIME_WIDTH 10
46
47namespace crocoddyl {
48
49// Generic stopwatch exception class
51 public:
52 StopwatchException(std::string error) : error(error) {}
53 std::string error;
54};
55
56enum StopwatchMode {
57 NONE = 0, // Clock is not initialized
58 CPU_TIME = 1, // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
59 REAL_TIME = 2 // Clock calculates time by asking the operating system how
60 // much real time passed
61};
62
155 protected:
156 struct PerformanceData;
157
158 public:
159 struct Watcher {
160 Stopwatch &w;
161 std::string n;
163
164 Watcher(Stopwatch &_w, std::string _n, PerformanceData *_p)
165 : w(_w), n(_n), p(_p) {}
166 inline void start() {
167 if (w.profiler_active) _start();
168 }
169 inline void stop() {
170 if (w.profiler_active) _stop();
171 }
172
173 private:
174 void _start();
175 void _stop();
176 };
177
179 Stopwatch(StopwatchMode _mode = NONE);
180
182 ~Stopwatch();
183
185 void enable_profiler();
186
188 void disable_profiler();
189
191 inline bool profiler_status() { return profiler_active; }
192
194 bool performance_exists(std::string perf_name);
195
197 void set_mode(StopwatchMode mode);
198
200 Watcher watcher(const std::string &perf_name);
201
203 void start(const std::string &perf_name);
204
206 void stop(const std::string &perf_name);
207
209 void pause(const std::string &perf_name);
210
212 void reset(const std::string &perf_name);
213
215 void reset_all();
216
218 void report(const std::string &perf_name, int precision = 2,
219 std::ostream &output = std::cout);
220
222 void report_all(int precision = 2, std::ostream &output = std::cout);
223
225 long double get_total_time(const std::string &perf_name);
226
228 long double get_average_time(const std::string &perf_name);
229
231 long double get_min_time(const std::string &perf_name);
232
234 long double get_max_time(const std::string &perf_name);
235
237 long double get_last_time(const std::string &perf_name);
238
242 long double get_time_so_far(const std::string &perf_name);
243
246 void turn_off();
247
249 void turn_on();
250
252 long double take_time();
253
254 protected:
258 : clock_start(0),
259 total_time(0),
260 min_time(std::numeric_limits<long double>::max()),
261 max_time(std::numeric_limits<long double>::min()),
262 last_time(0),
263 paused(false),
264 stops(0) {}
265
266 long double clock_start;
267 long double total_time;
268 long double min_time;
269 long double max_time;
270 long double last_time;
271 bool paused;
273 int stops;
274 };
275
276 PerformanceData &get_or_create_perf(const std::string &perf_name);
277
278 void stop_perf(PerformanceData &perf_info, long double clock_end);
279
280 bool active;
281 StopwatchMode mode;
282 std::map<std::string, PerformanceData>
285};
286
287Stopwatch &getProfiler();
288
289} // namespace crocoddyl
290
291#ifndef WIN32
292#pragma GCC visibility pop
293#endif
294
295#endif
A class representing a stopwatch.
bool active
Flag to hold the clock's status.
long double get_total_time(const std::string &perf_name)
Returns total execution time of a certain performance.
void reset_all()
Resets all the performance records.
bool profiler_status()
Return if the profiler is enable or disable.
long double take_time()
Take time, depends on mode.
long double get_time_so_far(const std::string &perf_name)
Return the time since the start of the last measurement of a given performance.
void stop(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
void turn_on()
Turn on clock, restore clock operativity after a turn_off().
long double get_last_time(const std::string &perf_name)
Return last measurement of a certain performance.
std::map< std::string, PerformanceData > * records_of
Dynamic collection of performance data.
void start(const std::string &perf_name)
Start the stopwatch related to a certain piece of code.
void reset(const std::string &perf_name)
Reset a certain performance record.
Watcher watcher(const std::string &perf_name)
create a Start the stopwatch related to a certain piece of code
void disable_profiler()
Disable the profiler.
bool profiler_active
Indicates if the profiler is enabled.
void report_all(int precision=2, std::ostream &output=std::cout)
Dump the data of all the performance records.
long double get_max_time(const std::string &perf_name)
Returns maximum execution time of a certain performance.
long double get_average_time(const std::string &perf_name)
Returns average execution time of a certain performance.
void set_mode(StopwatchMode mode)
Initialize stopwatch to use a certain time taking mode.
~Stopwatch()
Destructor.
long double get_min_time(const std::string &perf_name)
Returns minimum execution time of a certain performance.
void turn_off()
Turn off clock, all the Stopwatch::* methods return without doing anything after this method is calle...
bool performance_exists(std::string perf_name)
Tells if a performance with a certain ID exists.
void pause(const std::string &perf_name)
Stops the stopwatch related to a certain piece of code.
void enable_profiler()
Enable the profiler.
StopwatchMode mode
Time taking mode.
void report(const std::string &perf_name, int precision=2, std::ostream &output=std::cout)
Dump the data of a certain performance record.
Struct to hold the performance data.
long double min_time
Minimum time.
long double max_time
Maximum time.
long double total_time
Cumulative total time.
int stops
How many cycles have been this stopwatch executed?