Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
RootPerfMonSvc.cpp
Go to the documentation of this file.
1 //====================================================================
2 // RootPerfMonSvc implementation
3 //--------------------------------------------------------------------
4 //
5 // Description: Implementation of the ROOT data storage
6 //
7 // Author : M.Frank
8 //
9 //====================================================================
10 
11 // Framework include files
12 #include "RootCnv/RootPerfMonSvc.h"
15 #include "GaudiKernel/Incident.h"
16 #include "GaudiKernel/MsgStream.h"
17 #include "GaudiKernel/System.h"
18 #include "RootUtils.h"
19 
20 #include "TBranch.h"
21 #include "TDirectory.h"
22 #include "TMap.h"
23 #include "TSystem.h"
24 
25 using namespace std;
26 using namespace Gaudi;
27 typedef const string& CSTR;
28 
29 #define S_OK StatusCode::SUCCESS
30 #define S_FAIL StatusCode::FAILURE
31 
32 // Small routine to issue exceptions
33 StatusCode RootPerfMonSvc::error( CSTR msg ) {
34  if ( m_log ) {
35  log() << MSG::ERROR << "Error: " << msg << endmsg;
36  return S_FAIL;
37  }
38  MsgStream m( msgSvc(), name() );
39  m << MSG::ERROR << "Error: " << msg << endmsg;
40  return S_FAIL;
41 }
42 
43 // Initialize the Db data persistency service
44 StatusCode RootPerfMonSvc::initialize() {
45  string cname;
47  if ( !status.isSuccess() ) return error( "Failed to initialize Service base class." );
48  m_log.reset( new MsgStream( msgSvc(), name() ) );
49  m_incidentSvc = service( "IncidentSvc" );
50  if ( !m_incidentSvc ) return error( "Unable to localize interface from service:IncidentSvc" );
51 
52  m_incidentSvc->addListener( this, IncidentType::BeginEvent, 1, false, false );
53  m_incidentSvc->addListener( this, "NEW_STREAM", 1, false, false );
54  m_incidentSvc->addListener( this, "CONNECTED_OUTPUT", 1, false, false );
55 
56  if ( m_ioPerfStats.empty() ) return error( "Performance monitoring file IOPerfStats was not defined." );
57 
58  TDirectory::TContext ctxt( nullptr );
59  m_perfFile.reset( new TFile( m_ioPerfStats.value().c_str(), "RECREATE" ) );
60  if ( !m_perfFile ) return error( "Could not create ROOT file." );
61 
62  if ( !( m_perfTree = new TTree( "T", "performance measurement" ) ) ) return error( "Could not create tree." );
63 
64  m_perfTree->Branch( "utime", &m_utime, "utime/l" );
65  m_perfTree->Branch( "stime", &m_stime, "stime/l" );
66  m_perfTree->Branch( "vsize", &m_vsize, "vsize/l" );
67  m_perfTree->Branch( "rss", &m_rss, "rss/L" );
68  m_perfTree->Branch( "time", &m_time, "time/L" );
69  m_eventNumber = 0;
70  m_perfTree->Branch( "event_number", &m_eventNumber, "event_number/L" );
71  m_perfTree->Branch( "event_type", &m_eventType, "event_type/I" );
72 
73  if ( m_setStreams.empty() ) m_setStreams = "undefined";
74  if ( m_basketSize.empty() ) m_basketSize = "undefined";
75  if ( m_bufferSize.empty() ) m_bufferSize = "undefined";
76  if ( m_splitLevel.empty() ) m_splitLevel = "undefined";
77 
78  auto map = new TMap();
79  map->Add( new TObjString( "streams" ), new TObjString( m_setStreams.value().c_str() ) );
80  map->Add( new TObjString( "basket_size" ), new TObjString( m_basketSize.value().c_str() ) );
81  map->Add( new TObjString( "buffer_size" ), new TObjString( m_bufferSize.value().c_str() ) );
82  map->Add( new TObjString( "split_level" ), new TObjString( m_splitLevel.value().c_str() ) );
83  map->Write( "params", TObject::kSingleKey );
84  return S_OK;
85 }
86 
87 void RootPerfMonSvc::record( EventType eventType ) {
88  SysProcStat data;
89  m_eventType = eventType;
90  m_utime = data.utime;
91  m_stime = data.stime;
92  m_vsize = data.vsize;
93  m_rss = data.rss;
94  m_time = data.time;
95  m_perfTree->Fill();
96 }
97 
98 void RootPerfMonSvc::handle( const Incident& incident ) {
99  std::string t = incident.type();
100  if ( !t.compare( IncidentType::BeginEvent ) ) {
101  m_eventNumber++;
102  record( EVENT );
103  return;
104  }
105  if ( !t.compare( "CONNECTED_OUTPUT" ) ) { m_outputs.insert( incident.source() ); }
106 }
107 
108 // Stop the performance monitoring service
109 StatusCode RootPerfMonSvc::stop() {
110  char text[64];
111  record( FSR );
112  auto map = new TMap();
113  for ( const auto& i : m_outputs ) {
114  const char* fn = i.c_str();
115  Long_t id, siz, flags, tim;
116  if ( 0 == gSystem->GetPathInfo( fn, &id, &siz, &flags, &tim ) ) {
117  ::sprintf( text, "%ld", siz );
118  map->Add( new TObjString( fn ), new TObjString( text ) );
119  }
120  }
121  TDirectory::TContext ctxt( m_perfFile.get() );
122  map->Write( "Outputs", TObject::kSingleKey );
123  return S_OK;
124 }
125 
126 // Finalize the performance monitoring service
127 StatusCode RootPerfMonSvc::finalize() {
128  record( FSR );
129  log() << MSG::INFO;
130  m_log.reset();
131  m_incidentSvc.reset();
132 
133  m_perfFile->Write();
134  m_perfFile->Close();
135  m_perfFile.reset();
136 
137  return Service::finalize();
138 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:60
long unsigned stime
Definition: SysProcStat.h:16
const std::string & type() const
Access to the incident type.
Definition: Incident.h:38
StatusCode finalize() override
Definition: Service.cpp:164
const std::string & source() const
Access to the source of the incident.
Definition: Incident.h:44
bool isSuccess() const
Definition: StatusCode.h:267
T log(T...args)
#define S_OK
#define S_FAIL
STL namespace.
STL class.
STL class.
long unsigned vsize
Definition: SysProcStat.h:16
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
constexpr double m
Definition: SystemOfUnits.h:92
const string & CSTR
long unsigned utime
Definition: SysProcStat.h:16
Base class for all Incidents (computing events).
Definition: Incident.h:17
T sprintf(T...args)
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
T compare(T...args)