The Gaudi Framework  master (37c0b60a)
ProcStats Class Reference

#include </builds/gaudi/Gaudi/GaudiAud/src/ProcStats.h>

Collaboration diagram for ProcStats:

Classes

class  unique_fd
 

Public Member Functions

 ProcStats ()
 
bool fetch (procInfo &fill_me)
 
auto pageSize () const noexcept
 

Static Public Member Functions

static ProcStatsinstance ()
 

Private Member Functions

void open_ufd ()
 

Private Attributes

unique_fd m_ufd
 
double m_pg_size { 0 }
 
procInfo m_curr
 
bool m_valid { false }
 
std::mutex m_mutex
 

Detailed Description

Definition at line 38 of file ProcStats.h.

Constructor & Destructor Documentation

◆ ProcStats()

ProcStats::ProcStats ( )
inline

Definition at line 41 of file ProcStats.h.

41 { open_ufd(); }

Member Function Documentation

◆ fetch()

bool ProcStats::fetch ( procInfo fill_me)

Definition at line 259 of file ProcStats.cpp.

259  {
260  if ( !m_valid ) { return false; }
261 
262  std::scoped_lock lock{ m_mutex };
263 
264 #if defined( __linux__ ) or defined( __APPLE__ )
265 
266  auto read_proc = [&]() {
267  bool ok = true;
268  int cnt{ 0 };
269  char buf[500];
270  linux_proc pinfo;
271  m_ufd.lseek( 0, SEEK_SET );
272  if ( ( cnt = m_ufd.read( buf, sizeof( buf ) ) ) < 0 ) { ok = false; }
273  if ( cnt > 0 ) {
274  buf[std::min( static_cast<std::size_t>( cnt ), sizeof( buf ) - 1 )] = '\0';
275  sscanf(
276  buf,
277  // 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 20 1 2 3 4 5 6 7 8 9
278  // 30 1 2 3 4 5
279  "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu "
280  "%lu %lu %lu %lu %lu %lu %lu",
281  &pinfo.pid, pinfo.comm, &pinfo.state, &pinfo.ppid, &pinfo.pgrp, &pinfo.session, &pinfo.tty, &pinfo.tpgid,
282  &pinfo.flags, &pinfo.minflt, &pinfo.cminflt, &pinfo.majflt, &pinfo.cmajflt, &pinfo.utime, &pinfo.stime,
283  &pinfo.cutime, &pinfo.cstime, &pinfo.priority, &pinfo.nice, &pinfo.num_threads, &pinfo.itrealvalue,
284  &pinfo.starttime, &pinfo.vsize, &pinfo.rss, &pinfo.rlim, &pinfo.startcode, &pinfo.endcode, &pinfo.startstack,
285  &pinfo.kstkesp, &pinfo.kstkeip, &pinfo.signal, &pinfo.blocked, &pinfo.sigignore, &pinfo.sigcatch,
286  &pinfo.wchan );
287  // resident set size in pages
288  const auto pr_size = static_cast<double>( pinfo.vsize );
289  const auto pr_rssize = static_cast<double>( pinfo.rss );
290  constexpr double MB = 1.0 / ( 1024 * 1024 );
291  f.vsize = pr_size * MB;
292  f.rss = pr_rssize * m_pg_size * MB;
293  if ( 0 == pinfo.vsize ) { ok = false; }
294  }
295  return ok;
296  };
297 
298  // attempt to read from proc
299  if ( !read_proc() ) {
300  std::cerr << "ProcStats : -> Problems reading proc file. Will try reopening..." << std::endl;
301  open_ufd();
302  if ( !read_proc() ) { return false; }
303  }
304 
305 #else
306  f.vsize = 0;
307  f.rss = 0;
308  return false;
309 #endif // __linux__ or __APPLE__
310 
311  m_curr.rss = f.rss;
312  m_curr.vsize = f.vsize;
313 
314  return true;
315 }

◆ instance()

ProcStats * ProcStats::instance ( )
static

Definition at line 239 of file ProcStats.cpp.

239  {
240  static ProcStats inst{};
241  return &inst;
242 }

◆ open_ufd()

void ProcStats::open_ufd ( )
private

Definition at line 244 of file ProcStats.cpp.

244  {
245  m_valid = false;
246 #if defined( __linux__ ) or defined( __APPLE__ )
247  m_ufd.close();
248  m_pg_size = sysconf( _SC_PAGESIZE ); // getpagesize();
249  const auto fname = "/proc/" + std::to_string( getpid() ) + "/stat";
250  m_ufd.open( fname.c_str(), O_RDONLY );
251  if ( !m_ufd ) {
252  std::cerr << "ProcStats : Failed to open " << fname << std::endl;
253  } else {
254  m_valid = true;
255  }
256 #endif // __linux__ or __APPLE__
257 }

◆ pageSize()

auto ProcStats::pageSize ( ) const
inlinenoexcept

Definition at line 49 of file ProcStats.h.

49 { return m_pg_size; }

Member Data Documentation

◆ m_curr

procInfo ProcStats::m_curr
private

Definition at line 106 of file ProcStats.h.

