1*e137d3e0Schristos #!/usr/sbin/dtrace -CZs
2*e137d3e0Schristos /*
3*e137d3e0Schristos  * j_calltime.d - measure Java elapsed times for different types of operation.
4*e137d3e0Schristos  *                Written for the Java hotspot DTrace provider.
5*e137d3e0Schristos  *
6*e137d3e0Schristos  * $Id: j_calltime.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $
7*e137d3e0Schristos  *
8*e137d3e0Schristos  * This traces activity from all Java processes on the system with hotspot
9*e137d3e0Schristos  * provider support (1.6.0). Method calls are only visible when using the
10*e137d3e0Schristos  * flag "+ExtendedDTraceProbes". eg, java -XX:+ExtendedDTraceProbes classfile
11*e137d3e0Schristos  *
12*e137d3e0Schristos  * USAGE: j_calltime.d [top]	# hit Ctrl-C to end
13*e137d3e0Schristos  *
14*e137d3e0Schristos  * The "top" optional argument will truncate the output for each report
15*e137d3e0Schristos  * section to that many lines, with a default of 10.
16*e137d3e0Schristos  *
17*e137d3e0Schristos  * FIELDS:
18*e137d3e0Schristos  *		PID		Process ID
19*e137d3e0Schristos  *		TYPE		Type of call (method/gc/total)
20*e137d3e0Schristos  *		NAME		Name of call
21*e137d3e0Schristos  *		TOTAL		Total elapsed time for calls (us)
22*e137d3e0Schristos  *
23*e137d3e0Schristos  * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
24*e137d3e0Schristos  *
25*e137d3e0Schristos  * CDDL HEADER START
26*e137d3e0Schristos  *
27*e137d3e0Schristos  *  The contents of this file are subject to the terms of the
28*e137d3e0Schristos  *  Common Development and Distribution License, Version 1.0 only
29*e137d3e0Schristos  *  (the "License").  You may not use this file except in compliance
30*e137d3e0Schristos  *  with the License.
31*e137d3e0Schristos  *
32*e137d3e0Schristos  *  You can obtain a copy of the license at Docs/cddl1.txt
33*e137d3e0Schristos  *  or http://www.opensolaris.org/os/licensing.
34*e137d3e0Schristos  *  See the License for the specific language governing permissions
35*e137d3e0Schristos  *  and limitations under the License.
36*e137d3e0Schristos  *
37*e137d3e0Schristos  * CDDL HEADER END
38*e137d3e0Schristos  *
39*e137d3e0Schristos  * 09-Sep-2007	Brendan Gregg	Created this.
40*e137d3e0Schristos  */
41*e137d3e0Schristos 
42*e137d3e0Schristos #define TOP	10		/* default output truncation */
43*e137d3e0Schristos #define B_FALSE	0
44*e137d3e0Schristos 
45*e137d3e0Schristos #pragma D option quiet
46*e137d3e0Schristos #pragma D option defaultargs
47*e137d3e0Schristos 
48*e137d3e0Schristos dtrace:::BEGIN
49*e137d3e0Schristos {
50*e137d3e0Schristos 	printf("Tracing... Hit Ctrl-C to end.\n");
51*e137d3e0Schristos 	top = $1 != 0 ? $1 : TOP;
52*e137d3e0Schristos }
53*e137d3e0Schristos 
54*e137d3e0Schristos hotspot*:::method-entry
55*e137d3e0Schristos {
56*e137d3e0Schristos 	self->depth[arg0]++;
57*e137d3e0Schristos 	self->exclude[arg0, self->depth[arg0]] = 0;
58*e137d3e0Schristos 	self->method[arg0, self->depth[arg0]] = timestamp;
59*e137d3e0Schristos }
60*e137d3e0Schristos 
61*e137d3e0Schristos hotspot*:::method-return
62*e137d3e0Schristos /self->method[arg0, self->depth[arg0]]/
63*e137d3e0Schristos {
64*e137d3e0Schristos 	this->elapsed_incl = timestamp - self->method[arg0, self->depth[arg0]];
65*e137d3e0Schristos 	this->elapsed_excl = this->elapsed_incl -
66*e137d3e0Schristos 	    self->exclude[arg0, self->depth[arg0]];
67*e137d3e0Schristos 	self->method[arg0, self->depth[arg0]] = 0;
68*e137d3e0Schristos 	self->exclude[arg0, self->depth[arg0]] = 0;
69*e137d3e0Schristos 
70*e137d3e0Schristos 	this->class = (char *)copyin(arg1, arg2 + 1);
71*e137d3e0Schristos 	this->class[arg2] = '\0';
72*e137d3e0Schristos 	this->method = (char *)copyin(arg3, arg4 + 1);
73*e137d3e0Schristos 	this->method[arg4] = '\0';
74*e137d3e0Schristos 	this->name = strjoin(strjoin(stringof(this->class), "."),
75*e137d3e0Schristos 	    stringof(this->method));
76*e137d3e0Schristos 
77*e137d3e0Schristos 	@num[pid, "method", this->name] = count();
78*e137d3e0Schristos 	@num[0, "total", "-"] = count();
79*e137d3e0Schristos 	@types_incl[pid, "method", this->name] = sum(this->elapsed_incl);
80*e137d3e0Schristos 	@types_excl[pid, "method", this->name] = sum(this->elapsed_excl);
81*e137d3e0Schristos 	@types_excl[0, "total", "-"] = sum(this->elapsed_excl);
82*e137d3e0Schristos 
83*e137d3e0Schristos 	self->depth[arg0]--;
84*e137d3e0Schristos 	self->exclude[arg0, self->depth[arg0]] += this->elapsed_incl;
85*e137d3e0Schristos }
86*e137d3e0Schristos 
87*e137d3e0Schristos hotspot*:::gc-begin
88*e137d3e0Schristos {
89*e137d3e0Schristos 	self->gc = timestamp;
90*e137d3e0Schristos 	self->full = (boolean_t)arg0;
91*e137d3e0Schristos }
92*e137d3e0Schristos 
93*e137d3e0Schristos hotspot*:::gc-end
94*e137d3e0Schristos /self->gc/
95*e137d3e0Schristos {
96*e137d3e0Schristos 	this->elapsed = timestamp - self->gc;
97*e137d3e0Schristos 	self->gc = 0;
98*e137d3e0Schristos 
99*e137d3e0Schristos 	@num[pid, "gc", self->full == B_FALSE ? "GC" : "Full GC"] = count();
100*e137d3e0Schristos 	@types[pid, "gc", self->full == B_FALSE ? "GC" : "Full GC"] =
101*e137d3e0Schristos 	    sum(this->elapsed);
102*e137d3e0Schristos 	self->full = 0;
103*e137d3e0Schristos }
104*e137d3e0Schristos 
105*e137d3e0Schristos dtrace:::END
106*e137d3e0Schristos {
107*e137d3e0Schristos 	trunc(@num, top);
108*e137d3e0Schristos 	printf("\nTop %d counts,\n", top);
109*e137d3e0Schristos 	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "COUNT");
110*e137d3e0Schristos 	printa("   %6d %-10s %-48s %@8d\n", @num);
111*e137d3e0Schristos 
112*e137d3e0Schristos 	trunc(@types, top);
113*e137d3e0Schristos 	normalize(@types, 1000);
114*e137d3e0Schristos 	printf("\nTop %d elapsed times (us),\n", top);
115*e137d3e0Schristos 	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
116*e137d3e0Schristos 	printa("   %6d %-10s %-48s %@8d\n", @types);
117*e137d3e0Schristos 
118*e137d3e0Schristos 	trunc(@types_excl, top);
119*e137d3e0Schristos 	normalize(@types_excl, 1000);
120*e137d3e0Schristos 	printf("\nTop %d exclusive method elapsed times (us),\n", top);
121*e137d3e0Schristos 	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
122*e137d3e0Schristos 	printa("   %6d %-10s %-48s %@8d\n", @types_excl);
123*e137d3e0Schristos 
124*e137d3e0Schristos 	trunc(@types_incl, top);
125*e137d3e0Schristos 	normalize(@types_incl, 1000);
126*e137d3e0Schristos 	printf("\nTop %d inclusive method elapsed times (us),\n", top);
127*e137d3e0Schristos 	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
128*e137d3e0Schristos 	printa("   %6d %-10s %-48s %@8d\n", @types_incl);
129*e137d3e0Schristos }
130