3 # cpudists - print CPU time distributions by Kernel/Idle/Processes.
4 # Written using DTrace (Solaris 10 3/05).
6 # $Id: cpudists 3 2007-08-01 10:50:08Z brendan $
8 # USAGE: cpudists [-ahV] [-t top] [interval [count]]
10 # -a # print all processes
11 # -V # don't print timestamps
12 # -t num # print top num only
14 # cpudists 1 # print every 1 second
15 # cpudists -a 10 # print all processes every 10 secs
19 # value The following or the process name,
20 # IDLE Idle time - CPU running idle thread
21 # KERNEL Kernel time - Kernel servicing interrupts, ...
22 # PROCESS Process time - PIDs running on the system
23 # count Number of occurances at least this duration (ns)
26 # * This takes into account multiple CPU servers, the total
27 # seconds consumed will be a multiple of the CPU count and interval.
31 # COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
35 # The contents of this file are subject to the terms of the
36 # Common Development and Distribution License, Version 1.0 only
37 # (the "License"). You may not use this file except in compliance
40 # You can obtain a copy of the license at Docs/cddl1.txt
41 # or http://www.opensolaris.org/os/licensing.
42 # See the License for the specific language governing permissions
43 # and limitations under the License.
47 # Author: Brendan Gregg [Sydney, Australia]
49 # 27-Apr-2005 Brendan Gregg Created this.
50 # 22-Sep-2005 " " Fixed key corruption bug.
51 # 22-Sep-2005 " " Last update.
55 ##############################
56 # --- Process Arguments ---
58 opt_all=0; opt_time=1; opt_top=0; top=0; interval=1; count=1
60 while getopts aht:V name
65 t) opt_top=1; top=$OPTARG ;;
67 USAGE: cpudists [-ahV] [-t top] [interval [count]]
68 cpudists # default output
69 -a # print all processes
70 -V # don't print times
71 -t num # print top num only
76 shift `expr $OPTIND - 1`
78 if [ "$1" -gt 0 ]; then
79 interval=$1; count=-1; shift
81 if [ "$1" -gt 0 ]; then
86 #################################
87 # --- Main Program, DTrace ---
90 #pragma D option quiet
93 * Command line arguments
95 inline int OPT_all = '$opt_all';
96 inline int OPT_time = '$opt_time';
97 inline int OPT_top = '$opt_top';
98 inline int TOP = '$top';
99 inline int INTERVAL = '$interval';
100 inline int COUNTER = '$count';
102 /* Initialise variables */
110 /* Flag this thread as idle */
111 sysinfo:unix:idle_enter:idlethread
116 /* Save kernel time between running threads */
120 this->elapsed = timestamp - cpustart[cpu];
121 @Procs["KERNEL"] = quantize(this->elapsed);
124 /* Save the elapsed time of a thread */
127 profile:::profile-1sec
130 /* determine the name for this thread */
131 program[cpu] = pid == 0 ? idle[cpu] ? "IDLE" : "KERNEL" :
132 OPT_all ? execname : "PROCESS";
135 this->elapsed = timestamp - cpustart[cpu];
136 @Procs[program[cpu]] = quantize(this->elapsed);
137 cpustart[cpu] = timestamp;
140 /* Record the start time of a thread */
145 cpustart[cpu] = timestamp;
155 /secs == 0 && OPT_time/
157 printf("%Y,\n", walltimestamp);
164 OPT_top ? trunc(@Procs, TOP) : 1;
165 printa("%16s %@16d\n", @Procs);
178 /* cleanup for Ctrl-C */