Repo-Init
 
Loading...
Searching...
No Matches
ProcessMetrics Class Reference

#include <ProcessMetrics.hpp>

Classes

struct  _oldCpu
 Structure to store the old CPU times. More...
 

Public Member Functions

 ProcessMetrics (std::shared_ptr< std::atomic_flag > checkFlag, const std::shared_ptr< prometheus::Registry > &reg)
 
 ProcessMetrics (const ProcessMetrics &)=delete
 Copy constructor.
 
 ProcessMetrics (ProcessMetrics &&)=delete
 Move constructor.
 
ProcessMetricsoperator= (ProcessMetrics)=delete
 Copy assignment operator.
 
ProcessMetricsoperator= (ProcessMetrics &&)=delete
 Move assignment operator.
 
 ~ProcessMetrics ()
 

Protected Member Functions

double getCpuUsage ()
 
std::pair< size_t, size_t > getDiskIO ()
 
void update ()
 
void threadRunner () noexcept
 

Static Protected Member Functions

static long int getMemoryUsage ()
 
static long int getPageFaults ()
 
static size_t getThreadCount ()
 
static size_t getFileDescriptorCount ()
 

Static Private Member Functions

static size_t countDirectoryEntries (const std::string &path)
 

Private Attributes

std::atomic_flag _shouldStop {false}
 Flag to stop monitoring.
 
std::unique_ptr< std::thread > _thread
 Thread handler.
 
std::shared_ptr< std::atomic_flag > _checkFlag
 Runtime check flag.
 
prometheus::Gauge * _pInitTime
 Pointer to initialization time gauge.
 
prometheus::Gauge * _pCurrentTime
 Pointer to the current time gauge.
 
prometheus::Gauge * _pMemory
 Pointer to the memory usage gauge.
 
prometheus::Gauge * _pPageFaults
 Pointer to the page faults gauge.
 
prometheus::Gauge * _pCpuUsage
 Pointer to the CPU usage gauge.
 
prometheus::Gauge * _pDiskRead
 Pointer to the disk read gauge.
 
prometheus::Gauge * _pDiskWrite
 Pointer to the disk write gauge.
 
prometheus::Gauge * _pThreadCount
 Pointer to the thread count gauge.
 
prometheus::Gauge * _pFileDescriptorCount
 Pointer to the file descriptor count gauge.
 
size_t _oldReadBytes {0}
 Variable to store the old read bytes.
 
size_t _oldWriteBytes {0}
 Variable to store the old write bytes.
 
clock_t _oldCpuTime {0}
 Variable to store the old CPU time.
 

Detailed Description

Class that provides metrics related to the current process.

Definition at line 14 of file ProcessMetrics.hpp.

Constructor & Destructor Documentation

◆ ProcessMetrics() [1/3]

ProcessMetrics::ProcessMetrics ( std::shared_ptr< std::atomic_flag > checkFlag,
const std::shared_ptr< prometheus::Registry > & reg )

Constructs a ProcessMetrics object.

Parameters
[in]regThe Prometheus registry.
[in]checkFlagRuntime check flag

Definition at line 122 of file ProcessMetrics.cpp.

