ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllMemoryTracker.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllMemoryTracker.cc
1 #include "Fun4AllMemoryTracker.h"
2 
3 #include <TSystem.h>
4 
5 #include <iostream>
6 #include <utility> // for pair, make_pair
7 
8 using namespace std;
9 
11 
13  : Fun4AllBase("Fun4AllMemoryTracker")
14 {
15 }
16 
18 {
19  ProcInfo_t procinfo;
20  gSystem->GetProcInfo(&procinfo);
21  return procinfo.fMemResident;
22 }
23 
25 {
26  mMemoryTrackerMap.clear();
27  mStartMem.clear();
28 }
29 
30 void Fun4AllMemoryTracker::Snapshot(const string &trackername, const string &group)
31 {
32  string name = CreateFullTrackerName(trackername, group);
33  auto iter = mMemoryTrackerMap.find(name);
34  if (iter != mMemoryTrackerMap.end())
35  {
36  iter->second.push_back(GetRSSMemory());
37  }
38  else
39  {
40  vector<int> mvec;
41  mvec.push_back(GetRSSMemory());
42  mMemoryTrackerMap.insert(make_pair(name, mvec));
43  }
44  if (Verbosity() > 0)
45  {
46  cout << "Snapshot name: " << name << ", mem: " << GetRSSMemory() << endl;
47  }
48  return;
49 }
50 
51 void Fun4AllMemoryTracker::Start(const string &trackername, const string &group)
52 {
53  string name = CreateFullTrackerName(trackername, group);
54  auto iter = mStartMem.find(name);
55  int RSSMemory = GetRSSMemory();
56  if (iter != mStartMem.end())
57  {
58  iter->second = RSSMemory;
59  }
60  else
61  {
62  mStartMem.insert(make_pair(name, RSSMemory));
63  }
64  if (Verbosity() > 0)
65  {
66  cout << "Start name: " << name << ", mem: " << RSSMemory << endl;
67  }
68 }
69 
70 void Fun4AllMemoryTracker::Stop(const string &trackername, const string &group)
71 {
72  string name = CreateFullTrackerName(trackername, group);
73  auto iter = mStartMem.find(name);
74  int RSSMemory = GetRSSMemory();
75  if (iter != mStartMem.end())
76  {
77  int diff = RSSMemory - iter->second;
78  auto iterM = mMemoryTrackerMap.find(name);
79  if (iterM != mMemoryTrackerMap.end())
80  {
81  iterM->second.push_back(diff);
82  }
83  else
84  {
85  vector<int> mvec;
86  mvec.push_back(diff);
87  mMemoryTrackerMap.insert(make_pair(name, mvec));
88  }
89  if (Verbosity() > 0)
90  {
91  cout << "Stop name: " << name << ", mem: " << RSSMemory << ", diff: " << diff << endl;
92  }
93  }
94  return;
95 }
96 
97 string Fun4AllMemoryTracker::CreateFullTrackerName(const string &trackername, const string &group)
98 {
99  string name = trackername;
100  if (!group.empty())
101  {
102  name = group + "_" + name;
103  }
104  return name;
105 }
106 
108 {
109  map<string, std::vector<int>>::const_iterator iter;
110  if (name.empty())
111  {
112  for (iter = mMemoryTrackerMap.begin(); iter != mMemoryTrackerMap.end(); ++iter)
113  {
114  cout << iter->first << ": ";
115  vector<int> memvec = iter->second;
116  for (auto vit = memvec.begin(); vit != memvec.end(); ++vit)
117  {
118  cout << *vit << " ";
119  }
120  cout << endl;
121  }
122  }
123  else
124  {
125  iter = mMemoryTrackerMap.find(name);
126  if (iter != mMemoryTrackerMap.end())
127  {
128  cout << "SubsysReco/OutputManager: " << iter->first << endl;
129  vector<int> memvec = iter->second;
130  for (auto vit = memvec.begin(); vit != memvec.end(); ++vit)
131  {
132  cout << *vit << " ";
133  }
134  cout << endl;
135  }
136  else
137  {
138  cout << "No Memory Tracker with name " << name << " found" << endl;
139  cout << "Existing Memory Trackers:" << endl;
140  for (iter = mMemoryTrackerMap.begin(); iter != mMemoryTrackerMap.end(); ++iter)
141  {
142  cout << iter->first << endl;
143  }
144  }
145  }
146  return;
147 }
148 
149 vector<int> Fun4AllMemoryTracker::GetMemoryVector(const std::string &name) const
150 {
151  vector<int> memvec;
152  auto iter = mMemoryTrackerMap.find(name);
153  if (iter != mMemoryTrackerMap.end())
154  {
155  memvec = iter->second;
156  }
157  return memvec;
158 }