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
+ 11
11
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 10
9
@@ -251,11 +251,11 @@ ProcStats::ProcStats() {
#if defined( __linux__ ) or defined( __APPLE__ )
m_pg_size = sysconf( _SC_PAGESIZE ); // getpagesize();
m_fname = "/proc/" + std::to_string( getpid() ) + "/stat";
const auto fname = "/proc/" + std::to_string( getpid() ) + "/stat";
m_ufd.open( m_fname.c_str(), O_RDONLY );
m_ufd.open( fname.c_str(), O_RDONLY );
if ( !m_ufd ) {
cerr << "Failed to open " << m_fname << endl;
cerr << "Failed to open " << fname << endl;
return;
}
#endif // __linux__ or __APPLE__
@@ -267,23 +267,24 @@ bool ProcStats::fetch( procInfo& f ) {
#if defined( __linux__ ) or defined( __APPLE__ )
std::scoped_lock lock{ m_mutex };
std::scoped_lock lock{m_mutex};
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( m_buf, sizeof( m_buf ) ) ) < 0 ) {
if ( ( cnt = m_ufd.read( buf, sizeof( buf ) ) ) < 0 ) {
cout << "LINUX Read of Proc file failed:" << endl;
return false;
}
if ( cnt > 0 ) {
m_buf[std::min( static_cast<std::size_t>( cnt ), sizeof( m_buf ) - 1 )] = '\0';
buf[std::min( static_cast<std::size_t>( cnt ), sizeof( buf ) - 1 )] = '\0';
sscanf( m_buf,
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
// 30 1 2 3 4 5
"%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 "
Loading