3 # cputimes - print CPU time consumed by Kernel/Idle/Processes.
4 # Written using DTrace (Solaris 10 3/05).
6 # $Id: cputimes 3 2007-08-01 10:50:08Z brendan $
8 # This program accurately measures time consumed by the kernel, but in
9 # doing so creates extra kernel load of it's own. The extra kernel
10 # activity can be measured by running one cputimes and then another, and
11 # comparing the difference in kernel consumed time. This method can be
12 # used to estimate the load created by other DTrace scripts.
14 # USAGE: cputimes [-ahTV] [-t top] [interval [count]]
16 # -a # print all processes
18 # -V # don't print timestamps
19 # -t num # print top num lines only
21 # cputimes 1 # print every 1 second
22 # cputimes -a 10 # print all processes every 10 secs
23 # cputimes -at 8 5 # print top 8 lines every 5 secs
27 # THREADS The following or the process name,
28 # IDLE Idle time - CPU running idle thread
29 # KERNEL Kernel time - Kernel servicing interrupts, ...
30 # PROCESS Process time - PIDs running on the system
31 # TIME (ns) Sum of the CPU time, ns (nanoseconds)
34 # * This takes into account multiple CPU servers, the total
35 # seconds consumed will be a multiple of the CPU count and interval.
38 # Heisenberg's uncertainty principle.
40 # COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
44 # The contents of this file are subject to the terms of the
45 # Common Development and Distribution License, Version 1.0 only
46 # (the "License"). You may not use this file except in compliance
49 # You can obtain a copy of the license at Docs/cddl1.txt
50 # or http://www.opensolaris.org/os/licensing.
51 # See the License for the specific language governing permissions
52 # and limitations under the License.
56 # Author: Brendan Gregg [Sydney, Australia]
58 # 27-Apr-2005 Brendan Gregg Created this.
59 # 22-Sep-2005 " " Fixed a key corruption bug.
60 # 22-Sep-2005 " " Last update.
64 ##############################
65 # --- Process Arguments ---
67 opt_all=0; opt_time=1; opt_top=0; opt_totals=0
68 top=0; interval=1; count=1
70 while getopts aht:TV name
76 t) opt_top=1; top=$OPTARG ;;
78 USAGE: cputimes [-ahTV] [-t top] [interval [count]]
79 cputimes # default output
80 -a # print all processes
82 -V # don't print times
83 -t num # print top num lines only
85 cputimes 1 # print every 1 second
86 cputimes -a 10 # all processes per 10 sec
87 cputimes -at 8 5 # top 8 lines every 5 secs
92 shift `expr $OPTIND - 1`
94 if [ "$1" -gt 0 ]; then
95 interval=$1; count=-1; shift
97 if [ "$1" -gt 0 ]; then
102 #################################
103 # --- Main Program, DTrace ---
105 /usr/sbin/dtrace -n '
106 #pragma D option quiet
109 * Command line arguments
111 inline int OPT_all = '$opt_all';
112 inline int OPT_time = '$opt_time';
113 inline int OPT_totals = '$opt_totals';
114 inline int OPT_top = '$opt_top';
115 inline int TOP = '$top';
116 inline int INTERVAL = '$interval';
117 inline int COUNTER = '$count';
119 /* Initialise variables */
127 /* Flag this thread as idle */
128 sysinfo:unix:idle_enter:idlethread
133 /* Save kernel time between running threads */
137 this->elapsed = timestamp - cpustart[cpu];
138 @Procs["KERNEL"] = sum(this->elapsed);
141 /* Save the elapsed time of a thread */
144 profile:::profile-1sec
147 /* determine the name for this thread */
148 program[cpu] = pid == 0 ? idle[cpu] ? "IDLE" : "KERNEL" :
149 OPT_all ? execname : "PROCESS";
152 this->elapsed = timestamp - cpustart[cpu];
153 @Procs[program[cpu]] = sum(this->elapsed);
154 cpustart[cpu] = timestamp;
157 /* Record the start time of a thread */
162 cpustart[cpu] = timestamp;
175 OPT_time ? printf("%Y,\n", walltimestamp) : 1;
176 printf("%16s %16s\n", "THREADS", "TIME (ns)");
183 OPT_top ? trunc(@Procs, TOP) : 1;
184 printa("%16s %@16d\n", @Procs);
197 /* cleanup for Ctrl-C */