aboutsummaryrefslogtreecommitdiff
path: root/Kernel/cputimes
blob: 881bf90efec6dc4146116c338779cdd422f469d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/sh
#
# cputimes - print CPU time consumed by Kernel/Idle/Processes.
#            Written using DTrace (Solaris 10 3/05).
#
# $Id: cputimes 3 2007-08-01 10:50:08Z brendan $
#
# This program accurately measures time consumed by the kernel, but in
# doing so creates extra kernel load of it's own. The extra kernel
# activity can be measured by running one cputimes and then another, and
# comparing the difference in kernel consumed time. This method can be
# used to estimate the load created by other DTrace scripts.
#
# USAGE:	cputimes [-ahTV] [-t top] [interval [count]]
#
#		-a                # print all processes
#		-T                # print totals
#		-V                # don't print timestamps
#		-t num            # print top num lines only
#  eg,
#		cputimes 1        # print every 1 second
#		cputimes -a 10    # print all processes every 10 secs
#		cputimes -at 8 5  # print top 8 lines every 5 secs
#
#
# FIELDS: 
#		THREADS         The following or the process name,
#		IDLE            Idle time - CPU running idle thread
#		KERNEL          Kernel time - Kernel servicing interrupts, ...
#		PROCESS         Process time - PIDs running on the system
#		TIME (ns)       Sum of the CPU time, ns (nanoseconds)
#
# NOTES:
# * This takes into account multiple CPU servers, the total 
# seconds consumed will be a multiple of the CPU count and interval.
#
# SEE ALSO: cpudists
#           Heisenberg's uncertainty principle.
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
# CDDL HEADER START
#
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
#
# CDDL HEADER END
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 27-Apr-2005   Brendan Gregg   Created this.
# 22-Sep-2005      "      "	Fixed a key corruption bug.
# 22-Sep-2005      "      "	Last update.
#


##############################
# --- Process Arguments ---
#
opt_all=0; opt_time=1; opt_top=0; opt_totals=0
top=0; interval=1; count=1

while getopts aht:TV name
do
        case $name in
        a)      opt_all=1 ;;
        T)      opt_totals=1 ;;
        V)      opt_time=0 ;;
        t)      opt_top=1; top=$OPTARG ;;
        h|?)    cat <<-END >&2
		USAGE: cputimes [-ahTV] [-t top] [interval [count]]
		       cputimes                  # default output
		               -a                # print all processes
		               -T                # print totals
		               -V                # don't print times
		               -t num            # print top num lines only
		          eg,
		               cputimes 1        # print every 1 second
		               cputimes -a 10    # all processes per 10 sec
		               cputimes -at 8 5  # top 8 lines every 5 secs
		END
		exit 1
        esac
done
shift `expr $OPTIND - 1`

if [ "$1" -gt 0 ]; then
        interval=$1; count=-1; shift
fi
if [ "$1" -gt 0 ]; then
	count=$1; shift
fi


#################################
# --- Main Program, DTrace ---
#
/usr/sbin/dtrace -n '
 #pragma D option quiet

 /*
  * Command line arguments
  */
 inline int OPT_all    = '$opt_all';
 inline int OPT_time   = '$opt_time';
 inline int OPT_totals = '$opt_totals';
 inline int OPT_top    = '$opt_top';
 inline int TOP        = '$top';
 inline int INTERVAL   = '$interval';
 inline int COUNTER    = '$count';

 /* Initialise variables */
 dtrace:::BEGIN
 {
	cpustart[cpu] = 0;
	counts = COUNTER;
	secs = INTERVAL;
 }

 /* Flag this thread as idle */
 sysinfo:unix:idle_enter:idlethread
 {
	idle[cpu] = 1;
 }

 /* Save kernel time between running threads */
 sched:::on-cpu 
 /cpustart[cpu]/
 {
	this->elapsed = timestamp - cpustart[cpu];
	@Procs["KERNEL"] = sum(this->elapsed);
 }

 /* Save the elapsed time of a thread */
 sched:::off-cpu,
 sched:::remain-cpu,
 profile:::profile-1sec
 /cpustart[cpu]/
 {
	/* determine the name for this thread */
	program[cpu] = pid == 0 ? idle[cpu] ? "IDLE" : "KERNEL" :
	    OPT_all ? execname : "PROCESS";

	/* save elapsed */
	this->elapsed = timestamp - cpustart[cpu];
	@Procs[program[cpu]] = sum(this->elapsed);
	cpustart[cpu] = timestamp;
 }

 /* Record the start time of a thread */
 sched:::on-cpu,
 sched:::remain-cpu
 {
	idle[cpu] = 0;
	cpustart[cpu] = timestamp;
 }


 profile:::tick-1sec
 {
	secs--;
 }

 /* Print time */
 profile:::tick-1sec 
 /secs == 0/
 { 
	OPT_time ? printf("%Y,\n", walltimestamp) : 1;
	printf("%16s %16s\n", "THREADS", "TIME (ns)");
 }

 /* Print report */
 profile:::tick-1sec 
 /secs == 0/ 
 { 
	OPT_top ? trunc(@Procs, TOP) : 1;
	printa("%16s %@16d\n", @Procs);
	trunc(@Procs);
	secs = INTERVAL;
	counts--;
 }

 /* End of program */
 profile:::tick-1sec 
 /counts == 0/ 
 {
	exit(0);
 }

 /* cleanup for Ctrl-C */
 dtrace:::END
 {
	trunc(@Procs);
 }
'