124 : _checkFlag(std::move(checkFlag))
125{
126 if (reg == nullptr)
127 {
128 throw std::invalid_argument("Registry is nullptr");
129 }
130
131 _pInitTime =
132 &prometheus::BuildGauge().Name("init_time").Help("Initialization time of application").Register(*reg).Add({});
134 &prometheus::BuildGauge().Name("current_time").Help("Current time of application").Register(*reg).Add({});
135 _pMemory =
136 &prometheus::BuildGauge().Name("memory_usage").Help("Memory usage of application").Register(*reg).Add({});
138 &prometheus::BuildGauge().Name("page_faults").Help("Page faults of application").Register(*reg).Add({});
139 _pCpuUsage = &prometheus::BuildGauge().Name("cpu_usage").Help("CPU usage of application").Register(*reg).Add({});
140 _pDiskRead = &prometheus::BuildGauge().Name("disk_read").Help("Disk read of application").Register(*reg).Add({});
141 _pDiskWrite = &prometheus::BuildGauge().Name("disk_write").Help("Disk write of application").Register(*reg).Add({});
143 &prometheus::BuildGauge().Name("thread_count").Help("Thread count of application").Register(*reg).Add({});
144 _pFileDescriptorCount = &prometheus::BuildGauge()
145 .Name("file_descriptor_count")
146 .Help("File descriptor count of application")
147 .Register(*reg)
148 .Add({});
149
150 _pInitTime->SetToCurrentTime();
151
152 _thread = std::make_unique<std::thread>(&ProcessMetrics::threadRunner, this);
153}
prometheus::Gauge * _pMemory
Pointer to the memory usage gauge.
prometheus::Gauge * _pInitTime
Pointer to initialization time gauge.
prometheus::Gauge * _pCurrentTime
Pointer to the current time gauge.
prometheus::Gauge * _pCpuUsage
Pointer to the CPU usage gauge.
void threadRunner() noexcept
prometheus::Gauge * _pPageFaults
Pointer to the page faults gauge.
prometheus::Gauge * _pFileDescriptorCount
Pointer to the file descriptor count gauge.
prometheus::Gauge * _pDiskRead
Pointer to the disk read gauge.
prometheus::Gauge * _pThreadCount
Pointer to the thread count gauge.
std::shared_ptr< std::atomic_flag > _checkFlag
Runtime check flag.
std::unique_ptr< std::thread > _thread
Thread handler.
prometheus::Gauge * _pDiskWrite
Pointer to the disk write gauge.
Here is the call graph for this function:

◆ ProcessMetrics() [2/3]

ProcessMetrics::ProcessMetrics ( const ProcessMetrics & )
delete

Copy constructor.

◆ ProcessMetrics() [3/3]

ProcessMetrics::ProcessMetrics ( ProcessMetrics && )
delete

Move constructor.

◆ ~ProcessMetrics()

ProcessMetrics::~ProcessMetrics ( )

Deconstructs a ProcessMetrics object.

Definition at line 155 of file ProcessMetrics.cpp.

156{
157 _shouldStop.test_and_set();
158 if (_thread && _thread->joinable())
159 {
160 _thread->join();
161 }
162}
std::atomic_flag _shouldStop
Flag to stop monitoring.

Member Function Documentation

◆ countDirectoryEntries()

size_t ProcessMetrics::countDirectoryEntries ( const std::string & path)
staticprivate

Counts the number of entries in a directory.

Parameters
[in]pathThe path of the directory.
Returns
The number of entries in the directory.

Definition at line 12 of file ProcessMetrics.cpp.

13{
14 DIR *dir = nullptr;
15 size_t count = 0;
16 if ((dir = opendir(path.c_str())) != nullptr)
17 {
18 while (readdir(dir) != nullptr) // NOLINT(concurrency-mt-unsafe)
19 {
20 ++count;
21 }
22 closedir(dir);
23 }
24
25 return count;
26}
Here is the caller graph for this function:

◆ getCpuUsage()

double ProcessMetrics::getCpuUsage ( )
protected

Gets the CPU usage of the process.

Returns
The CPU usage as a percentage.

Definition at line 42 of file ProcessMetrics.cpp.

43{
44 if (_oldCpuTime == 0)
45 {
46 _oldCpuTime = times(&_oldCpu);
47 if (_oldCpuTime == static_cast<clock_t>(-1))
48 {
49 _oldCpuTime = 0;
50 }
51 return 0.0;
52 }
53
54 struct tms nowCpu{};
55 auto nowCpuTime = times(&nowCpu);
56
57 const double usage = 100.0 * static_cast<double>(nowCpu.tms_utime - _oldCpu.tms_utime) /
58 static_cast<double>(nowCpuTime - _oldCpuTime);
59
60 std::swap(_oldCpu, nowCpu);
61 std::swap(_oldCpuTime, nowCpuTime);
62
63 return usage;
64}
clock_t _oldCpuTime
Variable to store the old CPU time.
Here is the caller graph for this function:

