The Gaudi Framework  master (37c0b60a)
GenericTimer.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 
13 #include <Gaudi/Chrono/Counters.h>
14 
15 #include <atomic>
16 #include <chrono>
17 
18 namespace Gaudi {
19  namespace Timers {
42  template <typename Clock, typename Unit>
43  class GenericTimer {
44  public:
47 
51  class ScopeTimer {
52  public:
54  ScopeTimer( Stats_t& stat ) : m_stats( stat ), m_t0( Clock::now() ) {}
55 
57  ~ScopeTimer() { stop(); }
58 
61  auto elapsed() const {
62  // Fenced according to https://codereview.stackexchange.com/q/196245
63  std::atomic_thread_fence( std::memory_order_relaxed );
64  auto dt = Clock::now() - m_t0;
65  std::atomic_thread_fence( std::memory_order_relaxed );
66  return std::chrono::duration_cast<Unit>( dt );
67  }
68 
69  private:
71  void stop() const { m_stats += elapsed(); }
72 
74  typename Clock::time_point m_t0;
75  };
76 
78  GenericTimer() = default;
79 
81  template <class OWNER>
82  GenericTimer( OWNER* o, const std::string& name ) {
83  o->declareCounter( name, std::string{ "timer:" } + typeid( Unit::rep ).name(), m_stats );
84  }
85 
87  GenericTimer( const GenericTimer& ) = delete;
88 
90  [[nodiscard]] auto operator()() const { return ScopeTimer( m_stats ); }
91 
93  const Stats_t& stats() const { return m_stats; }
94 
95  private:
96  mutable Stats_t m_stats;
97  };
98 
99  } // namespace Timers
100 } // namespace Gaudi
Gaudi::Timers::GenericTimer
A generic timer based on std::chrono and Gaudi::Accumulators.
Definition: GenericTimer.h:43
std::string
STL class.
Gaudi::Timers::GenericTimer::stats
const Stats_t & stats() const
Return accumulated timing statistics.
Definition: GenericTimer.h:93
Gaudi::Timers::GenericTimer::ScopeTimer::m_t0
Clock::time_point m_t0
start time of timer
Definition: GenericTimer.h:74
Gaudi::Timers::GenericTimer::ScopeTimer
A scoped timer that starts/stops on con/de-struction.
Definition: GenericTimer.h:51
Gaudi::Timers::GenericTimer::GenericTimer
GenericTimer()=default
Default constructor.
Gaudi::Timers::GenericTimer::ScopeTimer::ScopeTimer
ScopeTimer(Stats_t &stat)
Start Scoped timer accumulating into stat.
Definition: GenericTimer.h:54
Gaudi::Timers::GenericTimer::ScopeTimer::stop
void stop() const
Stop the timer, accumulate elapsed time and return current measurement.
Definition: GenericTimer.h:71
Gaudi::Timers::GenericTimer::operator()
auto operator()() const
Create (and start) a ScopeTimer.
Definition: GenericTimer.h:90
Counters.h
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Timers::GenericTimer::GenericTimer
GenericTimer(OWNER *o, const std::string &name)
Constructor attaching the statistics counter to an owner.
Definition: GenericTimer.h:82
Gaudi::Timers::GenericTimer::ScopeTimer::m_stats
Stats_t & m_stats
reference to statistics counter
Definition: GenericTimer.h:73
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::Timers::GenericTimer::ScopeTimer::~ScopeTimer
~ScopeTimer()
Destructor stopping timer.
Definition: GenericTimer.h:57
Gaudi::Timers::GenericTimer::m_stats
Stats_t m_stats
statistics counter
Definition: GenericTimer.h:96
Gaudi::Timers::GenericTimer::ScopeTimer::elapsed
auto elapsed() const
Return the elapsed time without stopping the timer.
Definition: GenericTimer.h:61
Gaudi::Timers::GenericTimer::Stats_t
Gaudi::Accumulators::StatCounter< Unit > Stats_t
Type of Counter used for accumulating time measurements.
Definition: GenericTimer.h:46
Gaudi::Accumulators::StatCounter< Unit >
Gaudi::Timers::GenericTimer::GenericTimer
GenericTimer(const GenericTimer &)=delete
No copy.
std::atomic_thread_fence
T atomic_thread_fence(T... args)