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