ATLAS Offline Software
MemoryMonitoringTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // MemoryMonitoringTool.cxx, (c) ATLAS Detector software
8 
9 // class include
10 #include "MemoryMonitoringTool.h"
11 
12 // stl includes
13 #include <fstream>
14 
17  const std::string& n,
18  const IInterface* p ) :
19  base_class(t,n,p),
20  m_numCalls(0),
21  m_prevCallMemUsage(0),
22  m_accumulatedCallMemory(0.),
23  m_accumulatedIncrMemory(0.),
24  m_table()
25 {
26 }
27 
30  // clean up memory
31  for (auto & p : m_table){
32  delete p;
33  }
34  // reset summary table vector
35  m_table.clear();
36 
37 }
38 
39 
42 {
43  ATH_MSG_VERBOSE("initialize() ...");
44 
45  ATH_MSG_VERBOSE("initialize() successful");
46  return StatusCode::SUCCESS;
47 }
48 
49 
52 {
53  ATH_MSG_VERBOSE("finalize() ...");
54  ATH_MSG_VERBOSE("finalize() successful");
55  return StatusCode::SUCCESS;
56 }
57 
58 
61  return double( computeCurMemoryUsage());
62 }
63 
64 
66 void ISF::MemoryMonitoringTool::dumpCurrent( const char *infoStr,
67  bool considerInSummary) {
68 
69  // get current memory usage
70  int curMem = computeCurMemoryUsage();
71 
72  // store for different stats
73  int curDiff = curMem-m_prevCallMemUsage;
74 
75  // use these stats for the summary table
76  if (considerInSummary) {
77  m_accumulatedCallMemory += curMem;
78  m_accumulatedIncrMemory += curDiff;
79  ++m_numCalls;
80  }
81 
82  ATH_MSG_INFO( "Current Process Memory Usage " << infoStr
83  << " (VmMem,diff-previous) kBytes: "
84  << curMem<<" "<< curDiff );
85 
86  m_prevCallMemUsage = curMem;
87 
88  return;
89 }
90 
91 
93 void ISF::MemoryMonitoringTool::recordCurrent( const char *infoStr) {
94 
95  int curUsage = computeCurMemoryUsage();
96  ATH_MSG_VERBOSE("recording current memory footprint of "
97  << curUsage << " kBytes");
98 
99  // create new InfoUsagePair and store it locally in vector
100  InfoUsagePair *curInfoUsage = new InfoUsagePair(infoStr, curUsage);
101  m_table.push_back( curInfoUsage);
102 
103  return;
104 }
105 
106 
109 
110  ATH_MSG_INFO("*****************************************************\n"<<
111  "* (VmMem) MEMORY SUMMARY: (kBytes)");
112 
113  // loop over all (infoStr,memUsage) pairs and print them
114  InfoUsagePairVector::const_iterator it = m_table.begin();
115  InfoUsagePairVector::const_iterator itEnd = m_table.end();
116  for ( ; it != itEnd; ++it) {
117  const char *curInfo = (*it)->first;
118  int curUsage = (*it)->second;
119 
120  ATH_MSG_INFO("* Memory Usage " << curInfo << "\t\t: " << curUsage);
121  }
122 
123  if (m_numCalls) {
124  //cppcheck-suppress uselessAssignmentPtrArg
125  if (!desc) desc = "MemoryMonitor call";
126  ATH_MSG_INFO( "* Average memory per " << desc << " : "
127  << (int)(m_accumulatedCallMemory/m_numCalls) );
128  ATH_MSG_INFO( "* Average memory increase per " << desc << " : "
129  << (int)(m_accumulatedIncrMemory/m_numCalls));
130  }
131 
132  ATH_MSG_INFO("*****************************************************");
133 
134  return;
135 }
136 
139  // this code is copied from:
140  // atlasoff: Simulation/G4Sim/SimHelpers/SimHelpers/MemorySnooper.h
141 
142  int pid=getpid();
143  std::ostringstream is;
144  is<<pid;
145  std::string spid=is.str();
146  std::string filename="mem"+spid+".txt";
147  std::string temp="cat /proc/"+spid+"/status | grep VmSize >"+filename;
148  system(temp.c_str());
149  std::ifstream in(filename.c_str());
150  std::string text,mem,text1;
151  in>>text>>mem>>text1;
152  temp="rm -f "+filename;
153  system(temp.c_str());
154  float memsize=atof(mem.c_str());
155 
156  // finally return the memory size
157  return int(memsize);
158 }
ISF::MemoryMonitoringTool::initialize
StatusCode initialize()
Athena algtool Hooks.
Definition: MemoryMonitoringTool.cxx:41
ISF::MemoryMonitoringTool::dumpCurrent
virtual void dumpCurrent(const char *infoStr, bool considerInSummary)
dump current memory monitoring information
Definition: MemoryMonitoringTool.cxx:66
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ISF::MemoryMonitoringTool::computeCurMemoryUsage
int computeCurMemoryUsage() const
consolidate /proc to determine current memory usage
Definition: MemoryMonitoringTool.cxx:138
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ISF::MemoryMonitoringTool::dumpSummary
virtual void dumpSummary(const char *dumpCallDescr) const
dump all internally stored memory monitoring information
Definition: MemoryMonitoringTool.cxx:108
skel.it
it
Definition: skel.GENtoEVGEN.py:396
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
ISF::MemoryMonitoringTool::~MemoryMonitoringTool
~MemoryMonitoringTool()
Destructor.
Definition: MemoryMonitoringTool.cxx:29
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ISF::InfoUsagePair
std::pair< const char *, int > InfoUsagePair
these datatypes used to store (infoStr,memUsage) paired info
Definition: MemoryMonitoringTool.h:26
CaloCondBlobAlgs_fillNoiseFromASCII.desc
desc
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:54
ISF::MemoryMonitoringTool::getCurrent
virtual double getCurrent() const
return current memory usage in kBytes
Definition: MemoryMonitoringTool.cxx:60
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ParticleGun_EoverP_Config.pid
pid
Definition: ParticleGun_EoverP_Config.py:62
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
CxxUtils::atof
double atof(std::string_view str)
Converts a string into a double / float.
Definition: Control/CxxUtils/Root/StringUtils.cxx:91
ISF::MemoryMonitoringTool::MemoryMonitoringTool
MemoryMonitoringTool(const std::string &t, const std::string &n, const IInterface *p)
Constructor with parameters.
Definition: MemoryMonitoringTool.cxx:16
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
makeTransCanvas.text
text
Definition: makeTransCanvas.py:11
ISF::MemoryMonitoringTool::recordCurrent
virtual void recordCurrent(const char *infoStr)
store the current memory monitoring information internally
Definition: MemoryMonitoringTool.cxx:93
ISF::MemoryMonitoringTool::finalize
StatusCode finalize()
Athena algtool Hook.
Definition: MemoryMonitoringTool.cxx:51
MemoryMonitoringTool.h