j_cputime.d revision 1.1 1 1.1 christos #!/usr/sbin/dtrace -CZs
2 1.1 christos /*
3 1.1 christos * j_cputime.d - measure Java on-CPU times for different types of operation.
4 1.1 christos * Written for the Java hotspot DTrace provider.
5 1.1 christos *
6 1.1 christos * $Id: j_cputime.d,v 1.1 2015/09/30 22:01:09 christos Exp $
7 1.1 christos *
8 1.1 christos * This traces activity from all Java processes on the system with hotspot
9 1.1 christos * provider support (1.6.0). Method calls are only visible when using the
10 1.1 christos * flag "+ExtendedDTraceProbes". eg, java -XX:+ExtendedDTraceProbes classfile
11 1.1 christos *
12 1.1 christos * USAGE: j_cputime.d [top] # hit Ctrl-C to end
13 1.1 christos *
14 1.1 christos * The "top" optional argument will truncate the output for each report
15 1.1 christos * section to that many lines, with a default of 10.
16 1.1 christos *
17 1.1 christos * FIELDS:
18 1.1 christos * PID Process ID
19 1.1 christos * TYPE Type of call (method/gc/total)
20 1.1 christos * NAME Name of call
21 1.1 christos * TOTAL Total on-CPU time for calls (us)
22 1.1 christos *
23 1.1 christos * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
24 1.1 christos *
25 1.1 christos * CDDL HEADER START
26 1.1 christos *
27 1.1 christos * The contents of this file are subject to the terms of the
28 1.1 christos * Common Development and Distribution License, Version 1.0 only
29 1.1 christos * (the "License"). You may not use this file except in compliance
30 1.1 christos * with the License.
31 1.1 christos *
32 1.1 christos * You can obtain a copy of the license at Docs/cddl1.txt
33 1.1 christos * or http://www.opensolaris.org/os/licensing.
34 1.1 christos * See the License for the specific language governing permissions
35 1.1 christos * and limitations under the License.
36 1.1 christos *
37 1.1 christos * CDDL HEADER END
38 1.1 christos *
39 1.1 christos * 09-Sep-2007 Brendan Gregg Created this.
40 1.1 christos */
41 1.1 christos
42 1.1 christos #define TOP 10 /* default output truncation */
43 1.1 christos #define B_FALSE 0
44 1.1 christos
45 1.1 christos #pragma D option quiet
46 1.1 christos #pragma D option defaultargs
47 1.1 christos
48 1.1 christos dtrace:::BEGIN
49 1.1 christos {
50 1.1 christos printf("Tracing... Hit Ctrl-C to end.\n");
51 1.1 christos top = $1 != 0 ? $1 : TOP;
52 1.1 christos }
53 1.1 christos
54 1.1 christos hotspot*:::method-entry
55 1.1 christos {
56 1.1 christos self->depth[arg0]++;
57 1.1 christos self->exclude[arg0, self->depth[arg0]] = 0;
58 1.1 christos self->method[arg0, self->depth[arg0]] = vtimestamp;
59 1.1 christos }
60 1.1 christos
61 1.1 christos hotspot*:::method-return
62 1.1 christos /self->method[arg0, self->depth[arg0]]/
63 1.1 christos {
64 1.1 christos this->oncpu_incl = vtimestamp - self->method[arg0, self->depth[arg0]];
65 1.1 christos this->oncpu_excl = this->oncpu_incl -
66 1.1 christos self->exclude[arg0, self->depth[arg0]];
67 1.1 christos self->method[arg0, self->depth[arg0]] = 0;
68 1.1 christos self->exclude[arg0, self->depth[arg0]] = 0;
69 1.1 christos
70 1.1 christos this->class = (char *)copyin(arg1, arg2 + 1);
71 1.1 christos this->class[arg2] = '\0';
72 1.1 christos this->method = (char *)copyin(arg3, arg4 + 1);
73 1.1 christos this->method[arg4] = '\0';
74 1.1 christos this->name = strjoin(strjoin(stringof(this->class), "."),
75 1.1 christos stringof(this->method));
76 1.1 christos
77 1.1 christos @num[pid, "method", this->name] = count();
78 1.1 christos @num[0, "total", "-"] = count();
79 1.1 christos @types_incl[pid, "method", this->name] = sum(this->oncpu_incl);
80 1.1 christos @types_excl[pid, "method", this->name] = sum(this->oncpu_excl);
81 1.1 christos @types_excl[0, "total", "-"] = sum(this->oncpu_excl);
82 1.1 christos
83 1.1 christos self->depth[arg0]--;
84 1.1 christos self->exclude[arg0, self->depth[arg0]] += this->oncpu_incl;
85 1.1 christos }
86 1.1 christos
87 1.1 christos hotspot*:::gc-begin
88 1.1 christos {
89 1.1 christos self->gc = vtimestamp;
90 1.1 christos self->full = (boolean_t)arg0;
91 1.1 christos }
92 1.1 christos
93 1.1 christos hotspot*:::gc-end
94 1.1 christos /self->gc/
95 1.1 christos {
96 1.1 christos this->oncpu = vtimestamp - self->gc;
97 1.1 christos self->gc = 0;
98 1.1 christos
99 1.1 christos @num[pid, "gc", self->full == B_FALSE ? "GC" : "Full GC"] = count();
100 1.1 christos @types[pid, "gc", self->full == B_FALSE ? "GC" : "Full GC"] =
101 1.1 christos sum(this->oncpu);
102 1.1 christos self->full = 0;
103 1.1 christos }
104 1.1 christos
105 1.1 christos dtrace:::END
106 1.1 christos {
107 1.1 christos trunc(@num, top);
108 1.1 christos printf("\nTop %d counts,\n", top);
109 1.1 christos printf(" %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "COUNT");
110 1.1 christos printa(" %6d %-10s %-48s %@8d\n", @num);
111 1.1 christos
112 1.1 christos trunc(@types, top);
113 1.1 christos normalize(@types, 1000);
114 1.1 christos printf("\nTop %d on-CPU times (us),\n", top);
115 1.1 christos printf(" %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
116 1.1 christos printa(" %6d %-10s %-48s %@8d\n", @types);
117 1.1 christos
118 1.1 christos trunc(@types_excl, top);
119 1.1 christos normalize(@types_excl, 1000);
120 1.1 christos printf("\nTop %d exclusive method on-CPU times (us),\n", top);
121 1.1 christos printf(" %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
122 1.1 christos printa(" %6d %-10s %-48s %@8d\n", @types_excl);
123 1.1 christos
124 1.1 christos trunc(@types_incl, top);
125 1.1 christos normalize(@types_incl, 1000);
126 1.1 christos printf("\nTop %d inclusive method on-CPU times (us),\n", top);
127 1.1 christos printf(" %6s %-10s %-48s %8s\n", "PID", "TYPE", "NAME", "TOTAL");
128 1.1 christos printa(" %6d %-10s %-48s %@8d\n", @types_incl);
129 1.1 christos }
130