From ac0963c608806953ae2fc49de0707be228ea2961 Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Thu, 8 Nov 2018 21:09:56 +0100 Subject: [PATCH 1/3] Use /proc/meminfo for Memory module --- include/modules/memory.hpp | 5 ++++- src/modules/memory.cpp | 35 ++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index 7fd984d0..9e2c6a9e 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include "util/chrono.hpp" #include "ALabel.hpp" @@ -12,6 +12,9 @@ class Memory : public ALabel { Memory(const Json::Value&); auto update() -> void; private: + unsigned long memtotal_; + unsigned long memfree_; + void parseMeminfo(); waybar::util::SleeperThread thread_; }; diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index b2093e8e..108380f5 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -13,13 +13,30 @@ waybar::modules::Memory::Memory(const Json::Value& config) auto waybar::modules::Memory::update() -> void { - struct sysinfo info = {}; - if (sysinfo(&info) == 0) { - auto total = info.totalram * info.mem_unit; - auto freeram = info.freeram * info.mem_unit; - int used_ram_percentage = 100 * (total - freeram) / total; - label_.set_text(fmt::format(format_, used_ram_percentage)); - auto used_ram_gigabytes = (total - freeram) / std::pow(1024, 3); - label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); - } + parseMeminfo(); + int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_; + label_.set_text(fmt::format(format_, used_ram_percentage)); + auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2); + label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); +} + +void waybar::modules::Memory::parseMeminfo() +{ + int memtotal, memfree, memavail, membuffer, memcache; + FILE* info = fopen("/proc/meminfo","r"); + if(fscanf (info, "MemTotal: %d kB MemFree: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &membuffer, &memcache) < 4) { // Old meminfo format + fclose(info); + info = fopen("/proc/meminfo","r"); + if(fscanf(info, "MemTotal: %d kB MemFree: %d kB MemAvailable: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &memavail, &membuffer, &memcache) < 5) { // Current meminfo format + memtotal_ = -1; + memfree_ = -1; + } else { + memtotal_ = memtotal; + memfree_ = memavail; + } + } else { + memtotal_ = memtotal; + memfree_ = memfree + (membuffer + memcache); + } + fclose(info); } From 6b62079d8a8eaf49d2d1f546c9807399a877d963 Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Fri, 9 Nov 2018 00:17:30 +0100 Subject: [PATCH 2/3] rewind stream instead of opening a new one --- src/modules/memory.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 108380f5..97e4ab31 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -25,8 +25,7 @@ void waybar::modules::Memory::parseMeminfo() int memtotal, memfree, memavail, membuffer, memcache; FILE* info = fopen("/proc/meminfo","r"); if(fscanf (info, "MemTotal: %d kB MemFree: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &membuffer, &memcache) < 4) { // Old meminfo format - fclose(info); - info = fopen("/proc/meminfo","r"); + rewind(info); if(fscanf(info, "MemTotal: %d kB MemFree: %d kB MemAvailable: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &memavail, &membuffer, &memcache) < 5) { // Current meminfo format memtotal_ = -1; memfree_ = -1; From 9ea0815dea8cef8186fbb5b6f4079237f26fcb34 Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Fri, 9 Nov 2018 16:24:13 +0100 Subject: [PATCH 3/3] Use ifstream to reaad /proc/meminfo --- include/modules/memory.hpp | 2 +- src/modules/memory.cpp | 60 ++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index 9e2c6a9e..5301e932 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include "util/chrono.hpp" #include "ALabel.hpp" diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 97e4ab31..6de1eb3e 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -14,28 +14,56 @@ waybar::modules::Memory::Memory(const Json::Value& config) auto waybar::modules::Memory::update() -> void { parseMeminfo(); - int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_; - label_.set_text(fmt::format(format_, used_ram_percentage)); - auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2); - label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); + if(memtotal_ > 0 && memfree_ >= 0) { + int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_; + label_.set_text(fmt::format(format_, used_ram_percentage)); + auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2); + label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); + label_.show(); + } else { + label_.hide(); + } } void waybar::modules::Memory::parseMeminfo() { - int memtotal, memfree, memavail, membuffer, memcache; - FILE* info = fopen("/proc/meminfo","r"); - if(fscanf (info, "MemTotal: %d kB MemFree: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &membuffer, &memcache) < 4) { // Old meminfo format - rewind(info); - if(fscanf(info, "MemTotal: %d kB MemFree: %d kB MemAvailable: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &memavail, &membuffer, &memcache) < 5) { // Current meminfo format - memtotal_ = -1; - memfree_ = -1; - } else { - memtotal_ = memtotal; - memfree_ = memavail; + long memtotal = -1, memfree = -1, membuffer = -1, memcache = -1, memavail = -1; + int count = 0; + std::string line; + std::ifstream info("/proc/meminfo"); + if(info.is_open()) { + while(getline(info, line)) { + auto posDelim = line.find(":"); + std::string name = line.substr(0, posDelim); + long value = std::stol(line.substr(posDelim + 1)); + + if(name.compare("MemTotal") == 0) { + memtotal = value; + count++; + } else if(name.compare("MemAvailable") == 0) { + memavail = value; + count++; + } else if(name.compare("MemFree") == 0) { + memfree = value; + count++; + } else if(name.compare("Buffers") == 0) { + membuffer = value; + count++; + } else if(name.compare("Cached") == 0) { + memcache = value; + count++; + } + if (count >= 5 || (count >= 4 && memavail >= -1)) { + info.close(); + } } } else { - memtotal_ = memtotal; + throw std::runtime_error("Can't open /proc/meminfo"); + } + memtotal_ = memtotal; + if(memavail >= 0) { + memfree_ = memavail; + } else { memfree_ = memfree + (membuffer + memcache); } - fclose(info); }