From ee0b0ba2752ff46351d55e67b620309e5bd6d89d Mon Sep 17 00:00:00 2001 From: Luca G Date: Fri, 26 Jan 2024 11:28:32 +0100 Subject: [PATCH] code documentation --- lib/xlpmg/communicator_api.h | 4 +-- src/systeminfo/SystemInfo.cpp | 55 ++++++++++++++++++----------------- src/systeminfo/SystemInfo.h | 40 +++++++++++++++++++++---- 3 files changed, 65 insertions(+), 34 deletions(-) diff --git a/lib/xlpmg/communicator_api.h b/lib/xlpmg/communicator_api.h index 2e5d3763..a0c9e81d 100644 --- a/lib/xlpmg/communicator_api.h +++ b/lib/xlpmg/communicator_api.h @@ -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. diff --git a/src/systeminfo/SystemInfo.cpp b/src/systeminfo/SystemInfo.cpp index 696ed790..58c05135 100644 --- a/src/systeminfo/SystemInfo.cpp +++ b/src/systeminfo/SystemInfo.cpp @@ -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; @@ -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; @@ -137,7 +139,7 @@ void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double #endif } -void tsunami_lab::systeminfo::SystemInfo::readCPUData(std::vector &data) +void tsunami_lab::systeminfo::SystemInfo::readCPUData(std::vector &o_data) { std::ifstream file("/proc/stat"); std::string line; @@ -152,45 +154,44 @@ void tsunami_lab::systeminfo::SystemInfo::readCPUData(std::vector &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 tsunami_lab::systeminfo::SystemInfo::getCPUUsage() { -std::vector cpuUsage; + std::vector l_cpuUsage; #ifdef __linux__ if (m_firstCPURead) { m_firstCPURead = false; - readCPUData(lastData); - return std::vector(lastData.size(), 0.0); + readCPUData(m_lastData); + return std::vector(m_lastData.size(), 0.0); } - std::vector currentData; - readCPUData(currentData); + std::vector 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(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(l_idleDelta) / l_totalDelta)); + cpuUsage.push_back(l_coreUsage); } - return cpuUsage; #elif __APPLE__ || __MACH__ std::array l_buffer; std::unique_ptr l_pipe(popen("top -l 2 | grep -E '^CPU' | tail -1 | LC_NUMERIC='C' awk '{s=$3+$5} END {print s}' &", "r"), pclose); @@ -200,8 +201,8 @@ std::vector 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; } \ No newline at end of file diff --git a/src/systeminfo/SystemInfo.h b/src/systeminfo/SystemInfo.h index ba96cb35..f2d5af10 100644 --- a/src/systeminfo/SystemInfo.h +++ b/src/systeminfo/SystemInfo.h @@ -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 @@ -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 { @@ -38,14 +42,40 @@ class tsunami_lab::systeminfo::SystemInfo unsigned long long guest_nice; }; - std::vector lastData; + //! Vector for last read cpu usage data; Linux only + [[maybe_unused]] std::vector m_lastData; - void readCPUData(std::vector& data); + /** + * Reads CPU usage data from /proc/stat + * + * @param o_data output vector for collected data. + */ + void readCPUData(std::vector& 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 getCPUUsage(); }; #endif \ No newline at end of file