Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:16

0001 #include "Fun4AllMonitoring.h"
0002 
0003 #pragma GCC diagnostic push
0004 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
0005 #include <boost/algorithm/string.hpp>
0006 #pragma GCC diagnostic pop
0007 
0008 #include <boost/tokenizer.hpp>
0009 
0010 #include <unistd.h>
0011 #include <cstdint>
0012 #include <fstream>
0013 #include <iostream>
0014 #include <regex>
0015 #include <sstream>
0016 #include <vector>
0017 
0018 Fun4AllMonitoring *Fun4AllMonitoring::mInstance = nullptr;
0019 
0020 Fun4AllMonitoring::Fun4AllMonitoring()
0021   : Fun4AllBase("Fun4AllMonitoring")
0022 {
0023 }
0024 
0025 void Fun4AllMonitoring::Snapshot(const std::string & /*what*/)
0026 {
0027   if (mOutFileName.empty())
0028   {
0029     return;
0030   }
0031   Get_Memory();
0032   if (mEvent == 0)  // called for the first time, write header
0033   {
0034     std::ofstream outfile(mOutFileName, std::ios_base::trunc);
0035     outfile << "Event     HeapPss     mmap   OtherPss" << std::endl;
0036     outfile.close();
0037   }
0038   std::ofstream outfile(mOutFileName, std::ios_base::app);
0039   outfile << mEvent << " " << mHeapPss << " " << mMMapPSS << " " << mOtherPss << std::endl;
0040   outfile.close();
0041   mHeapPss = 0;
0042   mOtherPss = 0;
0043   mMMapPSS = 0;
0044   mEvent++;
0045   return;
0046 }
0047 
0048 void Fun4AllMonitoring::Get_Memory()
0049 {
0050   std::ifstream smap_stat("/proc/self/smaps");
0051   std::string instring;
0052   std::string libraryname;
0053   int i = 0;
0054   while (smap_stat)
0055   {
0056     getline(smap_stat, instring);
0057     if (instring.empty())
0058     {
0059       continue;
0060     }
0061     boost::trim(instring);  // remove leading + trailing spaces
0062     std::regex reg(R"(\s+)");
0063     instring = std::regex_replace(instring, reg, " ");  // replace multiple spaces with one space
0064     std::string firststring = instring.substr(0, instring.find(' '));
0065     if (firststring.find('-') != std::string::npos || (firststring.find("0000000") != std::string::npos && i == 0))
0066     {
0067       std::vector<std::string> tokens;
0068       boost::split(tokens, instring, boost::is_any_of(" "));
0069 
0070       if (tokens.size() == 6)
0071       {
0072         libraryname = tokens.back();
0073       }
0074       else
0075       {
0076         libraryname = "mmap";
0077       }
0078       i++;
0079     }
0080     else
0081     {
0082       boost::char_separator<char> sep(" ");
0083       using tokenizer = boost::tokenizer<boost::char_separator<char>>;
0084       tokenizer tok(instring, sep);
0085       tokenizer::iterator tok_iter = tok.begin();
0086       if ((*tok_iter).find("Pss") != std::string::npos)
0087       {
0088         ++tok_iter;
0089         std::string number = *tok_iter;
0090         boost::trim(number);
0091         if (libraryname.find("[heap]") != std::string::npos)
0092         {
0093           mHeapPss += std::stol(number);
0094         }
0095         else if (libraryname == "mmap")
0096         {
0097           mMMapPSS += std::stol(number);
0098         }
0099         else
0100         {
0101           mOtherPss += std::stol(number);
0102         }
0103       }
0104     }
0105   }
0106 }
0107 
0108 void Fun4AllMonitoring::PrintsMaps()
0109 {
0110   static int icnt = 0;
0111   std::stringstream smaps;
0112   smaps << "/proc/" << getpid() << "/smaps" << std::ends;
0113   std::ifstream smap_stat(smaps.str());
0114   std::string fname = "smaps.list." + std::to_string(icnt);
0115   std::ofstream outfile(fname);
0116   std::string fname1 = "smapsfilt.list." + std::to_string(icnt);
0117   std::ofstream outfilefilt(fname1);
0118   std::string key_str;
0119   std::string value_str;
0120   uint64_t vmem = 0;
0121   uint64_t rss = 0;
0122   while (smap_stat)
0123   {
0124     smap_stat >> key_str >> value_str;
0125     outfile << smap_stat.rdbuf();
0126     if (key_str == "Size:")
0127     {
0128       outfilefilt << key_str << " " << value_str << std::endl;
0129       vmem += std::stol(value_str);
0130     }
0131     else if (key_str == "Rss:")
0132     {
0133       rss += std::stol(value_str);
0134       outfilefilt << key_str << " " << value_str << std::endl;
0135     }
0136   }
0137   std::cout << "vmem: " << vmem << ", rss: " << rss << std::endl;
0138   outfile.close();
0139   outfilefilt.close();
0140   //  std::string cmd = "cat /proc/" + std::to_string(getpid()) + "/smaps";
0141   //  gSystem->Exec(cmd.c_str());
0142   icnt++;
0143   return;
0144 }
0145 
0146 void Fun4AllMonitoring::OutFileName(const std::string &fname)
0147 {
0148   mOutFileName = fname;
0149 }