DP3
Timer.h
Go to the documentation of this file.
1 // Timer.h: Accurate timer
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #ifndef LOFAR_COMMON_TIMER_H
7 #define LOFAR_COMMON_TIMER_H
8 
9 #include "PrettyUnits.h"
10 
11 #include <chrono>
12 #include <iomanip>
13 #include <iostream>
14 #include <string>
15 
16 #if defined __ia64__ && defined __INTEL_COMPILER
17 #include <ia64regs.h>
18 #endif
19 
20 namespace dp3 {
21 namespace common {
22 
24 
41 template <class Clock>
42 class BaseTimer {
43  public:
46  explicit BaseTimer(const std::string& name = std::string())
47  : name_(name), count_(0), duration_(std::chrono::seconds{0}) {}
48 
50  void start() { start_ = Clock::now(); }
52  void stop() {
53  duration_ += Clock::now() - start_;
54  ++count_;
55  }
56 
58  void reset() {
59  count_ = 0;
60  duration_ = std::chrono::seconds{0};
61  }
62 
64  std::ostream& print(std::ostream& str) const {
65  if (name_.empty()) {
66  str << "timer: ";
67  } else {
68  str << std::left << std::setw(25) << name_ << ": " << std::right;
69  }
70  if (count_ == 0) {
71  str << "not used";
72  } else {
73  const double total = getElapsed();
74  // clang-format off
75  str << "avg = " << PrettyTime(total / count_)
76  << ", total = " << PrettyTime(total)
77  << ", count = " << std::setw(9) << count_;
78  // clang-format on
79  }
80  return str;
81  }
82 
84  double getElapsed() const {
85  return std::chrono::duration<double>{duration_}.count();
86  }
87 
89  double getAverage() const { return getElapsed() / getCount(); }
90 
92  uint64_t getCount() const { return count_; }
93 
95  BaseTimer& operator+=(const BaseTimer& other) {
96  duration_ += other.duration_;
97  count_ += other.count_;
98 
99  return *this;
100  }
101 
103  class StartStop {
104  public:
105  StartStop(BaseTimer<Clock>& timer) : timer_(timer) { timer_.start(); }
106  ~StartStop() { timer_.stop(); }
107 
109  StartStop(const StartStop&) = delete;
110  StartStop& operator=(const StartStop&) = delete;
111 
112  private:
113  BaseTimer<Clock>& timer_;
114  };
115 
116  private:
117  std::string name_;
118 
120  uint64_t count_;
121 
123  typename Clock::duration duration_;
124 
126  typename Clock::time_point start_;
127 };
128 
130 
145 template <class T, class Clock = std::chrono::steady_clock>
146 #if __cplusplus > 201703L
147 requires requires(T& t, double d) {
148  t += d;
149 }
150 #endif
152  public:
153  explicit ScopedMicroSecondAccumulator(T& accumulator)
154  : accumulator_(accumulator) {
155  timer_.start();
156  }
158 
160  timer_.stop();
161  accumulator_ += timer_.getElapsed() * 1e6;
162  }
163 
164  private:
165  BaseTimer<Clock> timer_;
166  T& accumulator_;
167 };
168 
169 } // namespace common
170 } // namespace dp3
171 
172 #endif
Internal class to do an automatic start/stop.
Definition: Timer.h:103
StartStop(BaseTimer< Clock > &timer)
Definition: Timer.h:105
~StartStop()
Definition: Timer.h:106
StartStop & operator=(const StartStop &)=delete
StartStop(const StartStop &)=delete
Forbid copy.
Very accurate timer for elapsed times.
Definition: Timer.h:42
std::ostream & print(std::ostream &str) const
Prints the timer.
Definition: Timer.h:64
void start()
Starts the timer.
Definition: Timer.h:50
double getElapsed() const
Get the elapsed time (in seconds).
Definition: Timer.h:84
uint64_t getCount() const
Get the total number of times start/stop is done.
Definition: Timer.h:92
BaseTimer(const std::string &name=std::string())
Definition: Timer.h:46
BaseTimer & operator+=(const BaseTimer &other)
Accumulate timer statistics.
Definition: Timer.h:95
double getAverage() const
Get the average time (in seconds) between start/stop.
Definition: Timer.h:89
void reset()
Resets the timer to zero.
Definition: Timer.h:58
void stop()
Stops the timer.
Definition: Timer.h:52
Print time in a human-readable way.
Definition: PrettyUnits.h:21
ScopedMicroSecondAccumulator(T &accumulator)
Definition: Timer.h:153
~ScopedMicroSecondAccumulator()
Definition: Timer.h:159
ScopedMicroSecondAccumulator(const ScopedMicroSecondAccumulator &)=delete
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53