ATLAS Offline Software
PerfStats.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // ROOT include(s):
6 #include <TTree.h>
7 #include <TFile.h>
8 #include <TTimeStamp.h>
9 
10 // Local include(s):
12 #include "xAODCore/tools/Utils.h"
13 #include "xAODCore/tools/IOStats.h"
15 
17 
18 namespace xAOD {
19 
20  // Initialize the static variable(s):
21  PerfStats* PerfStats::s_instance = nullptr;
23 
29 
30  lock_t lock (s_mutex);
31  // Since this object can only be deleted by deleting the global
32  // gPerfStats object, make sure that all the objects get deleted
33  // if the user asked for it...
34  s_instance = nullptr;
35  if( m_otherPerfStats ) {
36  delete m_otherPerfStats;
37  }
38  }
39 
45  PerfStats& PerfStats::instance() {
46 
47  lock_t lock (s_mutex);
48  // Construct the object if it is now available at the moment:
49  if( ! s_instance ) {
50  s_instance = new PerfStats();
51  }
52 
53  return *s_instance;
54  }
55 
61  void PerfStats::start( bool clear ) {
62 
63  lock_t lock (s_mutex);
64  // Return right away if we are running already:
65  if( m_running ) return;
66 
67  // Clear the statistics collected so far if required:
68  if( clear ) IOStats::instance().stats().Clear();
69 
70  // Let the user know what we're doing:
71  Info( "start", "Starting performance monitoring" );
72 
73  // Record the starting time:
74  m_startTime = TTimeStamp();
75  // Remember that we are running:
76  m_running = true;
77 
78  return;
79  }
80 
84  void PerfStats::stop() {
85 
86  lock_t lock (s_mutex);
87  // Return right away if we are not running:
88  if( ! m_running ) return;
89 
90  // Calculate the time elapsed from when the analysis started:
91  const ::Double_t elapsed = TTimeStamp().AsDouble() -
93  // Save it:
94  ReadStats& stats = IOStats::instance().stats();
95  stats.setProcessTime( stats.processTime() + elapsed );
96 
97  // Remember that we are stopped:
98  m_running = false;
99 
100  // Let the user know what we've done:
101  Info( "stop", "Performance monitoring stopped after %s",
102  Utils::timeToString( elapsed ).c_str() );
103 
104  return;
105  }
106 
107  void PerfStats::SimpleEvent( EEventType type ) {
108 
109  // Forward the call if possible:
110  if( m_otherPerfStats ) {
111  m_otherPerfStats->SimpleEvent( type );
112  }
113 
114  return;
115  }
116 
117  void PerfStats::PacketEvent( const char* slave, const char* slavename,
118  const char* filename,
119  ::Long64_t eventsprocessed,
120  ::Double_t latency,
121  ::Double_t proctime, ::Double_t cputime,
122  ::Long64_t bytesRead ) {
123 
124  // Forward the call if possible:
125  if( m_otherPerfStats ) {
126  m_otherPerfStats->PacketEvent( slave, slavename, filename,
127  eventsprocessed, latency, proctime,
128  cputime, bytesRead );
129  }
130 
131  return;
132  }
133 
134  void PerfStats::FileEvent( const char* slave, const char* slavename,
135  const char* nodename, const char* filename,
136  ::Bool_t isStart ) {
137 
138  // Forward the call if possible:
139  if( m_otherPerfStats ) {
140  m_otherPerfStats->FileEvent( slave, slavename, nodename, filename,
141  isStart );
142  }
143 
144  return;
145  }
146 
147  void PerfStats::FileOpenEvent( ::TFile* file, const char* filename,
148  ::Double_t start ) {
149 
150  // Forward the call if possible:
151  if( m_otherPerfStats ) {
152  m_otherPerfStats->FileOpenEvent( file, filename, start );
153  }
154 
155  return;
156  }
157 
167  void PerfStats::FileReadEvent( ::TFile* file, ::Int_t len,
168  ::Double_t start ) {
169 
170  // Do the calculation without delay:
171  const ::Double_t tnow = TTimeStamp();
172  const ::Double_t dtime = tnow - start;
173 
174  // Accumulate the reading time statistics:
175  ReadStats& stats = IOStats::instance().stats();
176  stats.setReadTime( stats.readTime() + dtime );
177 
178  // Accumulate the amount of read data:
179  stats.setBytesRead( stats.bytesRead() + len );
180  stats.setFileReads( stats.fileReads() + 1 );
181 
182  // Forward the call if possible:
183  if( m_otherPerfStats ) {
184  m_otherPerfStats->FileReadEvent( file, len, start );
185  }
186 
187  return;
188  }
189 
199  void PerfStats::UnzipEvent( ::TObject* tree, ::Long64_t pos,
200  ::Double_t start, ::Int_t complen,
201  ::Int_t objlen ) {
202 
203  // Do the calculation without delay:
204  const ::Double_t tnow = TTimeStamp();
205  const ::Double_t dtime = tnow - start;
206 
207  // Just accumulate the zipping time statistics:
208  ReadStats& stats = IOStats::instance().stats();
209  stats.setUnzipTime( stats.unzipTime() + dtime );
210 
211  // Get the cache size from the tree:
212  ::TTree* t = dynamic_cast< ::TTree* >( tree );
213  if( ! t ) {
214  Warning( "UnzipEvent", "Couldn't cast object to TTree" );
215  } else {
216  stats.setCacheSize( t->GetCacheSize() );
217  }
218 
219  // Forward the call if possible:
220  if( m_otherPerfStats ) {
221  m_otherPerfStats->UnzipEvent( tree, pos, start, complen, objlen );
222  }
223 
224  return;
225  }
226 
227  void PerfStats::RateEvent( ::Double_t proctime, ::Double_t deltatime,
228  ::Long64_t eventsprocessed,
229  ::Long64_t bytesRead ) {
230 
231  // Forward the call if possible:
232  if( m_otherPerfStats ) {
233  m_otherPerfStats->RateEvent( proctime, deltatime, eventsprocessed,
234  bytesRead );
235  }
236 
237  return;
238  }
239 
248  void PerfStats::SetBytesRead( ::Long64_t num ) {
249 
250  // Forward the call if possible:
251  if( m_otherPerfStats ) {
252  m_otherPerfStats->SetBytesRead( num );
253  }
254 
255  return;
256  }
257 
258  ::Long64_t PerfStats::GetBytesRead() const {
259 
260  // Forward the call if possible:
261  if( m_otherPerfStats ) {
262  return m_otherPerfStats->GetBytesRead();
263  } else {
264  return IOStats::instance().stats().bytesRead();
265  }
266  }
267 
274  void PerfStats::SetNumEvents( ::Long64_t num ) {
275 
276  // Forward the call if possible:
277  if( m_otherPerfStats ) {
278  m_otherPerfStats->SetNumEvents( num );
279  }
280 
281  return;
282  }
283 
290  ::Long64_t PerfStats::GetNumEvents() const {
291 
292  // Forward the call if possible:
293  if( m_otherPerfStats ) {
294  return m_otherPerfStats->GetNumEvents();
295  }
296 
297  return 0;
298  }
299 
300  /* Some methods that are pure virtual in the basaclass and need
301  a definition - forwarding them to the actuall ROOT TPerfStats
302  new in ROOT 6.14
303  */
304  void PerfStats::PrintBasketInfo( Option_t *option ) const {
305  if( m_otherPerfStats ) m_otherPerfStats->PrintBasketInfo( option );
306  }
307 
308  void PerfStats::UpdateBranchIndices( TObjArray *branches ) {
309  if( m_otherPerfStats ) m_otherPerfStats->UpdateBranchIndices( branches );
310  }
311 
312  #define FWD_CALL(CALL) \
313  void PerfStats::CALL( TBranch *b, size_t basketNumber ) { \
314  if( m_otherPerfStats ) m_otherPerfStats->CALL( b, basketNumber ); \
315  } \
316  void PerfStats::CALL( size_t bi, size_t basketNumber ) { \
317  if( m_otherPerfStats ) m_otherPerfStats->CALL( bi, basketNumber ); \
318  } struct dummyforsemi
319 
320  FWD_CALL(SetLoaded);
321  FWD_CALL(SetLoadedMiss);
322  FWD_CALL(SetMissed);
323  FWD_CALL(SetUsed);
324  #undef FWD_CALL
325 
333  : m_otherPerfStats( nullptr ), m_running( false ), m_startTime( 0.0 ),
334  m_tree( nullptr ), m_file( nullptr ), m_treeWarningPrinted( false ) {
335 
336  // locked via instance().
337 
338  // Remember a possible former performance monitoring object:
339  if( gPerfStats && ( gPerfStats != this ) ) {
340  m_otherPerfStats = gPerfStats;
341  Info( "PerfStats",
342  "Will forward calls to former gPerfStats object" );
343  }
344 
345  // This object is now the performance monitoring object:
346  gPerfStats = this;
347  }
348 
349 #if ROOT_VERSION_CODE >= ROOT_VERSION( 6, 23, 2 )
350  void PerfStats::SetFile( TFile* file ) {
354 
355  m_file = file;
356  }
357 #endif // ROOT version
358 
359 } // namespace xAOD
xAOD::PerfStats::PerfStats
PerfStats()
The constructor is protected, as it's a singleton.
xAOD::PerfStats::UnzipEvent
virtual void UnzipEvent(::TObject *tree, ::Long64_t pos, ::Double_t start, ::Int_t complen, ::Int_t objlen)
Function called in general when a file unzipping operation happens.
xAOD::ReadStats::Clear
void Clear(::Option_t *opt="")
Clear the statistics information (inherited from TNamed...)
xAOD::PerfStats::lock_t
std::lock_guard< std::mutex > lock_t
Definition: PerfStats.h:126
xAOD::ReadStats::bytesRead
::Long64_t bytesRead() const
Get how many bytes were read in total during the analysis.
xAOD::IOStats::stats
ReadStats & stats()
Access the object belonging to the current thread.
Definition: IOStats.cxx:17
xAOD::PerfStats::start
void start(bool clear=true)
Start the statistics collection.
ClassImp
ClassImp(xAOD::PerfStats) namespace xAOD
Definition: PerfStats.cxx:16
xAOD::PerfStats::UpdateBranchIndices
virtual void UpdateBranchIndices(TObjArray *branches)
Update the fBranchIndexCache collection to match the current TTree given the ordered list of branch n...
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
xAOD::PerfStats::FileOpenEvent
virtual void FileOpenEvent(::TFile *file, const char *filename, ::Double_t start)
Function called by PROOF when a file is opened.
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
tree
TChain * tree
Definition: tile_monitor.h:30
xAOD::PerfStats::FileEvent
virtual void FileEvent(const char *slave, const char *slavename, const char *nodename, const char *filename, ::Bool_t isStart)
PROOF specific function, not implemented here.
xAOD::PerfStats::PrintBasketInfo
virtual void PrintBasketInfo(Option_t *option="") const
Print the TTree basket read caching statistics.
xAOD::PerfStats::GetNumEvents
virtual ::Long64_t GetNumEvents() const
Function used by PROOF to set the number of processed events correctly.
xAOD::PerfStats::FileReadEvent
virtual void FileReadEvent(::TFile *file, ::Int_t len, ::Double_t start)
Function called in general when a file reading operation happens.
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
xAOD::PerfStats
Class for collecting information about the xAOD file access pattern.
Definition: PerfStats.h:34
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
IOStats.h
trigbs_dumpHLTContentInBS.stats
stats
Definition: trigbs_dumpHLTContentInBS.py:91
PerfStats.h
xAOD::PerfStats::~PerfStats
~PerfStats()
Destructor, sometimes called by PROOF.
m_file
std::unique_ptr< TFile > m_file
description: this is a custom writer for the old-school drivers that don't use an actual writer
Definition: OutputStreamData.cxx:52
xAOD::PerfStats::PacketEvent
virtual void PacketEvent(const char *slave, const char *slavename, const char *filename, ::Long64_t eventsprocessed, ::Double_t latency, ::Double_t proctime, ::Double_t cputime, ::Long64_t bytesRead)
PROOF specific function, not implemented here.
runLayerRecalibration.branches
list branches
Definition: runLayerRecalibration.py:98
xAOD::PerfStats::m_file
::TFile * m_file
The currently open xAOD file.
Definition: PerfStats.h:139
ReadStats.h
xAOD::PerfStats::SimpleEvent
virtual void SimpleEvent(EEventType type)
Generic function called when a specified event happens.
xAOD::PerfStats::RateEvent
virtual void RateEvent(::Double_t proctime, ::Double_t deltatime, ::Long64_t eventsprocessed, ::Long64_t bytesRead)
PROOF specific function, not implemented here.
Utils.h
file
TFile * file
Definition: tile_monitor.h:29
xAOD::IOStats::instance
static IOStats & instance()
Singleton object accessor.
Definition: IOStats.cxx:11
xAOD::PerfStats::GetBytesRead
virtual ::Long64_t GetBytesRead() const
Function used by PROOF to set the read bytes correctly on the master.
trigbs_pickEvents.num
num
Definition: trigbs_pickEvents.py:76
xAOD::PerfStats::s_mutex
static std::mutex s_mutex
Lock controlling access to the singleton.
Definition: PerfStats.h:125
xAOD::Utils::timeToString
std::string timeToString(::Double_t secs)
Function creating a human-readable elapsed time printout.
Definition: Event/xAOD/xAODCore/Root/Utils.cxx:99
xAOD::PerfStats::SetBytesRead
virtual void SetBytesRead(::Long64_t num)
Function used by PROOF to set the read bytes correctly on the master.
xAOD::PerfStats::instance
static PerfStats & instance()
Function accessing the singleton instance.
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
VKalVrtAthena::varHolder_detail::clear
void clear(T &var)
Definition: NtupleVars.h:48
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
xAOD::PerfStats::m_running
bool m_running
Flag showing whether the statistic collection is ongoing or not.
Definition: PerfStats.h:132
xAOD::PerfStats::m_otherPerfStats
::TVirtualPerfStats * m_otherPerfStats
Another performance monitoring object.
Definition: PerfStats.h:129
xAOD::PerfStats::stop
void stop()
Stop the statistics collection.
xAOD::PerfStats::SetNumEvents
virtual void SetNumEvents(::Long64_t num)
Function used by PROOF to set the number of processed events correctly.
xAOD::PerfStats::m_startTime
::Double_t m_startTime
Time when the statistics collection was started.
Definition: PerfStats.h:134
xAOD::PerfStats::SetFile
virtual void SetFile(TFile *file)
Function letting us know that a new file was opened.
FWD_CALL
#define FWD_CALL(CALL)