-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtimer.h
More file actions
489 lines (447 loc) · 17.4 KB
/
timer.h
File metadata and controls
489 lines (447 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <chrono>
#include <cstdint>
#include <ctime>
#include <memory>
#include <string>
namespace pisa {
namespace common {
/**
* @brief SimpleTimer
* @details
* This class provides operations to track time.
*
* This timer is as precise as EventTimer. Difference between these classes
* is in the features and flexibility offered.
*
* To measure execution time of a portion of code, sandwich the code between
* calls to `start()` and `stop()`.
*/
class SimpleTimer
{
public:
/**
* @brief Constructor for the SimpleTimer Class
* @param high_precision - flag to use high precision for time
* @param start_active - flag to start timer instantly
*/
SimpleTimer(bool high_precision = false, bool start_active = false)
{
m_active = false;
m_high_precision_mode = high_precision;
if (start_active == true)
{
start();
}
}
/**
* @brief start timer instantly
*/
void start()
{
if (m_high_precision_mode)
m_high_start_time = std::chrono::high_resolution_clock::now();
else
m_start_time = std::chrono::system_clock::now();
m_active = true;
}
/**
* @brief Stop timer instanstly
* @details Use `elapsedSeconds()`, `elapsedMilliseconds()` and `elapsedMicroseconds()`
* methods to retrieve the time elapsed between the last call to `start()` and
* this call.
*/
void stop()
{
if (m_high_precision_mode)
m_high_end_time = std::chrono::high_resolution_clock::now();
else
m_end_time = std::chrono::system_clock::now();
m_active = false;
}
/**
* @brief indicate the timer is active or not
* @return boolean - status of the timer
*/
bool isActive() { return m_active; }
/**
* @brief number of ellapse time that timer is holding
* @param micro - indicates returning in ms or micro-second
* @return number of elappsed millli-second or micro-second
*/
double elapsedMilliseconds(bool micro = false)
{
std::chrono::time_point<std::chrono::system_clock> endTime;
std::chrono::time_point<std::chrono::high_resolution_clock> highEndTime;
if (m_active)
{
if (m_high_precision_mode)
highEndTime = std::chrono::high_resolution_clock::now();
else
endTime = std::chrono::system_clock::now();
}
else
{
if (m_high_precision_mode)
highEndTime = m_high_end_time;
else
endTime = m_end_time;
}
if (micro == false)
{
if (m_high_precision_mode)
return std::chrono::duration<double, std::milli>(highEndTime - m_high_start_time).count();
else
return std::chrono::duration<double, std::milli>(endTime - m_start_time).count();
}
else
{
if (m_high_precision_mode)
return std::chrono::duration<double, std::micro>(highEndTime - m_high_start_time).count();
else
return std::chrono::duration<double, std::micro>(endTime - m_start_time).count();
}
}
/**
* @brief number of elapse time that timer is holding
* @return number of elapsed second
*/
double elapsedSeconds() { return elapsedMilliseconds() / 1000.0; }
/**
* @brief number of elapse time that timer is holding
* @return number of elappsed micro second
*/
double elapsedMicroSeconds() { return elapsedMilliseconds(true); }
private:
// Standard
std::chrono::time_point<std::chrono::system_clock> m_start_time;
std::chrono::time_point<std::chrono::system_clock> m_end_time;
// High
std::chrono::time_point<std::chrono::high_resolution_clock> m_high_start_time;
std::chrono::time_point<std::chrono::high_resolution_clock> m_high_end_time;
bool m_active;
bool m_high_precision_mode;
};
/**
* @brief Type of objects returned by an EventTimer.
* @details Methods of this class are templated to allow transparent
* conversion to different time scales based on the time interval
* template argument specified as a std::ratio.
*/
class TimingReportEvent
{
public:
template <bool>
friend class EventTimer;
/**
* Default time interval used for all interval-templated operations.
* Defaults to seconds.
*/
using DefaultTimeInterval = std::ratio<1, 1>; // all times in seconds by default: report will convert to other units if needed
// set a timing to 0 to ignore in reports
/**
* @brief Construct a new TimingReportEvent object.
* @param[in] _id Optional ID to associate with this event.
* @param[in] _description Optional text description to add to this event.
*/
TimingReportEvent(std::uint32_t _id = 0, const std::string &_description = std::string()) :
id(_id),
description(_description),
m_cpu_time_start(0.0),
m_cpu_time_end(0.0),
m_wall_time_start(0.0),
m_wall_time_end(0.0),
m_iterations(1),
m_ratio_numerator(1),
m_ratio_denominator(1)
{
}
typedef std::shared_ptr<TimingReportEvent> Ptr;
/**
* @brief Construct a new smart pointer to a TimingReportEvent object.
* @param[in] id Optional ID to associate with this event.
* @param[in] description Optional text description to add to this event.
*/
static TimingReportEvent::Ptr create(std::uint32_t id = 0, const std::string &description = std::string())
{
return TimingReportEvent::Ptr(new TimingReportEvent(id, description));
}
/**
* @brief ID of this event.
*/
std::uint32_t id;
/**
* @brief Description of this event.
*/
std::string description;
template <class TimeInterval = DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Retrieves the absolute CPU timing at which this event started.
* @return System dependent absolute CPU timing at which this event started.
* @details This value is generally meaningless on its own. The total CPU
* time for this event is actually computed as the difference between
* timeEndCPU() and timeStartCPU() in the same TimeInterval i.e.
*
* @code
* double elapsed_time = timeEndCPU() - timeStartCPU();
* @endcode
* @sa elapsedCPUTime()
*/
double timeStartCPU() const
{
return m_cpu_time_start * convertTimeInterval<TimeInterval>();
}
template <class TimeInterval = DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Retrieves the absolute CPU timing at which this event ended.
* @return System dependent absolute CPU timing at which this event ended.
* @details This value is generally meaningless on its own. The total CPU
* time for this event is actually computed as the difference between
* timeEndCPU() and timeStartCPU() in the same TimeInterval i.e.
*
* @code
* double elapsed_time = timeEndCPU() - timeStartCPU();
* @endcode
* @sa elapsedCPUTime()
*/
double timeEndCPU() const
{
return m_cpu_time_end * convertTimeInterval<TimeInterval>();
}
template <class TimeInterval = DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Retrieves the absolute wall timing at which this event started.
* @return System dependent absolute wall timing at which this event started.
* @details This value is generally meaningless on its own. The total wall
* time for this event is actually computed as the difference between
* timeEndWall() and timeStartWall() in the same TimeInterval i.e.
*
* @code
* double elapsed_time = timeEndWall() - timeStartWall();
* @endcode
* @sa elapsedWallTime()
*/
double timeStartWall() const
{
return m_wall_time_start * convertTimeInterval<TimeInterval>();
}
template <class TimeInterval = DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Retrieves the absolute wall timing at which this event ended.
* @return System dependent absolute wall timing at which this event ended.
* @details This value is generally meaningless on its own. The total wall
* time for this event is actually computed as the difference between
* timeEndWall() and timeStartWall() in the same TimeInterval i.e.
*
* @code
* double elapsed_time = timeEndWall() - timeStartWall();
* @endcode
* @sa elapsedWallTime()
*/
double timeEndWall() const
{
return m_wall_time_end * convertTimeInterval<TimeInterval>();
}
/**
* @brief Number of iterations that occurred in this event.
* @details This value is for information purposes only and can be used
* to express a bulk of events that may be bundled in this event report.
*/
std::uint64_t iterations() const { return m_iterations; }
template <class TimeInterval = DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Retrieves the elapsed CPU time for this event.
* @return The elapsed CPU time for this event in the specified TimeInterval scale.
* @details If no threads are idle (waiting on mutexes or sleeping) during the event
* timed, the following is true:
*
* @code
* elapsedCPUTime() = elapsedWallTime() * number_of_threads
* @endcode
*
* The above is in ideal conditions, but in practice, this is an approximation.
*
* The following indicates idle threads or high levels of contention among threads:
*
* @code
* elapsedCPUTime() < < elapsedWallTime() * number_of_threads
* @endcode
*/
double elapsedCPUTime() const
{
return (m_cpu_time_end - m_cpu_time_start) * convertTimeInterval<TimeInterval>();
}
template <class TimeInterval = DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Retrieves the elapsed wall time for this event.
* @return The elapsed wall time for this event in the specified TimeInterval scale.
*/
double elapsedWallTime() const
{
return (m_wall_time_end - m_wall_time_start) * convertTimeInterval<TimeInterval>();
}
protected:
template <class TimeInterval = DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Sets the timing values for this timing event.
* @param[in] cpu_time_start CPU start time in the specified time interval unit.
* @param[in] cpu_time_end CPU end time in the specified time interval unit.
* @param[in] wall_time_start Wall start time in the specified time interval unit.
* @param[in] wall_time_end Wall end time in the specified time interval unit.
* @param[in] iterations Number of iterations contained this event.
*/
void setTimings(double cpu_time_start, double cpu_time_end,
double wall_time_start, double wall_time_end,
std::uint64_t iterations)
{
m_ratio_numerator = TimeInterval::num;
m_ratio_denominator = TimeInterval::den;
m_cpu_time_start = (cpu_time_start > cpu_time_end ? cpu_time_end : cpu_time_start);
m_cpu_time_end = (cpu_time_start > cpu_time_end ? cpu_time_start : cpu_time_end);
m_wall_time_start = (wall_time_start > wall_time_end ? wall_time_end : wall_time_start);
m_wall_time_end = (wall_time_start > wall_time_end ? wall_time_start : wall_time_end);
m_iterations = iterations;
}
private:
template <class TimeInterval>
double convertTimeInterval() const
{
return (static_cast<double>(m_ratio_numerator) * TimeInterval::den) / (m_ratio_denominator * TimeInterval::num);
}
double m_cpu_time_start;
double m_cpu_time_end;
double m_wall_time_start;
double m_wall_time_end;
std::uint64_t m_iterations;
std::intmax_t m_ratio_numerator;
std::intmax_t m_ratio_denominator;
};
template <bool high_precision = false>
/**
* @brief Timer class that allows advanced time tracking of events and
* time interval manipulation.
*
* If template parameter `high_precision` is true, then this timer will
* attempt to use the highest precision clock available in the system
* to measure wall time, otherwise, the system clock will be used.
*
* This timer is as precise as SimpleTimer. Difference between these classes
* is in the features and flexibility offered. If only basic timing is
* needed, SimpleTimer will offer a quick solution. If extra features
* such as CPU time, flexible time scales, then, this class is better.
*
* To measure execution time of a portion of code, sandwich the code between
* calls to `start()` and `stop()`.
*/
class EventTimer
{
public:
/**
* @brief Constructs a new EventTimer object.
* @param[in] start_active If `true`, the timer is constructed and started.
* Otherwise, the timer is idle and waiting to be started.
*/
EventTimer(bool start_active = false)
{
m_active = false;
m_cpu_start_time = std::clock();
m_start_time = ClockType::now();
// compute the 0 time
m_cpu_init_time = std::clock();
m_init_time = ClockType::now();
if (start_active)
start();
}
/**
* @brief Starts measuring time from this call and until stopped.
*/
void start()
{
m_active = true;
m_cpu_start_time = std::clock();
m_start_time = ClockType::now();
}
template <class TimeInterval = TimingReportEvent::DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Stops measuring time.
* @param[in] iterations Number of iterations measured in this event.
* @return A TimingReportEvent with the measurement details.
* @details
* The returned event report will reflect the timings between the latest call
* to `start()` and this call.
* @sa TimingReportEvent::iterations()
*/
TimingReportEvent::Ptr stop(std::uint64_t iterations = 1)
{
return stop<TimeInterval>(0, iterations, nullptr);
}
template <class TimeInterval = TimingReportEvent::DefaultTimeInterval> // TimeInterval = std::nano, std::micro, std::milli, std::ratio<1, 1>, etc.
/**
* @brief Stops measuring time.
* @param[in] id Optional ID to associate with this event.
* @param[in] iterations Number of iterations measured in this event.
* @param[in] description Optional text description to add to this event.
* @return A TimingReportEvent with the measurement details.
* @details
* The returned event report will reflect the timings between the latest call
* to `start()` and this call.
* @sa TimingReportEvent::iterations()
*/
TimingReportEvent::Ptr stop(std::uint32_t id,
std::uint64_t iterations,
const char *description)
{
double cpu_end_time = getCPUElapsedTime<TimeInterval>();
double wall_end_time = getWallElapsedTime<TimeInterval>();
m_active = false;
TimingReportEvent::Ptr retval = TimingReportEvent::create(id,
description ? std::string(description) : std::string());
retval->setTimings<TimeInterval>(
getCPUElapsedTime<TimeInterval>(m_cpu_start_time), cpu_end_time,
getWallElapsedTime<TimeInterval>(m_start_time), wall_end_time,
iterations);
return retval;
}
/**
* @brief Retrieves whether the timer is active.
* @returns `true` if a call to `start()` has been made without a matching call
* to `stop()`, i.e. the timer is active.
* @returns `false` otherwise.
*/
bool isActive() const { return m_active; }
private:
typedef typename std::conditional<high_precision,
std::chrono::high_resolution_clock,
std::chrono::system_clock>::type ClockType;
template <class TimeInterval>
double getCPUElapsedTime() const
{
return getCPUElapsedTime<TimeInterval>(std::clock());
}
template <class TimeInterval>
double getCPUElapsedTime(std::clock_t cpu_end_time) const
{
return (cpu_end_time - m_cpu_init_time) * static_cast<double>(TimeInterval::den) / (static_cast<double>(CLOCKS_PER_SEC) * static_cast<double>(TimeInterval::num));
}
template <class TimeInterval>
double getWallElapsedTime() const
{
return getWallElapsedTime<TimeInterval>(ClockType::now());
}
template <class TimeInterval>
double getWallElapsedTime(const std::chrono::time_point<ClockType> &end_time) const
{
return std::chrono::duration<double, TimeInterval>(end_time - m_init_time).count();
}
std::chrono::time_point<ClockType> m_init_time;
std::clock_t m_cpu_init_time;
std::chrono::time_point<ClockType> m_start_time;
std::clock_t m_cpu_start_time;
bool m_active;
};
} // namespace common
} // namespace pisa