The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
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(); }
void 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}
procInfo m_curr
Definition ProcStats.h:106
double m_pg_size
Definition ProcStats.h:105
unique_fd m_ufd
Definition ProcStats.h:104
std::mutex m_mutex
Definition ProcStats.h:108
bool m_valid
Definition ProcStats.h:107
unsigned long utime
unsigned long flags
unsigned long rlim
unsigned long vsize
unsigned long endcode
unsigned long startcode
unsigned long blocked
unsigned long signal
unsigned long cmajflt
unsigned long startstack
unsigned long majflt
unsigned long minflt
unsigned long stime
unsigned long long starttime
unsigned long sigcatch
unsigned long sigignore
unsigned long kstkeip
unsigned long kstkesp
unsigned long wchan
unsigned long cminflt

◆ 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.

105{ 0 };

◆ 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.

107{ false };

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