Skip to content
Snippets Groups Projects
Commit f17c66bd authored by Christopher Rob Jones's avatar Christopher Rob Jones
Browse files

ProcStats: default init members of linux_proc struct

parent a018d4bb
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !1410. Comments created here will be created in the context of that merge request.
...@@ -198,41 +198,41 @@ ...@@ -198,41 +198,41 @@
(divide by sysconf(_SC_CLK_TCK). (divide by sysconf(_SC_CLK_TCK).
*/ */
struct linux_proc { struct linux_proc {
int pid; int pid{-1};
char comm[400]; char comm[400];
char state; char state;
int ppid; int ppid{-1};
int pgrp; int pgrp{-1};
int session; int session{-1};
int tty; int tty{-1};
int tpgid; int tpgid{-1};
unsigned long flags; unsigned long flags{0};
unsigned long minflt; unsigned long minflt{0};
unsigned long cminflt; unsigned long cminflt{0};
unsigned long majflt; unsigned long majflt{0};
unsigned long cmajflt; unsigned long cmajflt{0};
unsigned long utime; unsigned long utime{0};
unsigned long stime; unsigned long stime{0};
long cutime; long cutime{0};
long cstime; long cstime{0};
long priority; long priority{0};
long nice; long nice{0};
long num_threads; long num_threads{0};
long itrealvalue; long itrealvalue{0};
unsigned long long starttime; unsigned long long starttime{0};
unsigned long vsize; unsigned long vsize{0};
long rss; long rss{0};
unsigned long rlim; unsigned long rlim{0};
unsigned long startcode; unsigned long startcode{0};
unsigned long endcode; unsigned long endcode{0};
unsigned long startstack; unsigned long startstack{0};
unsigned long kstkesp; unsigned long kstkesp{0};
unsigned long kstkeip; unsigned long kstkeip{0};
unsigned long signal; unsigned long signal{0};
unsigned long blocked; unsigned long blocked{0};
unsigned long sigignore; unsigned long sigignore{0};
unsigned long sigcatch; unsigned long sigcatch{0};
unsigned long wchan; unsigned long wchan{0};
}; };
#endif // __linux__ or __APPLE__ #endif // __linux__ or __APPLE__
...@@ -259,48 +259,47 @@ void ProcStats::open_ufd() { ...@@ -259,48 +259,47 @@ void ProcStats::open_ufd() {
bool ProcStats::fetch( procInfo& f ) { bool ProcStats::fetch( procInfo& f ) {
if ( !m_valid ) { return false; } if ( !m_valid ) { return false; }
std::scoped_lock lock{ m_mutex }; std::scoped_lock lock{m_mutex};
#if defined( __linux__ ) or defined( __APPLE__ ) #if defined( __linux__ ) or defined( __APPLE__ )
double pr_size{ 0 }, pr_rssize{ 0 }; auto read_proc = [&]() {
linux_proc pinfo; bool ok = true;
int cnt{ 0 }; int cnt{0};
char buf[500]; char buf[500];
linux_proc pinfo;
m_ufd.lseek( 0, SEEK_SET ); m_ufd.lseek( 0, SEEK_SET );
if ( ( cnt = m_ufd.read( buf, sizeof( buf ) ) ) < 0 ) { ok = false; }
if ( ( cnt = m_ufd.read( buf, sizeof( buf ) ) ) < 0 ) { if ( cnt > 0 ) {
std::cerr << "ProcStats : LINUX Read of Proc file failed:" << std::endl; buf[std::min( static_cast<std::size_t>( cnt ), sizeof( buf ) - 1 )] = '\0';
return false; sscanf(
} buf,
// 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
if ( cnt > 0 ) { // 30 1 2 3 4 5
buf[std::min( static_cast<std::size_t>( cnt ), sizeof( buf ) - 1 )] = '\0'; "%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 "
"%lu %lu %lu %lu %lu %lu %lu",
sscanf( buf, &pinfo.pid, pinfo.comm, &pinfo.state, &pinfo.ppid, &pinfo.pgrp, &pinfo.session, &pinfo.tty, &pinfo.tpgid,
// 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 &pinfo.flags, &pinfo.minflt, &pinfo.cminflt, &pinfo.majflt, &pinfo.cmajflt, &pinfo.utime, &pinfo.stime,
// 30 1 2 3 4 5 &pinfo.cutime, &pinfo.cstime, &pinfo.priority, &pinfo.nice, &pinfo.num_threads, &pinfo.itrealvalue,
"%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 " &pinfo.starttime, &pinfo.vsize, &pinfo.rss, &pinfo.rlim, &pinfo.startcode, &pinfo.endcode, &pinfo.startstack,
"%lu %lu %lu %lu %lu %lu %lu", &pinfo.kstkesp, &pinfo.kstkeip, &pinfo.signal, &pinfo.blocked, &pinfo.sigignore, &pinfo.sigcatch,
&pinfo.pid, pinfo.comm, &pinfo.state, &pinfo.ppid, &pinfo.pgrp, &pinfo.session, &pinfo.tty, &pinfo.tpgid, &pinfo.wchan );
&pinfo.flags, &pinfo.minflt, &pinfo.cminflt, &pinfo.majflt, &pinfo.cmajflt, &pinfo.utime, &pinfo.stime, // resident set size in pages
&pinfo.cutime, &pinfo.cstime, &pinfo.priority, &pinfo.nice, &pinfo.num_threads, &pinfo.itrealvalue, const auto pr_size = static_cast<double>( pinfo.vsize );
&pinfo.starttime, &pinfo.vsize, &pinfo.rss, &pinfo.rlim, &pinfo.startcode, &pinfo.endcode, const auto pr_rssize = static_cast<double>( pinfo.rss );
&pinfo.startstack, &pinfo.kstkesp, &pinfo.kstkeip, &pinfo.signal, &pinfo.blocked, &pinfo.sigignore, constexpr double MB = 1.0 / ( 1024 * 1024 );
&pinfo.sigcatch, &pinfo.wchan ); f.vsize = pr_size * MB;
f.rss = pr_rssize * m_pg_size * MB;
// resident set size in pages if ( 0 == pinfo.vsize ) { ok = false; }
pr_size = (double)pinfo.vsize;
pr_rssize = (double)pinfo.rss;
f.vsize = pr_size / ( 1024 * 1024 );
f.rss = pr_rssize * m_pg_size / ( 1024 * 1024 );
if ( 0 == pinfo.vsize ) {
std::cerr << "ProcStats : 0==vsize -> Will try reopening process proc stats" << std::endl;
open_ufd();
} }
return ok;
};
// attempt to read from proc
if ( !read_proc() ) {
std::cerr << "ProcStats : -> Problems reading proc file. Will try reopening..." << std::endl;
open_ufd();
if ( !read_proc() ) { return false; }
} }
#else #else
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment