GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/tsid/utils/stop-watch.hpp Lines: 0 4 0.0 %
Date: 2024-02-02 08:47:34 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 __invdyn_stopwatch_H__
28
#define __invdyn_stopwatch_H__
29
30
#include "tsid/utils/Stdafx.hh"
31
32
#ifndef WIN32
33
/* The classes below are exported */
34
#pragma GCC visibility push(default)
35
#endif
36
37
// #define START_PROFILER(name)
38
// #define STOP_PROFILER(name)
39
#define START_PROFILER(name) getProfiler().start(name)
40
#define STOP_PROFILER(name) getProfiler().stop(name)
41
42
#define STOP_WATCH_MAX_NAME_LENGTH 60
43
#define STOP_WATCH_TIME_WIDTH 10
44
45
// Generic stopwatch exception class
46
struct StopwatchException {
47
 public:
48
  StopwatchException(std::string error) : error(error) {}
49
  std::string error;
50
};
51
52
enum StopwatchMode {
53
  NONE = 0,      // Clock is not initialized
54
  CPU_TIME = 1,  // Clock calculates time ranges using ctime and CLOCKS_PER_SEC
55
  REAL_TIME = 2  // Clock calculates time by asking the operating system how
56
  // much real time passed
57
};
58
59
/**
60
    @brief A class representing a stopwatch.
61
62
    @code
63
    Stopwatch swatch();
64
    @endcode
65
66
    The Stopwatch class can be used to measure execution time of code,
67
    algorithms, etc., // TODO: he Stopwatch can be initialized in two
68
    time-taking modes, CPU time and real time:
69
70
    @code
71
    swatch.set_mode(REAL_TIME);
72
    @endcode
73
74
    CPU time is the time spent by the processor on a certain piece of code,
75
    while real time is the real amount of time taken by a certain piece of
76
    code to execute (i.e. in general if you are doing hard work such as
77
    image or video editing on a different process the measured time will
78
    probably increase).
79
80
    How does it work? Basically, one wraps the code to be measured with the
81
    following method calls:
82
83
    @code
84
    swatch.start("My astounding algorithm");
85
    // Hic est code
86
    swatch.stop("My astounding algorithm");
87
    @endcode
88
89
    A string representing the code ID is provided so that nested portions of
90
    code can be profiled separately:
91
92
    @code
93
    swatch.start("My astounding algorithm");
94
95
    swatch.start("My astounding algorithm - Super smart init");
96
    // Initialization
97
    swatch.stop("My astounding algorithm - Super smart init");
98
99
    swatch.start("My astounding algorithm - Main loop");
100
    // Loop
101
    swatch.stop("My astounding algorithm - Main loop");
102
103
    swatch.stop("My astounding algorithm");
104
    @endcode
105
106
    Note: ID strings can be whatever you like, in the previous example I have
107
    used "My astounding algorithm - *" only to enforce the fact that the
108
    measured code portions are part of My astounding algorithm, but there's no
109
    connection between the three measurements.
110
111
    If the code for a certain task is scattered through different files or
112
    portions of the same file one can use the start-pause-stop method:
113
114
    @code
115
    swatch.start("Setup");
116
    // First part of setup
117
    swatch.pause("Setup");
118
119
    swatch.start("Main logic");
120
    // Main logic
121
    swatch.stop("Main logic");
122
123
    swatch.start("Setup");
124
    // Cleanup (part of the setup)
125
    swatch.stop("Setup");
126
    @endcode
127
128
    Finally, to report the results of the measurements just run:
129
130
    @code
131
    swatch.report("Code ID");
132
    @endcode
133
134
    Thou can also provide an additional std::ostream& parameter to report() to
135
    redirect the logging on a different output. Also, you can use the
136
    get_total/min/max/average_time() methods to get the individual numeric data,
137
    without all the details of the logging. You can also extend Stopwatch to
138
    implement your own logging syntax.
139
140
    To report all the measurements:
141
142
    @code
143
    swatch.report_all();
144
    @endcode
145
146
    Same as above, you can redirect the output by providing a std::ostream&
147
    parameter.
148
149
*/
150
class Stopwatch {
151
 public:
152
  /** Constructor */
153
  Stopwatch(StopwatchMode _mode = NONE);
154
155
  /** Destructor */
156
  ~Stopwatch();
157
158
  /** Tells if a performance with a certain ID exists */
159
  bool performance_exists(std::string perf_name);
160
161
  /** Initialize stopwatch to use a certain time taking mode */
162
  void set_mode(StopwatchMode mode);
163
164
  /** Start the stopwatch related to a certain piece of code */
165
  void start(std::string perf_name);
166
167
  /** Stops the stopwatch related to a certain piece of code */
168
  void stop(std::string perf_name);
169
170
  /** Stops the stopwatch related to a certain piece of code */
171
  void pause(std::string perf_name);
172
173
  /** Reset a certain performance record */
174
  void reset(std::string perf_name);
175
176
  /** Resets all the performance records */
177
  void reset_all();
178
179
  /** Dump the data of a certain performance record */
180
  void report(std::string perf_name, int precision = 2,
181
              std::ostream& output = std::cout);
182
183
  /** Dump the data of all the performance records */
184
  void report_all(int precision = 2, std::ostream& output = std::cout);
185
186
  /** Returns total execution time of a certain performance */
187
  long double get_total_time(std::string perf_name);
188
189
  /** Returns average execution time of a certain performance */
190
  long double get_average_time(std::string perf_name);
191
192
  /** Returns minimum execution time of a certain performance */
193
  long double get_min_time(std::string perf_name);
194
195
  /** Returns maximum execution time of a certain performance */
196
  long double get_max_time(std::string perf_name);
197
198
  /** Return last measurement of a certain performance */
199
  long double get_last_time(std::string perf_name);
200
201
  /** Return the time since the start of the last measurement of a given
202
      performance. */
203
  long double get_time_so_far(std::string perf_name);
204
205
  /**	Turn off clock, all the Stopwatch::* methods return without doing
206
        anything after this method is called. */
207
  void turn_off();
208
209
  /** Turn on clock, restore clock operativity after a turn_off(). */
210
  void turn_on();
211
212
  /** Take time, depends on mode */
213
  long double take_time();
214
215
 protected:
216
  /** Struct to hold the performance data */
217
  struct PerformanceData {
218
    PerformanceData()
219
        : clock_start(0),
220
          total_time(0),
221
          min_time(0),
222
          max_time(0),
223
          last_time(0),
224
          paused(false),
225
          stops(0) {}
226
227
    /** Start time */
228
    long double clock_start;
229
230
    /** Cumulative total time */
231
    long double total_time;
232
233
    /** Minimum time */
234
    long double min_time;
235
236
    /** Maximum time */
237
    long double max_time;
238
239
    /** Last time */
240
    long double last_time;
241
242
    /** Tells if this performance has been paused, only for internal use */
243
    bool paused;
244
245
    /** How many cycles have been this stopwatch executed? */
246
    int stops;
247
  };
248
249
  /** Flag to hold the clock's status */
250
  bool active;
251
252
  /** Time taking mode */
253
  StopwatchMode mode;
254
255
  /** Pointer to the dynamic structure which holds the collection of performance
256
      data */
257
  std::map<std::string, PerformanceData>* records_of;
258
};
259
260
Stopwatch& getProfiler();
261
262
#ifndef WIN32
263
#pragma GCC visibility pop
264
#endif
265
266
#endif