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
+ 74
67
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 50
51
@@ -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.
@@ -202,73 +198,71 @@ using std::endl;
(divide by sysconf(_SC_CLK_TCK).
*/
struct linux_proc {
int pid;
int pid{-1};
char comm[400];
char state;
int ppid;
int pgrp;
int session;
int tty;
int tpgid;
unsigned long flags;
unsigned long minflt;
unsigned long cminflt;
unsigned long majflt;
unsigned long cmajflt;
unsigned long utime;
unsigned long stime;
long cutime;
long cstime;
long priority;
long nice;
long num_threads;
long itrealvalue;
unsigned long long starttime;
unsigned long vsize;
long rss;
unsigned long rlim;
unsigned long startcode;
unsigned long endcode;
unsigned long startstack;
unsigned long kstkesp;
unsigned long kstkeip;
unsigned long signal;
unsigned long blocked;
unsigned long sigignore;
unsigned long sigcatch;
unsigned long wchan;
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__
ProcStats* ProcStats::instance() {
static std::once_flag alloc_instance_once;
static std::unique_ptr<ProcStats> inst;
std::call_once( alloc_instance_once, []() { inst = std::make_unique<ProcStats>(); } );
return inst.get();
static ProcStats inst{};
return &inst;
}
ProcStats::ProcStats() {
void ProcStats::open_ufd() {
m_valid = false;
#if defined( __linux__ ) or defined( __APPLE__ )
m_pg_size = sysconf( _SC_PAGESIZE ); // getpagesize();
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; }
#if defined( __linux__ ) or defined( __APPLE__ )
std::scoped_lock lock{m_mutex};
#if defined( __linux__ ) or defined( __APPLE__ )
double pr_size{0}, pr_rssize{0};
linux_proc pinfo;
int cnt{0};
@@ -277,7 +271,7 @@ bool ProcStats::fetch( procInfo& f ) {
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;
}
@@ -302,6 +296,11 @@ bool ProcStats::fetch( procInfo& f ) {
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();
}
}
#else
Loading