The Gaudi Framework  v36r10 (fc05264c)
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 
12 #pragma once
13 
14 // Class: ProcStats
15 // Description: Keeps statistics on memory usage
16 // Author: Jim Kowalkowski (FNAL), modified by M. Shapiro (LBNL)
17 
18 #include <mutex>
19 #include <string>
20 #include <vector>
21 
22 #if defined( __linux__ ) or defined( __APPLE__ )
23 # include <fcntl.h>
24 # include <sys/stat.h>
25 # include <sys/types.h>
26 # include <unistd.h>
27 #endif // __linux__ or __APPLE__
28 
29 struct procInfo {
30  procInfo() = default;
31  procInfo( double sz, double rss_sz ) : vsize( sz ), rss( rss_sz ) {}
32 
33  bool operator==( const procInfo& p ) const {
34 #ifdef __ICC
35 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
36 # pragma warning( push )
37 # pragma warning( disable : 1572 )
38 #endif
39 
40  return ( vsize == p.vsize && rss == p.rss );
41 
42 #ifdef __ICC
43 // re-enable icc remark #1572
44 # pragma warning( pop )
45 #endif
46  }
47 
48  // see proc(4) man pages for units and a description
49  double vsize{ 0 }; // in MB (used to be in pages?)
50  double rss{ 0 }; // in MB (used to be in pages?)
51 };
52 
53 class ProcStats {
54 
55 public:
57 
58 private:
59  void open_ufd();
60 
61 public:
62  static ProcStats* instance();
63  bool fetch( procInfo& fill_me );
64  auto pageSize() const noexcept { return m_pg_size; }
65 
66 private:
67  class unique_fd {
68 
69  private:
70  int m_fd{ -1 };
71 
72  private:
73  unique_fd( const unique_fd& ) = delete;
74  unique_fd& operator=( const unique_fd& ) = delete;
75 
76  public:
77  unique_fd( const int fd = -1 ) : m_fd( fd ) {}
78  unique_fd( unique_fd&& other ) {
79  m_fd = other.m_fd;
80  other.m_fd = -1;
81  }
82  ~unique_fd() { close(); }
83 
84  public:
85  explicit operator bool() const { return m_fd != -1; }
86  template <typename... Args>
87  unique_fd& open( Args&&... args ) {
88  m_fd = ::open( std::forward<Args>( args )... );
89  return *this;
90  }
91  int close() {
92  int r = 0;
93  if ( m_fd != -1 ) {
94  r = ::close( m_fd );
95  m_fd = -1;
96  }
97  return r;
98  }
99 
100  public:
101 #define unique_fd_forward( fun ) \
102  template <typename... Args> \
103  auto fun( Args&&... args ) const { \
104  return ::fun( m_fd, std::forward<Args>( args )... ); \
105  }
106  // clang-format off
107  unique_fd_forward( lseek )
109  unique_fd_forward( write )
110  unique_fd_forward( fcntl )
111  unique_fd_forward( fsync )
112  unique_fd_forward( fchown )
113  unique_fd_forward( stat )
114  // clang-format on
115 #undef unique_fd_forward
116  };
117 
118 private:
120  double m_pg_size{ 0 };
122  bool m_valid{ false };
124 };
gaudirun.fd
fd
Definition: gaudirun.py:627
procInfo
Definition: ProcStats.h:29
ProcStats::instance
static ProcStats * instance()
Definition: ProcStats.cpp:239
ProcStats
Definition: ProcStats.h:53
ProcStats::unique_fd::unique_fd
unique_fd(unique_fd &&other)
Definition: ProcStats.h:78
procInfo::vsize
double vsize
Definition: ProcStats.h:49
procInfo::procInfo
procInfo(double sz, double rss_sz)
Definition: ProcStats.h:31
ProcStats::unique_fd::unique_fd
unique_fd(const int fd=-1)
Definition: ProcStats.h:77
HistoDumpEx.r
r
Definition: HistoDumpEx.py:20
ProcStats::m_pg_size
double m_pg_size
Definition: ProcStats.h:120
ProcStats::unique_fd::open
unique_fd & open(Args &&... args)
Definition: ProcStats.h:87
ProcStats::unique_fd::~unique_fd
~unique_fd()
Definition: ProcStats.h:82
ProcStats::unique_fd::close
int close()
Definition: ProcStats.h:91
ProcStats::m_mutex
std::mutex m_mutex
Definition: ProcStats.h:123
ProcStats::m_curr
procInfo m_curr
Definition: ProcStats.h:121
hivetimeline.read
def read(f, regex=".*", skipevents=0)
Definition: hivetimeline.py:33
ProcStats::m_ufd
unique_fd m_ufd
Definition: ProcStats.h:119
ProcStats::ProcStats
ProcStats()
Definition: ProcStats.h:56
gaudirun.args
args
Definition: gaudirun.py:336
ProcStats::open_ufd
void open_ufd()
Definition: ProcStats.cpp:244
ProcStats::fetch
bool fetch(procInfo &fill_me)
Definition: ProcStats.cpp:259
unique_fd_forward
#define unique_fd_forward(fun)
Definition: ProcStats.h:101
ProcStats::unique_fd::operator=
unique_fd & operator=(const unique_fd &)=delete
ProcStats::pageSize
auto pageSize() const noexcept
Definition: ProcStats.h:64
ProcStats::unique_fd
Definition: ProcStats.h:67
std::mutex
STL class.
procInfo::rss
double rss
Definition: ProcStats.h:50
ProcStats::m_valid
bool m_valid
Definition: ProcStats.h:122
ProcStats::unique_fd::m_fd
int m_fd
Definition: ProcStats.h:70
procInfo::procInfo
procInfo()=default
ProcStats::unique_fd::unique_fd
unique_fd(const unique_fd &)=delete
procInfo::operator==
bool operator==(const procInfo &p) const
Definition: ProcStats.h:33