Skip to content

CPU and RAM percentage usage as 2-digit integer padded outputs #3

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scripts/cpu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ get_cpu_color(){
print_cpu_usage() {
local cpu_pused=$(get_cpu_usage_or_collect)
local cpu_color=$(get_cpu_color "$cpu_pused")

local cpu_view="$cpu_view_tmpl"
cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%.1f%%" "$cpu_pused")}"
cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%2.0f%%" "$cpu_pused")}"
cpu_view="${cpu_view//'#{cpu.color}'/$(echo "$cpu_color" | awk '{ print $1 }')}"
cpu_view="${cpu_view//'#{cpu.color2}'/$(echo "$cpu_color" | awk '{ print $2 }')}"
cpu_view="${cpu_view//'#{cpu.color3}'/$(echo "$cpu_color" | awk '{ print $3 }')}"
Expand All @@ -60,7 +60,7 @@ start_cpu_collect_if_required() {
if [ -f "$collect_cpu_pidfile" ] && ps -p "$(cat "$collect_cpu_pidfile")" > /dev/null 2>&1; then
return;
fi

jobs >/dev/null 2>&1
"$CURRENT_DIR/cpu_collect.sh" &>/dev/null &
if [ -n "$(jobs -n)" ]; then
Expand Down
34 changes: 17 additions & 17 deletions scripts/mem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ print_mem() {
local mem_usage
local scale
local size_format

if is_osx; then
mem_usage=$(get_mem_usage_osx)
elif is_linux; then
elif is_linux; then
mem_usage=$(get_mem_usage_linux)
elif is_freebsd; then
mem_usage=$(get_mem_usage_freebsd)
Expand All @@ -53,16 +53,16 @@ print_mem() {
local mem_total=$(echo "$mem_free + $mem_used" | calc)
local mem_pused=$(echo "($mem_used / $mem_total) * 100" | calc)
local mem_pfree=$(echo "($mem_free / $mem_total) * 100" | calc)

# Calculate colors for mem and swap
local mem_color=$(get_mem_color "$mem_pused")

local mem_view="$mem_view_tmpl"
mem_view="${mem_view//'#{mem.used}'/$(printf "$size_format" "$mem_used" "$size_unit")}"
mem_view="${mem_view//'#{mem.pused}'/$(printf "%.0f%%" "$mem_pused")}"
mem_view="${mem_view//'#{mem.pused}'/$(printf "%2.0f%%" "$mem_pused")}"
mem_view="${mem_view//'#{mem.free}'/$(printf "$size_format" "$mem_free" "$size_unit")}"
mem_view="${mem_view//'#{mem.pfree}'/$(printf "%.0f%%" "$mem_pfree")}"
mem_view="${mem_view//'#{mem.total}'/$(printf "$size_format" "$mem_total" "$size_unit")}"
mem_view="${mem_view//'#{mem.pfree}'/$(printf "%2.0f%%" "$mem_pfree")}"
mem_view="${mem_view//'#{mem.total}'/$(printf "$size_format" "$mem_total" "$size_unit")}"
mem_view="${mem_view//'#{mem.color}'/$(echo "$mem_color" | awk '{ print $1 }')}"
mem_view="${mem_view//'#{mem.color2}'/$(echo "$mem_color" | awk '{ print $2 }')}"
mem_view="${mem_view//'#{mem.color3}'/$(echo "$mem_color" | awk '{ print $3 }')}"
Expand All @@ -76,19 +76,19 @@ print_mem() {
# free = free + inactive + speculative + occupied by compressor
# see `vm_stat` command
get_mem_usage_osx(){

local page_size=$(sysctl -nq "vm.pagesize")
vm_stat | awk -v page_size=$page_size -F ':' '
BEGIN { free=0; used=0 }
/Pages active/ ||
/Pages wired/ {

/Pages active/ ||
/Pages wired/ {
gsub(/^[ \t]+|[ \t]+$/, "", $2); used+=$2;
}
/Pages free/ ||
/Pages inactive/ ||
/Pages speculative/ ||
/Pages occupied by compressor/ {
/Pages free/ ||
/Pages inactive/ ||
/Pages speculative/ ||
/Pages occupied by compressor/ {
gsub(/^[ \t]+|[ \t]+$/, "", $2); free+=$2;
}

Expand All @@ -103,7 +103,7 @@ get_mem_usage_freebsd(){

# 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.
# 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

Expand All @@ -112,7 +112,7 @@ get_mem_usage_linux(){
</proc/meminfo awk '
BEGIN { total=0; free=0; }
/MemTotal:/ { total=$2; }

/MemFree:/ { free+=$2; }
/Buffers:/ { free+=$2; }
/Cached:/ { free+=$2; }
Expand Down