Merge pull request #547 from nobounce/collect-loadavg-function

Improve load average retrieval on linux and prefer `emplace_back`
This commit is contained in:
Jakob P. Liljenberg 2023-07-16 15:59:57 +02:00 committed by GitHub
commit 4631508ac4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 22 deletions

View file

@ -112,7 +112,7 @@ namespace Cpu {
vector<deque<long long>> core_percent;
vector<deque<long long>> temp;
long long temp_max = 0;
array<float, 3> load_avg;
array<double, 3> load_avg;
};
//* Collect cpu stats and temperatures

View file

@ -392,14 +392,10 @@ namespace Cpu {
return current_cpu;
auto &cpu = current_cpu;
double avg[3];
if (getloadavg(avg, sizeof(avg)) < 0) {
if (getloadavg(cpu.load_avg.data(), cpu.load_avg.size()) < 0) {
Logger::error("failed to get load averages");
}
cpu.load_avg = { (float)avg[0], (float)avg[1], (float)avg[2]};
vector<array<long, CPUSTATES>> cpu_time(Shared::coreCount);
size_t size = sizeof(long) * CPUSTATES * Shared::coreCount;
if (sysctlbyname("kern.cp_times", &cpu_time[0], &size, NULL, 0) == -1) {

View file

@ -16,6 +16,7 @@ indent = tab
tab-size = 4
*/
#include <cstdlib>
#include <robin_hood.h>
#include <fstream>
#include <ranges>
@ -672,16 +673,13 @@ namespace Cpu {
if (Config::getB("show_cpu_freq"))
cpuHz = get_cpuHz();
if (getloadavg(cpu.load_avg.data(), cpu.load_avg.size()) < 0) {
Logger::error("failed to get load averages");
}
ifstream cread;
try {
//? Get cpu load averages from /proc/loadavg
cread.open(Shared::procPath / "loadavg");
if (cread.good()) {
cread >> cpu.load_avg[0] >> cpu.load_avg[1] >> cpu.load_avg[2];
}
cread.close();
//? Get cpu total times for all cores from /proc/stat
string cpu_name;
cread.open(Shared::procPath / "stat");
@ -696,7 +694,7 @@ namespace Cpu {
while (cmp_less(cpu.core_percent.size(), i)) {
core_old_totals.push_back(0);
core_old_idles.push_back(0);
cpu.core_percent.push_back({});
cpu.core_percent.emplace_back();
}
cpu.core_percent.at(i-1).push_back(0);
}
@ -714,7 +712,7 @@ namespace Cpu {
while (cmp_less(cpu.core_percent.size(), i)) {
core_old_totals.push_back(0);
core_old_idles.push_back(0);
cpu.core_percent.push_back({});
cpu.core_percent.emplace_back();
}
cpu.core_percent[i-1].push_back(0);
if (cpu.core_percent.at(i-1).size() > 40) cpu.core_percent.at(i-1).pop_front();
@ -730,7 +728,7 @@ namespace Cpu {
times.push_back(val);
}
cread.clear();
if (times.size() < 4) throw std::runtime_error("Malformatted /proc/stat");
if (times.size() < 4) throw std::runtime_error("Malformed /proc/stat");
//? Subtract fields 8-9 and any future unknown fields
const long long totals = max(0ll, total_sum - (times.size() > 8 ? std::accumulate(times.begin() + 8, times.end(), 0ll) : 0));
@ -769,7 +767,7 @@ namespace Cpu {
while (cmp_less(cpu.core_percent.size(), i)) {
core_old_totals.push_back(0);
core_old_idles.push_back(0);
cpu.core_percent.push_back({});
cpu.core_percent.emplace_back();
}
const long long calc_totals = max(0ll, totals - core_old_totals.at(i-1));
const long long calc_idles = max(0ll, idles - core_old_idles.at(i-1));

View file

@ -447,14 +447,10 @@ namespace Cpu {
return current_cpu;
auto &cpu = current_cpu;
double avg[3];
if (getloadavg(avg, sizeof(avg)) < 0) {
if (getloadavg(cpu.load_avg.data(), cpu.load_avg.size()) < 0) {
Logger::error("failed to get load averages");
}
cpu.load_avg = { (float)avg[0], (float)avg[1], (float)avg[2]};
natural_t cpu_count;
natural_t i;
kern_return_t error;