GenericTimer.h 2.77 KB
Newer Older
Frank Winklmeier's avatar
Frank Winklmeier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once

#include "Gaudi/Chrono/Counters.h"

#include <atomic>
#include <chrono>

namespace Gaudi {
  namespace Timers {
    /**
     * A generic timer based on std::chrono and Gaudi::Accumulators
     *
     * The timer is templated with a std::chrono compatible clock and the Unit
     * of precision (ms, us, ...). It accumulates all time measurements in a
     * Gaudi::Accumulators::StatCounter. A time measurement is done via the RAII helper
     * class created by calling operator().
     *
     * Usually one would use one of the predefined Timer types from Gaudi/Timers.h:
     *
     * @code
     *   // .h
     *   Gaudi::Timer m_timer;
     *
     *   // .cpp
     *   {
     *     auto timeit = m_timer();
     *     // code to be timed
     *   }
     *   info() << m_timer.stats() << endmsg;
     * @endcode
     */
    template <typename Clock, typename Unit>
    class GenericTimer {
    public:
      /// Type of Counter used for accumulating time measurements
      typedef Gaudi::Accumulators::StatCounter<Unit> Stats_t;

      /**
       * A scoped timer that starts/stops on con/de-struction
       */
      class ScopeTimer {
      public:
        /// Start Scoped timer accumulating into stat
        ScopeTimer( Stats_t& stat ) : m_stats( stat ), m_t0( Clock::now() ) {}

        /// Destructor stopping timer
        ~ScopeTimer() { stop(); }

        /// Return the elapsed time without stopping the timer
        /// @return std::chrono::duration<Unit>
        auto elapsed() const {
          // Fenced according to https://codereview.stackexchange.com/q/196245
          std::atomic_thread_fence( std::memory_order_relaxed );
          auto dt = Clock::now() - m_t0;
          std::atomic_thread_fence( std::memory_order_relaxed );
          return std::chrono::duration_cast<Unit>( dt );
        }

      private:
        /// Stop the timer, accumulate elapsed time and return current measurement
        void stop() const { m_stats += elapsed(); }

        Stats_t&                   m_stats; ///< reference to statistics counter
        typename Clock::time_point m_t0;    ///< start time of timer
      };

      /// Default constructor
      GenericTimer() = default;

      /// Constructor attaching the statistics counter to an owner
      template <class OWNER>
      GenericTimer( OWNER* o, const std::string& name ) {
        o->declareCounter( name, m_stats );
      }

      /// No copy
      GenericTimer( const GenericTimer& ) = delete;

      /// Create (and start) a ScopeTimer
      [[nodiscard]] auto operator()() const { return ScopeTimer( m_stats ); }

      /// Return accumulated timing statistics
      const Stats_t& stats() const { return m_stats; }

    private:
      mutable Stats_t m_stats; ///< statistics counter
    };

  } // namespace Timers
} // namespace Gaudi