The Gaudi Framework  v30r3 (a5ef0a68)
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  {
23 #ifdef __ICC
24 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
25 #pragma warning( push )
26 #pragma warning( disable : 1572 )
27 #endif
28 
29  return vsize == p.vsize && rss == p.rss;
30 
31 #ifdef __ICC
32 // re-enable icc remark #1572
33 #pragma warning( pop )
34 #endif
35  }
36 
37  // see proc(4) man pages for units and a description
38  double vsize; // in MB (used to be in pages?)
39  double rss; // in MB (used to be in pages?)
40 };
41 
42 class ProcStats
43 {
44 public:
45  static ProcStats* instance();
46 
47  bool fetch( procInfo& fill_me );
48  double pageSize() const { return pg_size; }
49 
50 private:
51  ProcStats();
52 
53  struct cleanup {
54  cleanup() {}
55  ~cleanup();
56  };
57 
58  friend struct cleanup;
59 
60  class unique_fd
61  {
62  int m_fd;
63  unique_fd( const unique_fd& ) = delete;
64  unique_fd& operator=( const unique_fd& ) = delete;
65 
66  public:
67  unique_fd( int fd = -1 ) : m_fd( fd ) {}
68  unique_fd( unique_fd&& other )
69  {
70  m_fd = other.m_fd;
71  other.m_fd = -1;
72  }
74  {
75  if ( m_fd != -1 ) ::close( m_fd );
76  }
77 
78  explicit operator bool() const { return m_fd != -1; }
79  template <typename... Args>
80  unique_fd& open( Args&&... args )
81  {
82  m_fd = ::open( std::forward<Args>( args )... );
83  return *this;
84  }
85 #define unique_fd_forward( fun ) \
86  template <typename... Args> \
87  auto fun( Args&&... args ) const->decltype(::fun( m_fd, std::forward<Args>( args )... ) ) \
88  { \
89  return ::fun( m_fd, std::forward<Args>( args )... ); \
90  }
92  unique_fd_forward( fsync ) unique_fd_forward( fchown ) unique_fd_forward( stat )
93 #undef unique_fd_forward
94  int close()
95  {
96  auto r = ::close( m_fd );
97  m_fd = -1;
98  return r;
99  }
100  };
101 
103  double pg_size;
106  char buf[500];
107  bool valid;
108 
109  static ProcStats* inst;
110 };
111 
112 #endif
static ProcStats * inst
Definition: ProcStats.h:109
unique_fd(int fd=-1)
Definition: ProcStats.h:67
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:80
double pg_size
Definition: ProcStats.h:103
procInfo curr
Definition: ProcStats.h:104
std::string fname
Definition: ProcStats.h:105
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
double rss
Definition: ProcStats.h:39
unique_fd fd
Definition: ProcStats.h:102
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:91
unique_fd(unique_fd &&other)
Definition: ProcStats.h:68
procInfo(double sz, double rss_sz)
Definition: ProcStats.h:19
bool valid
Definition: ProcStats.h:107
double vsize
Definition: ProcStats.h:38
#define unique_fd_forward(fun)
Definition: ProcStats.h:85
double pageSize() const
Definition: ProcStats.h:48
procInfo()
Definition: ProcStats.h:18