The Gaudi Framework  master (37c0b60a)
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  // see proc(4) man pages for units and a description
34  double vsize{ 0 }; // in MB (used to be in pages?)
35  double rss{ 0 }; // in MB (used to be in pages?)
36 };
37 
38 class ProcStats {
39 
40 public:
42 
43 private:
44  void open_ufd();
45 
46 public:
47  static ProcStats* instance();
48  bool fetch( procInfo& fill_me );
49  auto pageSize() const noexcept { return m_pg_size; }
50 
51 private:
52  class unique_fd {
53 
54  private:
55  int m_fd{ -1 };
56 
57  private:
58  unique_fd( const unique_fd& ) = delete;
59  unique_fd& operator=( const unique_fd& ) = delete;
60 
61  public:
62  unique_fd( const int fd = -1 ) : m_fd( fd ) {}
63  unique_fd( unique_fd&& other ) {
64  m_fd = other.m_fd;
65  other.m_fd = -1;
66  }
67  ~unique_fd() { close(); }
68 
69  public:
70  explicit operator bool() const { return m_fd != -1; }
71  template <typename... Args>
72  unique_fd& open( Args&&... args ) {
73  m_fd = ::open( std::forward<Args>( args )... );
74  return *this;
75  }
76  int close() {
77  int r = 0;
78  if ( m_fd != -1 ) {
79  r = ::close( m_fd );
80  m_fd = -1;
81  }
82  return r;
83  }
84 
85  public:
86 #define unique_fd_forward( fun ) \
87  template <typename... Args> \
88  auto fun( Args&&... args ) const { \
89  return ::fun( m_fd, std::forward<Args>( args )... ); \
90  }
91  // clang-format off
92  unique_fd_forward( lseek )
94  unique_fd_forward( write )
95  unique_fd_forward( fcntl )
96  unique_fd_forward( fsync )
97  unique_fd_forward( fchown )
98  unique_fd_forward( stat )
99  // clang-format on
100 #undef unique_fd_forward
101  };
102 
103 private:
105  double m_pg_size{ 0 };
107  bool m_valid{ false };
109 };
gaudirun.fd
fd
Definition: gaudirun.py:630
procInfo
Definition: ProcStats.h:29
ProcStats::instance
static ProcStats * instance()
Definition: ProcStats.cpp:239
ProcStats
Definition: ProcStats.h:38
ProcStats::unique_fd::unique_fd
unique_fd(unique_fd &&other)
Definition: ProcStats.h:63
procInfo::vsize
double vsize
Definition: ProcStats.h:34
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:62
ProcStats::m_pg_size
double m_pg_size
Definition: ProcStats.h:105
ProcStats::unique_fd::open
unique_fd & open(Args &&... args)
Definition: ProcStats.h:72
ProcStats::unique_fd::~unique_fd
~unique_fd()
Definition: ProcStats.h:67
ProcStats::unique_fd::close
int close()
Definition: ProcStats.h:76
ProcStats::m_mutex
std::mutex m_mutex
Definition: ProcStats.h:108
ProcStats::m_curr
procInfo m_curr
Definition: ProcStats.h:106
hivetimeline.read
def read(f, regex=".*", skipevents=0)
Definition: hivetimeline.py:32
ProcStats::m_ufd
unique_fd m_ufd
Definition: ProcStats.h:104
ProcStats::ProcStats
ProcStats()
Definition: ProcStats.h:41
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:86
ProcStats::unique_fd::operator=
unique_fd & operator=(const unique_fd &)=delete
ProcStats::pageSize
auto pageSize() const noexcept
Definition: ProcStats.h:49
ProcStats::unique_fd
Definition: ProcStats.h:52
std::mutex
STL class.
procInfo::rss
double rss
Definition: ProcStats.h:35
ProcStats::m_valid
bool m_valid
Definition: ProcStats.h:107
ProcStats::unique_fd::m_fd
int m_fd
Definition: ProcStats.h:55
procInfo::procInfo
procInfo()=default
ProcStats::unique_fd::unique_fd
unique_fd(const unique_fd &)=delete