The Gaudi Framework  master (e68eea06)
Loading...
Searching...
No Matches
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
29struct 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
38class ProcStats {
39
40public:
42
43private:
44 void open_ufd();
45
46public:
47 static ProcStats* instance();
48 bool fetch( procInfo& fill_me );
49 auto pageSize() const noexcept { return m_pg_size; }
50
51private:
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 }
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 )
93 unique_fd_forward( read )
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
103private:
105 double m_pg_size{ 0 };
107 bool m_valid{ false };
108 std::mutex m_mutex;
109};
#define unique_fd_forward(fun)
Definition ProcStats.h:86
unique_fd(const unique_fd &)=delete
unique_fd(unique_fd &&other)
Definition ProcStats.h:63
unique_fd(const int fd=-1)
Definition ProcStats.h:62
unique_fd & open(Args &&... args)
Definition ProcStats.h:72
unique_fd & operator=(const unique_fd &)=delete
procInfo m_curr
Definition ProcStats.h:106
double m_pg_size
Definition ProcStats.h:105
bool fetch(procInfo &fill_me)
void open_ufd()
unique_fd m_ufd
Definition ProcStats.h:104
static ProcStats * instance()
auto pageSize() const noexcept
Definition ProcStats.h:49
std::mutex m_mutex
Definition ProcStats.h:108
bool m_valid
Definition ProcStats.h:107
procInfo(double sz, double rss_sz)
Definition ProcStats.h:31
procInfo()=default
double vsize
Definition ProcStats.h:34
double rss
Definition ProcStats.h:35