ATLAS Offline Software
Loading...
Searching...
No Matches
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
14namespace 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>
70 Timer<unit>::operator double() const {
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>
96 public:
97 ScopedTimer(T& timer) : m_timer(timer) {
98 m_timer.start();
99 }
101 m_timer.stop();
102 }
103 private:
105 };
106
107} // namespace Monitored
108
109#endif /* AthenaMonitoringKernel_MonitoredTimer_h */
const std::string & name() const
virtual bool hasStringRepresentation() const override
indcates that the stored content can be converted to strings
virtual std::string getString(size_t i) const override
Timer(Timer &&)=default
clock_type::time_point m_startTime
std::chrono::high_resolution_clock clock_type
Timer & operator=(Timer const &)=delete
virtual double get(size_t) const override
Timer(Timer const &)=delete
Timer(std::string name)
clock_type::time_point m_stopTime
virtual size_t size() const override
gives size of vector representation
Generic monitoring tool for athena components.
void checkNamingConvention(std::string_view name)
STL namespace.