3 # sampleproc - sample processes on the CPUs.
4 # Written using DTrace (Solaris 10 3/05).
6 # This program samples which process is on each CPU, at a particular
7 # configurable rate. This can be used as an estimate for which process
8 # is consuming the most CPU time.
10 # $Id: sampleproc 8 2007-08-06 05:55:26Z brendan $
12 # USAGE: sampleproc [hertz] # hit Ctrl-C to end sample
16 # COMMAND Command name
17 # COUNT Number of samples
18 # PERCENT Percent of CPU usage
20 # BASED ON: /usr/demo/dtrace/prof.d
23 # DTrace Guide "profile Provider" chapter (docs.sun.com)
25 # PORTIONS: Copyright (c) 2005 Brendan Gregg.
29 # The contents of this file are subject to the terms of the
30 # Common Development and Distribution License, Version 1.0 only
31 # (the "License"). You may not use this file except in compliance
34 # You can obtain a copy of the license at Docs/cddl1.txt
35 # or http://www.opensolaris.org/os/licensing.
36 # See the License for the specific language governing permissions
37 # and limitations under the License.
41 # 09-Jun-2005 Brendan Gregg Created this.
42 # 09-Jul-2005 " " Last update.
48 USAGE: sampleproc [hertz]
50 sampleproc # defaults to 100 hertz
51 sampleproc 1000 # 1000 hertz
57 if (( $# == 0 )); then
59 elif (( $# == 1 )); then
61 if [[ "$hertz" = *[a-zA-Z]* ]]; then
62 print "ERROR2: $hertz hertz is invalid." >&2
65 if (( hertz > 5000 )); then
66 print "ERROR3: $hertz hertz is too fast (max 5000)." >&2
69 if (( hertz < 1 )); then
70 print "ERROR4: $hertz hertz is too low (min 1)." >&2
79 #pragma D option quiet
83 printf("Sampling at %d hertz... Hit Ctrl-C to end.\n",$1);
84 self->start = timestamp;
89 @Proc[pid, execname] = count();
90 @BigProc[pid, execname] = sum(1000); /* dont ask */
95 this->end = timestamp;
97 printf("%5s %-20s %10s\n", "PID", "CMD", "COUNT");
98 printa("%5d %-20s %10@d\n", @Proc);
101 ((`ncpus_online * $1 * (this->end - self->start))/100000000));
102 printf("\n%5s %-20s %10s\n", "PID", "CMD", "PERCENT");
103 printa("%5d %-20s %10@d\n", @BigProc);