Skip to content

Commit

Permalink
Fixes used RAM value when reading from /proc/meminfo (and fixes units)
Browse files Browse the repository at this point in the history
Details :
* Used RAM value : <KittyKatt/screenFetch#386 (comment)>
* RAM unit : We were also computing Mebibytes, not Megabytes (see upcoming `feature/btrfs` changes for `Disk` entry).
  • Loading branch information
Samuel FORESTIER committed Mar 26, 2020
1 parent 811a023 commit 90975d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
13 changes: 8 additions & 5 deletions archey/entries/ram.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ def __init__(self):
}

total = ram['MemTotal']
# Here, let's imitate the `free` command behavior
# (https://gitlab.com/procps-ng/procps/blob/master/proc/sysinfo.c#L787)
used = total - (ram['MemFree'] + ram['Cached'] + ram['Buffers'])
# Here, let's imitate Neofetch's behavior.
# See <https://github.com/dylanaraps/neofetch/wiki/Frequently-Asked-Questions>.
used = total + ram['Shmem'] - (
ram['MemFree'] + ram['Cached'] + ram['SReclaimable'] + ram['Buffers'])
# Imitates what `free` does when the obtained value happens to be incorrect.
# See <https://gitlab.com/procps-ng/procps/blob/master/proc/sysinfo.c#L790>.
if used < 0:
used += ram['Cached'] + ram['Buffers']
used = total - ram['MemFree']

# Based on the RAM percentage usage, select the corresponding threshold color.
color_selector = bisect(
[ram_limits['warning'], ram_limits['danger']],
(used / total) * 100
)

self.value = '{0}{1} MB{2} / {3} MB'.format(
self.value = '{0}{1} MiB{2} / {3} MiB'.format(
COLOR_DICT['sensors'][color_selector],
int(used),
COLOR_DICT['clear'],
Expand Down
45 changes: 26 additions & 19 deletions archey/test/test_archey_ram.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,37 @@ def test_free_dash_m_danger(self, _, __):
'archey.entries.ram.open',
mock_open(
read_data="""\
MemTotal: 7590580 kB
MemFree: 1502940 kB
MemAvailable: 3173804 kB
Buffers: 156976 kB
Cached: 2289164 kB
SwapCached: 220 kB
Active: 3426704 kB
Inactive: 2254292 kB
Active(anon): 2221228 kB
Inactive(anon): 1725868 kB
Active(file): 1205476 kB
Inactive(file): 528424 kB
Unevictable: 32 kB
Mlocked: 32 kB
SwapTotal: 7790588 kB
SwapFree: 7785396 kB
Dirty: 200 kB
"""), # Some content have been truncated (because the following is useless)
MemTotal: 7581000 kB
MemFree: 716668 kB
MemAvailable: 3632244 kB
Buffers: 478524 kB
Cached: 2807032 kB
SwapCached: 67092 kB
Active: 3947284 kB
Inactive: 2447708 kB
Active(anon): 2268724 kB
Inactive(anon): 1106220 kB
Active(file): 1678560 kB
Inactive(file): 1341488 kB
Unevictable: 128 kB
Mlocked: 128 kB
SwapTotal: 7811068 kB
SwapFree: 7277708 kB
Dirty: 144 kB
Writeback: 0 kB
AnonPages: 3067204 kB
Mapped: 852272 kB
Shmem: 451056 kB
Slab: 314100 kB
SReclaimable: 200792 kB
SUnreclaim: 113308 kB
"""), # Some lines have been ignored as they are useless for computations.
create=True
)
def test_proc_meminfo(self, _, __):
"""Test `/proc/meminfo` parsing (when `free` is not available)"""
ram = RAM().value
self.assertTrue(all(i in ram for i in ['\x1b[0;33m', '3556', '7412']))
self.assertTrue(all(i in ram for i in ['\x1b[0;33m', '3739', '7403']))


if __name__ == '__main__':
Expand Down

0 comments on commit 90975d4

Please sign in to comment.