| The Gaudi Framework v32r1 (f65d50dc)

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