1#!/usr/sbin/dtrace -Zs
2/*
3 * j_flowtime.d - snoop Java execution with method flow and delta times.
4 *                Written for the Java hotspot DTrace provider.
5 *
6 * $Id: j_flowtime.d 41 2007-09-17 02:20:10Z brendan $
7 *
8 * This traces activity from all Java processes on the system with hotspot
9 * provider support (1.6.0) and the flag "+ExtendedDTraceProbes". eg,
10 * java -XX:+ExtendedDTraceProbes classfile
11 *
12 * USAGE: j_flowtime.d		# hit Ctrl-C to end
13 *
14 * This watches Java method entries and returns, and indents child
15 * method calls.
16 *
17 * FIELDS:
18 *		C		CPU-id
19 *		PID		Process ID
20 *		TID		Thread ID
21 *		TIME(us)	Time since boot (us)
22 *		DELTA(us)	Elapsed time from previous line to this line
23 *		CLASS.METHOD	Java class and method name
24 *
25 * LEGEND:
26 *		->		method entry
27 *		<-		method return
28 *
29 * WARNING: Watch the first column carefully, it prints the CPU-id. If it
30 * changes, then it is very likely that the output has been shuffled.
31 * Changes in TID will appear to shuffle output, as we change from one thread
32 * depth to the next. See Docs/Notes/ALLjavaflow.txt for additional notes.
33 *
34 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
35 *
36 * CDDL HEADER START
37 *
38 *  The contents of this file are subject to the terms of the
39 *  Common Development and Distribution License, Version 1.0 only
40 *  (the "License").  You may not use this file except in compliance
41 *  with the License.
42 *
43 *  You can obtain a copy of the license at Docs/cddl1.txt
44 *  or http://www.opensolaris.org/os/licensing.
45 *  See the License for the specific language governing permissions
46 *  and limitations under the License.
47 *
48 * CDDL HEADER END
49 *
50 * 09-Sep-2007	Brendan Gregg	Created this.
51 */
52
53/* increasing bufsize can reduce drops */
54#pragma D option bufsize=16m
55#pragma D option quiet
56#pragma D option switchrate=10
57
58self int depth[int];
59
60dtrace:::BEGIN
61{
62	printf("%3s %6s/%-5s %-16s %9s -- %s\n", "C", "PID", "TID", "TIME(us)",
63	    "DELTA(us)", "CLASS.METHOD");
64}
65
66hotspot*:::method-entry,
67hotspot*:::method-return
68/self->last == 0/
69{
70	self->last = timestamp;
71}
72
73hotspot*:::method-entry
74{
75	this->delta = (timestamp - self->last) / 1000;
76	this->class = (char *)copyin(arg1, arg2 + 1);
77	this->class[arg2] = '\0';
78	this->method = (char *)copyin(arg3, arg4 + 1);
79	this->method[arg4] = '\0';
80
81	printf("%3d %6d/%-5d %-16d %9d %*s-> %s.%s\n", cpu, pid, tid,
82	    timestamp / 1000, this->delta, self->depth[arg0] * 2, "",
83	    stringof(this->class), stringof(this->method));
84	self->depth[arg0]++;
85	self->last = timestamp;
86}
87
88hotspot*:::method-return
89{
90	this->delta = (timestamp - self->last) / 1000;
91	this->class = (char *)copyin(arg1, arg2 + 1);
92	this->class[arg2] = '\0';
93	this->method = (char *)copyin(arg3, arg4 + 1);
94	this->method[arg4] = '\0';
95
96	self->depth[arg0] -= self->depth[arg0] > 0 ? 1 : 0;
97	printf("%3d %6d/%-5d %-16d %9d %*s<- %s.%s\n", cpu, pid, tid,
98	    timestamp / 1000, this->delta, self->depth[arg0] * 2, "",
99	    stringof(this->class), stringof(this->method));
100	self->last = timestamp;
101}
102