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!
2 files
+ 39
28
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 15
12
@@ -30,10 +30,6 @@
# endif // __linux__
# include <cstdio>
using std::cerr;
using std::cout;
using std::endl;
/* Format of the Linux proc/stat (man 5 proc, kernel 2.6.35):
pid %d The process ID.
@@ -245,35 +241,37 @@ ProcStats* ProcStats::instance() {
return &inst;
}
ProcStats::ProcStats() {
void ProcStats::open_ufd() {
m_valid = false;
#if defined( __linux__ ) or defined( __APPLE__ )
m_ufd.close();
m_pg_size = sysconf( _SC_PAGESIZE ); // getpagesize();
const auto fname = "/proc/" + std::to_string( getpid() ) + "/stat";
m_ufd.open( fname.c_str(), O_RDONLY );
if ( !m_ufd ) {
cerr << "Failed to open " << fname << endl;
return;
std::cerr << "ProcStats : Failed to open " << fname << std::endl;
} else {
m_valid = true;
}
#endif // __linux__ or __APPLE__
m_valid = true;
}
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__ )
double pr_size{ 0 }, pr_rssize{ 0 };
double pr_size{0}, pr_rssize{0};
linux_proc pinfo;
int cnt{ 0 };
int cnt{0};
char buf[500];
m_ufd.lseek( 0, SEEK_SET );
if ( ( cnt = m_ufd.read( buf, sizeof( buf ) ) ) < 0 ) {
cout << "LINUX Read of Proc file failed:" << endl;
std::cerr << "ProcStats : LINUX Read of Proc file failed:" << std::endl;
return false;
}
@@ -310,5 +308,10 @@ bool ProcStats::fetch( procInfo& f ) {
m_curr.rss = f.rss;
m_curr.vsize = f.vsize;
if ( 0 == m_curr.vsize ) {
std::cerr << "ProcStats : 0==vsize -> Will try reopening process proc stats" << std::endl;
open_ufd();
}
return rc;
}
Loading