The Gaudi Framework  master (37c0b60a)
TimelineSvc.cpp
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 #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.value()
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 ) {
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 
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
TimelineSvc.h
Gaudi.Configuration.log
log
Definition: Configuration.py:28
Read.app
app
Definition: Read.py:36
std::move
T move(T... args)
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
TimelineSvc::m_partial
Gaudi::Property< bool > m_partial
Definition: TimelineSvc.h:41
TimelineSvc::outputTimeline
void outputTimeline()
Definition: TimelineSvc.cpp:86
StatusCode.h
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
TimelineEvent::event
size_t event
Definition: ITimelineSvc.h:29
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:80
ManySmallAlgs.alg
alg
Definition: ManySmallAlgs.py:81
TimelineEvent
Definition: ITimelineSvc.h:23
TimelineSvc::getTimelineEvent
bool getTimelineEvent(TimelineEvent &) const override
Definition: TimelineSvc.cpp:76
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
std::ofstream
STL class.
ITimelineSvc::TimelineRecorder
RAII helper to record timeline events.
Definition: ITimelineSvc.h:44
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
TimelineSvc::m_events
tbb::concurrent_vector< TimelineEvent > m_events
Definition: TimelineSvc.h:43
TimelineSvc::finalize
StatusCode finalize() override
Definition: TimelineSvc.cpp:47
MsgStream
Definition: MsgStream.h:33
TimelineSvc::getRecorder
TimelineRecorder getRecorder(std::string alg, const EventContext &ctx) override
Definition: TimelineSvc.cpp:60
TimelineSvc::reinitialize
StatusCode reinitialize() override
Definition: TimelineSvc.cpp:37
TimelineSvc
Definition: TimelineSvc.h:22
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
TimelineSvc::initialize
StatusCode initialize() override
Definition: TimelineSvc.cpp:17
EventContext
Definition: EventContext.h:34
TimelineSvc::m_dumpTimeline
Gaudi::Property< bool > m_dumpTimeline
Definition: TimelineSvc.h:40
TimelineEvent::algorithm
std::string algorithm
Definition: ITimelineSvc.h:31
TimelineSvc::m_timelineFile
Gaudi::Property< std::string > m_timelineFile
Definition: TimelineSvc.h:38
MsgStream.h
PrepareBase.out
out
Definition: PrepareBase.py:20