Waybar/src/modules/memory/linux.cpp

44 lines
1013 B
C++
Raw Normal View History

2019-08-09 10:40:33 +00:00
#include "modules/memory.hpp"
2021-10-28 17:10:46 +00:00
static unsigned zfsArcSize() {
std::ifstream zfs_arc_stats{"/proc/spl/kstat/zfs/arcstats"};
if (zfs_arc_stats.is_open()) {
2022-04-06 06:37:19 +00:00
std::string name;
std::string type;
2021-10-28 17:10:46 +00:00
unsigned long data{0};
std::string line;
while (std::getline(zfs_arc_stats, line)) {
std::stringstream(line) >> name >> type >> data;
if (name == "size") {
return data / 1024; // convert to kB
}
}
}
return 0;
}
2019-08-09 10:40:33 +00:00
void waybar::modules::Memory::parseMeminfo() {
const std::string data_dir_ = "/proc/meminfo";
2022-04-06 06:37:19 +00:00
std::ifstream info(data_dir_);
2019-08-09 10:40:33 +00:00
if (!info.is_open()) {
throw std::runtime_error("Can't open " + data_dir_);
}
std::string line;
while (getline(info, line)) {
auto posDelim = line.find(':');
if (posDelim == std::string::npos) {
continue;
}
std::string name = line.substr(0, posDelim);
2022-04-06 06:37:19 +00:00
int64_t value = std::stol(line.substr(posDelim + 1));
2019-08-09 10:40:33 +00:00
meminfo_[name] = value;
}
2021-10-28 17:10:46 +00:00
meminfo_["zfs_size"] = zfsArcSize();
2019-08-09 10:40:33 +00:00
}