Wednesday, April 29, 2009

CPU and MEM Usage of Processes

In Solaris, if you want to find out the percentage of CPU and memory utilisation of processes, you will use the /usr/ucb/ps aguxwww to get the details. However, sometimes the value from SZ column may be too big and overflow the formatting, as highlighted in red.
$ /usr/ucb/ps aguxwww
USER       PID %CPU %MEM   SZ  RSS TT       S    START  TIME COMMAND
root     23190  0.0  0.0 7608 4080 ?        S 08:27:47  0:00 /usr/lib/ssh/sshd
chihung  23252  0.0  0.0 3464 3040 pts/21   O 08:28:06  0:00 /usr/ucb/ps -aguxwww
root     22658  0.0  0.0    0    0 ?        S   Oct 25  0:00 zsched
root     22676  0.0  0.0 2024 1200 ?        S   Oct 25  0:16 /sbin/init
root     22678  0.0  0.110808 9496 ?        S   Oct 25  1:57 /lib/svc/bin/svc.startd
root     22680  0.0  0.1 9496 8560 ?        S   Oct 25  2:20 /lib/svc/bin/svc.configd
daemon   22720  0.0  0.0 3808 2128 ?        S   Oct 25  0:00 /usr/lib/crypto/kcfd
root     22726  0.0  0.0 4028 2824 ?        S   Oct 25  6:37 /usr/sbin/nscd
root     22782  0.0  0.0 2264 1048 ?        S   Oct 25  0:32 /usr/sbin/cron
daemon   22786  0.0  0.0 2280 1088 ?        S   Oct 25  0:00 /usr/sbin/rpcbind
daemon   22788  0.0  0.0 2336 1624 ?        S   Oct 25  0:00 /usr/lib/nfs/statd
daemon   22793  0.0  0.0 2068 1400 ?        S   Oct 25  0:00 /usr/lib/nfs/lockd
root     22794  0.0  0.0 1696  968 ?        S   Oct 25  0:03 /usr/lib/saf/sac -t 300
root     22798  0.0  0.0 1920 1184 ?        S   Oct 25  0:03 /usr/lib/saf/ttymon
root     22801  0.0  0.0 4972 3436 ?        S   Oct 25  0:00 /usr/lib/inet/inetd start
root     22804  0.0  0.0 1060  680 ?        S   Oct 25  0:15 /usr/lib/utmpd
root     22807  0.0  0.0 1952 1236 console  S   Oct 25  0:00 /usr/lib/saf/ttymon -g -d /dev/console -l console -T vt100 -m ldterm,ttcompat -
root     22870  0.0  0.0 4276  984 ?        S   Oct 25  0:00 /usr/lib/autofs/automountd
root     22871  0.0  0.0 4616 1968 ?        S   Oct 25  0:39 /usr/lib/autofs/automountd
root     22878  0.0  0.0 3352 1500 ?        S   Oct 25  0:00 /usr/lib/ssh/sshd
root     22882  0.0  0.0 3720 1756 ?        S   Oct 25  2:34 /usr/sbin/syslogd
root     22923  0.0  0.0 1700 1100 ?        S   Oct 25  0:00 /usr/sadm/lib/smc/bin/smcboot
root     22926  0.0  0.0 1696  748 ?        S   Oct 25  0:00 /usr/sadm/lib/smc/bin/smcboot
root     22927  0.0  0.0 1696  748 ?        S   Oct 25  0:00 /usr/sadm/lib/smc/bin/smcboot
root     22934  0.0  0.0 1888  716 ?        S   Oct 25  0:00 /usr/lib/locale/ja/wnn/dpkeyserv
root     22941  0.0  0.0 1572  648 ?        S   Oct 25  0:00 /usr/lib/locale/ja/wnn/jserver
root     22942  0.0  0.0 2264 1536 ?        S   Oct 25  0:00 /usr/lib/locale/ja/wnn/jserver_m
root     22946  0.0  0.0 1732  764 ?        S   Oct 25  0:00 /usr/lib/locale/ja/atokserver/atokmngdaemon
root     22951  0.0  0.0 2816 1400 ?        S   Oct 25  0:00 /usr/lib/im/htt -port 9010 -syslog -message_locale C
root     22953  0.0  0.0 4424 2448 ?        S   Oct 25  0:00 htt_server -port 9010 -syslog -message_locale C
root     22961  0.0  0.0 4400 1820 ?        S   Oct 25  0:00 /usr/dt/bin/dtlogin -daemon
root     23033  0.0  0.1 6828 5020 ?        S   Oct 25  1:28 /usr/sfw/sbin/snmpd
chihung  23197  0.0  0.0 7388 2004 ?        S 08:27:47  0:00 /usr/lib/ssh/sshd
chihung  23200  0.0  0.0 2524 1784 pts/21   S 08:27:47  0:00 -bash

