| The Gaudi Framework v32r1 (f65d50dc)

GenericTimer.h
Go to the documentation of this file.
1 #pragma once
2
3 #include "Gaudi/Chrono/Counters.h"
4
5 #include <atomic>
6 #include <chrono>
7
8 namespace Gaudi {
9 namespace Timers {
32 template <typename Clock, typename Unit>
33 class GenericTimer {
34 public:
36 typedef Gaudi::Accumulators::StatCounter<Unit> Stats_t;
37
41 class ScopeTimer {
42 public:
44 ScopeTimer( Stats_t& stat ) : m_stats( stat ), m_t0( Clock::now() ) {}
45
47 ~ScopeTimer() { stop(); }
48
51 auto elapsed() const {
52 // Fenced according to https://codereview.stackexchange.com/q/196245
53 std::atomic_thread_fence( std::memory_order_relaxed );
54 auto dt = Clock::now() - m_t0;
55 std::atomic_thread_fence( std::memory_order_relaxed );
56 return std::chrono::duration_cast<Unit>( dt );
57 }
58
59 private:
61 void stop() const { m_stats += elapsed(); }
62
63 Stats_t& m_stats;
64 typename Clock::time_point m_t0;
65 };
66
68 GenericTimer() = default;
69
71 template <class OWNER>
72 GenericTimer( OWNER* o, const std::string& name ) {
73 o->declareCounter( name, m_stats );
74 }
75
77 GenericTimer( const GenericTimer& ) = delete;
78
80 [[nodiscard]] auto operator()() const { return ScopeTimer( m_stats ); }
81
83 const Stats_t& stats() const { return m_stats; }
84
85 private:
86 mutable Stats_t m_stats;
87 };
88
89 } // namespace Timers
90 } // namespace Gaudi
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::Timers::GenericTimer::ScopeTimer::elapsed
auto elapsed() const
Return the elapsed time without stopping the timer.
Definition: GenericTimer.h:51
Gaudi::Timers::GenericTimer
A generic timer based on std::chrono and Gaudi::Accumulators.
Definition: GenericTimer.h:33
Gaudi::Accumulators::StatCounter
A counter aiming at computing average and sum2 / variance / standard deviation.
Definition: Counters.h:744
Gaudi::Timers::GenericTimer::GenericTimer
GenericTimer()=default
Default constructor.
Gaudi::Timers::GenericTimer::stats
const Stats_t & stats() const
Return accumulated timing statistics.
Definition: GenericTimer.h:83
Gaudi::Timers::GenericTimer::GenericTimer
GenericTimer(OWNER *o, const std::string &name)
Constructor attaching the statistics counter to an owner.
Definition: GenericTimer.h:72
std::string
STL class.
Gaudi::Timers::GenericTimer::ScopeTimer::m_t0
Clock::time_point m_t0
start time of timer
Definition: GenericTimer.h:64
Gaudi::Timers::GenericTimer::Stats_t
Gaudi::Accumulators::StatCounter< Unit > Stats_t
Type of Counter used for accumulating time measurements.
Definition: GenericTimer.h:36
Counters.h
Gaudi::Timers::GenericTimer::ScopeTimer::stop
void stop() const
Stop the timer, accumulate elapsed time and return current measurement.
Definition: GenericTimer.h:61
Gaudi::Timers::GenericTimer::ScopeTimer
A scoped timer that starts/stops on con/de-struction.
Definition: GenericTimer.h:41
Gaudi::Timers::GenericTimer::operator()
auto operator()() const
Create (and start) a ScopeTimer.
Definition: GenericTimer.h:80
Gaudi::Timers::GenericTimer::m_stats
Stats_t m_stats
statistics counter
Definition: GenericTimer.h:86
std::atomic_thread_fence
T atomic_thread_fence(T... args)
Gaudi::Timers::GenericTimer::ScopeTimer::ScopeTimer
ScopeTimer(Stats_t &stat)
Start Scoped timer accumulating into stat.
Definition: GenericTimer.h:44
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::Timers::GenericTimer::ScopeTimer::m_stats
Stats_t & m_stats
reference to statistics counter
Definition: GenericTimer.h:63
Gaudi::Timers::GenericTimer::ScopeTimer::~ScopeTimer
~ScopeTimer()
Destructor stopping timer.
Definition: GenericTimer.h:47