The Gaudi Framework  master (01b473db)
TimelineSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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>
15 
16 #include <fstream>
17 
20  if ( !sc.isSuccess() ) return sc;
21 
22  if ( msgLevel( MSG::DEBUG ) ) debug() << "initialize" << endmsg;
23 
24  m_events.clear();
25  // the initial allocation of tbb::concurrent_vector determines the size of
26  // the first allocated array, useful to reduce fragmentation
27  m_events.reserve( 1024 );
28 
29  if ( m_partial ) {
30  std::ofstream out( m_timelineFile + ".part", std::ofstream::trunc | std::ofstream::out );
31  out << "#start end algorithm thread slot event" << std::endl;
32  out.close();
33  }
34 
35  return StatusCode::SUCCESS;
36 }
37 
39 
40  MsgStream log( msgSvc(), name() );
41  log << MSG::DEBUG << "reinitialize" << endmsg;
42 
43  m_events.clear();
44 
45  return StatusCode::SUCCESS;
46 }
47 
49  if ( m_dumpTimeline && m_events.size() > 0 ) {
50  MsgStream log( msgSvc(), name() );
51 
52  log << MSG::INFO << "Outputting timeline with " << m_events.size() << " entries to file " << m_timelineFile.value()
53  << endmsg;
54 
56  }
57 
58  return StatusCode::SUCCESS;
59 }
60 
62  auto& newTimelineEvent = *m_events.emplace_back();
63  TimelineRecorder recorder{ newTimelineEvent, std::move( alg ), ctx };
64  if ( m_partial ) {
65  std::ofstream out( m_timelineFile + ".part", std::ofstream::app | std::ofstream::out );
66  out << std::chrono::duration_cast<std::chrono::nanoseconds>( newTimelineEvent.start.time_since_epoch() ).count()
67  << " "
68  << std::chrono::duration_cast<std::chrono::nanoseconds>( newTimelineEvent.end.time_since_epoch() ).count()
69  << " " << newTimelineEvent.algorithm << " " << newTimelineEvent.thread << " " << newTimelineEvent.slot << " "
70  << newTimelineEvent.event << std::endl;
71 
72  out.close();
73  }
74  return recorder;
75 }
76 
78  for ( const auto& candidate : m_events ) {
79  if ( candidate.algorithm == e.algorithm && candidate.event == e.event ) {
80  e = candidate;
81  return true;
82  }
83  }
84  return false;
85 }
86 
88  std::ofstream out( m_timelineFile, std::ofstream::out | std::ofstream::trunc );
89 
90  out << "#start end algorithm thread slot event" << std::endl;
91 
92  for ( const auto& e : m_events ) {
93  out << std::chrono::duration_cast<std::chrono::nanoseconds>( e.start.time_since_epoch() ).count() << " "
94  << std::chrono::duration_cast<std::chrono::nanoseconds>( e.end.time_since_epoch() ).count() << " "
95  << e.algorithm << " " << e.thread << " " << e.slot << " " << e.event << std::endl;
96  }
97 
98  out.close();
99 }
100 
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:22
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
TimelineSvc.h
Gaudi.Configuration.log
log
Definition: Configuration.py:28
Read.app
app
Definition: Read.py:36
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
MSG::INFO
@ INFO
Definition: IMessageSvc.h:22
TimelineSvc::m_partial
Gaudi::Property< bool > m_partial
Definition: TimelineSvc.h:40
TimelineSvc::outputTimeline
void outputTimeline()
Definition: TimelineSvc.cpp:87
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:147
TimelineEvent::event
size_t event
Definition: TimelineEvent.h:23
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:80
ManySmallAlgs.alg
alg
Definition: ManySmallAlgs.py:81
TimelineEvent
Definition: TimelineEvent.h:17
TimelineSvc::getTimelineEvent
bool getTimelineEvent(TimelineEvent &) const override
Definition: TimelineSvc.cpp:77
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:333
StatusCode
Definition: StatusCode.h:64
ITimelineSvc::TimelineRecorder
RAII helper to record timeline events.
Definition: ITimelineSvc.h:27
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:229
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:198
TimelineSvc::m_events
tbb::concurrent_vector< TimelineEvent > m_events
Definition: TimelineSvc.h:42
TimelineSvc::finalize
StatusCode finalize() override
Definition: TimelineSvc.cpp:48
MsgStream
Definition: MsgStream.h:29
TimelineSvc::getRecorder
TimelineRecorder getRecorder(std::string alg, const EventContext &ctx) override
Definition: TimelineSvc.cpp:61
TimelineEvent.h
TimelineSvc::reinitialize
StatusCode reinitialize() override
Definition: TimelineSvc.cpp:38
TimelineSvc
Definition: TimelineSvc.h:21
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:45
TimelineSvc::initialize
StatusCode initialize() override
Definition: TimelineSvc.cpp:18
EventContext
Definition: EventContext.h:34
TimelineSvc::m_dumpTimeline
Gaudi::Property< bool > m_dumpTimeline
Definition: TimelineSvc.h:39
TimelineEvent::algorithm
std::string algorithm
Definition: TimelineEvent.h:25
TimelineSvc::m_timelineFile
Gaudi::Property< std::string > m_timelineFile
Definition: TimelineSvc.h:37
MsgStream.h
Gaudi::Functional::details::out
OptOut && out
Definition: details.h:179