Operating System - HP-UX
1833685 Members
3437 Online
110062 Solutions
New Discussion

TIP: list top 5 process utilizing CPU/MEMORY with percentages

 
Richard Fogle
Occasional Contributor

TIP: list top 5 process utilizing CPU/MEMORY with percentages

This assumes you have a user that can access /dev/mem.

CPU:

UNIX95=1 /usr/bin/ps -e -o pcpu,args | /bin/sort -u -r | sed -e 's/\.[0-9][0-9]/&\%/g' | sed -n 2,6p

MEMORY:
UNIX95=1 /usr/bin/ps -e -o vsz,args | /bin/sort -u -r | sed -n 2,6p | /usr/bin/awk 'BEGIN {"echo phys_mem_pages/D | adb64 -k /stand/vmunix /dev/mem | sed -n 2p" | getline output; split(output,totalmem_array," ")}; {printf "%2.1f", ($1 / ((totalmem_array[2] * 4096) / 1024) * 100)} {print "% " $2}'

R
1 REPLY 1
Geoff Wild
Honored Contributor

Re: TIP: list top 5 process utilizing CPU/MEMORY with percentages

Yes - that's a little more eloquent then this script:

# cat memtop
#!/bin/sh
# memtop - show top memory users and pid
# VSZ is in KB
#
echo "VSZ(KB) PID RUSER COMMAND"
UNIX95= ps -e -o 'vsz pid ruser args' |sort -nr|head -30


Then this one for a process:

# cat processmem
#!/bin/sh
# processmem - display memory claimed by a process
#
#
if [ $# -lt 1 -o \( $# -gt 1 -a $# -lt 4 \) ]
then
echo "Usage:"
echo "processmem \"process\""
echo "Example:"
echo "processmem rpc"
exit 1
fi
echo " "

PROCESS=$1

mps=0
#for sz in `ps -elf | grep $PROCESS | grep -v grep | awk '{print $10}'`
for sz in `UNIX95= ps -e -o vsz=Kbytes -o ruser -o pid,args=Command-Line | sort -rnk1 | grep -v Kbytes | grep $PROCESS | awk '{print $1}'`
do
mps=`expr $mps + $sz`
done
#echo `expr $mps \* 4096`
echo "\nMemory claimed by $PROCESS: $mps Kbytes.\n"


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.