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 #ifdef __linux
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #endif
15 
16 struct procInfo
17 {
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 {
43 public:
44  static ProcStats* instance();
45 
46  bool fetch(procInfo& fill_me);
47  double pageSize() const { return pg_size; }
48 
49 private:
50  ProcStats();
51 
52  struct cleanup
53  {
54  cleanup() { }
55  ~cleanup();
56  };
57 
58  friend struct cleanup;
59 
60  class unique_fd {
61  int m_fd;
62  unique_fd(const unique_fd&) = delete;
63  unique_fd& operator=(const unique_fd&) = delete;
64  public:
65  unique_fd(int fd=-1) : m_fd(fd) {}
66  unique_fd(unique_fd&& other) { m_fd = other.m_fd; other.m_fd = -1; }
67  ~unique_fd() { if (m_fd != -1) ::close(m_fd); }
68 
69  explicit operator bool() const { return m_fd != -1; }
70  template <typename... Args>
71  unique_fd& open(Args&& ... args) { m_fd = ::open(std::forward<Args>(args)...); return *this; }
72 #define unique_fd_forward(fun) template <typename...Args> auto fun(Args&&... args) const \
73  -> decltype(::fun(m_fd,std::forward<Args>(args)...)) \
74  { return ::fun(m_fd,std::forward<Args>(args)...); }
76  unique_fd_forward(read)
77  unique_fd_forward(write)
78  unique_fd_forward(fcntl)
79  unique_fd_forward(fsync)
80  unique_fd_forward(fchown)
81  unique_fd_forward(stat)
82 #undef unique_fd_forward
83  int close() { auto r = ::close(m_fd); m_fd = -1; return r; }
84  };
85 
86 
88  double pg_size;
91  char buf[500];
92  bool valid;
93 
94  static ProcStats* inst;
95 };
96 
97 #endif
98 
static ProcStats * inst
Definition: ProcStats.h:94
unique_fd & operator=(const unique_fd &)=delete
static ProcStats * instance()
Definition: ProcStats.cpp:237
unique_fd(int fd=-1)
Definition: ProcStats.h:65
bool operator==(const procInfo &p) const
Definition: ProcStats.h:21
unique_fd & open(Args &&...args)
Definition: ProcStats.h:71
double pg_size
Definition: ProcStats.h:88
procInfo curr
Definition: ProcStats.h:89
std::string fname
Definition: ProcStats.h:90
STL class.
unique_fd(const unique_fd &)=delete
double rss
Definition: ProcStats.h:38
unique_fd fd
Definition: ProcStats.h:87
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:75
unique_fd(unique_fd &&other)
Definition: ProcStats.h:66
procInfo(double sz, double rss_sz)
Definition: ProcStats.h:19
list args
Definition: gaudirun.py:290
bool valid
Definition: ProcStats.h:92
char buf[500]
Definition: ProcStats.h:91
double vsize
Definition: ProcStats.h:37
bool fetch(procInfo &fill_me)
Definition: ProcStats.cpp:263
double pageSize() const
Definition: ProcStats.h:47
procInfo()
Definition: ProcStats.h:18