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 <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #endif // __linux__ or __APPLE__
16 
17 struct procInfo
18 {
19  procInfo() : vsize(0), rss(0) {}
20  procInfo(double sz, double rss_sz): vsize(sz),rss(rss_sz) {}
21 
22  bool operator==(const procInfo& p) const {
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  {
55  cleanup() { }
56  ~cleanup();
57  };
58 
59  friend struct cleanup;
60 
61  class unique_fd {
62  int m_fd;
63  unique_fd(const unique_fd&) = delete;
64  unique_fd& operator=(const unique_fd&) = delete;
65  public:
66  unique_fd(int fd=-1) : m_fd(fd) {}
67  unique_fd(unique_fd&& other) { m_fd = other.m_fd; other.m_fd = -1; }
68  ~unique_fd() { if (m_fd != -1) ::close(m_fd); }
69 
70  explicit operator bool() const { return m_fd != -1; }
71  template <typename... Args>
72  unique_fd& open(Args&& ... args) { m_fd = ::open(std::forward<Args>(args)...); return *this; }
73 #define unique_fd_forward(fun) template <typename...Args> auto fun(Args&&... args) const \
74  -> decltype(::fun(m_fd,std::forward<Args>(args)...)) \
75  { return ::fun(m_fd,std::forward<Args>(args)...); }
79  unique_fd_forward(fcntl)
80  unique_fd_forward(fsync)
81  unique_fd_forward(fchown)
82  unique_fd_forward(stat)
83 #undef unique_fd_forward
84  int close() { auto r = ::close(m_fd); m_fd = -1; return r; }
85  };
86 
87 
89  double pg_size;
92  char buf[500];
93  bool valid;
94 
95  static ProcStats* inst;
96 };
97 
98 #endif
99 
static ProcStats * inst
Definition: ProcStats.h:95
unique_fd(int fd=-1)
Definition: ProcStats.h:66
def read(f, regex='.*', skipevents=0)
Definition: hivetimeline.py:19
bool operator==(const procInfo &p) const
Definition: ProcStats.h:22
unique_fd & open(Args &&...args)
Definition: ProcStats.h:72
double pg_size
Definition: ProcStats.h:89
procInfo curr
Definition: ProcStats.h:90
std::string fname
Definition: ProcStats.h:91
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
double rss
Definition: ProcStats.h:39
unique_fd fd
Definition: ProcStats.h:88
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:76
#define unique_fd_forward(fun)
Definition: ProcStats.h:73
unique_fd(unique_fd &&other)
Definition: ProcStats.h:67
procInfo(double sz, double rss_sz)
Definition: ProcStats.h:20
bool valid
Definition: ProcStats.h:93
double vsize
Definition: ProcStats.h:38
double pageSize() const
Definition: ProcStats.h:48
procInfo()
Definition: ProcStats.h:19