| The Gaudi Framework v32r1 (f65d50dc)

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 }
69 ~unique_fd() {
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 }
84 unique_fd_forward( lseek ) unique_fd_forward( read ) unique_fd_forward( write ) unique_fd_forward( fcntl )
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
94 unique_fd fd;
95 double pg_size;
96 procInfo curr;
97 std::string fname;
98 char buf[500];
99 bool valid;
100
101 static ProcStats* inst;
102 };
103
104 #endif
ProcStats::inst
static ProcStats * inst
Definition: ProcStats.h:101
ProcStats::unique_fd::operator=
unique_fd & operator=(const unique_fd &)=delete
ProcStats::cleanup
Definition: ProcStats.h:51
procInfo::operator==
bool operator==(const procInfo &p) const
Definition: ProcStats.h:21
ProcStats::unique_fd::~unique_fd
~unique_fd()
Definition: ProcStats.h:69
ProcStats::unique_fd
Definition: ProcStats.h:58
ProcStats::instance
static ProcStats * instance()
Definition: ProcStats.cpp:239
ProcStats::unique_fd::open
unique_fd & open(Args &&... args)
Definition: ProcStats.h:75
ProcStats::unique_fd::unique_fd
unique_fd(int fd=-1)
Definition: ProcStats.h:64
hivetimeline.read
def read(f, regex='.*', skipevents=0)
Definition: hivetimeline.py:22
ProcStats::pg_size
double pg_size
Definition: ProcStats.h:95
ProcStats::curr
procInfo curr
Definition: ProcStats.h:96
ProcStats::fname
std::string fname
Definition: ProcStats.h:97
std::string
STL class.
ProcStats::cleanup::cleanup
cleanup()
Definition: ProcStats.h:52
ProcStats::unique_fd::unique_fd
unique_fd(const unique_fd &)=delete
ProcStats::cleanup::~cleanup
~cleanup()
Definition: ProcStats.cpp:232
gaudirun.args
args
Definition: gaudirun.py:307
procInfo::rss
double rss
Definition: ProcStats.h:38
ProcStats::fd
unique_fd fd
Definition: ProcStats.h:94
ProcStats::unique_fd::unique_fd_forward
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
ProcStats::unique_fd::unique_fd
unique_fd(unique_fd &&other)
Definition: ProcStats.h:65
ProcStats
Definition: ProcStats.h:41
ProcStats::unique_fd::m_fd
int m_fd
Definition: ProcStats.h:59
procInfo::procInfo
procInfo(double sz, double rss_sz)
Definition: ProcStats.h:19
ProcStats::valid
bool valid
Definition: ProcStats.h:99
ProcStats::buf
char buf[500]
Definition: ProcStats.h:98
procInfo::vsize
double vsize
Definition: ProcStats.h:37
ProcStats::pageSize
double pageSize() const
Definition: ProcStats.h:46
ProcStats::ProcStats
ProcStats()
Definition: ProcStats.cpp:247
ProcStats::fetch
bool fetch(procInfo &fill_me)
Definition: ProcStats.cpp:262
procInfo
Definition: ProcStats.h:17
procInfo::procInfo
procInfo()
Definition: ProcStats.h:18