1235368Sgnn#!/usr/sbin/dtrace -CZs
2235368Sgnn/*
3235368Sgnn * j_profile.d - sample stack traces with Java translations using DTrace.
4235368Sgnn *
5235368Sgnn * USAGE: j_profile.d { -p PID | -c cmd }	# hit Ctrl-C to end
6235368Sgnn * $Id: j_profile.d 19 2007-09-12 07:47:59Z brendan $
7235368Sgnn *
8235368Sgnn *
9235368Sgnn * This samples stack traces for the process specified. This stack trace
10235368Sgnn * will cross the JVM and system libraries, and insert translations for Java
11235368Sgnn * stack frames where appropriate. This is best explained with an example
12235368Sgnn * stack frame output,
13235368Sgnn *
14235368Sgnn *            Func_loop.func_c()V
15235368Sgnn *            Func_loop.func_b()V
16235368Sgnn *            Func_loop.func_a()V
17235368Sgnn *            Func_loop.main([Ljava/lang/String;)V
18235368Sgnn *            StubRoutines (1)
19235368Sgnn *            libjvm.so`__1cJJavaCallsLcall_helper6FpnJJavaValue_pnMmethodHan
20235368Sgnn *            libjvm.so`__1cCosUos_exception_wrapper6FpFpnJJavaValue_pnMmetho
21235368Sgnn *            libjvm.so`__1cJJavaCallsEcall6FpnJJavaValue_nMmethodHandle_pnRJ
22235368Sgnn *            libjvm.so`__1cRjni_invoke_static6FpnHJNIEnv__pnJJavaValue_pnI_j
23235368Sgnn *            libjvm.so`jni_CallStaticVoidMethod+0x15d
24235368Sgnn *            java`JavaMain+0xd30
25235368Sgnn *            libc.so.1`_thr_setup+0x52
26235368Sgnn *            libc.so.1`_lwp_start
27235368Sgnn *            101
28235368Sgnn *
29235368Sgnn * The lines at the top are Java frames, followed by the JVM (libjvm.so).
30235368Sgnn * The JVM symbols may be translated by passing the output through c++filt.
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#pragma D option quiet
52235368Sgnn#pragma D option jstackstrsize=1024
53235368Sgnn
54235368Sgnn/*
55235368Sgnn * Tunables
56235368Sgnn */
57235368Sgnn#define DEPTH	10		/* stack depth, frames */
58235368Sgnn#define RATE	101		/* sampling rate, Hertz */
59235368Sgnn#define TOP	25		/* number of stacks to output */
60235368Sgnn
61235368Sgnndtrace:::BEGIN
62235368Sgnn{
63235368Sgnn	printf("Sampling %d-level stacks at %d Hertz... Hit Ctrl-C to end.\n",
64235368Sgnn	    DEPTH, RATE);
65235368Sgnn}
66235368Sgnn
67235368Sgnnprofile-RATE
68235368Sgnn/pid == $target/
69235368Sgnn{
70235368Sgnn	@stacks[jstack(DEPTH)] = count();
71235368Sgnn}
72235368Sgnn
73235368Sgnndtrace:::END
74235368Sgnn{
75235368Sgnn	trunc(@stacks, TOP);
76235368Sgnn	printf("Top %d most frequently sampled stacks,\n", TOP);
77235368Sgnn	printa(@stacks);
78235368Sgnn}
79