ATLAS Offline Software
Loading...
Searching...
No Matches
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
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),
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
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) {
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
93void 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
108void ISF::MemoryMonitoringTool::dumpSummary(const char *desc) const {
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 << " : "
128 ATH_MSG_INFO( "* Average memory increase per " << desc << " : "
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}
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
virtual double getCurrent() const
return current memory usage in kBytes
virtual void recordCurrent(const char *infoStr)
store the current memory monitoring information internally
virtual void dumpSummary(const char *dumpCallDescr) const
dump all internally stored memory monitoring information
MemoryMonitoringTool(const std::string &t, const std::string &n, const IInterface *p)
Constructor with parameters.
StatusCode initialize()
Athena algtool Hooks.
StatusCode finalize()
Athena algtool Hook.
unsigned int m_numCalls
needed to compute per event stats
virtual void dumpCurrent(const char *infoStr, bool considerInSummary)
dump current memory monitoring information
InfoUsagePairVector m_table
storing all (infoStr,memUsage) pairs created in recordCurrent(..) calls
int computeCurMemoryUsage() const
consolidate /proc to determine current memory usage
std::pair< const char *, int > InfoUsagePair
these datatypes used to store (infoStr,memUsage) paired info