1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * j_flow.d - snoop Java execution showing method flow using DTrace.
4235368Sgnn *            Written for the Java hotspot DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: j_flow.d 38 2007-09-16 08:17:41Z 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_flow.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 *		TIME(us)	Time since boot, us
20235368Sgnn *		PID		Process ID
21235368Sgnn *		CLASS.METHOD	Java class and method name
22235368Sgnn *
23235368Sgnn * LEGEND:
24235368Sgnn *		->		method entry
25235368Sgnn *		<-		method return
26235368Sgnn *
27235368Sgnn * WARNING: Watch the first column carefully, it prints the CPU-id. If it
28235368Sgnn * changes, then it is very likely that the output has been shuffled.
29235368Sgnn * Changes in TID will appear to shuffle output, as we change from one thread
30235368Sgnn * depth to the next. See Docs/Notes/ALLjavaflow.txt for additional notes.
31235368Sgnn *
32235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
33235368Sgnn *
34235368Sgnn * CDDL HEADER START
35235368Sgnn *
36235368Sgnn *  The contents of this file are subject to the terms of the
37235368Sgnn *  Common Development and Distribution License, Version 1.0 only
38235368Sgnn *  (the "License").  You may not use this file except in compliance
39235368Sgnn *  with the License.
40235368Sgnn *
41235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
42235368Sgnn *  or http://www.opensolaris.org/os/licensing.
43235368Sgnn *  See the License for the specific language governing permissions
44235368Sgnn *  and limitations under the License.
45235368Sgnn *
46235368Sgnn * CDDL HEADER END
47235368Sgnn *
48235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
49235368Sgnn */
50235368Sgnn
51235368Sgnn/* increasing bufsize can reduce drops */
52235368Sgnn#pragma D option bufsize=16m
53235368Sgnn#pragma D option quiet
54235368Sgnn#pragma D option switchrate=10
55235368Sgnn
56235368Sgnnself int depth[int];
57235368Sgnn
58235368Sgnndtrace:::BEGIN
59235368Sgnn{
60235368Sgnn	printf("%3s %6s %-16s -- %s\n", "C", "PID", "TIME(us)", "CLASS.METHOD");
61235368Sgnn}
62235368Sgnn
63235368Sgnnhotspot*:::method-entry
64235368Sgnn{
65235368Sgnn	this->class = (char *)copyin(arg1, arg2 + 1);
66235368Sgnn	this->class[arg2] = '\0';
67235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
68235368Sgnn	this->method[arg4] = '\0';
69235368Sgnn
70235368Sgnn	printf("%3d %6d %-16d %*s-> %s.%s\n", cpu, pid, timestamp / 1000,
71235368Sgnn	    self->depth[arg0] * 2, "", stringof(this->class),
72235368Sgnn	    stringof(this->method));
73235368Sgnn	self->depth[arg0]++;
74235368Sgnn}
75235368Sgnn
76235368Sgnnhotspot*:::method-return
77235368Sgnn{
78235368Sgnn	this->class = (char *)copyin(arg1, arg2 + 1);
79235368Sgnn	this->class[arg2] = '\0';
80235368Sgnn	this->method = (char *)copyin(arg3, arg4 + 1);
81235368Sgnn	this->method[arg4] = '\0';
82235368Sgnn
83235368Sgnn	self->depth[arg0] -= self->depth[arg0] > 0 ? 1 : 0;
84235368Sgnn	printf("%3d %6d %-16d %*s<- %s.%s\n", cpu, pid, timestamp / 1000,
85235368Sgnn	    self->depth[arg0] * 2, "", stringof(this->class),
86235368Sgnn	    stringof(this->method));
87235368Sgnn}
88