i was about to say that i'd find it useful if the atime of /proc/n
reflected the last run time of a process, to make it easier to find
cpu hogs, but russ's point about ls -lt /proc's uselessness is bang
on.

so, by way of a little script to help with the above problem, which
bugs me every so often, here's "hogs", a poor man's replacement for
unix's "top" - it's like ps except that it sorts the processes by the
amount of time they've used over the last t (default 1) seconds.

it displays percentage of time used over the sample time (this might
be >100% with multiple cpus), and combines user, syscall and child
time into the second time column (to make it easier to find processes
that spawn lots of transient cpu-eating processes but don't do much
themselves); otherwise the output format is the same as for ps(1).

YMMV - i imagine others have similar scripts.

cat >$home/bin/rc/hogs <<'EOF'
#!/bin/rc
# usage: hogs [sampletime]
t=1
if(! ~ $#* 0){
        t = $1
}
{
        cd /proc
        for(i in [0-9]*){
                cat $i/status && echo $i
        }
        echo
        sleep $t
        for(i in [0-9]*){
                cat $i/status && echo $i
        }
} | awk '
        phase == 0 && /./ {
                t0[$13] = $4+$5+$7+$8;
                rt0[$13] = $6
                next
        }
        phase == 0 && /^$/ {
                phase = 1;
                next
        }
        phase != 0 {
                t1[$13] = $4+$5+$7+$8
                rt1[$13] = $6
                line[$13] = $0
        }
        END {
                for(i in line){
                        n = split(line[i], p, " ");
                        dt = ((t1[i] - t0[i] + 0.0) / (rt1[i] - rt0[i])) * 100
                        utime = (p[4]+p[4]+p[7]+p[8]) / 1000;
                        size = p[9];
                        if(dt >= 1 || utime >= 1){
                                printf("%-10s %8s %3d%% %4d.%.2d %7dK %-8.8s 
%s\n",
                                        p[2],
                                        i,
                                        dt,
                                        utime/60, utime%60,
                                        size,
                                        p[3],
                                        p[1]);
                        }
                }
        }' |
        sort +2rn -3rn +3rn |
        sed 's/(^[^ ]+ +[0-9]+ +[0-9]+% +[0-9]+)\./\1:/'
EOF

Reply via email to