diff --git a/home/roles/aerospace/default.nix b/home/roles/aerospace/default.nix index 7a7a1eb..486158d 100644 --- a/home/roles/aerospace/default.nix +++ b/home/roles/aerospace/default.nix @@ -599,21 +599,28 @@ in }; # SketchyBar memory monitoring plugin + # Shows actual memory pressure (excludes file cache/inactive pages) home.file.".config/sketchybar/plugins/memory.sh" = mkIf cfg.sketchybar.enable { executable = true; text = '' #!/bin/bash - MEMORY_STATS=$(vm_stat) - PAGES_FREE=$(echo "$MEMORY_STATS" | grep "Pages free" | awk '{print $3}' | tr -d '.') - PAGES_ACTIVE=$(echo "$MEMORY_STATS" | grep "Pages active" | awk '{print $3}' | tr -d '.') - PAGES_INACTIVE=$(echo "$MEMORY_STATS" | grep "Pages inactive" | awk '{print $3}' | tr -d '.') - PAGES_WIRED=$(echo "$MEMORY_STATS" | grep "Pages wired down" | awk '{print $4}' | tr -d '.') - PAGES_COMPRESSED=$(echo "$MEMORY_STATS" | grep "Pages stored in compressor" | awk '{print $5}' | tr -d '.') - - TOTAL_PAGES=$((PAGES_FREE + PAGES_ACTIVE + PAGES_INACTIVE + PAGES_WIRED + PAGES_COMPRESSED)) - USED_PAGES=$((PAGES_ACTIVE + PAGES_INACTIVE + PAGES_WIRED + PAGES_COMPRESSED)) - MEMORY_PERCENT=$((USED_PAGES * 100 / TOTAL_PAGES)) + # Use awk for all arithmetic to avoid bash integer overflow on large RAM systems + # Memory pressure = Anonymous (app memory) + Wired + Compressor RAM + # - Anonymous pages: app-allocated memory (heap, stack) - matches Activity Monitor's "App Memory" + # - Wired: kernel/system memory that can't be paged out + # - Pages occupied by compressor: actual RAM used by compressor (NOT "stored in compressor") + TOTAL_RAM=$(sysctl -n hw.memsize) + MEMORY_PERCENT=$(vm_stat | awk -v total_ram="$TOTAL_RAM" ' + /page size of/ { page_size = $8 } + /Anonymous pages/ { anon = $3 + 0 } + /Pages wired/ { wired = $4 + 0 } + /Pages occupied by compressor/ { compressor = $5 + 0 } + END { + used = (anon + wired + compressor) * page_size + printf "%.0f", used / total_ram * 100 + } + ') ${pkgs.sketchybar}/bin/sketchybar --set $NAME label="$MEMORY_PERCENT%" '';