1235368Sgnn#!/usr/sbin/dtrace -s
2235368Sgnn/*
3235368Sgnn * cswstat.d - context switch time stat.
4235368Sgnn *	       Uses DTrace (Solaris 10 03/05)
5235368Sgnn *
6235368Sgnn * This prints a context switch count and consumed time for context
7235368Sgnn * switching every second.
8235368Sgnn *
9235368Sgnn * $Id: cswstat.d 15 2007-09-11 09:09:25Z brendan $
10235368Sgnn *
11235368Sgnn * USAGE:	cswstat.d
12235368Sgnn *
13235368Sgnn * FIELDS:
14235368Sgnn *		TIME		Current time
15235368Sgnn *		NUM		Number of context switches
16235368Sgnn *		CSWTIME(us)	Time consumed context switching, us
17235368Sgnn *		AVGTIME(us)	Average context switch time, us
18235368Sgnn *
19235368Sgnn * THANKS: Toomas Soome
20235368Sgnn *
21235368Sgnn * COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
22235368Sgnn *
23235368Sgnn * CDDL HEADER START
24235368Sgnn *
25235368Sgnn *  The contents of this file are subject to the terms of the
26235368Sgnn *  Common Development and Distribution License, Version 1.0 only
27235368Sgnn *  (the "License").  You may not use this file except in compliance
28235368Sgnn *  with the License.
29235368Sgnn *
30235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
31235368Sgnn *  or http://www.opensolaris.org/os/licensing.
32235368Sgnn *  See the License for the specific language governing permissions
33235368Sgnn *  and limitations under the License.
34235368Sgnn *
35235368Sgnn * CDDL HEADER END
36235368Sgnn *
37235368Sgnn * 17-May-2005  Brendan Gregg   Created this.
38235368Sgnn * 03-Nov-2005	   "      "	Last update.
39235368Sgnn */
40235368Sgnn
41235368Sgnn#pragma D option quiet
42235368Sgnn
43235368Sgnndtrace:::BEGIN
44235368Sgnn{
45235368Sgnn	/* print header */
46235368Sgnn	printf("%-20s  %8s %12s %12s\n", "TIME", "NUM", "CSWTIME(us)",
47235368Sgnn	    "AVGTIME(us)");
48235368Sgnn	times = 0;
49235368Sgnn	num = 0;
50235368Sgnn}
51235368Sgnn
52235368Sgnnsched:::off-cpu
53235368Sgnn{
54235368Sgnn	/* csw start */
55235368Sgnn	num++;
56235368Sgnn	start[cpu] = timestamp;
57235368Sgnn}
58235368Sgnn
59235368Sgnnsched:::on-cpu
60235368Sgnn/start[cpu]/
61235368Sgnn{
62235368Sgnn	/* csw end */
63235368Sgnn	times += timestamp - start[cpu];
64235368Sgnn	start[cpu] = 0;
65235368Sgnn}
66235368Sgnn
67235368Sgnnprofile:::tick-1sec
68235368Sgnn{
69235368Sgnn	/* print output */
70235368Sgnn	printf("%20Y  %8d %12d %12d\n", walltimestamp, num, times/1000,
71235368Sgnn	    num == 0 ? 0 : times/(1000 * num));
72235368Sgnn	times = 0;
73235368Sgnn	num = 0;
74235368Sgnn}
75