The Gaudi Framework  master (37c0b60a)
MemStatAuditor.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 
22 #include "CommonAuditor.h"
23 #include "ProcStats.h"
25 #include <GaudiKernel/MsgStream.h>
26 #include <GaudiKernel/Stat.h>
27 
28 class MemStatAuditor : public CommonAuditor {
29 public:
30  using CommonAuditor::CommonAuditor;
31 
32  StatusCode initialize() override;
33 
34  void i_before( CustomEventTypeRef /*evt*/, std::string_view /*caller*/ ) override {
35  // It's not interesting to monitor the memory usage before the methods.
36  }
37 
38  void i_after( CustomEventTypeRef evt, std::string_view caller, const StatusCode& ) override {
39  i_printinfo( "Memory usage has changed after", evt, caller );
40  }
41 
42 private:
43  void i_printinfo( std::string_view msg, CustomEventTypeRef evt, std::string_view caller );
45 
47  double m_vSize = -1;
48 };
49 
51 
52 StatusCode MemStatAuditor::initialize() {
53  return CommonAuditor::initialize().andThen( [&]() -> StatusCode {
54  m_stat = serviceLocator()->service( "ChronoStatSvc" );
55  if ( !m_stat ) {
56  error() << "Cannot get ChronoStatSvc" << endmsg;
57  return StatusCode::FAILURE;
58  }
59  return StatusCode::SUCCESS;
60  } );
61 }
62 
63 void MemStatAuditor::i_printinfo( std::string_view msg, CustomEventTypeRef evt, std::string_view caller ) {
64  // cannot be exactly 0
65  double deltaVSize = 0.00001;
66 
67  if ( procInfo pInfo; ProcStats::instance()->fetch( pInfo ) ) {
68 
69  if ( pInfo.vsize > 0 ) {
70  if ( m_vSize > 0 ) { deltaVSize = pInfo.vsize - m_vSize; }
71  // store the current VSize to be able to monitor the increment
72  m_vSize = pInfo.vsize;
73  }
74 
75  info() << msg << " " << caller << " " << evt << " \tvirtual size = " << pInfo.vsize << " MB"
76  << " \tresident set size = " << pInfo.rss << " MB"
77  << " deltaVsize = " << deltaVSize << " MB" << endmsg;
78  }
79  // fill the stat for every call, not just when there is a change
80  // only monitor the increment in VSize
81  // Stat stv(m_stat, caller + ":VMemUsage", pInfo.vsize);
82  // Stat str(m_stat, caller + ":RMemUsage", pInfo.rss);
83  Stat sts( m_stat, std::string{ caller }.append( ":VMem" ), deltaVSize );
84 }
IOTest.evt
evt
Definition: IOTest.py:107
CommonAuditor
Base class with common functionalities shared by few auditor implementations.
Definition: CommonAuditor.h:18
MemStatAuditor::i_printinfo
void i_printinfo(std::string_view msg, CustomEventTypeRef evt, std::string_view caller)
Definition: MemStatAuditor.cpp:63
std::string
STL class.
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
procInfo
Definition: ProcStats.h:29
ProcStats::instance
static ProcStats * instance()
Definition: ProcStats.cpp:239
Auditor::initialize
virtual StatusCode initialize()
Definition: Auditor.cpp:79
MemStatAuditor
Definition: MemStatAuditor.cpp:28
MemStatAuditor::initialize
StatusCode initialize() override
Definition: MemStatAuditor.cpp:52
StatusCode
Definition: StatusCode.h:65
ProcStats.h
SmartIF< IChronoStatSvc >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
MemStatAuditor::m_stat
SmartIF< IChronoStatSvc > m_stat
Definition: MemStatAuditor.cpp:44
std::string::append
T append(T... args)
Stat.h
MemStatAuditor::i_after
void i_after(CustomEventTypeRef evt, std::string_view caller, const StatusCode &) override
catch all "after" method, implemented in the derived class
Definition: MemStatAuditor.cpp:38
Stat
Definition: Stat.h:56
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
IChronoStatSvc.h
MemStatAuditor::m_vSize
double m_vSize
vsize of the previous call to printinfo
Definition: MemStatAuditor.cpp:47
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
ProcStats::fetch
bool fetch(procInfo &fill_me)
Definition: ProcStats.cpp:259
MemStatAuditor::i_before
void i_before(CustomEventTypeRef, std::string_view) override
catch all "before" method, implemented in the derived class
Definition: MemStatAuditor.cpp:34
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
CommonAuditor.h
MsgStream.h