Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ProcStats.h
Go to the documentation of this file.
1 #ifndef GAUDIAUD_PROCSTATS_H
2 #define GAUDIAUD_PROCSTATS_H
3 
4 // Class: ProcStats
5 // Description: Keeps statistics on memory usage
6 // Author: Jim Kowalkowski (FNAL), modified by M. Shapiro (LBNL)
7 
8 #include <string>
9 #include <vector>
10 #if defined( __linux__ ) or defined( __APPLE__ )
11 # include <fcntl.h>
12 # include <sys/stat.h>
13 # include <sys/types.h>
14 # include <unistd.h>
15 #endif // __linux__ or __APPLE__
16 
17 struct procInfo {
18  procInfo() : vsize( 0 ), rss( 0 ) {}
19  procInfo( double sz, double rss_sz ) : vsize( sz ), rss( rss_sz ) {}
20 
21  bool operator==( const procInfo& p ) const {
22 #ifdef __ICC
23 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
24 # pragma warning( push )
25 # pragma warning( disable : 1572 )
26 #endif
27 
28  return vsize == p.vsize && rss == p.rss;
29 
30 #ifdef __ICC
31 // re-enable icc remark #1572
32 # pragma warning( pop )
33 #endif
34  }
35 
36  // see proc(4) man pages for units and a description
37  double vsize; // in MB (used to be in pages?)
38  double rss; // in MB (used to be in pages?)
39 };
40 
41 class ProcStats {
42 public:
43  static ProcStats* instance();
44 
45  bool fetch( procInfo& fill_me );
46  double pageSize() const { return pg_size; }
47 
48 private:
49  ProcStats();
50 
51  struct cleanup {
52  cleanup() {}
53  ~cleanup();
54  };
55 
56  friend struct cleanup;
57 
58  class unique_fd {
59  int m_fd;
60  unique_fd( const unique_fd& ) = delete;
61  unique_fd& operator=( const unique_fd& ) = delete;
62 
63  public:
64  unique_fd( int fd = -1 ) : m_fd( fd ) {}
65  unique_fd( unique_fd&& other ) {
66  m_fd = other.m_fd;
67  other.m_fd = -1;
68  }
70  if ( m_fd != -1 ) ::close( m_fd );
71  }
72 
73  explicit operator bool() const { return m_fd != -1; }
74  template <typename... Args>
75  unique_fd& open( Args&&... args ) {
76  m_fd = ::open( std::forward<Args>( args )... );
77  return *this;
78  }
79 #define unique_fd_forward( fun ) \
80  template <typename... Args> \
81  auto fun( Args&&... args ) const->decltype( ::fun( m_fd, std::forward<Args>( args )... ) ) { \
82  return ::fun( m_fd, std::forward<Args>( args )... ); \
83  }
85  unique_fd_forward( fsync ) unique_fd_forward( fchown ) unique_fd_forward( stat )
86 #undef unique_fd_forward
87  int close() {
88  auto r = ::close( m_fd );
89  m_fd = -1;
90  return r;
91  }
92  };
93 
95  double pg_size;
98  char buf[500];
99  bool valid;
100 
101  static ProcStats* inst;
102 };
103 
104 #endif
static ProcStats * inst
Definition: ProcStats.h:101
unique_fd(int fd=-1)
Definition: ProcStats.h:64
def read(f, regex='.*', skipevents=0)
Definition: hivetimeline.py:22
bool operator==(const procInfo &p) const
Definition: ProcStats.h:21
unique_fd & open(Args &&...args)
Definition: ProcStats.h:75
double pg_size
Definition: ProcStats.h:95
procInfo curr
Definition: ProcStats.h:96
std::string fname
Definition: ProcStats.h:97
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
double rss
Definition: ProcStats.h:38
unique_fd fd
Definition: ProcStats.h:94
unique_fd_forward(lseek) unique_fd_forward(read) unique_fd_forward(write) unique_fd_forward(fcntl) unique_fd_forward(fsync) unique_fd_forward(fchown) unique_fd_forward(stat) int close()
Definition: ProcStats.h:84
unique_fd(unique_fd &&other)
Definition: ProcStats.h:65
procInfo(double sz, double rss_sz)
Definition: ProcStats.h:19
bool valid
Definition: ProcStats.h:99
double vsize
Definition: ProcStats.h:37
#define unique_fd_forward(fun)
Definition: ProcStats.h:79
double pageSize() const
Definition: ProcStats.h:46
procInfo()
Definition: ProcStats.h:18