Public Member Functions | Private Member Functions | Private Attributes | List of all members
art::LinuxProcMgr Class Reference

#include <LinuxProcMgr.h>

Public Member Functions

 LinuxProcMgr () noexcept(false)
 
 ~LinuxProcMgr () noexcept
 
LinuxProcData::proc_tuple getCurrentData () const noexcept(false)
 
double getVmPeak () const noexcept(false)
 
double getVmHWM () const noexcept(false)
 
 LinuxProcMgr (LinuxProcMgr const &)=delete
 
 LinuxProcMgr (LinuxProcMgr &&)=delete
 
LinuxProcMgroperator= (LinuxProcMgr const &)=delete
 
LinuxProcMgroperator= (LinuxProcMgr &&)=delete
 

Private Member Functions

double getStatusData_ (std::string const &field) const noexcept(false)
 

Private Attributes

pid_t const pid_
 
long const pgSize_
 
FILE *const file_
 

Detailed Description

Definition at line 22 of file LinuxProcMgr.h.

Constructor & Destructor Documentation

art::LinuxProcMgr::LinuxProcMgr ( )
noexcept

Definition at line 59 of file LinuxProcMgr.cc.

60  : pid_{getpid()}
61  , pgSize_{sysconf(_SC_PAGESIZE)}
62  , file_{new_proc_file_descriptor(pid_)}
63  {}
pid_t const pid_
Definition: LinuxProcMgr.h:47
long const pgSize_
Definition: LinuxProcMgr.h:48
FILE *const file_
Definition: LinuxProcMgr.h:49
art::LinuxProcMgr::~LinuxProcMgr ( )
noexcept

Definition at line 65 of file LinuxProcMgr.cc.

65 { fclose(file_); }
FILE *const file_
Definition: LinuxProcMgr.h:49
art::LinuxProcMgr::LinuxProcMgr ( LinuxProcMgr const &  )
delete
art::LinuxProcMgr::LinuxProcMgr ( LinuxProcMgr &&  )
delete

Member Function Documentation

LinuxProcData::proc_tuple art::LinuxProcMgr::getCurrentData ( ) const
noexcept

Definition at line 69 of file LinuxProcMgr.cc.

70  {
71  HEP_CONCURRENCY_ASSERT_ONLY_ONE_THREAD();
72 
73  int const seek_result{fseek(file_, 0, SEEK_SET)};
74  if (seek_result != 0) {
76  "Error while retrieving Linux proc data."}
77  << "\nCould not reset position indicator while retrieving proc "
78  "stat information.\n";
79  }
80 
81  char buf[400];
82  size_t const cnt{fread(buf, 1, sizeof(buf), file_)};
83 
84  if (cnt == 0) {
86  "Error while retrieving Linux proc data."}
87  << "\nCould not read proc stat information.\n";
88  }
89 
90  buf[cnt] = '\0';
91 
94 
95  std::istringstream iss{buf};
96  iss >> token_ignore(22) >> vsize >> rss;
97 
98  return LinuxProcData::make_proc_tuple(vsize, rss * pgSize_);
99  }
long const pgSize_
Definition: LinuxProcMgr.h:48
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
FILE *const file_
Definition: LinuxProcMgr.h:49
static auto make_proc_tuple(vsize_t::value_type const vsize={}, rss_t::value_type const rss={})
Definition: LinuxProcData.h:93
double art::LinuxProcMgr::getStatusData_ ( std::string const &  field) const
privatenoexcept

Definition at line 103 of file LinuxProcMgr.cc.

104  {
105  HEP_CONCURRENCY_ASSERT_ONLY_ONE_THREAD();
106 
107  std::ostringstream ost;
108  ost << "/proc/" << pid_ << "/status";
109 
110  auto file = fopen(ost.str().c_str(), "r");
111  if (file == nullptr) {
112  throw Exception{errors::Configuration} << " Failed to open: " << ost.str()
113  << std::endl;
114  }
115 
116  double value{};
117  std::regex const pattern{"^" + field + R"(:\s*(\d+)\s*kB)"};
118  while (!feof(file)) {
119  char buffer[128];
120  if (fgets(buffer, sizeof(buffer), file) != nullptr) {
121  std::cmatch cm;
122  if (std::regex_search(buffer, cm, pattern)) {
123  // Reported value from proc (although labeled 'kB') is
124  // actually in KiB. Will convert to base-10 MB.
125  value = std::stod(cm.str(1)) * LinuxProcData::KiB / LinuxProcData::MB;
126  break;
127  }
128  }
129  }
130  fclose(file);
131  return value;
132  }
static constexpr double cm
Definition: Units.h:68
static constexpr double KiB
pid_t const pid_
Definition: LinuxProcMgr.h:47
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string pattern
Definition: regex_t.cc:35
static constexpr double MB
QTextStream & endl(QTextStream &s)
double art::LinuxProcMgr::getVmHWM ( ) const
inlinenoexcept

Definition at line 34 of file LinuxProcMgr.h.

35  {
36  return getStatusData_("VmHWM");
37  }
double getStatusData_(std::string const &field) const noexcept(false)
double art::LinuxProcMgr::getVmPeak ( ) const
inlinenoexcept

Definition at line 29 of file LinuxProcMgr.h.

30  {
31  return getStatusData_("VmPeak");
32  }
double getStatusData_(std::string const &field) const noexcept(false)
LinuxProcMgr& art::LinuxProcMgr::operator= ( LinuxProcMgr const &  )
delete
LinuxProcMgr& art::LinuxProcMgr::operator= ( LinuxProcMgr &&  )
delete

Member Data Documentation

FILE* const art::LinuxProcMgr::file_
private

Definition at line 49 of file LinuxProcMgr.h.

long const art::LinuxProcMgr::pgSize_
private

Definition at line 48 of file LinuxProcMgr.h.

pid_t const art::LinuxProcMgr::pid_
private

Definition at line 47 of file LinuxProcMgr.h.


The documentation for this class was generated from the following files: