1235368Sgnn#!/usr/sbin/dtrace -CZs
2235368Sgnn/*
3235368Sgnn * j_cputime.d - measure Java on-CPU times for different types of operation.
4235368Sgnn *               Written for the Java hotspot DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: j_cputime.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_cputime.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 *		PID		Process ID
19235368Sgnn *		TYPE		Type of call (method/gc/total)
20235368Sgnn *		NAME		Name of call
21235368Sgnn *		TOTAL		Total on-CPU time for calls (us)
22235368Sgnn *
23235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
24235368Sgnn *
25235368Sgnn * CDDL HEADER START
26235368Sgnn *
27235368Sgnn *  The contents of this file are subject to the terms of the
28235368Sgnn *  Common Development and Distribution License, Version 1.0 only
29235368Sgnn *  (the "License").  You may not use this file except in compliance
30235368Sgnn *  with the License.
31235368Sgnn *
32235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
33235368Sgnn *  or http://www.opensolaris.org/os/licensing.
34235368Sgnn *  See the License for the specific language governing permissions
35235368Sgnn *  and limitations under the License.
36235368Sgnn *
37235368Sgnn * CDDL HEADER END
38235368Sgnn *
39235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
40235368Sgnn */
41235368Sgnn
42235368Sgnn#define TOP	10		/* default output truncation */
43235368Sgnn#define B_FALSE	0
44235368Sgnn
45235368Sgnn#pragma D option quiet
46235368Sgnn#pragma D option defaultargs
47235368Sgnn
48235368Sgnndtrace:::BEGIN
49235368Sgnn{
50235368Sgnn	printf("Tracing... Hit Ctrl-C to end.\n");
51235368Sgnn	top = $1 != 0 ? $1 : TOP;
52235368Sgnn}
53235368Sgnn
54235368Sgnnhotspot*:::method-entry
55235368Sgnn{
56235368Sgnn	self->depth[arg0]++;
57235368Sgnn	self->exclude[arg0, self->depth[arg0]] = 0;
58235368Sgnn	self->method[arg0, self->depth[arg0]] = vtimestamp;
59235368Sgnn}
60235368Sgnn
61235368Sgnnhotspot*:::method-return
62235368Sgnn/self->method[arg0, self->depth[arg0]]/
63235368Sgnn{
64235368Sgnn	this->oncpu_incl = vtimestamp - self->method[arg0, self->depth[arg0]];
65235368Sgnn	this->oncpu_excl = this->oncpu_incl -
66235368Sgnn	    self->exclude[arg0, self->depth[arg0]];
67235368Sgnn	self->method[arg0, self->depth[arg0]] = 0;
68235368Sgnn	self->exclude[arg0, self->depth[arg0]] = 0;
69235368Sgnn
70235368Sgnn	this->class = (char *)copyin(arg1, arg2 + 1);
71235368Sgnn	this->class[arg2] = '\0';
72235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
73235368Sgnn	this->method[arg4] = '\0';
74235368Sgnn	this->name = strjoin(strjoin(stringof(this->class), "."),
75235368Sgnn	    stringof(this->method));
76235368Sgnn
77235368Sgnn	@num[pid, "method", this->name] = count();
78235368Sgnn	@num[0, "total", "-"] = count();
79235368Sgnn	@types_incl[pid, "method", this->name] = sum(this->oncpu_incl);
80235368Sgnn	@types_excl[pid, "method", this->name] = sum(this->oncpu_excl);
81235368Sgnn	@types_excl[0, "total", "-"] = sum(this->oncpu_excl);
82235368Sgnn
83235368Sgnn	self->depth[arg0]--;
84235368Sgnn	self->exclude[arg0, self->depth[arg0]] += this->oncpu_incl;
85235368Sgnn}
86235368Sgnn
87235368Sgnnhotspot*:::gc-begin
88235368Sgnn{
89235368Sgnn	self->gc = vtimestamp;
90235368Sgnn	self->full = (boolean_t)arg0;
91235368Sgnn}
92235368Sgnn
93235368Sgnnhotspot*:::gc-end
94235368Sgnn/self->gc/
95235368Sgnn{
96235368Sgnn	this->oncpu = vtimestamp - self->gc;
97235368Sgnn	self->gc = 0;
98235368Sgnn
99235368Sgnn	@num[pid, "gc", self->full == B_FALSE ? "GC" : "Full GC"] = count();
100235368Sgnn	@types[pid, "gc", self->full == B_FALSE ? "GC" : "Full GC"] =
101235368Sgnn	    sum(this->oncpu);
102235368Sgnn	self->full = 0;
103235368Sgnn}
104235368Sgnn
105235368Sgnndtrace:::END
106235368Sgnn{
107235368Sgnn	trunc(@num, top);
108235368Sgnn	printf("\nTop %d counts,\n", top);
109235368Sgnn	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "COUNT");
110235368Sgnn	printa("   %6d %-10s %-48s %@8d\n", @num);
111235368Sgnn
112235368Sgnn	trunc(@types, top);
113235368Sgnn	normalize(@types, 1000);
114235368Sgnn	printf("\nTop %d on-CPU times (us),\n", top);
115235368Sgnn	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
116235368Sgnn	printa("   %6d %-10s %-48s %@8d\n", @types);
117235368Sgnn
118235368Sgnn	trunc(@types_excl, top);
119235368Sgnn	normalize(@types_excl, 1000);
120235368Sgnn	printf("\nTop %d exclusive method on-CPU times (us),\n", top);
121235368Sgnn	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
122235368Sgnn	printa("   %6d %-10s %-48s %@8d\n", @types_excl);
123235368Sgnn
124235368Sgnn	trunc(@types_incl, top);
125235368Sgnn	normalize(@types_incl, 1000);
126235368Sgnn	printf("\nTop %d inclusive method on-CPU times (us),\n", top);
127235368Sgnn	printf("   %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
128235368Sgnn	printa("   %6d %-10s %-48s %@8d\n", @types_incl);
129235368Sgnn}
130