From 410312a4a8da3a10de8d0d084be22519421b516c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Varko=C4=8Dek?= Date: Mon, 14 Sep 2026 09:09:59 +0200 Subject: [PATCH] Added swaptop function --- .bashrc.d/15_swaptop.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 .bashrc.d/15_swaptop.sh diff --git a/.bashrc.d/15_swaptop.sh b/.bashrc.d/15_swaptop.sh new file mode 100644 index 0000000..92183d0 --- /dev/null +++ b/.bashrc.d/15_swaptop.sh @@ -0,0 +1,60 @@ +# --- enable/disable this file --- +SRC_ENABLED=true + +case "$SRC_ENABLED" in + 1|true|yes|on) unset SRC_ENABLED ;; # enabled — clean up and continue + *) unset SRC_ENABLED; return ;; # disabled — clean up and skip this file +esac +# --------------------------------- + +swaptop() +{ + local limit="${1:-10}" + local width="${2:-200}" + local p pid swap cmd + + { + for p in /proc/[0-9]*; do + [ -r "$p/smaps" ] || continue + + pid="${p##*/}" + + swap=$(awk ' + /^Swap:/ { swap += $2 } + /^SwapPss:/ { pss += $2; have_pss = 1 } + END { + if (have_pss) + print pss + else + print swap + } + ' "$p/smaps" 2>/dev/null) + + [ "${swap:-0}" -gt 0 ] || continue + + cmd=$(tr '\0\t\n' ' ' < "$p/cmdline" 2>/dev/null) + + if [ -z "$cmd" ]; then + cmd="[$(cat "$p/comm" 2>/dev/null)]" + fi + + printf '%d\t%s\t%s\n' "$swap" "$pid" "$cmd" + done + } | + sort -nr -k1,1 | + head -n "$limit" | + awk -F '\t' -v width="$width" ' + BEGIN { + printf "%10s %7s %s\n", "SWAP", "PID", "COMMAND" + printf "%10s %7s %s\n", "----------", "-------", "-------" + } + { + line = sprintf("%9.1fM %7s %s", $1 / 1024, $2, $3) + + if (length(line) > width) + line = substr(line, 1, width - 3) "..." + + print line + } + ' +}