Here I will show you couple of 'clean' ways to get processes' %CPU and %MEM.

  • With OpenSolaris, you can browse the source code and modify the formatting of the /usr/ucb/ps ps(1b). You can download the source code from here.
  • You can use the "-o" flag in /usr/bin/ps ps(1) to specify what you want the output to be. Here I get ps to list out only the PID, User, %CPU, %MEM and the command. Handy, isn't. man ps to find out all the format specification, or type in some garbage flags for a quick help (see below)
    $ ps -ef -o 'pid,user,pcpu,pmem,args'
      PID     USER %CPU %MEM COMMAND
    22951     root  0.0  0.0 /usr/lib/im/htt -port 9010 -syslog -message_locale C
    22680     root  0.0  0.1 /lib/svc/bin/svc.configd
    22878     root  0.0  0.0 /usr/lib/ssh/sshd
    22793   daemon  0.0  0.0 /usr/lib/nfs/lockd
    22927     root  0.0  0.0 /usr/sadm/lib/smc/bin/smcboot
    22798     root  0.0  0.0 /usr/lib/saf/ttymon
    22942     root  0.0  0.0 /usr/lib/locale/ja/wnn/jserver_m
    22961     root  0.0  0.0 /usr/dt/bin/dtlogin -daemon
    22678     root  0.0  0.1 /lib/svc/bin/svc.startd
    22953     root  0.0  0.0 htt_server -port 9010 -syslog -message_locale C
    22804     root  0.0  0.0 /usr/lib/utmpd
    22788   daemon  0.0  0.0 /usr/lib/nfs/statd
    22871     root  0.0  0.0 /usr/lib/autofs/automountd
    22720   daemon  0.0  0.0 /usr/lib/crypto/kcfd
    22923     root  0.0  0.0 /usr/sadm/lib/smc/bin/smcboot
    22882     root  0.0  0.0 /usr/sbin/syslogd
    23033     root  0.0  0.1 /usr/sfw/sbin/snmpd
    22676     root  0.0  0.0 /sbin/init
    22926     root  0.0  0.0 /usr/sadm/lib/smc/bin/smcboot
    22934     root  0.0  0.0 /usr/lib/locale/ja/wnn/dpkeyserv
    22794     root  0.0  0.0 /usr/lib/saf/sac -t 300
    22941     root  0.0  0.0 /usr/lib/locale/ja/wnn/jserver
    22807     root  0.0  0.0 /usr/lib/saf/ttymon -g -d /dev/console -l console -T vt100 -m ldterm,ttcompat -
    22782     root  0.0  0.0 /usr/sbin/cron
    22726     root  0.0  0.0 /usr/sbin/nscd
    22786   daemon  0.0  0.0 /usr/sbin/rpcbind
    22870     root  0.0  0.0 /usr/lib/autofs/automountd
    23197  chihung  0.0  0.0 /usr/lib/ssh/sshd
    23190     root  0.0  0.0 /usr/lib/ssh/sshd
    23200  chihung  0.0  0.0 -bash
    23467  chihung  0.0  0.0 ps -ef -o pid,user,pcpu,pmem,args
    22946     root  0.0  0.0 /usr/lib/locale/ja/atokserver/atokmngdaemon
    22801     root  0.0  0.0 /usr/lib/inet/inetd start
    22658     root  0.0  0.0 zsched
    
    $ ps -garbage
    ps: arbage is an invalid non-numeric argument for -g option
    usage: ps [ -aAdeflcjLPyZ ] [ -o format ] [ -t termlist ]
            [ -u userlist ] [ -U userlist ] [ -G grouplist ]
            [ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ]
      'format' is one or more of:
            user ruser group rgroup uid ruid gid rgid pid ppid pgid sid taskid ctid
            pri opri pcpu pmem vsz rss osz nice class time etime stime zone zoneid
            f s c lwp nlwp psr tty addr wchan fname comm args projid project pset
    
    
  • Compile this ps and ptime mashup. This gives you the CPU breakdown in USR and SYS over a certain interval.

With this, you should be able to plot the CPU/MEM utilisation of processes.

Labels:

0 Comments:

Post a Comment

<< Home