Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Enhancing --memory_unit functionality #2225

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

TriMoon
Copy link

@TriMoon TriMoon commented Oct 31, 2022

Description

Improving 0435dcd

Features

Improving dylanaraps@0435dcd
- Implementing dylanaraps#1170 (comment) to avoid awk usage.
- Adding ability to configure precision of output using `mem_precision` which defaults to `2`.
- Added `tib` to accommodate TiB mentioned in dylanaraps#1170 (comment)
added `tib` to description of `memory_unit`.
hykilpikonna added a commit to hykilpikonna/hyfetch that referenced this pull request Oct 31, 2022
…functionality

Upstream PR: dylanaraps/neofetch#2225
Thanks to @TriMoon

Co-authored-by: ©TriMoon™ <TriMoon@users.noreply.github.com>
hykilpikonna added a commit to hykilpikonna/hyfetch that referenced this pull request Oct 31, 2022
…functionality

Upstream PR: dylanaraps/neofetch#2225
Thanks to @TriMoon

Co-authored-by: ©TriMoon™ <TriMoon@users.noreply.github.com>
@hykilpikonna
Copy link

There are a lot of shellcheck warnings...

image

Added `tib` to usage output of `-memory_unit`.
@TriMoon
Copy link
Author

TriMoon commented Oct 31, 2022

There are a lot of shellcheck warnings...

I'm editing online, will check/address those warnings later 😉

@hykilpikonna
Copy link

I'm editing online, will check/address those warnings later 😉

I just fixed it for you:

    case $memory_unit in
        tib)
            mem_label=TiB
            memory_unit_divider=$((1024 * 1024))
        ;;

        gib)
            mem_label=GiB
            memory_unit_divider=1024
        ;;

        kib)
            mem_used=$((mem_used * 1024))
            mem_total=$((mem_total * 1024))
            mem_label=KiB
        ;;
    esac

    case $memory_unit in
        tib|gib)
            printf -v mem_used "%'.*f" \
                        "${mem_precision:-2}" \
                        "$((mem_used / memory_unit_divider)).$((mem_used % memory_unit_divider))"
            printf -v mem_total "%'.*f" \
                        "${mem_precision:-2}" \
                        "$((mem_total / memory_unit_divider)).$((mem_total % memory_unit_divider))"
        ;;
    esac

But I can't push to your branch though

@TriMoon
Copy link
Author

TriMoon commented Oct 31, 2022

Hahaha that's a way to do it also ofcourse, by creating an extra block....
Will think about it, let me first finish my addition of the precision documentation 😉

I know i should have started with a "Draft" pull request but ahh well heheheh

@hykilpikonna
Copy link

hykilpikonna commented Oct 31, 2022

Looking into this, I actually found another problem, which is that memory bars only work when the memory unit is MiB (the bar function takes in MiB values even though they have been converted).

Can be fixed by keeping track of an original mb value:

image

 Added `--memory_precision` flag, default=`2`.
Fixing shellcheck warnings mentioned in dylanaraps#2225 (comment)
Separating calculations as shown in dylanaraps#2225 (comment)
Making calculations general purpose.
@TriMoon
Copy link
Author

TriMoon commented Oct 31, 2022

@hykilpikonna, i just noticed the current non-patched version does wrong arithmetic in it's attempt to convert KB into MiB,

neofetch/neofetch

Lines 2538 to 2649 in 0435dcd

