Skip to content
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

Add "free memory" to the memory module's label options #2028

Open
jallbrit opened this issue Mar 3, 2020 · 5 comments · May be fixed by #2037
Open

Add "free memory" to the memory module's label options #2028

jallbrit opened this issue Mar 3, 2020 · 5 comments · May be fixed by #2037
Labels
feature good first issue

Comments

@jallbrit
Copy link

@jallbrit jallbrit commented Mar 3, 2020

There are two ways to measure memory usage in Linux:

  • Available memory is being used for something, but is readily available. Linux does this to run programs more efficiently if RAM is not being used.
  • Free memory is not being used for anything. You could call it wasted RAM. It is almost always very low and lower than available memory.

linuxatemyram.com explains this concept a little bit.

Currently, polybar's memory module only allows displaying available memory. For most users, this is all they care about- it is a better representation of how much memory is available.

Free memory is also a metric of interest for some users, including myself.

Current Situation

The memory module currently labels memory as follows:

  • %percentage_free%: percentage of memory that is available memory
  • %percentage_used%: 100 - %percentage_free%

Possible Solution

An option should be made to display this "free memory" via the memory module, accessible in the label via %percentage_free% and %percentage_free_used% like so:

  • %percentage_available_used%: memory owned by processes and unusable
  • %percentage_available%: memory available for use
  • %percentage_free_used%: memory being used (will almost always be very high)
  • %percentage_free%: memory completely unused (will almost always be very low)

This naming scheme could definitely be improved.

This way, I can see how much RAM is being used but is available (100 - available memory), and also see how much RAM is truly being used (100 - free memory).

@Lomadriel Lomadriel added feature good first issue labels Mar 8, 2020
@Lomadriel
Copy link
Member

@Lomadriel Lomadriel commented Mar 8, 2020

If someone want to implement this, you just need to add the tokens in the memory module, the parsing of the memory file is already done:

try {
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);
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;
}
kb_total = parsed["MemTotal"];
kb_swap_total = parsed["SwapTotal"];
kb_swap_free = parsed["SwapFree"];
// newer kernels (3.4+) have an accurate available memory field,
// see https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
// 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());
}
m_perc_memfree = math_util::percentage(kb_avail, kb_total);
m_perc_memused = 100 - m_perc_memfree;
m_perc_swap_free = math_util::percentage(kb_swap_free, kb_swap_total);
m_perc_swap_used = 100 - m_perc_swap_free;

@jallbrit
Copy link
Author

@jallbrit jallbrit commented Mar 9, 2020

I will give this a shot, thanks for the guidance.

@patrick96
Copy link
Member

@patrick96 patrick96 commented Mar 9, 2020

@jallbrit You seem to be mixing free and available in your post, the description in the beginning is correct but starting at Current Situtation you have switched the two. Currently polybar's %*_free% tokens show you the Available memory and you also want to display Free memory.

It is a bit unfortunate that we have already named our tokens mb_free and so on, even though it would have been better to call them mb_avail. This means to not introduce breaking changes, we cannot change what the %*_free% tokens display. If you can come up with good names for the actual free memory tokens without them conflicting with the existing tokens that would be great.

Otherwise the best approach would be to add for all %*_free% memory tokens (except for swap) an %*_avail% token, deprecate the %*_free% tokens and for version 4.0.0 we can introduce a breaking change and make the %*_free% tokens display actual free memory.

@jallbrit
Copy link
Author

@jallbrit jallbrit commented Mar 9, 2020

@patrick96 thank you for pointing this out, I will update the issue. I agree that we should add %*_available% tokens that mimic the current %*_free% tokens, and maybe add self-explanatory tokens called %*_true_free% to represent true free memory, which will eventually become just %*_free% once the deprecated versions are removed. Thoughts on name of these "truly free memory" tokens?

Is it also unfortunate that %*_used% is even necessary, as I don't see a way to mathematically calculate 100 - %*_percentage_*% inside polybar's config without an explicit tag that already has it done.

jallbrit pushed a commit to jallbrit/polybar that referenced this issue Mar 9, 2020
Add labels to memory module that allow users to display both free and
available memory separately. Closes polybar#2028.

Free memory is memory that is truly not being used, while available
memory includes memory that is technically being used but is readily
available for other processes.
@jallbrit jallbrit linked a pull request Mar 9, 2020 that will close this issue
@ktprograms
Copy link

@ktprograms ktprograms commented Jan 22, 2022

I'm not sure if this is the best place to ask this, but is there any option to show used memory (not including available/cache memory)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants