Analysis Software
Documentation for sPHENIX simulation 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 
9 
11  : Fun4AllBase("Fun4AllMemoryTracker")
12 {
13 }
14 
16 {
17  ProcInfo_t procinfo;
18  gSystem->GetProcInfo(&procinfo);
19  return procinfo.fMemResident;
20 }
21 
23 {
24  mMemoryTrackerMap.clear();
25  mStartMem.clear();
26 }
27 
29 {
30  std::string name = CreateFullTrackerName(trackername, group);
31  auto iter = mMemoryTrackerMap.find(name);
32  if (iter != mMemoryTrackerMap.end())
33  {
34  iter->second.push_back(GetRSSMemory());
35  }
36  else
37  {
38  std::vector<int> mvec;
39  mvec.push_back(GetRSSMemory());
40  mMemoryTrackerMap.insert(make_pair(name, mvec));
41  }
42  if (Verbosity() > 0)
43  {
44  std::cout << "Snapshot name: " << name << ", mem: " << GetRSSMemory() << std::endl;
45  }
46  return;
47 }
48 
49 void Fun4AllMemoryTracker::Start(const std::string &trackername, const std::string &group)
50 {
51  std::string name = CreateFullTrackerName(trackername, group);
52  auto iter = mStartMem.find(name);
53  int RSSMemory = GetRSSMemory();
54  if (iter != mStartMem.end())
55  {
56  iter->second = RSSMemory;
57  }
58  else
59  {
60  mStartMem.insert(make_pair(name, RSSMemory));
61  }
62  if (Verbosity() > 0)
63  {
64  std::cout << "Start name: " << name << ", mem: " << RSSMemory << std::endl;
65  }
66 }
67 
68 void Fun4AllMemoryTracker::Stop(const std::string &trackername, const std::string &group)
69 {
70  std::string name = CreateFullTrackerName(trackername, group);
71  auto iter = mStartMem.find(name);
72  int RSSMemory = GetRSSMemory();
73  if (iter != mStartMem.end())
74  {
75  int diff = RSSMemory - iter->second;
76  auto iterM = mMemoryTrackerMap.find(name);
77  if (iterM != mMemoryTrackerMap.end())
78  {
79  iterM->second.push_back(diff);
80  }
81  else
82  {
83  std::vector<int> mvec;
84  mvec.push_back(diff);
85  mMemoryTrackerMap.insert(make_pair(name, mvec));
86  }
87  if (Verbosity() > 0)
88  {
89  std::cout << "Stop name: " << name << ", mem: " << RSSMemory << ", diff: " << diff << std::endl;
90  }
91  }
92  return;
93 }
94 
96 {
97  std::string name = trackername;
98  if (!group.empty())
99  {
100  name = group + "_" + name;
101  }
102  return name;
103 }
104 
106 {
107  std::map<std::string, std::vector<int>>::const_iterator iter;
108  if (name.empty())
109  {
110  for (iter = mMemoryTrackerMap.begin(); iter != mMemoryTrackerMap.end(); ++iter)
111  {
112  std::cout << iter->first << ": ";
113  std::vector<int> memvec = iter->second;
114  for (int &vit : memvec)
115  {
116  std::cout << vit << " ";
117  }
118  std::cout << std::endl;
119  }
120  }
121  else
122  {
123  iter = mMemoryTrackerMap.find(name);
124  if (iter != mMemoryTrackerMap.end())
125  {
126  std::cout << "SubsysReco/OutputManager: " << iter->first << std::endl;
127  std::vector<int> memvec = iter->second;
128  for (int &vit : memvec)
129  {
130  std::cout << vit << " ";
131  }
132  std::cout << std::endl;
133  }
134  else
135  {
136  std::cout << "No Memory Tracker with name " << name << " found" << std::endl;
137  std::cout << "Existing Memory Trackers:" << std::endl;
138  for (iter = mMemoryTrackerMap.begin(); iter != mMemoryTrackerMap.end(); ++iter)
139  {
140  std::cout << iter->first << std::endl;
141  }
142  }
143  }
144  return;
145 }
146 
148 {
149  std::vector<int> memvec;
150  auto iter = mMemoryTrackerMap.find(name);
151  if (iter != mMemoryTrackerMap.end())
152  {
153  memvec = iter->second;
154  }
155  return memvec;
156 }