Skip to content

Commit

Permalink
code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 26, 2024
1 parent 8631bda commit ee0b0ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/xlpmg/communicator_api.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @author Luca-Philipp Grumbach
*
* # Description
* Communicator API.
* @brief Communicator API.
*
* This file provides an interface with pre-defined
* messages to enable a correct transmission of
* information between client and server.
Expand Down
55 changes: 28 additions & 27 deletions src/systeminfo/SystemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double
l_line.pop_back(); // remove dot

int l_i = l_line.length() - 1; // last character
// iterate to start of number
while (l_i != 0 && !isspace(l_line[l_i]))
{
--l_i;
Expand All @@ -125,6 +126,7 @@ void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double
l_line.pop_back(); // remove dot

int l_i = l_line.length() - 1; // last character
// iterate to start of number
while (l_i != 0 && !isspace(l_line[l_i]))
{
--l_i;
Expand All @@ -137,7 +139,7 @@ void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double
#endif
}

void tsunami_lab::systeminfo::SystemInfo::readCPUData(std::vector<CPUData> &data)
void tsunami_lab::systeminfo::SystemInfo::readCPUData(std::vector<CPUData> &o_data)
{
std::ifstream file("/proc/stat");
std::string line;
Expand All @@ -152,45 +154,44 @@ void tsunami_lab::systeminfo::SystemInfo::readCPUData(std::vector<CPUData> &data
{
CPUData cpu;
ss >> cpu.user >> cpu.nice >> cpu.system >> cpu.idle >> cpu.iowait >> cpu.irq >> cpu.softirq >> cpu.steal >> cpu.guest >> cpu.guest_nice;
data.push_back(cpu);
o_data.push_back(cpu);
}
}
}

std::vector<float> tsunami_lab::systeminfo::SystemInfo::getCPUUsage()
{
std::vector<float> cpuUsage;
std::vector<float> l_cpuUsage;
#ifdef __linux__
if (m_firstCPURead)
{
m_firstCPURead = false;
readCPUData(lastData);
return std::vector<float>(lastData.size(), 0.0);
readCPUData(m_lastData);
return std::vector<float>(m_lastData.size(), 0.0);
}

std::vector<CPUData> currentData;
readCPUData(currentData);
std::vector<CPUData> l_currentData;
readCPUData(l_currentData);

for (size_t i = 0; i < currentData.size(); ++i)
for (size_t i = 0; i < l_currentData.size(); ++i)
{
unsigned long long totalDelta = (currentData[i].user + currentData[i].nice +
currentData[i].system + currentData[i].idle +
currentData[i].iowait + currentData[i].irq +
currentData[i].softirq + currentData[i].steal +
currentData[i].guest + currentData[i].guest_nice) -
(lastData[i].user + lastData[i].nice +
lastData[i].system + lastData[i].idle +
lastData[i].iowait + lastData[i].irq +
lastData[i].softirq + lastData[i].steal +
lastData[i].guest + lastData[i].guest_nice);

unsigned long long idleDelta = (currentData[i].idle + currentData[i].iowait) -
(lastData[i].idle + lastData[i].iowait);

double coreUsage = 100.0 * (1.0 - (static_cast<float>(idleDelta) / totalDelta));
cpuUsage.push_back(coreUsage);
unsigned long long l_totalDelta = (l_currentData[i].user + l_currentData[i].nice +
l_currentData[i].system + l_currentData[i].idle +
l_currentData[i].iowait + l_currentData[i].irq +
l_currentData[i].softirq + l_currentData[i].steal +
l_currentData[i].guest + l_currentData[i].guest_nice) -
(m_lastData[i].user + m_lastData[i].nice +
m_lastData[i].system + m_lastData[i].idle +
m_lastData[i].iowait + m_lastData[i].irq +
m_lastData[i].softirq + m_lastData[i].steal +
m_lastData[i].guest + m_lastData[i].guest_nice);

unsigned long long l_idleDelta = (l_currentData[i].idle + l_currentData[i].iowait) -
(m_lastData[i].idle + m_lastData[i].iowait);

double l_coreUsage = 100.0 * (1.0 - (static_cast<float>(l_idleDelta) / l_totalDelta));
cpuUsage.push_back(l_coreUsage);
}
return cpuUsage;
#elif __APPLE__ || __MACH__
std::array<char, 128> l_buffer;
std::unique_ptr<FILE, decltype(&pclose)> l_pipe(popen("top -l 2 | grep -E '^CPU' | tail -1 | LC_NUMERIC='C' awk '{s=$3+$5} END {print s}' &", "r"), pclose);
Expand All @@ -200,8 +201,8 @@ std::vector<float> cpuUsage;
}
while (fgets(l_buffer.data(), l_buffer.size(), l_pipe.get()) != nullptr)
{
cpuUsage.push_back(std::stof(l_buffer.data()));
l_cpuUsage.push_back(std::stof(l_buffer.data()));
}
#endif
return cpuUsage;
return l_cpuUsage;
}
40 changes: 35 additions & 5 deletions src/systeminfo/SystemInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Richard Hofmann
*
* # Description
* Provides info on the system usage.
* Provides info on the system such as CPU and RAM usage.
**/
#ifndef TSUNAMI_LAB_SYSTEMINFO_SYSTEMINFO_H
#define TSUNAMI_LAB_SYSTEMINFO_SYSTEMINFO_H
Expand All @@ -21,8 +21,12 @@ namespace tsunami_lab
class tsunami_lab::systeminfo::SystemInfo
{
private:
double m_bytesToGB = 0.00000000093132257; // 1/(1024*1024*1024) Converts bytes to GB
bool m_firstCPURead = true;

//! Conversion constant from bytes to gigabytes (1/1000^3)
double m_bytesToGB = 0.000000001;

//! To check whether its the first time of reading cpu usage data; Linux only
[[maybe_unused]] bool m_firstCPURead = true;

struct CPUData
{
Expand All @@ -38,14 +42,40 @@ class tsunami_lab::systeminfo::SystemInfo
unsigned long long guest_nice;
};

std::vector<CPUData> lastData;
//! Vector for last read cpu usage data; Linux only
[[maybe_unused]] std::vector<CPUData> m_lastData;

void readCPUData(std::vector<CPUData>& data);
/**
* Reads CPU usage data from /proc/stat
*
* @param o_data output vector for collected data.
*/
void readCPUData(std::vector<CPUData>& o_data);

public:
/**
* Contructor.
*/
SystemInfo();

/**
* Gets RAM usage data.
*
* @param o_totalRAM output pointer for total RAM value.
* @param o_usedRAM output pointer for used RAM value.
*/
void getRAMUsage(double &o_totalRAM, double &o_usedRAM);

/**
* @brief Gets CPU usage data.
*
* This function collects CPU usage data. On MacOS it will return one
* value as the overall CPU usage over all cores. This value is read from
* the "top" command. On Linux, /proc/stat is read and the
* values are calculated for each core.
*
* @return Vector with CPU usage in percent for each core.
*/
std::vector<float> getCPUUsage();
};
#endif

0 comments on commit ee0b0ba

Please sign in to comment.