pstime - a mash-up of ps(1) and ptime(1)
By Tim Cook on Apr 27, 2009
I have done some testing in the past where I needed to know the amount of CPU consumed by a process more accurately than I can get from the standard set of operating system utilities.
Recently I hit the same issue - I wanted to collect CPU consumption of mysqld.
To capture process CPU utilization over an interval on Solaris, about the best I can get is the output from a plain "prstat" command, which might look like:
mashie ) prstat -c -p `pgrep mysqld` 5 2 Please wait... PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP 7141 mysql 278M 208M cpu0 39 0 0:38:13 40% mysqld/45 Total: 1 processes, 45 lwps, load averages: 0.63, 0.33, 0.18 PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP 7141 mysql 278M 208M cpu1 32 0 0:38:18 41% mysqld/45 Total: 1 processes, 45 lwps, load averages: 0.68, 0.34, 0.18
I am after data from the second sample only (still not sure exactly how prstat gets data for the fist sample, which comes out almost instantaneously), so you can guess I will need some sed/perl that is a litte more complicated than I would prefer.
pstime reads PROCFS (i.e.. the virtualized file-system mounted on /proc) and captures CPU utilization figures for processes. It will report the %USR and %SYS either for a specific list of processes, or every process running on the system (i.e., running at both sample points). The start sample time is recorded in high resolution at the time a process' data is captured, and then again after N seconds, where N is the first parameter supplied to pstime.
The default output of pstime is expressed as either a percentage of whole system CPU, or CPU seconds, with four significant digits. Solaris itself records the original figures in nanosecond resolution, although we do not expect today's hardware to be that accurate.
Here is an example:
mashie ) pstime 10 `pgrep sysbench\\|mysqld` UID PID %USR %SYS COMMAND mysql 7141 44.17 3.391 /u/dist/mysql60-debug/bin/mysqld --defaults-file=/et mysql 19870 2.517 2.490 sysbench --test=oltp --oltp-read-only=on --max-time= mysql 19869 0.000 0.000 /bin/sh -p ./run-sysbench