forked from btraceio/btrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfiler.java
More file actions
227 lines (206 loc) · 8.39 KB
/
Profiler.java
File metadata and controls
227 lines (206 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the Classpath exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.btrace;
import com.sun.btrace.annotations.Property;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* Profiler is a highly specialized aggregation-like data collector optimized
* for high-speed collection of the application execution call tree data.
* <p>
* It is exposable as MBean via {@linkplain Property} annotation
*
* @since 1.2
*
* @author Jaroslav Bachorik
*/
public abstract class Profiler {
/**
* Record represents an atomic unit in the application execution call tree
*
* @since 1.2
*/
final public static class Record {
final public static Comparator<Record> COMPARATOR = new Comparator<Record>() {
@Override
public int compare(Record o1, Record o2) {
if (o1 == null && o2 != null) return 1;
if (o1 != null && o2 == null) return -1;
if (o1 == null && o2 == null) return 0;
return o1.blockName.compareTo(o2.blockName);
}
};
final public String blockName;
public long wallTime = 0, wallTimeMax = 0, wallTimeMin = Long.MAX_VALUE;
public long selfTime = 0, selfTimeMax = 0, selfTimeMin = Long.MAX_VALUE;
public long invocations = 1;
public boolean onStack = false;
public Record referring = null;
public Record(String blockName) {
this.blockName = blockName;
}
public Record duplicate() {
Record r = new Record(blockName);
r.invocations = invocations;
r.selfTime = selfTime;
r.selfTimeMax = selfTimeMax;
r.selfTimeMin = selfTimeMin;
r.wallTime = wallTime;
r.wallTimeMax = wallTimeMax;
r.wallTimeMin = wallTimeMin;
return r;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Record other = (Record) obj;
if ((this.blockName == null) ? (other.blockName != null) : !this.blockName.equals(other.blockName)) {
return false;
}
if (this.wallTime != other.wallTime) {
return false;
}
if (this.selfTime != other.selfTime) {
return false;
}
return this.invocations == other.invocations;
}
@Override
public int hashCode() {
int hash = 7;
hash = 17 * hash + (this.blockName != null ? this.blockName.hashCode() : 0);
hash = 17 * hash + (int) (this.wallTime ^ (this.wallTime >>> 32));
hash = 17 * hash + (int) (this.selfTime ^ (this.selfTime >>> 32));
hash = 17 * hash + (int) (this.invocations ^ (this.invocations >>> 32));
return hash;
}
@Override
public String toString() {
return "Record{\n" + "\tblockName=" + blockName + "\n," +
"\twallTime=" + wallTime + ",\n" +
"\twallTime.min=" + wallTimeMin + ",\n" +
"\twallTime.max=" + wallTimeMax + ",\n" +
"\tselfTime=" + selfTime + ",\n" +
"\tselfTime.min=" + selfTimeMin + ",\n" +
"\tselfTime.max=" + selfTimeMax + ",\n" +
"\tinvocations=" + invocations + ",\nonStack=" + onStack + '}';
}
}
/**
* Snapshot is an immutable image of the current profiling data collected
* by the {@linkplain Profiler}
* <br><br>
* It is created by calling {@linkplain Profiler#snapshot()} method
*
* @since 1.2
*/
final public static class Snapshot {
final public long timeStamp;
final public long timeInterval;
final public Record[] total;
public Snapshot(Record[] data, long startTs, long stopTs) {
this.timeStamp = stopTs;
this.timeInterval = stopTs - startTs;
this.total = data;
}
List<Object[]> getGridData() {
List<Object[]> rslt = new ArrayList<Object[]>();
Object[] titleRow = new Object[]{"Block", "Invocations", "SelfTime.Total", "SelfTime.Avg", "SelfTime.Min",
"SelfTime.Max", "WallTime.Total", "WallTime.Avg", "WallTime.Min", "WallTime.Max"};
rslt.add(titleRow);
for(Record r : total) {
if (r != null) {
Object[] row = new Object[]{r.blockName, r.invocations, r.selfTime, r.selfTime / r.invocations,
r.selfTimeMin < Long.MAX_VALUE ? r.selfTimeMin : "N/A",
r.selfTimeMax > 0 ? r.selfTimeMax : "N/A",
r.wallTime, r.wallTime / r.invocations,
r.wallTimeMin < Long.MAX_VALUE ? r.wallTimeMin : "N/A",
r.wallTimeMax > 0 ? r.wallTimeMax : "N/A"
};
rslt.add(row);
}
}
return rslt;
}
}
/**
* Helper interface to make accessing a {@linkplain Profiler} as an MBean
* type safe.
*/
public static interface MBeanValueProvider {
Snapshot getMBeanValue();
}
/**
* This property exposes the time of creating this particular {@linkplain Profiler} instance.
* <br>
* The unit is milliseconds.
*/
final public long START_TIME;
/**
* Creates a new {@linkplain Profiler} instance
*/
public Profiler() {
this.START_TIME = System.currentTimeMillis();
}
/**
* Records the event of entering an execution unit (eg. method)<br>
* Must be paired with a call to {@linkplain Profiler#recordExit(java.lang.String, long) }
* with the same blockName, eventually
* @param blockName The execution unit identifier (eg. method FQN)
*/
public abstract void recordEntry(String blockName);
/**
* Records the event of exiting an execution unit (eg. method)<br>
* Must be preceded by a call to {@linkplain Profiler#recordEntry(java.lang.String) }
* with the same blockName
* @param blockName The execution unit identifier (eg. method FQN)
* @param duration Invocation duration in nanoseconds
*/
public abstract void recordExit(String blockName, long duration);
/**
* Creates an immutable snapshot of the collected profiling data
* @return Returns the immutable {@linkplain Snapshot} instance
*/
final public Snapshot snapshot() {
return snapshot(false);
}
/**
* Creates an immutable snapshot of the collected profiling data.<br>
* Makes it possible to reset the profiler after creating the snapshot, eventually
* @param reset Signals the profiler to perform reset right after getting the snapshot (in an atomic transaction)
* @return Returns the immutable {@linkplain Snapshot} instance
*/
public abstract Snapshot snapshot(boolean reset);
/**
* Resets all the collected data
*/
public abstract void reset();
}