ATLAS Offline Software
MonitoredTimer.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef AthenaMonitoringKernel_MonitoredTimer_h
6 #define AthenaMonitoringKernel_MonitoredTimer_h
7 
8 #include <chrono>
9 #include <vector>
10 #include <string_view>
11 
13 
14 namespace Monitored {
15  void checkNamingConvention( std::string_view name );
31  template< typename unit=std::chrono::microseconds>
32  class Timer : public IMonitoredVariable {
33  public:
34  Timer(std::string name );
35  Timer(Timer&&) = default;
36  Timer(Timer const&) = delete;
37 
38  void start(); //<! (re)starts the timer
39  void stop(); //<! stops the timer
40 
41  operator double() const;
42 
43  virtual double get(size_t) const override { return double(*this); }
44  virtual std::string getString([[maybe_unused]] size_t i) const override { return {}; }
45  virtual bool hasStringRepresentation() const override { return false; };
46  virtual size_t size() const override { return 1; }
47 
48  private:
49 
50  typedef std::chrono::high_resolution_clock clock_type;
51  clock_type::time_point m_startTime;
52  clock_type::time_point m_stopTime;
53 
54  Timer& operator=(Timer const&) = delete;
55  };
56 
57 
58  template<typename unit>
59  Timer<unit>::Timer(std::string tname) : IMonitoredVariable(std::move(tname)) {
61  start();
62  }
63  template<typename unit>
64  void Timer<unit>::start() { m_startTime = clock_type::now(); }
65 
66  template<typename unit>
67  void Timer<unit>::stop() { m_stopTime = clock_type::now(); }
68 
69  template<typename unit>
71  clock_type::time_point stopTime = m_stopTime;
72  if (stopTime == clock_type::time_point()) // never stopped
73  stopTime = clock_type::now();
74  auto d = std::chrono::duration_cast<unit>(stopTime - m_startTime);
75  return d.count();
76  }
77 
94  template<typename T>
95  class ScopedTimer {
96  public:
98  m_timer.start();
99  }
101  m_timer.stop();
102  }
103  private:
105  };
106 
107 } // namespace Monitored
108 
109 #endif /* AthenaMonitoringKernel_MonitoredTimer_h */
Monitored::checkNamingConvention
void checkNamingConvention(std::string_view name)
Definition: MonitoredTimer.cxx:10
Monitored::Timer::m_stopTime
clock_type::time_point m_stopTime
Definition: MonitoredTimer.h:52
Monitored::ScopedTimer::~ScopedTimer
~ScopedTimer()
Definition: MonitoredTimer.h:100
IMonitoredVariable.h
hist_file_dump.d
d
Definition: hist_file_dump.py:137
Monitored::Timer::Timer
Timer(std::string name)
Definition: MonitoredTimer.h:59
Monitored::ScopedTimer::m_timer
T & m_timer
Definition: MonitoredTimer.h:104
Monitored::Timer::get
virtual double get(size_t) const override
Definition: MonitoredTimer.h:43
Monitored::Timer::getString
virtual std::string getString([[maybe_unused]] size_t i) const override
Definition: MonitoredTimer.h:44
Monitored::Timer::clock_type
std::chrono::high_resolution_clock clock_type
Definition: MonitoredTimer.h:50
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
Monitored::Timer::Timer
Timer(Timer &&)=default
Monitored::Timer::Timer
Timer(Timer const &)=delete
Monitored::ScopedTimer::ScopedTimer
ScopedTimer(T &timer)
Definition: MonitoredTimer.h:97
Monitored::IMonitoredVariable
Definition: IMonitoredVariable.h:14
python.handimod.now
now
Definition: handimod.py:675
Monitored::Timer::start
void start()
Definition: MonitoredTimer.h:64
lumiFormat.i
int i
Definition: lumiFormat.py:85
Monitored
Generic monitoring tool for athena components.
Definition: GenericMonitoringTool.h:30
Monitored::Timer::operator=
Timer & operator=(Timer const &)=delete
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:225
Monitored::Timer::size
virtual size_t size() const override
gives size of vector representation
Definition: MonitoredTimer.h:46
Monitored::Timer::stop
void stop()
Definition: MonitoredTimer.h:67
Monitored::IMonitoredVariable::name
const std::string & name() const
Definition: IMonitoredVariable.h:19
Monitored::ScopedTimer
Helper class to create a scoped timer.
Definition: MonitoredTimer.h:95
Monitored::Timer::hasStringRepresentation
virtual bool hasStringRepresentation() const override
indcates that the stored content can be converted to strings
Definition: MonitoredTimer.h:45
Monitored::Timer::m_startTime
clock_type::time_point m_startTime
Definition: MonitoredTimer.h:51
Monitored::Timer
A monitored timer.
Definition: MonitoredTimer.h:32