The Gaudi Framework master (9cc87df9)
Loading...
Searching...
No Matches
TimelineSvc.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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
12// Framework includes
15#include <GaudiKernel/Service.h>
18
19// STL includes
20#include <algorithm>
21#include <fstream>
22#include <ranges>
23#include <string>
24
25// External libraries
26#include <boost/functional/hash.hpp>
27#include <tbb/concurrent_unordered_map.h>
28
29class TimelineSvc : public extends<Service, ITimelineSvc> {
30public:
31 using extends::extends;
32
33 StatusCode initialize() override;
34 StatusCode reinitialize() override;
35 StatusCode finalize() override;
36
37 TimelineRecorder getRecorder( std::string alg, const EventContext& ctx ) override;
38
39 bool getTimelineEvent( TimelineEvent& ) const override;
40 bool getFirstMatching( TimelineEvent& ) const override;
41 bool getLastMatching( TimelineEvent& ) const override;
42 bool isEnabled() const override { return m_isEnabled; }
43
44private:
45 void outputTimeline();
46
47 Gaudi::Property<std::string> m_timelineFile{ this, "TimelineFile", "timeline.csv", "" };
48 Gaudi::Property<bool> m_isEnabled{ this, "RecordTimeline", false, "Enable recording of the timeline events" };
49 Gaudi::Property<bool> m_dumpTimeline{ this, "DumpTimeline", false, "Enable dumping of the timeline events" };
50 Gaudi::Property<bool> m_partial{ this, "Partial", false, "" };
51
53 tbb::concurrent_unordered_multimap<key_type, TimelineEvent, boost::hash<key_type>> m_events;
54};
55
58 if ( !sc.isSuccess() ) return sc;
59
60 if ( msgLevel( MSG::DEBUG ) ) debug() << "initialize" << endmsg;
61
62 m_events.clear();
63 // the initial allocation of tbb::concurrent_vector determines the size of
64 // the first allocated array, useful to reduce fragmentation
65 m_events.reserve( 1024 );
66
67 if ( m_partial ) {
68 std::ofstream out( m_timelineFile + ".part", std::ofstream::trunc | std::ofstream::out );
69 out << "#start end algorithm thread slot event" << std::endl;
70 out.close();
71 }
72
74}
75
77
78 MsgStream log( msgSvc(), name() );
79 log << MSG::DEBUG << "reinitialize" << endmsg;
80
81 m_events.clear();
82
84}
85
87 if ( m_dumpTimeline && m_events.size() > 0 ) {
88 MsgStream log( msgSvc(), name() );
89
90 log << MSG::INFO << "Outputting timeline with " << m_events.size() << " entries to file " << m_timelineFile.value()
91 << endmsg;
92
94 }
95
97}
98
100 auto& newTimelineEvent = m_events.emplace( std::make_tuple( alg, ctx.evt() ), TimelineEvent{} ).first->second;
101 newTimelineEvent.algorithm = std::move( alg );
102 newTimelineEvent.slot = ctx.slot();
103 newTimelineEvent.event = ctx.evt();
104 TimelineRecorder recorder{ newTimelineEvent };
105 if ( m_partial ) {
106 std::ofstream out( m_timelineFile + ".part", std::ofstream::app | std::ofstream::out );
107 out << std::chrono::duration_cast<std::chrono::nanoseconds>( newTimelineEvent.start.time_since_epoch() ).count()
108 << " "
109 << std::chrono::duration_cast<std::chrono::nanoseconds>( newTimelineEvent.end.time_since_epoch() ).count()
110 << " " << newTimelineEvent.algorithm << " " << newTimelineEvent.thread << " " << newTimelineEvent.slot << " "
111 << newTimelineEvent.event << std::endl;
112
113 out.close();
114 }
115 return recorder;
116}
117
119
121 auto [begin, end] = m_events.equal_range( std::make_tuple( e.algorithm, e.event ) );
122 if ( begin == end ) { return false; }
123 auto it =
124 std::min_element( begin, end, []( const auto& a, const auto& b ) { return a.second.start < b.second.start; } );
125 e = it->second;
126 return true;
127}
128
130 auto [begin, end] = m_events.equal_range( std::make_tuple( e.algorithm, e.event ) );
131 if ( begin == end ) { return false; }
132 auto it =
133 std::max_element( begin, end, []( const auto& a, const auto& b ) { return a.second.start < b.second.start; } );
134 e = it->second;
135 return true;
136}
137
140
141 out << "#start end algorithm thread slot event" << std::endl;
142
143 auto flat_events = std::vector<TimelineEvent>();
144 flat_events.reserve( m_events.size() );
145
146 std::ranges::copy( m_events | std::views::values, std::back_inserter( flat_events ) );
147
148 std::sort( flat_events.begin(), flat_events.end(), []( const auto& a, const auto& b ) { return a.start < b.start; } );
149 for ( const auto& e : flat_events ) {
150 out << std::chrono::duration_cast<std::chrono::nanoseconds>( e.start.time_since_epoch() ).count() << " "
151 << std::chrono::duration_cast<std::chrono::nanoseconds>( e.end.time_since_epoch() ).count() << " "
152 << e.algorithm << " " << e.thread << " " << e.slot << " " << e.event << std::endl;
153 }
154
155 out.close();
156}
157
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
complex< _Tp > log(const complex< _Tp > &)
constexpr tuple< typename __decay_and_strip< _Elements >::__type... > make_tuple(_Elements &&... __args)
constexpr _OI move(_II __first, _II __last, _OI __result)
constexpr _ForwardIterator min_element(_ForwardIterator __first, _ForwardIterator __last)
constexpr void sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
constexpr _ForwardIterator max_element(_ForwardIterator __first, _ForwardIterator __last)
constexpr back_insert_iterator< _Container > back_inserter(_Container &__x)
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
constexpr auto begin(_Container &__cont) -> decltype(__cont.begin())
constexpr auto end(_Container &__cont) -> decltype(__cont.end())
static const openmode out
static const openmode app
static const openmode trunc
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
This class represents an entry point to all the event specific data.
Implementation of property with value of concrete type.
Definition Property.h:35
const ValueType & value() const
Definition Property.h:246
RAII helper to record timeline events.
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
StatusCode initialize() override
Definition Service.cpp:118
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isSuccess() const
Definition StatusCode.h:302
constexpr static const auto SUCCESS
Definition StatusCode.h:99
Gaudi::Property< bool > m_isEnabled
Gaudi::Property< bool > m_partial
void outputTimeline()
bool getTimelineEvent(TimelineEvent &) const override
StatusCode initialize() override
StatusCode reinitialize() override
bool getFirstMatching(TimelineEvent &) const override
StatusCode finalize() override
Gaudi::Property< std::string > m_timelineFile
bool isEnabled() const override
std::tuple< std::string, std::size_t > key_type
TimelineRecorder getRecorder(std::string alg, const EventContext &ctx) override
Gaudi::Property< bool > m_dumpTimeline
bool getLastMatching(TimelineEvent &) const override
tbb::concurrent_unordered_multimap< key_type, TimelineEvent, boost::hash< key_type > > m_events
Base class used to extend a class implementing other interfaces.
Definition extends.h:19
@ DEBUG
Definition IMessageSvc.h:22
@ INFO
Definition IMessageSvc.h:22
std::string algorithm