The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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
14
15#include <atomic>
16#include <chrono>
17
18namespace Gaudi {
19 namespace Timers {
42 template <typename Clock, typename Unit>
44 public:
47
51 class ScopeTimer {
52 public:
54 ScopeTimer( Stats_t& stat ) : m_stats( stat ), m_t0( Clock::now() ) {}
55
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
A scoped timer that starts/stops on con/de-struction.
Clock::time_point m_t0
start time of timer
Stats_t & m_stats
reference to statistics counter
void stop() const
Stop the timer, accumulate elapsed time and return current measurement.
ScopeTimer(Stats_t &stat)
Start Scoped timer accumulating into stat.
auto elapsed() const
Return the elapsed time without stopping the timer.
~ScopeTimer()
Destructor stopping timer.
const Stats_t & stats() const
Return accumulated timing statistics.
Gaudi::Accumulators::StatCounter< Unit > Stats_t
Type of Counter used for accumulating time measurements.
GenericTimer(OWNER *o, const std::string &name)
Constructor attaching the statistics counter to an owner.
Stats_t m_stats
statistics counter
GenericTimer(const GenericTimer &)=delete
No copy.
auto operator()() const
Create (and start) a ScopeTimer.
GenericTimer()=default
Default constructor.
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
A counter aiming at computing average and sum2 / variance / standard deviation.