1132718Skan#!/usr/sbin/dtrace -Zs
2132718Skan/*
3132718Skan * j_stat.d - Java operation stats using DTrace.
4132718Skan *            Written for the Java hotspot DTrace provider.
5132718Skan *
6132718Skan * $Id: j_stat.d 64 2007-10-04 08:35:29Z claire $
7132718Skan *
8132718Skan * This traces activity from all Java processes on the system with hotspot
9132718Skan * provider support (1.6.0). Method calls and object allocation are only
10132718Skan * visible when using the flag "+ExtendedDTraceProbes". eg,
11132718Skan * java -XX:+ExtendedDTraceProbes classfile
12132718Skan *
13132718Skan * USAGE: j_stat.d [interval [count]]
14132718Skan *
15132718Skan * FIELDS:
16132718Skan *		EXEC/s		Java programs executed per second, including
17132718Skan *				those without Java provider support
18132718Skan *		THREAD/s	Threads created, per second
19132718Skan *		METHOD/s	Methods called, per second
20132718Skan *		OBJNEW/s	Objects created, per second
21132718Skan *		CLOAD/s		Class loads, per second
22132718Skan *		EXCP/s		Exceptions raised, per second
23132718Skan *		GC/s		Garbage collects, per second
24132718Skan *
25132718Skan * The numbers are per second counts for the interval specified. The default
26132718Skan * interval is 1 second.
27132718Skan *
28132718Skan * If you see a count in "EXECS" but not in the other columns, then your
29132718Skan * Java software is probably not running with the DTrace hotspot provider.
30132718Skan *
31132718Skan * If you see counts in "CLOAD" but not in "METHODS", then you Java
32132718Skan * software probably isn't running with "+ExtendedDTraceProbes".
33132718Skan *
34132718Skan * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
35132718Skan *
36132718Skan * CDDL HEADER START
37132718Skan *
38132718Skan *  The contents of this file are subject to the terms of the
39132718Skan *  Common Development and Distribution License, Version 1.0 only
40132718Skan *  (the "License").  You may not use this file except in compliance
41132718Skan *  with the License.
42132718Skan *
43132718Skan *  You can obtain a copy of the license at Docs/cddl1.txt
44132718Skan *  or http://www.opensolaris.org/os/licensing.
45132718Skan *  See the License for the specific language governing permissions
46132718Skan *  and limitations under the License.
47132718Skan *
48132718Skan * CDDL HEADER END
49132718Skan *
50132718Skan * 09-Sep-2007	Brendan Gregg	Created this.
51132718Skan */
52132718Skan
53132718Skan#pragma D option quiet
54132718Skan#pragma D option defaultargs
55132718Skan
56132718Skaninline int SCREEN = 21;
57132718Skan
58132718Skandtrace:::BEGIN
59132718Skan{
60132718Skan	execs = threads = methods = objnew = cload = gc = exception = 0;
61132718Skan	lines = SCREEN + 1;
62132718Skan	interval = $1 ? $1 : 1;
63132718Skan	counts = $2 ? $2 : -1;
64132718Skan	secs = interval;
65132718Skan	first = 1;
66132718Skan}
67132718Skan
68132718Skanprofile:::tick-1sec
69132718Skan{
70132718Skan	secs--;
71132718Skan}
72132718Skan
73132718Skan/*
74132718Skan * Print Header
75132718Skan */
76132718Skandtrace:::BEGIN,
77132718Skanprofile:::tick-1sec
78132718Skan/first || (secs == 0 && lines > SCREEN)/
79132718Skan{
80132718Skan	printf("%-20s %6s %8s %8s %8s %8s %6s %6s\n", "TIME", "EXEC/s",
81132718Skan	    "THREAD/s", "METHOD/s", "OBJNEW/s", "CLOAD/s", "EXCP/s", "GC/s");
82132718Skan	lines = 0;
83132718Skan	first = 0;
84132718Skan}
85132718Skan
86132718Skan/*
87132718Skan * Tally Data
88132718Skan */
89132718Skanproc:::exec-success
90132718Skan/execname == "java"/
91132718Skan{
92132718Skan	execs++;
93132718Skan}
94132718Skan
95132718Skanhotspot*:::thread-start
96132718Skan{
97132718Skan	threads++;
98132718Skan}
99132718Skan
100132718Skanhotspot*:::method-entry
101132718Skan{
102132718Skan	methods++;
103132718Skan}
104132718Skan
105132718Skanhotspot*:::object-alloc
106132718Skan{
107132718Skan	oalloc++;
108132718Skan}
109132718Skan
110132718Skanhotspot*:::class-loaded
111132718Skan{
112132718Skan	cload++;
113132718Skan}
114132718Skan
115132718Skanhotspot*:::gc-begin
116132718Skan{
117132718Skan	gc++;
118132718Skan}
119132718Skan
120132718Skanhotspot*:::ExceptionOccurred-entry
121132718Skan{
122132718Skan	exception++;
123132718Skan}
124132718Skan
125132718Skan/*
126132718Skan * Print Output
127132718Skan */
128132718Skanprofile:::tick-1sec
129132718Skan/secs == 0/
130132718Skan{
131132718Skan	printf("%-20Y %6d %8d %8d %8d %8d %6d %6d\n", walltimestamp,
132132718Skan	    execs / interval, threads / interval, methods / interval,
133132718Skan	    oalloc / interval, cload / interval, exception / interval,
134132718Skan	    gc / interval);
135132718Skan	execs = threads = methods = oalloc = cload = gc = exception = 0;
136132718Skan	secs = interval;
137132718Skan	lines++;
138132718Skan	counts--;
139132718Skan}
140132718Skan
141132718Skan/*
142132718Skan * End
143132718Skan */
144132718Skanprofile:::tick-1sec
145132718Skan/counts == 0/
146132718Skan{
147132718Skan        exit(0);
148132718Skan}
149132718Skan