Notes to Self

Alex Sokolsky's Notes on Computers and Programming

Top Consumers Recipes

See performance-monitoring.sh

Note the differences in the way CPU consumption is measured:

Hence, ps gives you historical value, in contrast to the instanteneous value given by top.

Identify CPU usage of a Process

Details -

Note:

By pid:

top -b -n 2 -d 0.2 -p _pid_ | tail -1 | awk '{print $9}'

By name:

top -b -n 2 -d 0.2 -p `pidof -s _pname_` | tail -1 | awk '{print $9}'

If the process has multiple instances:

#!/usr/bin/env bash
#
# Given process name, run top on its pids, sort by the increasing CPU%
#
pname=$1
pids=$(pidof $pname)
p_options=''
# top takes <20 -p options ;-(
p_count=0
for pid in $pids; do
    p_options="$p_options -p $pid"
    p_count=$((p_count+1))
    if [[ "$p_count" -gt 19 ]]; then
        break
    fi
done

top -b -n 2 -d 0.2 $p_options|tail -$p_count|sort -k9

Top 5 Processes by CPU usage

ps -eo pcpu,pid,user,args | sort -k 1 -r | head -n 5

Top 5 Processes by Memory

ps -eo pmem,pid,user,args | sort -k 1 -r | head -n 5

Top 5 Processes by I/O

Assumes iotop.

sudo iotop -o -b -n 5

Top 5 Processes by Network I/O

Assumes iftop.

sudo iftop -P -n -t -s 5