The Gaudi Framework  v33r1 (b1225454)
ProcStats.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIAUD_PROCSTATS_H
12 #define GAUDIAUD_PROCSTATS_H
13 
14 // Class: ProcStats
15 // Description: Keeps statistics on memory usage
16 // Author: Jim Kowalkowski (FNAL), modified by M. Shapiro (LBNL)
17 
18 #include <string>
19 #include <vector>
20 #if defined( __linux__ ) or defined( __APPLE__ )
21 # include <fcntl.h>
22 # include <sys/stat.h>
23 # include <sys/types.h>
24 # include <unistd.h>
25 #endif // __linux__ or __APPLE__
26 
27 struct procInfo {
28  procInfo() : vsize( 0 ), rss( 0 ) {}
29  procInfo( double sz, double rss_sz ) : vsize( sz ), rss( rss_sz ) {}
30 
31  bool operator==( const procInfo& p ) const {
32 #ifdef __ICC
33 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
34 # pragma warning( push )
35 # pragma warning( disable : 1572 )
36 #endif
37 
38  return vsize == p.vsize && rss == p.rss;
39 
40 #ifdef __ICC
41 // re-enable icc remark #1572
42 # pragma warning( pop )
43 #endif
44  }
45 
46  // see proc(4) man pages for units and a description
47  double vsize; // in MB (used to be in pages?)
48  double rss; // in MB (used to be in pages?)
49 };
50 
51 class ProcStats {
52 public:
53  static ProcStats* instance();
54 
55  bool fetch( procInfo& fill_me );
56  double pageSize() const { return pg_size; }
57 
58 private:
59  ProcStats();
60 
61  struct cleanup {
62  cleanup() {}
63  ~cleanup();
64  };
65 
66  friend struct cleanup;
67 
68  class unique_fd {
69  int m_fd;
70  unique_fd( const unique_fd& ) = delete;
71  unique_fd& operator=( const unique_fd& ) = delete;
72 
73  public:
74  unique_fd( int fd = -1 ) : m_fd( fd ) {}
75  unique_fd( unique_fd&& other ) {
76  m_fd = other.m_fd;
77  other.m_fd = -1;
78  }
80  if ( m_fd != -1 ) ::close( m_fd );
81  }
82 
83  explicit operator bool() const { return m_fd != -1; }
84  template <typename... Args>
85  unique_fd& open( Args&&... args ) {
86  m_fd = ::open( std::forward<Args>( args )... );
87  return *this;
88  }
89 #define unique_fd_forward( fun ) \
90  template <typename... Args> \
91  auto fun( Args&&... args ) const->decltype( ::fun( m_fd, std::forward<Args>( args )... ) ) { \
92  return ::fun( m_fd, std::forward<Args>( args )... ); \
93  }
95  unique_fd_forward( fsync ) unique_fd_forward( fchown ) unique_fd_forward( stat )
96 #undef unique_fd_forward
97  int close() {
98  auto r = ::close( m_fd );
99  m_fd = -1;
100  return r;
101  }
102  };
103 
105  double pg_size;
108  char buf[500];
109  bool valid;
110 
111  static ProcStats* inst;
112 };
113 
114 #endif
static ProcStats * inst
Definition: ProcStats.h:111
unique_fd & operator=(const unique_fd &)=delete
bool operator==(const procInfo &p) const
Definition: ProcStats.h:31
static ProcStats * instance()
Definition: ProcStats.cpp:249
unique_fd & open(Args &&... args)
Definition: ProcStats.h:85
unique_fd(int fd=-1)
Definition: ProcStats.h:74
def read(f, regex='.*', skipevents=0)
Definition: hivetimeline.py:33
double pg_size
Definition: ProcStats.h:105
procInfo curr
Definition: ProcStats.h:106
std::string fname
Definition: ProcStats.h:107
STL class.
unique_fd(const unique_fd &)=delete
double rss
Definition: ProcStats.h:48
unique_fd fd
Definition: ProcStats.h:104
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:94
unique_fd(unique_fd &&other)
Definition: ProcStats.h:75
procInfo(double sz, double rss_sz)
Definition: ProcStats.h:29
bool valid
Definition: ProcStats.h:109
char buf[500]
Definition: ProcStats.h:108
double vsize
Definition: ProcStats.h:47
double pageSize() const
Definition: ProcStats.h:56
bool fetch(procInfo &fill_me)
Definition: ProcStats.cpp:272
procInfo()
Definition: ProcStats.h:28