Skip to content
Snippets Groups Projects

ProcStats : Use mutex lock to make fetch() thread safe.

Merged Christopher Rob Jones requested to merge jonrob/Gaudi:ProcStats-thread-safe into master
All threads resolved!
Compare and Show latest version
2 files
+ 48
55
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 47
54
@@ -198,41 +198,41 @@
(divide by sysconf(_SC_CLK_TCK).
*/
struct linux_proc {
int pid{-1};
int pid{ -1 };
char comm[400];
char state;
int ppid{-1};
int pgrp{-1};
int session{-1};
int tty{-1};
int tpgid{-1};
unsigned long flags{0};
unsigned long minflt{0};
unsigned long cminflt{0};
unsigned long majflt{0};
unsigned long cmajflt{0};
unsigned long utime{0};
unsigned long stime{0};
long cutime{0};
long cstime{0};
long priority{0};
long nice{0};
long num_threads{0};
long itrealvalue{0};
unsigned long long starttime{0};
unsigned long vsize{0};
long rss{0};
unsigned long rlim{0};
unsigned long startcode{0};
unsigned long endcode{0};
unsigned long startstack{0};
unsigned long kstkesp{0};
unsigned long kstkeip{0};
unsigned long signal{0};
unsigned long blocked{0};
unsigned long sigignore{0};
unsigned long sigcatch{0};
unsigned long wchan{0};
int ppid{ -1 };
int pgrp{ -1 };
int session{ -1 };
int tty{ -1 };
int tpgid{ -1 };
unsigned long flags{ 0 };
unsigned long minflt{ 0 };
unsigned long cminflt{ 0 };
unsigned long majflt{ 0 };
unsigned long cmajflt{ 0 };
unsigned long utime{ 0 };
unsigned long stime{ 0 };
long cutime{ 0 };
long cstime{ 0 };
long priority{ 0 };
long nice{ 0 };
long num_threads{ 0 };
long itrealvalue{ 0 };
unsigned long long starttime{ 0 };
unsigned long vsize{ 0 };
long rss{ 0 };
unsigned long rlim{ 0 };
unsigned long startcode{ 0 };
unsigned long endcode{ 0 };
unsigned long startstack{ 0 };
unsigned long kstkesp{ 0 };
unsigned long kstkeip{ 0 };
unsigned long signal{ 0 };
unsigned long blocked{ 0 };
unsigned long sigignore{ 0 };
unsigned long sigcatch{ 0 };
unsigned long wchan{ 0 };
};
#endif // __linux__ or __APPLE__
@@ -259,20 +259,17 @@ void ProcStats::open_ufd() {
bool ProcStats::fetch( procInfo& f ) {
if ( !m_valid ) { return false; }
std::scoped_lock lock{m_mutex};
std::scoped_lock lock{ m_mutex };
#if defined( __linux__ ) or defined( __APPLE__ )
linux_proc pinfo;
auto read_proc = [&]() {
int cnt{0};
char buf[500];
bool ok = true;
int cnt{ 0 };
char buf[500];
linux_proc pinfo;
m_ufd.lseek( 0, SEEK_SET );
if ( ( cnt = m_ufd.read( buf, sizeof( buf ) ) ) < 0 ) {
std::cerr << "ProcStats : LINUX Read of Proc file failed:" << std::endl;
return false;
}
if ( ( cnt = m_ufd.read( buf, sizeof( buf ) ) ) < 0 ) { ok = false; }
if ( cnt > 0 ) {
buf[std::min( static_cast<std::size_t>( cnt ), sizeof( buf ) - 1 )] = '\0';
sscanf(
@@ -288,23 +285,19 @@ bool ProcStats::fetch( procInfo& f ) {
&pinfo.kstkesp, &pinfo.kstkeip, &pinfo.signal, &pinfo.blocked, &pinfo.sigignore, &pinfo.sigcatch,
&pinfo.wchan );
// resident set size in pages
const auto pr_size = (double)pinfo.vsize;
const auto pr_rssize = (double)pinfo.rss;
f.vsize = pr_size / ( 1024 * 1024 );
f.rss = pr_rssize * m_pg_size / ( 1024 * 1024 );
const auto pr_size = static_cast<double>( pinfo.vsize );
const auto pr_rssize = static_cast<double>( pinfo.rss );
constexpr double MB = 1.0 / ( 1024 * 1024 );
f.vsize = pr_size * MB;
f.rss = pr_rssize * m_pg_size * MB;
if ( 0 == pinfo.vsize ) { ok = false; }
}
return true;
return ok;
};
// attempt to read from proc
if ( !read_proc() ) {
std::cerr << "ProcStats : -> Will try reopening process proc stats" << std::endl;
open_ufd();
if ( !read_proc() ) { return false; }
}
if ( 0 == pinfo.vsize ) {
std::cerr << "ProcStats : 0==vsize -> Will try reopening process proc stats" << std::endl;
std::cerr << "ProcStats : -> Problems reading proc file. Will try reopening..." << std::endl;
open_ufd();
if ( !read_proc() ) { return false; }
}
Loading