The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MemStatAuditor.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 "ProcStats.h"
23 #include <Gaudi/Auditor.h>
25 #include <GaudiKernel/MsgStream.h>
26 #include <GaudiKernel/Stat.h>
27 
29 public:
30  using Auditor::Auditor;
31 
32  StatusCode initialize() override;
33 
34  void before( std::string const& /*evt*/, std::string const& /*caller*/, EventContext const& ) override {
35  // It's not interesting to monitor the memory usage before the methods.
36  }
37 
38  void after( std::string const& evt, std::string const& caller, EventContext const&, 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, std::string const& evt, std::string_view caller );
45 
47  double m_vSize = -1;
48 };
49 
51 
52 StatusCode MemStatAuditor::initialize() {
53  return Auditor::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, std::string const& 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
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
procInfo
Definition: ProcStats.h:29
ProcStats::instance
static ProcStats * instance()
Definition: ProcStats.cpp:239
MemStatAuditor::before
void before(std::string const &, std::string const &, EventContext const &) override
Definition: MemStatAuditor.cpp:34
MemStatAuditor::after
void after(std::string const &evt, std::string const &caller, EventContext const &, const StatusCode &) override
Definition: MemStatAuditor.cpp:38
MemStatAuditor
Definition: MemStatAuditor.cpp:28
MemStatAuditor::initialize
StatusCode initialize() override
Definition: MemStatAuditor.cpp:52
StatusCode
Definition: StatusCode.h:64
MemStatAuditor::i_printinfo
void i_printinfo(std::string_view msg, std::string const &evt, std::string_view caller)
Definition: MemStatAuditor.cpp:63
ProcStats.h
SmartIF< IChronoStatSvc >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:198
MemStatAuditor::m_stat
SmartIF< IChronoStatSvc > m_stat
Definition: MemStatAuditor.cpp:44
Stat.h
Stat
Definition: Stat.h:48
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
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:45
ProcStats::fetch
bool fetch(procInfo &fill_me)
Definition: ProcStats.cpp:259
EventContext
Definition: EventContext.h:34
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
Gaudi::Auditor
Base class from which all concrete auditor classes should be derived.
Definition: Auditor.h:33
MsgStream.h
Auditor.h