New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement memory updating for pre-3.4 kernels #410

merged 1 commit into from Feb 4, 2017
Changes from all commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+22 −13
Diff settings


Just for now

Implement memory updating for pre-3.4 kernels

Since 3.4, `/proc/meminfo` contains a `MemAvailable` field, which polybar uses to determine
`(gb|mb)_free` and `(gb|mb)_used`. This commit adds a fallback for when `MemAvailable` does
not exist, allowing a fairly-accurate approximation on older kernels.

This commit also removes the reliance on the exact order fields appear in `/proc/meminfo`.
  • Loading branch information...
Xyene committed Feb 3, 2017
commit 2ac08324ff1dd03239901a46a2b8174cdd463041
Copy path View file
@@ -33,25 +33,34 @@ namespace modules {
bool memory_module::update() {
unsigned long long kb_total{0ULL};
unsigned long long kb_avail{0ULL};
unsigned long long kb_free{0ULL};

try {
std::ifstream in(PATH_MEMORY_INFO);
std::stringstream buffer;
string str;
std::ifstream meminfo(PATH_MEMORY_INFO);
std::map<std::string, unsigned long long int> parsed;

std::string line;
while (std::getline(meminfo, line)) {
size_t sep_off = line.find(':');
size_t value_off = line.find_first_of("123456789", sep_off);

for (int i = 3; i > 0 && std::getline(in, str); i--) {
size_t off = str.find_first_of("1234567890", str.find(':'));
if (off != string::npos && str.size() > off) {
buffer << std::strtoull(&str[off], nullptr, 10) << std::endl;
if (sep_off == std::string::npos || value_off == std::string::npos) continue;

std::string id = line.substr(0, sep_off);
unsigned long long int value = std::strtoull(&line[value_off], nullptr, 10);
parsed[id] = value;

buffer >> kb_total;
buffer >> kb_free;
buffer >> kb_avail;
kb_total = parsed["MemTotal"];

// newer kernels (3.4+) have an accurate available memory field,
// see
// for details
if (parsed.count("MemAvailable")) {
kb_avail = parsed["MemAvailable"];
} else {
// old kernel; give a best-effort approximation of available memory
kb_avail = parsed["MemFree"] + parsed["Buffers"] + parsed["Cached"] + parsed["SReclaimable"] - parsed["Shmem"];
} catch (const std::exception& err) {
m_log.err("Failed to read memory values (what: %s)", err.what());
ProTip! Use n and p to navigate between commits in a pull request.