The Gaudi Framework  v30r3 (a5ef0a68)
TimelineSvc.cpp
Go to the documentation of this file.
1 #include "TimelineSvc.h"
4 
5 #include <fstream>
6 
8 {
10  if ( !sc.isSuccess() ) return sc;
11 
12  if ( msgLevel( MSG::DEBUG ) ) debug() << "initialize" << endmsg;
13 
14  m_events.clear();
15  // the initial allocation of tbb::concurrent_vector determines the size of
16  // the first allocated array, useful to reduce fragmentation
17  m_events.reserve( 1024 );
18 
19  if ( m_partial ) {
20  std::ofstream out( m_timelineFile + ".part", std::ofstream::trunc | std::ofstream::out );
21  out << "#start end algorithm thread slot event" << std::endl;
22  out.close();
23  }
24 
25  return StatusCode::SUCCESS;
26 }
27 
29 {
30 
31  MsgStream log( msgSvc(), name() );
32  log << MSG::DEBUG << "reinitialize" << endmsg;
33 
34  m_events.clear();
35 
36  return StatusCode::SUCCESS;
37 }
38 
40 {
41  if ( m_dumpTimeline && m_events.size() > 0 ) {
42  MsgStream log( msgSvc(), name() );
43 
44  log << MSG::INFO << "Outputting timeline with " << m_events.size() << " entries to file " << m_timelineFile
45  << endmsg;
46 
48  }
49 
50  return StatusCode::SUCCESS;
51 }
52 
54 {
55  TimelineRecorder recorder{*m_events.emplace_back(), std::move( alg ), ctx};
56  if ( m_partial ) {
57  auto& e = m_events.back();
58  std::ofstream out( m_timelineFile + ".part", std::ofstream::app | std::ofstream::out );
59  out << std::chrono::duration_cast<std::chrono::nanoseconds>( e.start.time_since_epoch() ).count() << " "
60  << std::chrono::duration_cast<std::chrono::nanoseconds>( e.end.time_since_epoch() ).count() << " "
61  << e.algorithm << " " << e.thread << " " << e.slot << " " << e.event << std::endl;
62 
63  out.close();
64  }
65  return recorder;
66 }
67 
69 {
70  for ( const auto& candidate : m_events ) {
71  if ( candidate.algorithm == e.algorithm && candidate.event == e.event ) {
72  e = candidate;
73  return true;
74  }
75  }
76  return false;
77 }
78 
80 {
81  std::ofstream out( m_timelineFile, std::ofstream::out | std::ofstream::trunc );
82 
83  out << "#start end algorithm thread slot event" << std::endl;
84 
85  for ( const auto& e : m_events ) {
86  out << std::chrono::duration_cast<std::chrono::nanoseconds>( e.start.time_since_epoch() ).count() << " "
87  << std::chrono::duration_cast<std::chrono::nanoseconds>( e.end.time_since_epoch() ).count() << " "
88  << e.algorithm << " " << e.thread << " " << e.slot << " " << e.event << std::endl;
89  }
90 
91  out.close();
92 }
93 
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:63
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:288
Gaudi::Property< bool > m_partial
Definition: TimelineSvc.h:32
bool isSuccess() const
Definition: StatusCode.h:287
T endl(T...args)
std::string algorithm
Definition: ITimelineSvc.h:21
T duration_cast(T...args)
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
STL class.
#define DECLARE_COMPONENT(type)
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
T close(T...args)
tbb::concurrent_vector< TimelineEvent > m_events
Definition: TimelineSvc.h:34
bool getTimelineEvent(TimelineEvent &) const override
Definition: TimelineSvc.cpp:68
StatusCode finalize() override
Definition: TimelineSvc.cpp:39
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
TimelineRecorder getRecorder(std::string alg, const EventContext &ctx) override
Definition: TimelineSvc.cpp:53
T move(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
StatusCode initialize() override
Definition: TimelineSvc.cpp:7
Gaudi::Property< std::string > m_timelineFile
Definition: TimelineSvc.h:29
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
RAII helper to record timeline events.
Definition: ITimelineSvc.h:35
void outputTimeline()
Definition: TimelineSvc.cpp:79
StatusCode reinitialize() override
Definition: TimelineSvc.cpp:28
Gaudi::Property< bool > m_dumpTimeline
Definition: TimelineSvc.h:31
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)