From dc81200eeacfc0e3bbf5a893640a64d1504d9932 Mon Sep 17 00:00:00 2001 From: Alexey Samoshkin Date: Tue, 21 Nov 2017 16:00:49 +0200 Subject: [PATCH 1/2] Implement memory calculation for Linux systems --- scripts/helpers.sh | 5 +++++ scripts/mem.sh | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 75429ab..b9ff115 100755 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -20,6 +20,11 @@ is_osx() { [ $(uname) == "Darwin" ] } +is_linux(){ + [ $(uname -s) == "Linux" ] +} + + command_exists() { local command="$1" type "$command" >/dev/null 2>&1 diff --git a/scripts/mem.sh b/scripts/mem.sh index 899d91b..6951f6d 100755 --- a/scripts/mem.sh +++ b/scripts/mem.sh @@ -44,10 +44,9 @@ print_mem() { local size_format if is_osx; then + mem_usage=$(get_mem_usage_linux) + elif is_linux; then mem_usage=$(get_mem_usage_osx) - else - # TODO: implement memory calculation for linux - mem_usage="TODO" fi # get_mem_usage* function returns values in KiB @@ -87,8 +86,6 @@ print_mem() { # Calculate colors for mem and swap local mem_color=$(get_mem_color "$mem_pused") local swap_color=$(get_swap_color "$swap_pused") - - echo $mem_view_tmpl; local mem_view="$mem_view_tmpl" mem_view="${mem_view//'#{mem.used}'/$(printf "$size_format" "$mem_used" "$size_unit")}" @@ -144,6 +141,37 @@ get_mem_usage_osx(){ printf "%s %s %s" "$free_used" "$swap_free" "$swap_used" } + +# Method #1. Sum up free+buffers+cached, treat it as "available" memory, assuming buff+cache can be reclaimed. Note, that this assumption is not 100% correct, buff+cache most likely cannot be 100% reclaimed, but this is how memory calculation is used to be done on Linux + +# Method #2. If "MemAvailable" is provided by system, use it. This is more correct method, because we're not relying on fragile "free+buffer+cache" equation. + +# See: Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7 - Red Hat Customer Portal - https://access.redhat.com/solutions/406773 + +# See: kernel/git/torvalds/linux.git - /proc/meminfo: provide estimated available memory - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 +get_mem_usage_linux(){ + local mem_free_used=$( Date: Tue, 21 Nov 2017 18:04:02 +0200 Subject: [PATCH 2/2] Fix wrong OSX/Linux condition for memory calculation --- scripts/mem.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/mem.sh b/scripts/mem.sh index 6951f6d..354d284 100755 --- a/scripts/mem.sh +++ b/scripts/mem.sh @@ -44,9 +44,9 @@ print_mem() { local size_format if is_osx; then - mem_usage=$(get_mem_usage_linux) - elif is_linux; then mem_usage=$(get_mem_usage_osx) + elif is_linux; then + mem_usage=$(get_mem_usage_linux) fi # get_mem_usage* function returns values in KiB