cpudists revision 1.1 1 1.1 christos #!/usr/bin/sh
2 1.1 christos #
3 1.1 christos # cpudists - print CPU time distributions by Kernel/Idle/Processes.
4 1.1 christos # Written using DTrace (Solaris 10 3/05).
5 1.1 christos #
6 1.1 christos # $Id: cpudists,v 1.1 2015/09/30 22:01:07 christos Exp $
7 1.1 christos #
8 1.1 christos # USAGE: cpudists [-ahV] [-t top] [interval [count]]
9 1.1 christos #
10 1.1 christos # -a # print all processes
11 1.1 christos # -V # don't print timestamps
12 1.1 christos # -t num # print top num only
13 1.1 christos # eg,
14 1.1 christos # cpudists 1 # print every 1 second
15 1.1 christos # cpudists -a 10 # print all processes every 10 secs
16 1.1 christos #
17 1.1 christos #
18 1.1 christos # FIELDS:
19 1.1 christos # value The following or the process name,
20 1.1 christos # IDLE Idle time - CPU running idle thread
21 1.1 christos # KERNEL Kernel time - Kernel servicing interrupts, ...
22 1.1 christos # PROCESS Process time - PIDs running on the system
23 1.1 christos # count Number of occurances at least this duration (ns)
24 1.1 christos #
25 1.1 christos # NOTES:
26 1.1 christos # * This takes into account multiple CPU servers, the total
27 1.1 christos # seconds consumed will be a multiple of the CPU count and interval.
28 1.1 christos #
29 1.1 christos # SEE ALSO: cputimes
30 1.1 christos #
31 1.1 christos # COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
32 1.1 christos #
33 1.1 christos # CDDL HEADER START
34 1.1 christos #
35 1.1 christos # The contents of this file are subject to the terms of the
36 1.1 christos # Common Development and Distribution License, Version 1.0 only
37 1.1 christos # (the "License"). You may not use this file except in compliance
38 1.1 christos # with the License.
39 1.1 christos #
40 1.1 christos # You can obtain a copy of the license at Docs/cddl1.txt
41 1.1 christos # or http://www.opensolaris.org/os/licensing.
42 1.1 christos # See the License for the specific language governing permissions
43 1.1 christos # and limitations under the License.
44 1.1 christos #
45 1.1 christos # CDDL HEADER END
46 1.1 christos #
47 1.1 christos # Author: Brendan Gregg [Sydney, Australia]
48 1.1 christos #
49 1.1 christos # 27-Apr-2005 Brendan Gregg Created this.
50 1.1 christos # 22-Sep-2005 " " Fixed key corruption bug.
51 1.1 christos # 22-Sep-2005 " " Last update.
52 1.1 christos #
53 1.1 christos
54 1.1 christos
55 1.1 christos ##############################
56 1.1 christos # --- Process Arguments ---
57 1.1 christos #
58 1.1 christos opt_all=0; opt_time=1; opt_top=0; top=0; interval=1; count=1
59 1.1 christos
60 1.1 christos while getopts aht:V name
61 1.1 christos do
62 1.1 christos case $name in
63 1.1 christos a) opt_all=1 ;;
64 1.1 christos V) opt_time=0 ;;
65 1.1 christos t) opt_top=1; top=$OPTARG ;;
66 1.1 christos h|?) cat <<-END >&2
67 1.1 christos USAGE: cpudists [-ahV] [-t top] [interval [count]]
68 1.1 christos cpudists # default output
69 1.1 christos -a # print all processes
70 1.1 christos -V # don't print times
71 1.1 christos -t num # print top num only
72 1.1 christos END
73 1.1 christos exit 1
74 1.1 christos esac
75 1.1 christos done
76 1.1 christos shift `expr $OPTIND - 1`
77 1.1 christos
78 1.1 christos if [ "$1" -gt 0 ]; then
79 1.1 christos interval=$1; count=-1; shift
80 1.1 christos fi
81 1.1 christos if [ "$1" -gt 0 ]; then
82 1.1 christos count=$1; shift
83 1.1 christos fi
84 1.1 christos
85 1.1 christos
86 1.1 christos #################################
87 1.1 christos # --- Main Program, DTrace ---
88 1.1 christos #
89 1.1 christos /usr/sbin/dtrace -n '
90 1.1 christos #pragma D option quiet
91 1.1 christos
92 1.1 christos /*
93 1.1 christos * Command line arguments
94 1.1 christos */
95 1.1 christos inline int OPT_all = '$opt_all';
96 1.1 christos inline int OPT_time = '$opt_time';
97 1.1 christos inline int OPT_top = '$opt_top';
98 1.1 christos inline int TOP = '$top';
99 1.1 christos inline int INTERVAL = '$interval';
100 1.1 christos inline int COUNTER = '$count';
101 1.1 christos
102 1.1 christos /* Initialise variables */
103 1.1 christos dtrace:::BEGIN
104 1.1 christos {
105 1.1 christos cpustart[cpu] = 0;
106 1.1 christos counts = COUNTER;
107 1.1 christos secs = INTERVAL;
108 1.1 christos }
109 1.1 christos
110 1.1 christos /* Flag this thread as idle */
111 1.1 christos sysinfo:unix:idle_enter:idlethread
112 1.1 christos {
113 1.1 christos idle[cpu] = 1;
114 1.1 christos }
115 1.1 christos
116 1.1 christos /* Save kernel time between running threads */
117 1.1 christos sched:::on-cpu
118 1.1 christos /cpustart[cpu]/
119 1.1 christos {
120 1.1 christos this->elapsed = timestamp - cpustart[cpu];
121 1.1 christos @Procs["KERNEL"] = quantize(this->elapsed);
122 1.1 christos }
123 1.1 christos
124 1.1 christos /* Save the elapsed time of a thread */
125 1.1 christos sched:::off-cpu,
126 1.1 christos sched:::remain-cpu,
127 1.1 christos profile:::profile-1sec
128 1.1 christos /cpustart[cpu]/
129 1.1 christos {
130 1.1 christos /* determine the name for this thread */
131 1.1 christos program[cpu] = pid == 0 ? idle[cpu] ? "IDLE" : "KERNEL" :
132 1.1 christos OPT_all ? execname : "PROCESS";
133 1.1 christos
134 1.1 christos /* save elapsed */
135 1.1 christos this->elapsed = timestamp - cpustart[cpu];
136 1.1 christos @Procs[program[cpu]] = quantize(this->elapsed);
137 1.1 christos cpustart[cpu] = timestamp;
138 1.1 christos }
139 1.1 christos
140 1.1 christos /* Record the start time of a thread */
141 1.1 christos sched:::on-cpu,
142 1.1 christos sched:::remain-cpu
143 1.1 christos {
144 1.1 christos idle[cpu] = 0;
145 1.1 christos cpustart[cpu] = timestamp;
146 1.1 christos }
147 1.1 christos
148 1.1 christos profile:::tick-1sec
149 1.1 christos {
150 1.1 christos secs--;
151 1.1 christos }
152 1.1 christos
153 1.1 christos /* Print time */
154 1.1 christos profile:::tick-1sec
155 1.1 christos /secs == 0 && OPT_time/
156 1.1 christos {
157 1.1 christos printf("%Y,\n", walltimestamp);
158 1.1 christos }
159 1.1 christos
160 1.1 christos /* Print report */
161 1.1 christos profile:::tick-1sec
162 1.1 christos /secs == 0/
163 1.1 christos {
164 1.1 christos OPT_top ? trunc(@Procs, TOP) : 1;
165 1.1 christos printa("%16s %@16d\n", @Procs);
166 1.1 christos trunc(@Procs);
167 1.1 christos secs = INTERVAL;
168 1.1 christos counts--;
169 1.1 christos }
170 1.1 christos
171 1.1 christos /* End of program */
172 1.1 christos profile:::tick-1sec
173 1.1 christos /counts == 0/
174 1.1 christos {
175 1.1 christos exit(0);
176 1.1 christos }
177 1.1 christos
178 1.1 christos /* cleanup for Ctrl-C */
179 1.1 christos dtrace:::END
180 1.1 christos {
181 1.1 christos trunc(@Procs);
182 1.1 christos }
183 1.1 christos '
184 1.1 christos
185