Skip to content

Commit

Permalink
lib: improve usage of vectors in CPUMonitor class
Browse files Browse the repository at this point in the history
Signed-off-by: Cedric CHEDALEUX <[email protected]>
  • Loading branch information
cedric-chedaleux committed Jun 5, 2024
1 parent c988aae commit 8cd9ff8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/util/cpumonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
using namespace std;

uprofile::CpuMonitor::CpuMonitor() :
m_nbCpus(getNumberOfCPUCores())
m_nbCpus(getNumberOfCPUCores()),
m_lastIdleTimes(m_nbCpus, 0),
m_lastTotalTimes(m_nbCpus, 0)
{
m_lastIdleTimes.resize(m_nbCpus, 0);
m_lastTotalTimes.resize(m_nbCpus, 0);
}

uprofile::CpuMonitor::~CpuMonitor()
Expand Down Expand Up @@ -60,7 +60,7 @@ void uprofile::CpuMonitor::extractCpuTimes(const string& cpuInfo, size_t& idleTi

vector<float> uprofile::CpuMonitor::getUsage()
{
vector<float> usages;
vector<float> usages(m_nbCpus, 0);
#if defined(__linux__)
ifstream procStat("/proc/stat");
// /proc/stat dumps the following info:
Expand All @@ -85,7 +85,7 @@ vector<float> uprofile::CpuMonitor::getUsage()

// To compute CPU load, we compute the time the CPU has been idle since the last read.
float cpuLoad = 100.0 * (1.0 - (float)(idleTime - m_lastIdleTimes[cpuIndex]) / (totalTime - m_lastTotalTimes[cpuIndex]));
usages.push_back(cpuLoad);
usages[cpuIndex] = cpuLoad;

// Save the times value for the next read
m_lastIdleTimes[cpuIndex] = idleTime;
Expand Down
3 changes: 1 addition & 2 deletions lib/util/cpumonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ class CpuMonitor
static size_t getNumberOfCPUCores();
static void extractCpuTimes(const string& cpuInfo, size_t& idleTime, size_t& totalTime);

size_t m_nbCpus;
// Store the last idle and total time for each CPU
vector<size_t> m_lastIdleTimes;
vector<size_t> m_lastTotalTimes;

size_t m_nbCpus;
};
}

Expand Down

0 comments on commit 8cd9ff8

Please sign in to comment.