|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +# based on https://github.com/thewtex/tmux-mem-cpu-load |
| 4 | +# shellcheck disable=SC2001 |
| 5 | +__tp_mem_used_info() { |
| 6 | + if tp_shell_is_macos; then |
| 7 | + local stats |
| 8 | + local bytes_per_page |
| 9 | + local free_pages |
| 10 | + local external_pages |
| 11 | + local mem_total_bytes |
| 12 | + local mem_used_bytes |
| 13 | + |
| 14 | + stats=$(vm_stat | tr '\n' ' ') |
| 15 | + bytes_per_page=$(echo "$stats" | sed -e 's/.*page size of \([0-9]*\).*/\1/') |
| 16 | + mem_total_bytes=$(sysctl hw.memsize | sed -e 's/^hw.memsize: \([0-9*]\)/\1/') |
| 17 | + free_pages=$(echo "$stats" | sed -e 's/.*Pages free: *\([0-9]*\).*/\1/') |
| 18 | + external_pages=$(echo "$stats" | sed -e 's/.*File-backed pages: *\([0-9]*\).*/\1/') |
| 19 | + mem_used_bytes=$(echo "$mem_total_bytes - ($free_pages + $external_pages) * $bytes_per_page" | bc -l) |
| 20 | + |
| 21 | + echo "$mem_used_bytes" "$mem_total_bytes" |
| 22 | + |
| 23 | + elif tp_shell_is_linux; then |
| 24 | + local meminfo |
| 25 | + local mem_total |
| 26 | + local mem_total_bytes |
| 27 | + local mem_free |
| 28 | + local shmem |
| 29 | + local buffers |
| 30 | + local cached |
| 31 | + local s_reclaimable |
| 32 | + local mem_used_bytes |
| 33 | + |
| 34 | + meminfo=$(tr '\n' ' ' < /proc/meminfo) |
| 35 | + mem_total=$(echo "$meminfo" | sed -e 's/^MemTotal: *\([0-9]*\).*/\1/') |
| 36 | + mem_total_bytes=$(echo "$mem_total * 1024" | bc -l) |
| 37 | + mem_free=$(echo "$meminfo" | sed -e 's/.* MemFree: *\([0-9]*\).*/\1/') |
| 38 | + shmem=$(echo "$meminfo" | sed -e 's/.* Shmem: *\([0-9]*\).*/\1/') |
| 39 | + buffers=$(echo "$meminfo" | sed -e 's/.* Buffers: *\([0-9]*\).*/\1/') |
| 40 | + cached=$(echo "$meminfo" | sed -e 's/.* Cached: *\([0-9]*\).*/\1/') |
| 41 | + s_reclaimable=$(echo "$meminfo" | sed -e 's/.* SReclaimable: *\([0-9]*\).*/\1/') |
| 42 | + mem_used_bytes=$(echo "($mem_total - $mem_free + $shmem - $buffers - $cached - $s_reclaimable) * 1024" | bc -l) |
| 43 | + |
| 44 | + echo "$mem_used_bytes" "$mem_total_bytes" |
| 45 | + fi |
| 46 | +}; |
| 47 | + |
| 48 | +tp_mem_used_gigabytes() { |
| 49 | + read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info) |
| 50 | + __round "$(echo "$mem_used_bytes / 1073741824" | bc -l)" 2 |
| 51 | +} |
| 52 | + |
| 53 | +tp_mem_used_megabytes() { |
| 54 | + read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info) |
| 55 | + __round "$(echo "$mem_used_bytes / 1048576" | bc -l)" 0 |
| 56 | +} |
| 57 | + |
| 58 | +tp_mem_used_percentage_at_least() { |
| 59 | + local threshold_percentage="$1" |
| 60 | + read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info) |
| 61 | + echo "($mem_used_bytes / $mem_total_bytes) * 100 >= $threshold_percentage" | bc -l |
| 62 | +} |
| 63 | + |
| 64 | +# source https://askubuntu.com/a/179949 |
| 65 | +# Rounds positive numbers up to the number of digits to the right of the decimal point. |
| 66 | +# Example: "__round 1.2345 3" -> "((1000 * 1.2345) + 0.5) / 1000" -> "1.235" |
| 67 | +__round() { |
| 68 | + local number="$1" |
| 69 | + local digits="$2" |
| 70 | + |
| 71 | + env printf "%.${digits}f" "$(echo "scale=${digits};(((10^${digits})*${number})+0.5)/(10^${digits})" | bc)" |
| 72 | +}; |
| 73 | + |
0 commit comments