1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * This D script is used as an example in the Solaris Dynamic Tracing Guide
27  * wiki in the "sched Provider" Chapter.
28  *
29  * The full text of the this chapter may be found here:
30  *
31  *   http://wikis.sun.com/display/DTrace/sched+Provider
32  *
33  * On machines that have DTrace installed, this script is available as
34  * whoqueue.d in /usr/demo/dtrace, a directory that contains all D scripts
35  * used in the Solaris Dynamic Tracing Guide.  A table of the scripts and their
36  * corresponding chapters may be found here:
37  *
38  *   file:///usr/demo/dtrace/index.html
39  */
40 
41 #pragma D option quiet
42 #pragma D option nspec=4
43 #pragma D option specsize=100k
44 
45 int maxlen;
46 int spec[int];
47 
48 sched:::enqueue
49 {
50 	this->len = ++qlen[this->cpu = args[2]->cpu_id];
51 	in[args[0]->pr_addr] = timestamp;
52 }
53 
54 sched:::enqueue
55 /this->len > maxlen && spec[this->cpu]/
56 {
57 	/*
58 	 * There is already a speculation for this CPU.  We just set a new
59 	 * record, so we'll discard the old one.
60 	 */
61 	discard(spec[this->cpu]);
62 }
63 
64 sched:::enqueue
65 /this->len > maxlen/
66 {
67 	/*
68 	 * We have a winner.  Set the new maximum length and set the timestamp
69 	 * of the longest length.
70 	 */
71 	maxlen = this->len;
72 	longtime[this->cpu] = timestamp;
73 
74 	/*
75 	 * Now start a new speculation, and speculatively trace the length.
76 	 */
77 	this->spec = spec[this->cpu] = speculation();
78 	speculate(this->spec);
79 	printf("Run queue of length %d:\n", this->len);
80 }
81 
82 sched:::dequeue
83 /(this->in = in[args[0]->pr_addr]) &&
84     this->in <= longtime[this->cpu = args[2]->cpu_id]/
85 {
86 	speculate(spec[this->cpu]);
87 	printf("  %d/%d (%s)\n",
88 	    args[1]->pr_pid, args[0]->pr_lwpid,
89 	    stringof(args[1]->pr_fname));
90 }
91 
92 sched:::dequeue
93 /qlen[args[2]->cpu_id]/
94 {
95 	in[args[0]->pr_addr] = 0;
96 	this->len = --qlen[args[2]->cpu_id];
97 }
98 
99 sched:::dequeue
100 /this->len == 0 && spec[this->cpu]/
101 {
102 	/*
103 	 * We just processed the last thread that was enqueued at the time
104 	 * of longest length; commit the speculation, which by now contains
105 	 * each thread that was enqueued when the queue was longest.
106 	 */
107 	commit(spec[this->cpu]);
108 	spec[this->cpu] = 0;
109 }
110