summaryrefslogtreecommitdiffstats
path: root/bin/procmem
blob: 3a7ce76dcbdadc938be5da2efc671ce3d2461b88 (plain) (blame)
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
#!/bin/bash

if [ "$1" = "--help" -o "$1" = "-h" ]; then
    echo "Syntax: `basename $0` [pid]..."
    echo "If no pids are specified, all processes are listed"
    exit 1
fi

cd /proc 2> /dev/null || { echo "No /proc directory."; exit 1; }


get_cmdline() {
    local cmd
    cmd="`tr '\000' ' ' < $1/cmdline`"
    test -z "$cmd" && cmd="[`tr -d '()' < $1/stat | cut -d' ' -f2`]"
    echo "$cmd"
}

get_rss() {
    grep "^VmRSS:" $1/status | awk '{ print $2 }'
}


# Determine the width of the stdout tty (if it is a tty) for clipping
width=`stty -F $$/fd/1 size 2> /dev/null | awk '{ print $2 }'`

clip_line() {
    test -n "$width" && expr substr "$1" 1 "$width" || echo "$1"
}


test -z "$1" && set -- `echo [0-9]* | tr " " "\n" | sort -n`

header=$(printf "%5s %6s %6s %6s %6s %6s  %s\n" \
PID VSZ DEVMAP LIBMAP ALLOC RSS CMDLINE)

clip_line "$header"

for pid; do
    totalmap=0
    devmap=0
    libmap=0
    alloc=0

    test -f $pid/maps || continue

    { while read -u 99 range _ _ _ _ lib; do
        size=$(( -(0x${range/-/-0x}) ))
        let totalmap+=size
        case $lib in
            /dev/*)
            let devmap+=size
            ;;
            "")
            let alloc+=size
            ;;
            *)
            let libmap+=size
        esac
    done; } 99< $pid/maps  # '99<' is a workaround for a stupid bash bug

    output=$(printf "%5d %6d %6d %6d %6d %6d  %s\n" \
    $pid $((totalmap/1024)) $((devmap/1024)) $((libmap/1024)) \
    $((alloc/1024)) "`get_rss $pid`" "`get_cmdline $pid`")

    clip_line "$output"
done