]> Git — Sourcephile - sourcephile-nix.git/blob - pkgs/tools/system/swaplist/default.nix
mermet: fix dovecot and postfix
[sourcephile-nix.git] / pkgs / tools / system / swaplist / default.nix
1 { coreutils
2 , stdenv
3 , writeShellScriptBin
4 }:
5 let PATH = stdenv.lib.concatStringsSep ":" [ "${coreutils}/bin" ];
6 in
7 writeShellScriptBin "swaplist" ''
8 # SYNTAX:
9 # DESCRIPTION: print sorted swap usage of processes using it
10 export PATH=${PATH}
11 lastpid=
12 swap=0
13 sudo grep -H '^Swap:' /proc/*/smaps 2>/dev/null |
14 while IFS=: read -r file x size x
15 do
16 pid=''${file#/proc/}
17 pid=''${pid%/smaps}
18 size=''${size% kB}
19 size=''${size##* }
20 if test "$pid" = "$lastpid"
21 then swap=$(( swap + size ))
22 else
23 if test "$swap" -gt 0
24 then printf "%u pid=%u cmd=%s\n" "$swap" "$lastpid" "$(tr '\000' ' ' </proc/"$lastpid"/cmdline)"
25 fi
26 if test "$pid" = self
27 then break
28 else
29 lastpid=$pid
30 swap=$size
31 fi
32 fi
33 done |
34 sort -nk1,1
35 ''