cpu: make getCpuLoad more portable

../include/modules/cpu.hpp:4:10: fatal error: 'sys/sysinfo.h' file not found
 #include <sys/sysinfo.h>
          ^~~~~~~~~~~~~~~
This commit is contained in:
Jan Beich 2019-08-09 09:07:58 +00:00
parent c94ef092ff
commit d5df185ac6
2 changed files with 4 additions and 6 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <fmt/format.h> #include <fmt/format.h>
#include <sys/sysinfo.h> #include <unistd.h>
#include <cstdint> #include <cstdint>
#include <fstream> #include <fstream>
#include <numeric> #include <numeric>

View File

@ -23,11 +23,9 @@ auto waybar::modules::Cpu::update() -> void {
} }
uint16_t waybar::modules::Cpu::getCpuLoad() { uint16_t waybar::modules::Cpu::getCpuLoad() {
struct sysinfo info = {0}; double load[1];
if (sysinfo(&info) == 0) { if (getloadavg(load, 1) != -1) {
float f_load = 1.F / (1U << SI_LOAD_SHIFT); return load[0] * 100 / sysconf(_SC_NPROCESSORS_ONLN);
uint16_t load = info.loads[0] * f_load * 100 / get_nprocs();
return load;
} }
throw std::runtime_error("Can't get Cpu load"); throw std::runtime_error("Can't get Cpu load");
} }