The Gaudi Framework  master (9f3f8895)
Loading...
Searching...
No Matches
ProcStats.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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// Class: ProcStats
12// Purpose: To keep statistics on memory use
13
14#include "ProcStats.h"
15
16#if defined( __linux__ )
17# include <fstream>
18#elif defined( __APPLE__ )
19# include <mach/mach.h>
20#endif
21
23 static ProcStats inst{};
24 return &inst;
25}
26
28 constexpr double MB = 1.0 / ( 1024 * 1024 );
29#if defined( __linux__ )
30 // see proc(5): /proc/self/statm is "size resident shared text lib data dt" in pages
31 // size and resident are identical to the vsize and rss fields of /proc/self/stat
32 std::ifstream statm{ "/proc/self/statm" };
33 unsigned long size = 0, resident = 0; // in pages
34 if ( !( statm >> size >> resident ) || size == 0 ) { return false; }
35 f.vsize = size * m_pageSize * MB;
36 f.rss = resident * m_pageSize * MB;
37 return true;
38#elif defined( __APPLE__ )
39 task_basic_info_64_data_t info;
40 mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
41 if ( task_info( mach_task_self(), TASK_BASIC_INFO_64, reinterpret_cast<task_info_t>( &info ), &count ) !=
42 KERN_SUCCESS ) {
43 return false;
44 }
45 f.vsize = info.virtual_size * MB;
46 f.rss = info.resident_size * MB;
47 return true;
48#else
49 return false;
50#endif
51}
bool fetch(procInfo &fill_me)
Fill fill_me with the current memory usage of this process.
Definition ProcStats.cpp:27
ProcStats()=default
static ProcStats * instance()
Definition ProcStats.cpp:22
double vsize
Definition ProcStats.h:23
double rss
Definition ProcStats.h:24