◆ m_mutex

std::mutex ProcStats::m_mutex
private

Definition at line 108 of file ProcStats.h.

◆ m_pg_size

double ProcStats::m_pg_size { 0 }
private

Definition at line 105 of file ProcStats.h.

◆ m_ufd

unique_fd ProcStats::m_ufd
private

Definition at line 104 of file ProcStats.h.

◆ m_valid

bool ProcStats::m_valid { false }
private

Definition at line 107 of file ProcStats.h.


The documentation for this class was generated from the following files:
linux_proc::blocked
unsigned long blocked
Definition: ProcessDescriptor.cpp:299
linux_proc::vsize
unsigned long vsize
Definition: ProcessDescriptor.cpp:290
std::lock
T lock(T... args)
linux_proc::rss
long rss
Definition: ProcessDescriptor.cpp:291
linux_proc::endcode
unsigned long endcode
Definition: ProcessDescriptor.cpp:294
linux_proc::comm
char comm[400]
Definition: ProcessDescriptor.cpp:269
linux_proc::starttime
unsigned long long starttime
Definition: ProcessDescriptor.cpp:289
linux_proc::tty
int tty
Definition: ProcessDescriptor.cpp:274
linux_proc::startcode
unsigned long startcode
Definition: ProcessDescriptor.cpp:293
linux_proc::itrealvalue
long itrealvalue
Definition: ProcessDescriptor.cpp:288
linux_proc::wchan
unsigned long wchan
Definition: ProcessDescriptor.cpp:302
std::sscanf
T sscanf(T... args)
linux_proc::ppid
int ppid
Definition: ProcessDescriptor.cpp:271
ProcStats
Definition: ProcStats.h:38
linux_proc::tpgid
int tpgid
Definition: ProcessDescriptor.cpp:275
linux_proc::priority
long priority
Definition: ProcessDescriptor.cpp:285
procInfo::vsize
double vsize
Definition: ProcStats.h:34
linux_proc::majflt
unsigned long majflt
Definition: ProcessDescriptor.cpp:279
linux_proc::flags
unsigned long flags
Definition: ProcessDescriptor.cpp:276
linux_proc::stime
unsigned long stime
Definition: ProcessDescriptor.cpp:282
linux_proc::num_threads
long num_threads
Definition: ProcessDescriptor.cpp:287
GaudiMP.GMPBase.MB
MB
Definition: GMPBase.py:61
linux_proc::rlim
unsigned long rlim
Definition: ProcessDescriptor.cpp:292
std::cerr
ProcStats::m_pg_size
double m_pg_size
Definition: ProcStats.h:105
linux_proc::startstack
unsigned long startstack
Definition: ProcessDescriptor.cpp:295
linux_proc
Definition: ProcessDescriptor.cpp:267
ProcStats::unique_fd::open
unique_fd & open(Args &&... args)
Definition: ProcStats.h:72
std::to_string
T to_string(T... args)
ProcStats::unique_fd::close
int close()
Definition: ProcStats.h:76
ProcStats::m_mutex
std::mutex m_mutex
Definition: ProcStats.h:108
linux_proc::cminflt
unsigned long cminflt
Definition: ProcessDescriptor.cpp:278
ProcStats::m_curr
procInfo m_curr
Definition: ProcStats.h:106
linux_proc::signal
unsigned long signal
Definition: ProcessDescriptor.cpp:298
linux_proc::minflt
unsigned long minflt
Definition: ProcessDescriptor.cpp:277
linux_proc::pgrp
int pgrp
Definition: ProcessDescriptor.cpp:272
linux_proc::kstkesp
unsigned long kstkesp
Definition: ProcessDescriptor.cpp:296
linux_proc::sigcatch
unsigned long sigcatch
Definition: ProcessDescriptor.cpp:301
linux_proc::pid
int pid
Definition: ProcessDescriptor.cpp:268
std::min
T min(T... args)
ProcStats::m_ufd
unique_fd m_ufd
Definition: ProcStats.h:104
std::endl
T endl(T... args)
linux_proc::state
char state
Definition: ProcessDescriptor.cpp:270
ProcStats::open_ufd
void open_ufd()
Definition: ProcStats.cpp:244
linux_proc::sigignore
unsigned long sigignore
Definition: ProcessDescriptor.cpp:300
linux_proc::session
int session
Definition: ProcessDescriptor.cpp:273
procInfo::rss
double rss
Definition: ProcStats.h:35
std::size_t
ProcStats::m_valid
bool m_valid
Definition: ProcStats.h:107
linux_proc::cmajflt
unsigned long cmajflt
Definition: ProcessDescriptor.cpp:280
linux_proc::kstkeip
unsigned long kstkeip
Definition: ProcessDescriptor.cpp:297
linux_proc::utime
unsigned long utime
Definition: ProcessDescriptor.cpp:281
linux_proc::nice
long nice
Definition: ProcessDescriptor.cpp:286
linux_proc::cstime
long cstime
Definition: ProcessDescriptor.cpp:284
linux_proc::cutime
long cutime
Definition: ProcessDescriptor.cpp:283