diff --git a/GaudiAud/src/ProcStats.cpp b/GaudiAud/src/ProcStats.cpp index a96beee71ee614b9906986004d941c295cb45da6..cb05b3c7f436c6473714e22ff33605d1fef70688 100644 --- a/GaudiAud/src/ProcStats.cpp +++ b/GaudiAud/src/ProcStats.cpp @@ -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,17 +241,19 @@ 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 ) { @@ -273,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; } @@ -298,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 diff --git a/GaudiAud/src/ProcStats.h b/GaudiAud/src/ProcStats.h index 04521271341bbef29a644411e54b99056966f48c..08dcbddf5e00fd57d0ddb363b80a3cf344674adc 100644 --- a/GaudiAud/src/ProcStats.h +++ b/GaudiAud/src/ProcStats.h @@ -51,41 +51,49 @@ struct procInfo { }; class ProcStats { + public: - static ProcStats* instance(); + ProcStats() { open_ufd(); } + +private: + void open_ufd(); - bool fetch( procInfo& fill_me ); - auto pageSize() const noexcept { return m_pg_size; } - ProcStats(); +public: + static ProcStats* instance(); + bool fetch( procInfo& fill_me ); + auto pageSize() const noexcept { return m_pg_size; } private: class unique_fd { private: int m_fd{ -1 }; + + private: unique_fd( const unique_fd& ) = delete; unique_fd& operator=( const unique_fd& ) = delete; public: - unique_fd( int fd = -1 ) : m_fd( fd ) {} + unique_fd( const int fd = -1 ) : m_fd( fd ) {} unique_fd( unique_fd&& other ) { m_fd = other.m_fd; other.m_fd = -1; } - ~unique_fd() { - if ( m_fd != -1 ) { ::close( m_fd ); } - } + ~unique_fd() { close(); } + public: explicit operator bool() const { return m_fd != -1; } template <typename... Args> unique_fd& open( Args&&... args ) { m_fd = ::open( std::forward<Args>( args )... ); return *this; } - int close() { - auto r = ::close( m_fd ); - m_fd = -1; + int r = 0; + if ( m_fd != -1 ) { + r = ::close( m_fd ); + m_fd = -1; + } return r; }