◆ getDiskIO()

std::pair< size_t, size_t > ProcessMetrics::getDiskIO ( )
protected

Gets the disk read and write bytes of the process.

Returns
A pair of the disk read and write bytes.

Definition at line 66 of file ProcessMetrics.cpp.

67{
68 std::string buffer;
69 findFromFile("/proc/self/io", "read_bytes", buffer);
70 size_t readBytes = buffer.empty() ? 0 : std::stoull(buffer);
71
72 buffer.clear();
73 findFromFile("/proc/self/io", "write_bytes", buffer);
74 size_t writeBytes = buffer.empty() ? 0 : std::stoull(buffer);
75
76 std::pair<size_t, size_t> result = {readBytes - _oldReadBytes, writeBytes - _oldWriteBytes};
77
78 std::swap(_oldReadBytes, readBytes);
79 std::swap(_oldWriteBytes, writeBytes);
80
81 return result;
82}
std::vector< std::string > findFromFile(const std::string &filePath, const std::string &pattern, std::string &lastWord)
size_t _oldWriteBytes
Variable to store the old write bytes.
size_t _oldReadBytes
Variable to store the old read bytes.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFileDescriptorCount()

size_t ProcessMetrics::getFileDescriptorCount ( )
staticprotected

Gets the number of file descriptors in the process.

Returns
The number of file descriptors.

Definition at line 86 of file ProcessMetrics.cpp.

86{ return countDirectoryEntries("/proc/self/fd"); }
static size_t countDirectoryEntries(const std::string &path)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getMemoryUsage()

long int ProcessMetrics::getMemoryUsage ( )
staticprotected

Gets the memory usage of the process.

Returns
The memory usage in bytes.

Definition at line 28 of file ProcessMetrics.cpp.

29{
30 struct rusage r_usage{};
31 getrusage(RUSAGE_SELF, &r_usage);
32 return r_usage.ru_maxrss; // NOLINT(cppcoreguidelines-pro-type-union-access)
33}
Here is the caller graph for this function:

◆ getPageFaults()

long int ProcessMetrics::getPageFaults ( )
staticprotected

Gets the number of page faults of the process.

Returns
The number of page faults.

Definition at line 35 of file ProcessMetrics.cpp.

36{
37 struct rusage r_usage{};
38 getrusage(RUSAGE_SELF, &r_usage);
39 return r_usage.ru_majflt; // NOLINT(cppcoreguidelines-pro-type-union-access)
40}
Here is the caller graph for this function:

◆ getThreadCount()

size_t ProcessMetrics::getThreadCount ( )
staticprotected

Gets the number of threads in the process.

Returns
The number of threads.

Definition at line 84 of file ProcessMetrics.cpp.

