Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllMonitoring.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllMonitoring.cc
1 #include "Fun4AllMonitoring.h"
2 
3 #pragma GCC diagnostic push
4 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
5 #include <boost/algorithm/string.hpp>
6 #pragma GCC diagnostic pop
7 
8 #include <boost/tokenizer.hpp>
9 
10 #include <unistd.h>
11 #include <cstdint>
12 #include <fstream>
13 #include <iostream>
14 #include <regex>
15 #include <sstream>
16 #include <vector>
17 
19 
21  : Fun4AllBase("Fun4AllMonitoring")
22 {
23 }
24 
26 {
27  if (mOutFileName.empty())
28  {
29  return;
30  }
31  Get_Memory();
32  if (mEvent == 0) // called for the first time, write header
33  {
34  std::ofstream outfile(mOutFileName, std::ios_base::trunc);
35  outfile << "Event HeapPss mmap OtherPss" << std::endl;
36  outfile.close();
37  }
38  std::ofstream outfile(mOutFileName, std::ios_base::app);
39  outfile << mEvent << " " << mHeapPss << " " << mMMapPSS << " " << mOtherPss << std::endl;
40  outfile.close();
41  mHeapPss = 0;
42  mOtherPss = 0;
43  mMMapPSS = 0;
44  mEvent++;
45  return;
46 }
47 
49 {
50  std::ifstream smap_stat("/proc/self/smaps");
51  std::string instring;
52  std::string libraryname;
53  int i = 0;
54  while (smap_stat)
55  {
56  getline(smap_stat, instring);
57  if (instring.empty())
58  {
59  continue;
60  }
61  boost::trim(instring); // remove leading + trailing spaces
62  std::regex reg(R"(\s+)");
63  instring = std::regex_replace(instring, reg, " "); // replace multiple spaces with one space
64  std::string firststring = instring.substr(0, instring.find(' '));
65  if (firststring.find('-') != std::string::npos || (firststring.find("0000000") != std::string::npos && i == 0))
66  {
67  std::vector<std::string> tokens;
68  boost::split(tokens, instring, boost::is_any_of(" "));
69 
70  if (tokens.size() == 6)
71  {
72  libraryname = tokens.back();
73  }
74  else
75  {
76  libraryname = "mmap";
77  }
78  i++;
79  }
80  else
81  {
82  boost::char_separator<char> sep(" ");
83  using tokenizer = boost::tokenizer<boost::char_separator<char>>;
84  tokenizer tok(instring, sep);
85  tokenizer::iterator tok_iter = tok.begin();
86  if ((*tok_iter).find("Pss") != std::string::npos)
87  {
88  ++tok_iter;
89  std::string number = *tok_iter;
90  boost::trim(number);
91  if (libraryname.find("[heap]") != std::string::npos)
92  {
93  mHeapPss += std::stol(number);
94  }
95  else if (libraryname == "mmap")
96  {
97  mMMapPSS += std::stol(number);
98  }
99  else
100  {
101  mOtherPss += std::stol(number);
102  }
103  }
104  }
105  }
106 }
107 
109 {
110  static int icnt = 0;
111  std::stringstream smaps;
112  smaps << "/proc/" << getpid() << "/smaps" << std::ends;
113  std::ifstream smap_stat(smaps.str());
114  std::string fname = "smaps.list." + std::to_string(icnt);
115  std::ofstream outfile(fname);
116  std::string fname1 = "smapsfilt.list." + std::to_string(icnt);
117  std::ofstream outfilefilt(fname1);
118  std::string key_str;
119  std::string value_str;
120  uint64_t vmem = 0;
121  uint64_t rss = 0;
122  while (smap_stat)
123  {
124  smap_stat >> key_str >> value_str;
125  outfile << smap_stat.rdbuf();
126  if (key_str == "Size:")
127  {
128  outfilefilt << key_str << " " << value_str << std::endl;
129  vmem += std::stol(value_str);
130  }
131  else if (key_str == "Rss:")
132  {
133  rss += std::stol(value_str);
134  outfilefilt << key_str << " " << value_str << std::endl;
135  }
136  }
137  std::cout << "vmem: " << vmem << ", rss: " << rss << std::endl;
138  outfile.close();
139  outfilefilt.close();
140  // std::string cmd = "cat /proc/" + std::to_string(getpid()) + "/smaps";
141  // gSystem->Exec(cmd.c_str());
142  icnt++;
143  return;
144 }
145 
147 {
149 }