1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * j_flowtime.d - snoop Java execution with method flow and delta times.
4235368Sgnn *                Written for the Java hotspot DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: j_flowtime.d 41 2007-09-17 02:20:10Z brendan $
7235368Sgnn *
8235368Sgnn * This traces activity from all Java processes on the system with hotspot
9235368Sgnn * provider support (1.6.0) and the flag "+ExtendedDTraceProbes". eg,
10235368Sgnn * java -XX:+ExtendedDTraceProbes classfile
11235368Sgnn *
12235368Sgnn * USAGE: j_flowtime.d		# hit Ctrl-C to end
13235368Sgnn *
14235368Sgnn * This watches Java method entries and returns, and indents child
15235368Sgnn * method calls.
16235368Sgnn *
17235368Sgnn * FIELDS:
18235368Sgnn *		C		CPU-id
19235368Sgnn *		PID		Process ID
20235368Sgnn *		TID		Thread ID
21235368Sgnn *		TIME(us)	Time since boot (us)
22235368Sgnn *		DELTA(us)	Elapsed time from previous line to this line
23235368Sgnn *		CLASS.METHOD	Java class and method name
24235368Sgnn *
25235368Sgnn * LEGEND:
26235368Sgnn *		->		method entry
27235368Sgnn *		<-		method return
28235368Sgnn *
29235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it
30235368Sgnn * changes, then it is very likely that the output has been shuffled.
31235368Sgnn * Changes in TID will appear to shuffle output, as we change from one thread
32235368Sgnn * depth to the next. See Docs/Notes/ALLjavaflow.txt for additional notes.
33235368Sgnn *
34235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
35235368Sgnn *
36235368Sgnn * CDDL HEADER START
37235368Sgnn *
38235368Sgnn *  The contents of this file are subject to the terms of the
39235368Sgnn *  Common Development and Distribution License, Version 1.0 only
40235368Sgnn *  (the "License").  You may not use this file except in compliance
41235368Sgnn *  with the License.
42235368Sgnn *
43235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
44235368Sgnn *  or http://www.opensolaris.org/os/licensing.
45235368Sgnn *  See the License for the specific language governing permissions
46235368Sgnn *  and limitations under the License.
47235368Sgnn *
48235368Sgnn * CDDL HEADER END
49235368Sgnn *
50235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
51235368Sgnn */
52235368Sgnn
53235368Sgnn/* increasing bufsize can reduce drops */
54235368Sgnn#pragma D option bufsize=16m
55235368Sgnn#pragma D option quiet
56235368Sgnn#pragma D option switchrate=10
57235368Sgnn
58235368Sgnnself int depth[int];
59235368Sgnn
60235368Sgnndtrace:::BEGIN
61235368Sgnn{
62235368Sgnn	printf("%3s %6s/%-5s %-16s %9s -- %s\n", "C", "PID", "TID", "TIME(us)",
63235368Sgnn	    "DELTA(us)", "CLASS.METHOD");
64235368Sgnn}
65235368Sgnn
66235368Sgnnhotspot*:::method-entry,
67235368Sgnnhotspot*:::method-return
68235368Sgnn/self->last == 0/
69235368Sgnn{
70235368Sgnn	self->last = timestamp;
71235368Sgnn}
72235368Sgnn
73235368Sgnnhotspot*:::method-entry
74235368Sgnn{
75235368Sgnn	this->delta = (timestamp - self->last) / 1000;
76235368Sgnn	this->class = (char *)copyin(arg1, arg2 + 1);
77235368Sgnn	this->class[arg2] = '\0';
78235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
79235368Sgnn	this->method[arg4] = '\0';
80235368Sgnn
81235368Sgnn	printf("%3d %6d/%-5d %-16d %9d %*s-> %s.%s\n", cpu, pid, tid,
82235368Sgnn	    timestamp / 1000, this->delta, self->depth[arg0] * 2, "",
83235368Sgnn	    stringof(this->class), stringof(this->method));
84235368Sgnn	self->depth[arg0]++;
85235368Sgnn	self->last = timestamp;
86235368Sgnn}
87235368Sgnn
88235368Sgnnhotspot*:::method-return
89235368Sgnn{
90235368Sgnn	this->delta = (timestamp - self->last) / 1000;
91235368Sgnn	this->class = (char *)copyin(arg1, arg2 + 1);
92235368Sgnn	this->class[arg2] = '\0';
93235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
94235368Sgnn	this->method[arg4] = '\0';
95235368Sgnn
96235368Sgnn	self->depth[arg0] -= self->depth[arg0] > 0 ? 1 : 0;
97235368Sgnn	printf("%3d %6d/%-5d %-16d %9d %*s<- %s.%s\n", cpu, pid, tid,
98235368Sgnn	    timestamp / 1000, this->delta, self->depth[arg0] * 2, "",
99235368Sgnn	    stringof(this->class), stringof(this->method));
100235368Sgnn	self->last = timestamp;
101235368Sgnn}
102