1#!/usr/sbin/dtrace -s
2/*
3 * cswstat.d - context switch time stat.
4 *	       Uses DTrace (Solaris 10 03/05)
5 *
6 * This prints a context switch count and consumed time for context
7 * switching every second.
8 *
9 * 03-Nov-2005, ver 0.71
10 *
11 * USAGE:	cswstat.d
12 *
13 * FIELDS:
14 *		TIME		Current time
15 *		NUM		Number of context switches
16 *		CSWTIME		Time consumed context switching, us
17 *		AVGTIME		Average context switch time, us
18 *
19 * THANKS: Toomas Soome
20 *
21 * COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
22 *
23 * CDDL HEADER START
24 *
25 *  The contents of this file are subject to the terms of the
26 *  Common Development and Distribution License, Version 1.0 only
27 *  (the "License").  You may not use this file except in compliance
28 *  with the License.
29 *
30 *  You can obtain a copy of the license at Docs/cddl1.txt
31 *  or http://www.opensolaris.org/os/licensing.
32 *  See the License for the specific language governing permissions
33 *  and limitations under the License.
34 *
35 * CDDL HEADER END
36 *
37 * 17-May-2005  Brendan Gregg   Created this.
38 */
39
40#pragma D option quiet
41
42dtrace:::BEGIN
43{
44	/* print header */
45	printf("%-20s  %8s %12s %12s\n", "TIME", "NUM", "CSWTIME", "AVGTIME");
46	times = 0;
47	num = 0;
48}
49
50sched:::off-cpu
51{
52	/* csw start */
53	start[cpu] = timestamp;
54	num++;
55}
56
57sched:::on-cpu
58/start[cpu]/
59{
60	/* csw end */
61	times += timestamp - start[cpu];
62	start[cpu] = 0;
63}
64
65profile:::tick-1sec
66{
67	/* print output */
68	printf("%20Y  %8d %12d %12d\n", walltimestamp, num, times/1000,
69	    times/(1000*num));	/* assume num > 0 */
70	times = 0;
71	num = 0;
72}
73