The Gaudi Framework  v30r4 (9b837755)
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  auto& newTimelineEvent = *m_events.emplace_back();
56  TimelineRecorder recorder{newTimelineEvent, std::move( alg ), ctx};
57  if ( m_partial ) {
58  std::ofstream out( m_timelineFile + ".part", std::ofstream::app | std::ofstream::out );
59  out << std::chrono::duration_cast<std::chrono::nanoseconds>( newTimelineEvent.start.time_since_epoch() ).count()
60  << " "
61  << std::chrono::duration_cast<std::chrono::nanoseconds>( newTimelineEvent.end.time_since_epoch() ).count()
62  << " " << newTimelineEvent.algorithm << " " << newTimelineEvent.thread << " " << newTimelineEvent.slot << " "
63  << newTimelineEvent.event << std::endl;
64 
65  out.close();
66  }
67  return recorder;
68 }
69 
71 {
72  for ( const auto& candidate : m_events ) {
73  if ( candidate.algorithm == e.algorithm && candidate.event == e.event ) {
74  e = candidate;
75  return true;
76  }
77  }
78  return false;
79 }
80 
82 {
83  std::ofstream out( m_timelineFile, std::ofstream::out | std::ofstream::trunc );
84 
85  out << "#start end algorithm thread slot event" << std::endl;
86 
87  for ( const auto& e : m_events ) {
88  out << std::chrono::duration_cast<std::chrono::nanoseconds>( e.start.time_since_epoch() ).count() << " "
89  << std::chrono::duration_cast<std::chrono::nanoseconds>( e.end.time_since_epoch() ).count() << " "
90  << e.algorithm << " " << e.thread << " " << e.slot << " " << e.event << std::endl;
91  }
92 
93  out.close();
94 }
95 
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:31
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:70
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:81
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)