84{ return countDirectoryEntries("/proc/self/task"); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

ProcessMetrics & ProcessMetrics::operator= ( ProcessMetrics && )
delete

Move assignment operator.

◆ operator=() [2/2]

ProcessMetrics & ProcessMetrics::operator= ( ProcessMetrics )
delete

Copy assignment operator.

◆ threadRunner()

void ProcessMetrics::threadRunner ( )
protectednoexcept

Main thread function

Definition at line 101 of file ProcessMetrics.cpp.

102{
103 while (!_shouldStop._M_i)
104 {
105 try
106 {
107 update();
108 if (_checkFlag)
109 {
110 _checkFlag->test_and_set();
111 }
112 }
113 catch (const std::exception &e)
114 {
115 spdlog::error("Self monitoring failed: {}", e.what());
116 }
117
118 std::this_thread::sleep_for(std::chrono::seconds(SLEEP_INTERVAL_SEC));
119 }
120}
constexpr int SLEEP_INTERVAL_SEC
Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

void ProcessMetrics::update ( )
protected

Updates the metrics values.

Definition at line 88 of file ProcessMetrics.cpp.

89{
90 _pCurrentTime->SetToCurrentTime();
91 _pMemory->Set(static_cast<double>(getMemoryUsage()));
92 _pPageFaults->Set(static_cast<double>(getPageFaults()));
93 _pCpuUsage->Set(getCpuUsage());
94 auto [diskRead, diskWrite] = getDiskIO();
95 _pDiskRead->Set(static_cast<double>(diskRead));
96 _pDiskWrite->Set(static_cast<double>(diskWrite));
97 _pThreadCount->Set(static_cast<double>(getThreadCount()));
98 _pFileDescriptorCount->Set(static_cast<double>(getFileDescriptorCount()));
99}
std::pair< size_t, size_t > getDiskIO()
static long int getMemoryUsage()
static size_t getThreadCount()
static long int getPageFaults()
static size_t getFileDescriptorCount()
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _checkFlag

std::shared_ptr<std::atomic_flag> ProcessMetrics::_checkFlag
private

Runtime check flag.

Definition at line 18 of file ProcessMetrics.hpp.

◆ _oldCpuTime

clock_t ProcessMetrics::_oldCpuTime {0}
private

Variable to store the old CPU time.

Definition at line 33 of file ProcessMetrics.hpp.

33{0};

◆ _oldReadBytes

size_t ProcessMetrics::_oldReadBytes {0}
private

Variable to store the old read bytes.

Definition at line 30 of file ProcessMetrics.hpp.

30{0};

◆ _oldWriteBytes

size_t ProcessMetrics::_oldWriteBytes {0}
private

Variable to store the old write bytes.

Definition at line 31 of file ProcessMetrics.hpp.

31{0};

◆ _pCpuUsage

prometheus::Gauge* ProcessMetrics::_pCpuUsage
private

Pointer to the CPU usage gauge.

Definition at line 24 of file ProcessMetrics.hpp.

◆ _pCurrentTime

prometheus::Gauge* ProcessMetrics::_pCurrentTime
private

Pointer to the current time gauge.

Definition at line 21 of file ProcessMetrics.hpp.

◆ _pDiskRead

prometheus::Gauge* ProcessMetrics::_pDiskRead
private

Pointer to the disk read gauge.

Definition at line 25 of file ProcessMetrics.hpp.

◆ _pDiskWrite

prometheus::Gauge* ProcessMetrics::_pDiskWrite
private

Pointer to the disk write gauge.

Definition at line 26 of file ProcessMetrics.hpp.

◆ _pFileDescriptorCount

prometheus::Gauge* ProcessMetrics::_pFileDescriptorCount
private

Pointer to the file descriptor count gauge.

Definition at line 28 of file ProcessMetrics.hpp.

◆ _pInitTime

prometheus::Gauge* ProcessMetrics::_pInitTime
private

Pointer to initialization time gauge.

Definition at line 20 of file ProcessMetrics.hpp.

◆ _pMemory

prometheus::Gauge* ProcessMetrics::_pMemory
private

Pointer to the memory usage gauge.

Definition at line 22 of file ProcessMetrics.hpp.

◆ _pPageFaults

prometheus::Gauge* ProcessMetrics::_pPageFaults
private

Pointer to the page faults gauge.

Definition at line 23 of file ProcessMetrics.hpp.

◆ _pThreadCount

prometheus::Gauge* ProcessMetrics::_pThreadCount
private

Pointer to the thread count gauge.

Definition at line 27 of file ProcessMetrics.hpp.

◆ _shouldStop

std::atomic_flag ProcessMetrics::_shouldStop {false}
private

Flag to stop monitoring.

Definition at line 16 of file ProcessMetrics.hpp.

16{false};

◆ _thread

std::unique_ptr<std::thread> ProcessMetrics::_thread
private

Thread handler.

Definition at line 17 of file ProcessMetrics.hpp.


The documentation for this class was generated from the following files: