blob: b3a9a3130e71118a5168c326fae15b72c2e3e408 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#!/bin/bash
# order by
by="swap"
# reverse sort
rev="r"
if [ ! -z "$1" ]; then
case "$1" in
-s|--swap|swap)
by="swap"
shift
;;
-m|--memory|mem|memory)
by="memory"
shift
;;
-n|--name|-p|--process|--program|name|proc|process)
by="name"
rev=""
shift
;;
-h|--help|help)
echo "Statistics about memory and swap usage from all running processes."
echo "Usage: $0 [OPTION]"
echo " swap sort by swap usage (default)"
echo " memory sort by memory usage"
echo " name sort by process name"
echo " help print this help message"
exit 0
;;
*)
echo "Unrecognized option \"$1\"" >&2
echo "Try \"-h\" instead."
exit 1
esac
fi
echo "Check memory and swap usage for all programs sorted by $by."
echo "$(tput setaf 1)Attention! The reported memory usage is just the $(tput bold)virtual$(tput sgr0) $(tput setaf 1)usage, not the $(tput bold)real$(tput sgr0) $(tput setaf 1)one."
echo "Read more about this on Wikipedia. $(tput bold)https://en.wikipedia.org/wiki/Virtual_memory$(tput sgr0)"
echo ""
# loop over all running programs
for CMD in $(top -bn1 | tail -n +8 | grep -v $(basename "$0") | awk '{print $12}' | sort | uniq)
do
swap_count=0
mem_count=0
# loop over all pids and check for 'VmSize' and 'VmSwap' state in /proc/$PID/status
for tmp in $( for PID in $(pgrep $CMD 2>/dev/null); do grep -E 'Vm(Size|Swap)' /proc/$PID/status 2>/dev/null | paste - - | awk '{print $2 "|" $5}'; done )
do
mem_count=$(($mem_count+$(echo $tmp | cut -d\| -f1)))
swap_count=$(($swap_count+$(echo $tmp | cut -d\| -f2)))
done
# programs swap or memory use is not null
if [ $swap_count -gt 0 ] || [ $mem_count -gt 0 ]
then
case "$by" in
swap)
echo "$swap_count kB (Swap): $CMD $mem_count kB (Memory)"
;;
memory)
echo "$mem_count kB (Memory): $CMD $swap_count kB (Swap)"
;;
name)
echo "$CMD: $mem_count kB (Memory) $swap_count kB (Swap)"
;;
esac
fi
# sorting and formatting output
done | sort -k1 -t\ -h${rev} | column -t
|