1235368Sgnn#!/usr/sbin/dtrace -CZs
2235368Sgnn/*
3235368Sgnn * j_cpudist.d - measure Java on-CPU times for different types of operation.
4235368Sgnn *               Written for the Java hotspot DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: j_cpudist.d 59 2007-10-03 08:21:58Z brendan $
7235368Sgnn *
8235368Sgnn * This traces activity from all Java processes on the system with hotspot
9235368Sgnn * provider support (1.6.0). Method calls are only visible when using the
10235368Sgnn * flag "+ExtendedDTraceProbes". eg, java -XX:+ExtendedDTraceProbes classfile
11235368Sgnn *
12235368Sgnn * USAGE: j_cpudist.d [top]	# hit Ctrl-C to end
13235368Sgnn *
14235368Sgnn * The "top" optional argument will truncate the output for each report
15235368Sgnn * section to that many lines, with a default of 10.
16235368Sgnn *
17235368Sgnn * FIELDS:
18235368Sgnn *		1		Process ID
19235368Sgnn *		2		Type of call (method/gc)
20235368Sgnn *		3		Name of call
21235368Sgnn *
22235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
23235368Sgnn *
24235368Sgnn * CDDL HEADER START
25235368Sgnn *
26235368Sgnn *  The contents of this file are subject to the terms of the
27235368Sgnn *  Common Development and Distribution License, Version 1.0 only
28235368Sgnn *  (the "License").  You may not use this file except in compliance
29235368Sgnn *  with the License.
30235368Sgnn *
31235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
32235368Sgnn *  or http://www.opensolaris.org/os/licensing.
33235368Sgnn *  See the License for the specific language governing permissions
34235368Sgnn *  and limitations under the License.
35235368Sgnn *
36235368Sgnn * CDDL HEADER END
37235368Sgnn *
38235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
39235368Sgnn */
40235368Sgnn
41235368Sgnn#define TOP	10		/* default output truncation */
42235368Sgnn#define B_FALSE	0
43235368Sgnn
44235368Sgnn#pragma D option quiet
45235368Sgnn#pragma D option defaultargs
46235368Sgnn
47235368Sgnndtrace:::BEGIN
48235368Sgnn{
49235368Sgnn	printf("Tracing... Hit Ctrl-C to end.\n");
50235368Sgnn	top = $1 != 0 ? $1 : TOP;
51235368Sgnn}
52235368Sgnn
53235368Sgnnhotspot*:::method-entry
54235368Sgnn{
55235368Sgnn	self->depth[arg0]++;
56235368Sgnn	self->exclude[arg0, self->depth[arg0]] = 0;
57235368Sgnn	self->method[arg0, self->depth[arg0]] = vtimestamp;
58235368Sgnn}
59235368Sgnn
60235368Sgnnhotspot*:::method-return
61235368Sgnn/self->method[arg0, self->depth[arg0]]/
62235368Sgnn{
63235368Sgnn	this->oncpu_incl = vtimestamp - self->method[arg0, self->depth[arg0]];
64235368Sgnn	this->oncpu_excl = this->oncpu_incl -
65235368Sgnn	    self->exclude[arg0, self->depth[arg0]];
66235368Sgnn	self->method[arg0, self->depth[arg0]] = 0;
67235368Sgnn	self->exclude[arg0, self->depth[arg0]] = 0;
68235368Sgnn
69235368Sgnn	this->class = (char *)copyin(arg1, arg2 + 1);
70235368Sgnn	this->class[arg2] = '\0';
71235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
72235368Sgnn	this->method[arg4] = '\0';
73235368Sgnn	this->name = strjoin(strjoin(stringof(this->class), "."),
74235368Sgnn	    stringof(this->method));
75235368Sgnn
76235368Sgnn	@types_incl[pid, "method", this->name] =
77235368Sgnn	    quantize(this->oncpu_incl / 1000);
78235368Sgnn	@types_excl[pid, "method", this->name] =
79235368Sgnn	    quantize(this->oncpu_excl / 1000);
80235368Sgnn
81235368Sgnn	self->depth[arg0]--;
82235368Sgnn	self->exclude[arg0, self->depth[arg0]] += this->oncpu_incl;
83235368Sgnn}
84235368Sgnn
85235368Sgnnhotspot*:::gc-begin
86235368Sgnn{
87235368Sgnn	self->gc = vtimestamp;
88235368Sgnn	self->full = (boolean_t)arg0;
89235368Sgnn}
90235368Sgnn
91235368Sgnnhotspot*:::gc-end
92235368Sgnn/self->gc/
93235368Sgnn{
94235368Sgnn	this->oncpu = vtimestamp - self->gc;
95235368Sgnn
96235368Sgnn	@types[pid, "gc", self->full == B_FALSE ? "GC" : "Full GC"] =
97235368Sgnn	    quantize(this->oncpu / 1000);
98235368Sgnn
99235368Sgnn	self->gc = 0;
100235368Sgnn	self->full = 0;
101235368Sgnn}
102235368Sgnn
103235368Sgnndtrace:::END
104235368Sgnn{
105235368Sgnn	trunc(@types, top);
106235368Sgnn	printf("\nTop %d on-CPU times (us),\n", top);
107235368Sgnn	printa("   PID=%d, %s, %s %@d\n", @types);
108235368Sgnn
109235368Sgnn	trunc(@types_excl, top);
110235368Sgnn	printf("\nTop %d exclusive method on-CPU times (us),\n", top);
111235368Sgnn	printa("   PID=%d, %s, %s %@d\n", @types_excl);
112235368Sgnn
113235368Sgnn	trunc(@types_incl, top);
114235368Sgnn	printf("\nTop %d inclusive method on-CPU times (us),\n", top);
115235368Sgnn	printa("   PID=%d, %s, %s %@d\n", @types_incl);
116235368Sgnn}
117