-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJfrOverview.java
executable file
·364 lines (332 loc) · 12.1 KB
/
JfrOverview.java
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.StringJoiner;
import java.util.stream.Stream;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordedFrame;
import jdk.jfr.consumer.RecordedMethod;
import jdk.jfr.consumer.RecordedStackTrace;
import jdk.jfr.consumer.RecordingFile;
public class JfrOverview {
private static double totalAllocated;
private static long firstAllocationTime = -1;
static class DoubleMeasure {
private int count = 0;
private double total;
private double max;
private double value;
public double getAverage() {
return total / count;
}
public void add(double value) {
this.count++;
this.total += value;
this.max = Math.max(this.value, value);
this.value = value;
}
@Override
public String toString() {
return "DoubleMeasure{count=" + count + ", max=" + max + ", total=" + total + ", value=" + value + "}";
}
}
static class LongMeasure {
private int count = 0;
private long total;
private long max;
private long value;
public LongMeasure() {
this.count = 0;
this.total = 0;
this.max = 0;
this.value = 0;
}
public LongMeasure(long value) {
this.count = 1;
this.total += value;
this.max = value;
this.value = value;
}
public double getAverage() {
if (count == 0) {
return total;
}
return total / count;
}
public void add(long value) {
this.count++;
this.total += value;
this.max = Math.max(this.value, value);
this.value = value;
}
}
private static List<String> decodeDescriptors(String descriptor) {
List<String> descriptors = new ArrayList<>();
for (int index = 0; index < descriptor.length(); index++) {
String arrayBrackets = "";
while (descriptor.charAt(index) == '[') {
arrayBrackets += "[]";
index++;
}
char c = descriptor.charAt(index);
String type;
switch (c) {
case 'L':
int endIndex = descriptor.indexOf(';', index);
type = descriptor.substring(index + 1, endIndex);
index = endIndex;
break;
case 'I':
type = "int";
break;
case 'J':
type = "long";
break;
case 'Z':
type = "boolean";
break;
case 'D':
type = "double";
break;
case 'F':
type = "float";
break;
case 'S':
type = "short";
break;
case 'C':
type = "char";
break;
case 'B':
type = "byte";
break;
default:
type = "<unknown-descriptor-type>";
}
descriptors.add(type + arrayBrackets);
}
return descriptors;
}
private static String formatMethod(RecordedMethod m) {
StringBuilder sb = new StringBuilder();
String typeName = m.getType().getName();
typeName = typeName.substring(typeName.lastIndexOf('.') + 1);
sb.append(typeName).append(".").append(m.getName());
sb.append("(");
StringJoiner sj = new StringJoiner(", ");
String md = m.getDescriptor().replace("/", ".");
String parameter = md.substring(1, md.lastIndexOf(")"));
for (String qualifiedName : decodeDescriptors(parameter)) {
sj.add(qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1));
}
sb.append(sj.length() > 10 ? "..." : sj);
sb.append(")");
return sb.toString();
}
private static String topFrame(RecordedStackTrace stackTrace) {
if (stackTrace == null) {
return null;
}
List<RecordedFrame> frames = stackTrace.getFrames();
if (!frames.isEmpty()) {
RecordedFrame topFrame = frames.get(0);
if (topFrame.isJavaFrame()) {
return formatMethod(topFrame.getMethod());
}
}
return null;
}
public static record Record(String name, int count, long value, long total) {
String format() {
return "\"" + name + " total=" + total + ", count=" + count + "\"";
}
}
static class Histogram {
private static final HashMap<String, LongMeasure> histogram = new HashMap<>();
public void add(String key, long value) {
var measure = histogram.get(key);
if (measure == null) {
measure = new LongMeasure();
histogram.put(key, measure);
}
measure.add(value);
}
public List<Record> getRecords() {
ArrayList<Record> values = new ArrayList<>();
for (var e : histogram.entrySet()) {
var measure = e.getValue();
values.add(new Record(e.getKey(), measure.count, measure.value, measure.total));
}
return values;
}
public Stream<Record> byTotal() {
return getRecords()
.stream()
.sorted(Comparator.comparingLong(Record::total).reversed())
.limit(10);
}
public Stream<Record> byCount() {
return getRecords()
.stream()
.sorted(Comparator.comparingLong(Record::count).reversed())
.limit(10);
}
}
private static final LongMeasure ALLOC_TOTAL = new LongMeasure();
private static final DoubleMeasure ALLOC_RATE = new DoubleMeasure();
private static final Histogram ALLOCATION_TOP_FRAME = new Histogram();
private static void onAllocationSample(RecordedEvent event, long size) {
String topFrame = topFrame(event.getStackTrace());
if (topFrame != null) {
ALLOCATION_TOP_FRAME.add(topFrame, size);
}
ALLOC_TOTAL.add(size);
long timestamp = event.getEndTime().toEpochMilli();
totalAllocated += size;
if (firstAllocationTime > 0) {
long elapsedTime = timestamp - firstAllocationTime;
if (elapsedTime > 0) {
double rate = 1000.0 * (totalAllocated / elapsedTime);
ALLOC_RATE.add(rate);
}
} else {
firstAllocationTime = timestamp;
}
}
public static void main(String[] args) throws IOException {
if (args.length < 1) {
throw new IllegalArgumentException("Path to JFR recording required");
}
var recordingFile = new RecordingFile(Paths.get(args[0]));
DoubleMeasure machineCpu = new DoubleMeasure();
DoubleMeasure jvmSystem = new DoubleMeasure();
DoubleMeasure jvmUser = new DoubleMeasure();
LongMeasure youngGc = new LongMeasure();
LongMeasure oldGc = new LongMeasure();
LongMeasure usedHeap = new LongMeasure();
LongMeasure physicalMemory = new LongMeasure();
DoubleMeasure threads = new DoubleMeasure();
LongMeasure classes = new LongMeasure();
LongMeasure compilation = new LongMeasure();
LongMeasure initialHeap = new LongMeasure();
Histogram executionTopFrame = new Histogram();
String gcName = "Unknown";
while (recordingFile.hasMoreEvents()) {
var event = recordingFile.readEvent();
switch (event.getEventType().getName()) {
case "jdk.CPULoad" -> {
machineCpu.add(event.getDouble("machineTotal"));
jvmSystem.add(event.getDouble("jvmSystem"));
jvmUser.add(event.getDouble("jvmUser"));
}
case "jdk.YoungGarbageCollection" -> {
long nanos = event.getDuration().toNanos();
youngGc.add(nanos);
}
case "jdk.OldGarbageCollection" -> {
long nanos = event.getDuration().toNanos();
oldGc.add(nanos);
}
case "jdk.GCHeapSummary" -> {
usedHeap.add(event.getLong("heapUsed"));
}
case "jdk.PhysicalMemory" -> {
physicalMemory.add(event.getLong("totalSize"));
}
case "jdk.GCConfiguration" -> {
String gc = event.getString("oldCollector");
String yc = event.getString("youngCollector");
if (yc != null) {
gc += "/" + yc;
}
gcName = gc;
}
case "jdk.ObjectAllocationOutsideTLAB" -> {
onAllocationSample(event, event.getLong("allocationSize"));
}
case "jdk.ObjectAllocationInNewTLAB" -> {
onAllocationSample(event, event.getLong("tlabSize"));
}
case "jdk.ObjectAllocationSample" -> {
onAllocationSample(event, event.getLong("weight"));
}
case "jdk.ExecutionSample" -> {
String topFrame = topFrame(event.getStackTrace());
executionTopFrame.add(topFrame, 1);
}
case "jdk.JavaThreadStatistics" -> {
threads.add(event.getDouble("activeCount"));
}
case "jdk.ClassLoadingStatistics" -> {
long diff = event.getLong("loadedClassCount") - event.getLong("unloadedClassCount");
classes.add(diff);
}
case "jdk.Compilation" -> {
compilation.add(event.getDuration().toNanos());
}
case "jdk.GCHeapConfiguration" -> {
initialHeap.add(event.getLong("initialSize"));
}
}
}
String jsonResult = String.format(Locale.ENGLISH,
"""
{
"gc": {
"name": "%s",
"young": {
"count": %s,
"max_duration_ns": %s,
"avg_duration_ns": %s
},
"old": {
"count": %s,
"max_duration_ns": %s,
"avg_duration_ns": %s
}
},
"cpu": {
"system": %s,
"jvm_user": %s,
"jvm_system": %s
},
"heap": {
"initial": %s,
"used": %s
},
"alloc": {
"rate": %s,
"total": %s,
"top_frames_by_count": %s,
"top_frames_by_alloc": %s
},
"threads": %s,
"classes": %s
}
""",
gcName,
youngGc.count,
youngGc.max,
youngGc.getAverage(),
oldGc.count,
oldGc.max,
oldGc.getAverage(),
machineCpu.getAverage(),
jvmUser.getAverage(),
jvmSystem.getAverage(),
initialHeap.value,
usedHeap.value,
ALLOC_RATE.value,
ALLOC_TOTAL.total,
'[' + String.join(", ", ALLOCATION_TOP_FRAME.byCount().map(Record::format).toArray(String[]::new)) + ']',
'[' + String.join(", ", ALLOCATION_TOP_FRAME.byTotal().map(Record::format).toArray(String[]::new)) + ']',
threads.value,
classes.value
);
System.out.println(jsonResult);
}
}