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 "GaudiKernel/MsgStream.h"
15 #include "GaudiKernel/Incident.h"
16 #include "GaudiKernel/System.h"
17 #include "RootCnv/RootPerfMonSvc.h"
18 #include "RootUtils.h"
19 
20 #include "TDirectory.h"
21 #include "TSystem.h"
22 #include "TBranch.h"
23 #include "TMap.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() )
48  return error("Failed to initialize Service base class.");
49  m_log.reset( new MsgStream(msgSvc(),name()) );
50  m_incidentSvc = service("IncidentSvc");
51  if( !m_incidentSvc )
52  return error("Unable to localize interface from service:IncidentSvc");
53 
54  m_incidentSvc->addListener(this, IncidentType::BeginEvent, 1, false, false);
55  m_incidentSvc->addListener(this, "NEW_STREAM", 1, false, false);
56  m_incidentSvc->addListener(this, "CONNECTED_OUTPUT", 1, false, false);
57 
58  if (m_ioPerfStats.empty())
59  return error("Performance monitoring file IOPerfStats was not defined.");
60 
61  TDirectory::TContext ctxt(nullptr);
62  m_perfFile.reset( new TFile(m_ioPerfStats.value().c_str(),"RECREATE") );
63  if (!m_perfFile ) return error("Could not create ROOT file.");
64 
65  if (!(m_perfTree = new TTree("T", "performance measurement")))
66  return error("Could not create tree.");
67 
68  m_perfTree->Branch("utime", &m_utime, "utime/l");
69  m_perfTree->Branch("stime", &m_stime, "stime/l");
70  m_perfTree->Branch("vsize", &m_vsize, "vsize/l");
71  m_perfTree->Branch("rss", &m_rss, "rss/L");
72  m_perfTree->Branch("time", &m_time, "time/L");
73  m_eventNumber = 0;
74  m_perfTree->Branch("event_number", &m_eventNumber, "event_number/L");
75  m_perfTree->Branch("event_type", &m_eventType, "event_type/I");
76 
77  if (m_setStreams.empty())
78  m_setStreams = "undefined";
79  if (m_basketSize.empty())
80  m_basketSize = "undefined";
81  if (m_bufferSize.empty())
82  m_bufferSize = "undefined";
83  if (m_splitLevel.empty())
84  m_splitLevel = "undefined";
85 
86  auto map = new TMap();
87  map->Add(new TObjString("streams"), new TObjString(m_setStreams.value().c_str()));
88  map->Add(new TObjString("basket_size"), new TObjString(m_basketSize.value().c_str()));
89  map->Add(new TObjString("buffer_size"), new TObjString(m_bufferSize.value().c_str()));
90  map->Add(new TObjString("split_level"), new TObjString(m_splitLevel.value().c_str()));
91  map->Write("params", TObject::kSingleKey);
92  return S_OK;
93 }
94 
95 void RootPerfMonSvc::record(EventType eventType) {
96  SysProcStat data;
97  m_eventType = eventType;
98  m_utime = (ULong_t)data.utime;
99  m_stime = (ULong_t)data.stime;
100  m_vsize = (ULong_t)data.vsize;
101  m_rss = (Long_t)data.rss;
102  m_time = (Long_t)data.time;
103  m_perfTree->Fill();
104 }
105 
106 void RootPerfMonSvc::handle(const Incident& incident) {
107  std::string t = incident.type();
108  if ( !t.compare(IncidentType::BeginEvent) ) {
109  m_eventNumber++;
110  record(EVENT);
111  return;
112  }
113  if ( !t.compare("CONNECTED_OUTPUT") ) {
114  m_outputs.insert(incident.source());
115  }
116 }
117 
118 // Stop the performance monitoring service
119 StatusCode RootPerfMonSvc::stop() {
120  char text[64];
121  record(FSR);
122  auto map = new TMap();
123  for(const auto &i : m_outputs) {
124  const char* fn = i.c_str();
125  Long_t id, siz, flags, tim;
126  if ( 0 == gSystem->GetPathInfo(fn,&id,&siz,&flags,&tim) ) {
127  ::sprintf(text,"%ld",siz);
128  map->Add(new TObjString(fn), new TObjString(text));
129  }
130  }
131  TDirectory::TContext ctxt(m_perfFile.get());
132  map->Write("Outputs", TObject::kSingleKey);
133  return S_OK;
134 }
135 
136 // Finalize the performance monitoring service
137 StatusCode RootPerfMonSvc::finalize() {
138  record(FSR);
139  log() << MSG::INFO;
140  m_log.reset();
141  m_incidentSvc.reset();
142 
143  m_perfFile->Write();
144  m_perfFile->Close();
145  m_perfFile.reset();
146 
147  return Service::finalize();
148 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode initialize() override
Definition: Service.cpp:64
long unsigned stime
Definition: SysProcStat.h:17
const std::string & type() const
Access to the incident type.
Definition: Incident.h:41
StatusCode finalize() override
Definition: Service.cpp:174
const std::string & source() const
Access to the source of the incident.
Definition: Incident.h:47
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
T log(T...args)
#define S_OK
#define S_FAIL
STL namespace.
STL class.
STL class.
long unsigned vsize
Definition: SysProcStat.h:17
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
constexpr double m
Definition: SystemOfUnits.h:93
const string & CSTR
long unsigned utime
Definition: SysProcStat.h:17
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:244
T compare(T...args)