case $os in
"Linux" | "Windows")
# MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable
# Source: https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716
while IFS=":" read -r a b; do
case $a in
"MemTotal") ((mem_used+=${b/kB})); mem_total="${b/kB}" ;;
"Shmem") ((mem_used+=${b/kB})) ;;
"MemFree" | "Buffers" | "Cached" | "SReclaimable")
mem_used="$((mem_used-=${b/kB}))"
;;
esac
done < /proc/meminfo
mem_used="$((mem_used / 1024))"
mem_total="$((mem_total / 1024))"
;;
"Mac OS X" | "macOS" | "iPhone OS")
mem_total="$(($(sysctl -n hw.memsize) / 1024 / 1024))"
mem_wired="$(vm_stat | awk '/ wired/ { print $4 }')"
mem_active="$(vm_stat | awk '/ active/ { printf $3 }')"
mem_compressed="$(vm_stat | awk '/ occupied/ { printf $5 }')"
mem_compressed="${mem_compressed:-0}"
mem_used="$(((${mem_wired//.} + ${mem_active//.} + ${mem_compressed//.}) * 4 / 1024))"
;;
"BSD" | "MINIX")
# Mem total.
case $kernel_name in
"NetBSD"*) mem_total="$(($(sysctl -n hw.physmem64) / 1024 / 1024))" ;;
*) mem_total="$(($(sysctl -n hw.physmem) / 1024 / 1024))" ;;
esac
# Mem free.
case $kernel_name in
"NetBSD"*)
mem_free="$(($(awk -F ':|kB' '/MemFree:/ {printf $2}' /proc/meminfo) / 1024))"
;;
"FreeBSD"* | "DragonFly"*)
hw_pagesize="$(sysctl -n hw.pagesize)"
mem_inactive="$(($(sysctl -n vm.stats.vm.v_inactive_count) * hw_pagesize))"
mem_unused="$(($(sysctl -n vm.stats.vm.v_free_count) * hw_pagesize))"
mem_cache="$(($(sysctl -n vm.stats.vm.v_cache_count) * hw_pagesize))"
mem_free="$(((mem_inactive + mem_unused + mem_cache) / 1024 / 1024))"
;;
"MINIX")
mem_free="$(top -d 1 | awk -F ',' '/^Memory:/ {print $2}')"
mem_free="${mem_free/M Free}"
;;
"OpenBSD"*) ;;
*) mem_free="$(($(vmstat | awk 'END {printf $5}') / 1024))" ;;
esac
# Mem used.
case $kernel_name in
"OpenBSD"*)
mem_used="$(vmstat | awk 'END {printf $3}')"
mem_used="${mem_used/M}"
;;
*) mem_used="$((mem_total - mem_free))" ;;
esac
;;
"Solaris" | "AIX")
hw_pagesize="$(pagesize)"
case $os in
"Solaris")
pages_total="$(kstat -p unix:0:system_pages:pagestotal | awk '{print $2}')"
pages_free="$(kstat -p unix:0:system_pages:pagesfree | awk '{print $2}')"
;;
"AIX")
IFS=$'\n'"| " read -d "" -ra mem_stat <<< "$(svmon -G -O unit=page)"
pages_total="${mem_stat[11]}"
pages_free="${mem_stat[16]}"
;;
esac
mem_total="$((pages_total * hw_pagesize / 1024 / 1024))"
mem_free="$((pages_free * hw_pagesize / 1024 / 1024))"
mem_used="$((mem_total - mem_free))"
;;
"Haiku")
mem_total="$(($(sysinfo -mem | awk -F '\\/ |)' '{print $2; exit}') / 1024 / 1024))"
mem_used="$(sysinfo -mem | awk -F '\\/|)' '{print $2; exit}')"
mem_used="$((${mem_used/max} / 1024 / 1024))"
;;
"IRIX")
IFS=$'\n' read -d "" -ra mem_cmd <<< "$(pmem)"
IFS=" " read -ra mem_stat <<< "${mem_cmd[0]}"
mem_total="$((mem_stat[3] / 1024))"
mem_free="$((mem_stat[5] / 1024))"
mem_used="$((mem_total - mem_free))"
;;
"FreeMiNT")
mem="$(awk -F ':|kB' '/MemTotal:|MemFree:/ {printf $2, " "}' /kern/meminfo)"
mem_free="${mem/* }"
mem_total="${mem/$mem_free}"
mem_used="$((mem_total - mem_free))"
mem_total="$((mem_total / 1024))"
mem_used="$((mem_used / 1024))"
;;
esac

It does integer calculation instead of floating point...
fe. 1000 KB should convert to 0.9765625 MiB, but current calculation does $((1000 / 1024)) = 0 in multiple places in above function.

I already know i will use the same kind of printf coding i used in here to do the necessary floating-point calculations in bash, probably will make it a function...

So I'm going to first correct that before going further, as it will change more stuff.
(in due time when i get more time again)

@TriMoon
Copy link
Author

TriMoon commented Oct 31, 2022

Looking into this, I actually found another problem, which is that memory bars only work when the memory unit is MiB (the bar function takes in MiB values even though they have been converted).

Can be fixed by keeping track of an original mb value:

That won't be needed when we keep the original values in Bytes so we can display in more units as MiB-only like the current code does...

@TriMoon
Copy link
Author

TriMoon commented Oct 31, 2022

@hykilpikonna But I can't push to your branch though

Invited as collaborator 😉

@hykilpikonna
Copy link

Invited as collaborator 😉

Thanks! I just pushed my commit fixing the memory progress bar issue

So I'm going to first correct that before going further, as it will change more stuff. (in due time when i get more time again)

Yea, I think creating a function for floating-point calculations would be nice, and we can keep the raw value in bytes or kib to reduce the number of calculations and conversions. Let me know when it's ready to merge!

@TriMoon
Copy link
Author

TriMoon commented Nov 3, 2022

@hykilpikonna Let me know when it's ready to merge!

These changes, with your fix could be merged first, because the floating point calculation functionality would be something completely different and extra to this MR 😉
Will create a new one especially for that in due time.

hykilpikonna added a commit to hykilpikonna/hyfetch that referenced this pull request Nov 4, 2022
…functionality

Upstream PR: dylanaraps/neofetch#2225
Thanks to @TriMoon

Co-authored-by: ©TriMoon™ <TriMoon@users.noreply.github.com>
@hykilpikonna
Copy link

Okay, just merged into hyfetch!

HyFetch is a fork of neofetch with LGBTQ pride flags, but the repo also maintains an updated version of the original neofetch, addressing many pull requests that are not merged in the original repo.

Read the "Running Updated Original Neofetch" section for more info!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants