-
Notifications
You must be signed in to change notification settings - Fork 814
/
JfrReader.java
552 lines (486 loc) · 17.8 KB
/
JfrReader.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* Copyright 2020 Andrei Pangin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package one.jfr;
import one.jfr.event.*;
import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Parses JFR output produced by async-profiler.
*/
public class JfrReader implements Closeable {
private static final int BUFFER_SIZE = 2 * 1024 * 1024;
private static final int CHUNK_HEADER_SIZE = 68;
private static final int CHUNK_SIGNATURE = 0x464c5200;
private final FileChannel ch;
private ByteBuffer buf;
private long filePosition;
public boolean incomplete;
public long startNanos = Long.MAX_VALUE;
public long endNanos = Long.MIN_VALUE;
public long startTicks = Long.MAX_VALUE;
public long ticksPerSec;
public final Dictionary<JfrClass> types = new Dictionary<>();
public final Map<String, JfrClass> typesByName = new HashMap<>();
public final Dictionary<String> threads = new Dictionary<>();
public final Dictionary<ClassRef> classes = new Dictionary<>();
public final Dictionary<byte[]> symbols = new Dictionary<>();
public final Dictionary<MethodRef> methods = new Dictionary<>();
public final Dictionary<StackTrace> stackTraces = new Dictionary<>();
public final Map<Integer, String> frameTypes = new HashMap<>();
public final Map<Integer, String> threadStates = new HashMap<>();
public final Map<String, String> settings = new HashMap<>();
private int executionSample;
private int nativeMethodSample;
private int allocationInNewTLAB;
private int allocationOutsideTLAB;
private int allocationSample;
private int liveObject;
private int monitorEnter;
private int threadPark;
private int activeSetting;
private boolean activeSettingHasStack;
public JfrReader(String fileName) throws IOException {
this.ch = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ);
this.buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
buf.flip();
ensureBytes(CHUNK_HEADER_SIZE);
if (!readChunk(0)) {
throw new IOException("Incomplete JFR file");
}
}
@Override
public void close() throws IOException {
ch.close();
}
public long durationNanos() {
return endNanos - startNanos;
}
public long nanosToTicks(long nanos) {
return (long) ((nanos - startNanos) * (ticksPerSec / 1e9)) + startTicks;
}
public List<Event> readAllEvents() throws IOException {
return readAllEvents(null);
}
public <E extends Event> List<E> readAllEvents(Class<E> cls) throws IOException {
ArrayList<E> events = new ArrayList<>();
for (E event; (event = readEvent(cls)) != null; ) {
events.add(event);
}
Collections.sort(events);
return events;
}
public Event readEvent() throws IOException {
return readEvent(null);
}
@SuppressWarnings("unchecked")
public <E extends Event> E readEvent(Class<E> cls) throws IOException {
while (ensureBytes(CHUNK_HEADER_SIZE)) {
int pos = buf.position();
int size = getVarint();
int type = getVarint();
if (type == 'L' && buf.getInt(pos) == CHUNK_SIGNATURE) {
if (readChunk(pos)) {
continue;
}
break;
}
if (type == executionSample || type == nativeMethodSample) {
if (cls == null || cls == ExecutionSample.class) return (E) readExecutionSample();
} else if (type == allocationInNewTLAB) {
if (cls == null || cls == AllocationSample.class) return (E) readAllocationSample(true);
} else if (type == allocationOutsideTLAB || type == allocationSample) {
if (cls == null || cls == AllocationSample.class) return (E) readAllocationSample(false);
} else if (type == liveObject) {
if (cls == null || cls == LiveObject.class) return (E) readLiveObject();
} else if (type == monitorEnter) {
if (cls == null || cls == ContendedLock.class) return (E) readContendedLock(false);
} else if (type == threadPark) {
if (cls == null || cls == ContendedLock.class) return (E) readContendedLock(true);
} else if (type == activeSetting) {
readActiveSetting();
}
if ((pos += size) <= buf.limit()) {
buf.position(pos);
} else {
seek(filePosition + pos);
}
}
return null;
}
private ExecutionSample readExecutionSample() {
long time = getVarlong();
int tid = getVarint();
int stackTraceId = getVarint();
int threadState = getVarint();
return new ExecutionSample(time, tid, stackTraceId, threadState);
}
private AllocationSample readAllocationSample(boolean tlab) {
long time = getVarlong();
int tid = getVarint();
int stackTraceId = getVarint();
int classId = getVarint();
long allocationSize = getVarlong();
long tlabSize = tlab ? getVarlong() : 0;
return new AllocationSample(time, tid, stackTraceId, classId, allocationSize, tlabSize);
}
private LiveObject readLiveObject() {
long time = getVarlong();
int tid = getVarint();
int stackTraceId = getVarint();
int classId = getVarint();
long allocationSize = getVarlong();
long allocatimeTime = getVarlong();
return new LiveObject(time, tid, stackTraceId, classId, allocationSize, allocatimeTime);
}
private ContendedLock readContendedLock(boolean hasTimeout) {
long time = getVarlong();
long duration = getVarlong();
int tid = getVarint();
int stackTraceId = getVarint();
int classId = getVarint();
if (hasTimeout) getVarlong();
long until = getVarlong();
long address = getVarlong();
return new ContendedLock(time, tid, stackTraceId, duration, classId);
}
private void readActiveSetting() {
long time = getVarlong();
long duration = getVarlong();
int tid = getVarint();
if (activeSettingHasStack) getVarint();
long id = getVarlong();
String name = getString();
String value = getString();
settings.put(name, value);
}
private boolean readChunk(int pos) throws IOException {
if (pos + CHUNK_HEADER_SIZE > buf.limit() || buf.getInt(pos) != CHUNK_SIGNATURE) {
throw new IOException("Not a valid JFR file");
}
int version = buf.getInt(pos + 4);
if (version < 0x20000 || version > 0x2ffff) {
throw new IOException("Unsupported JFR version: " + (version >>> 16) + "." + (version & 0xffff));
}
long cpOffset = buf.getLong(pos + 16);
long metaOffset = buf.getLong(pos + 24);
if (cpOffset == 0 || metaOffset == 0) {
incomplete = true;
return false;
}
startNanos = Math.min(startNanos, buf.getLong(pos + 32));
endNanos = Math.max(endNanos, buf.getLong(pos + 32) + buf.getLong(pos + 40));
startTicks = Math.min(startTicks, buf.getLong(pos + 48));
ticksPerSec = buf.getLong(pos + 56);
types.clear();
typesByName.clear();
long chunkStart = filePosition + pos;
readMeta(chunkStart + metaOffset);
readConstantPool(chunkStart + cpOffset);
cacheEventTypes();
seek(chunkStart + CHUNK_HEADER_SIZE);
return true;
}
private void readMeta(long metaOffset) throws IOException {
seek(metaOffset);
ensureBytes(5);
ensureBytes(getVarint() - buf.position());
getVarint();
getVarlong();
getVarlong();
getVarlong();
String[] strings = new String[getVarint()];
for (int i = 0; i < strings.length; i++) {
strings[i] = getString();
}
readElement(strings);
}
private Element readElement(String[] strings) {
String name = strings[getVarint()];
int attributeCount = getVarint();
Map<String, String> attributes = new HashMap<>(attributeCount);
for (int i = 0; i < attributeCount; i++) {
attributes.put(strings[getVarint()], strings[getVarint()]);
}
Element e = createElement(name, attributes);
int childCount = getVarint();
for (int i = 0; i < childCount; i++) {
e.addChild(readElement(strings));
}
return e;
}
private Element createElement(String name, Map<String, String> attributes) {
switch (name) {
case "class": {
JfrClass type = new JfrClass(attributes);
if (!attributes.containsKey("superType")) {
types.put(type.id, type);
}
typesByName.put(type.name, type);
return type;
}
case "field":
return new JfrField(attributes);
default:
return new Element();
}
}
private void readConstantPool(long cpOffset) throws IOException {
long delta;
do {
seek(cpOffset);
ensureBytes(5);
ensureBytes(getVarint() - buf.position());
getVarint();
getVarlong();
getVarlong();
delta = getVarlong();
getVarint();
int poolCount = getVarint();
for (int i = 0; i < poolCount; i++) {
int type = getVarint();
readConstants(types.get(type));
}
} while (delta != 0 && (cpOffset += delta) > 0);
}
private void readConstants(JfrClass type) {
switch (type.name) {
case "jdk.types.ChunkHeader":
buf.position(buf.position() + (CHUNK_HEADER_SIZE + 3));
break;
case "java.lang.Thread":
readThreads(type.fields.size());
break;
case "java.lang.Class":
readClasses(type.fields.size());
break;
case "jdk.types.Symbol":
readSymbols();
break;
case "jdk.types.Method":
readMethods();
break;
case "jdk.types.StackTrace":
readStackTraces();
break;
case "jdk.types.FrameType":
readMap(frameTypes);
break;
case "jdk.types.ThreadState":
readMap(threadStates);
break;
default:
readOtherConstants(type.fields);
}
}
private void readThreads(int fieldCount) {
int count = threads.preallocate(getVarint());
for (int i = 0; i < count; i++) {
long id = getVarlong();
String osName = getString();
int osThreadId = getVarint();
String javaName = getString();
long javaThreadId = getVarlong();
readFields(fieldCount - 4);
threads.put(id, javaName != null ? javaName : osName);
}
}
private void readClasses(int fieldCount) {
int count = classes.preallocate(getVarint());
for (int i = 0; i < count; i++) {
long id = getVarlong();
long loader = getVarlong();
long name = getVarlong();
long pkg = getVarlong();
int modifiers = getVarint();
readFields(fieldCount - 4);
classes.put(id, new ClassRef(name));
}
}
private void readMethods() {
int count = methods.preallocate(getVarint());
for (int i = 0; i < count; i++) {
long id = getVarlong();
long cls = getVarlong();
long name = getVarlong();
long sig = getVarlong();
int modifiers = getVarint();
int hidden = getVarint();
methods.put(id, new MethodRef(cls, name, sig));
}
}
private void readStackTraces() {
int count = stackTraces.preallocate(getVarint());
for (int i = 0; i < count; i++) {
long id = getVarlong();
int truncated = getVarint();
StackTrace stackTrace = readStackTrace();
stackTraces.put(id, stackTrace);
}
}
private StackTrace readStackTrace() {
int depth = getVarint();
long[] methods = new long[depth];
byte[] types = new byte[depth];
int[] locations = new int[depth];
for (int i = 0; i < depth; i++) {
methods[i] = getVarlong();
int line = getVarint();
int bci = getVarint();
locations[i] = line << 16 | (bci & 0xffff);
types[i] = buf.get();
}
return new StackTrace(methods, types, locations);
}
private void readSymbols() {
int count = symbols.preallocate(getVarint());
for (int i = 0; i < count; i++) {
long id = getVarlong();
if (buf.get() != 3) {
throw new IllegalArgumentException("Invalid symbol encoding");
}
symbols.put(id, getBytes());
}
}
private void readMap(Map<Integer, String> map) {
int count = getVarint();
for (int i = 0; i < count; i++) {
map.put(getVarint(), getString());
}
}
private void readOtherConstants(List<JfrField> fields) {
int stringType = getTypeId("java.lang.String");
boolean[] numeric = new boolean[fields.size()];
for (int i = 0; i < numeric.length; i++) {
JfrField f = fields.get(i);
numeric[i] = f.constantPool || f.type != stringType;
}
int count = getVarint();
for (int i = 0; i < count; i++) {
getVarlong();
readFields(numeric);
}
}
private void readFields(boolean[] numeric) {
for (boolean n : numeric) {
if (n) {
getVarlong();
} else {
getString();
}
}
}
private void readFields(int count) {
while (count-- > 0) {
getVarlong();
}
}
private void cacheEventTypes() {
executionSample = getTypeId("jdk.ExecutionSample");
nativeMethodSample = getTypeId("jdk.NativeMethodSample");
allocationInNewTLAB = getTypeId("jdk.ObjectAllocationInNewTLAB");
allocationOutsideTLAB = getTypeId("jdk.ObjectAllocationOutsideTLAB");
allocationSample = getTypeId("jdk.ObjectAllocationSample");
liveObject = getTypeId("profiler.LiveObject");
monitorEnter = getTypeId("jdk.JavaMonitorEnter");
threadPark = getTypeId("jdk.ThreadPark");
activeSetting = getTypeId("jdk.ActiveSetting");
activeSettingHasStack = activeSetting >= 0 && typesByName.get("jdk.ActiveSetting").field("stackTrace") != null;
}
private int getTypeId(String typeName) {
JfrClass type = typesByName.get(typeName);
return type != null ? type.id : -1;
}
private int getVarint() {
int result = 0;
for (int shift = 0; ; shift += 7) {
byte b = buf.get();
result |= (b & 0x7f) << shift;
if (b >= 0) {
return result;
}
}
}
private long getVarlong() {
long result = 0;
for (int shift = 0; shift < 56; shift += 7) {
byte b = buf.get();
result |= (b & 0x7fL) << shift;
if (b >= 0) {
return result;
}
}
return result | (buf.get() & 0xffL) << 56;
}
private String getString() {
switch (buf.get()) {
case 0:
return null;
case 1:
return "";
case 3:
return new String(getBytes(), StandardCharsets.UTF_8);
case 4: {
char[] chars = new char[getVarint()];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) getVarint();
}
return new String(chars);
}
case 5:
return new String(getBytes(), StandardCharsets.ISO_8859_1);
default:
throw new IllegalArgumentException("Invalid string encoding");
}
}
private byte[] getBytes() {
byte[] bytes = new byte[getVarint()];
buf.get(bytes);
return bytes;
}
private void seek(long pos) throws IOException {
filePosition = pos;
ch.position(pos);
buf.rewind().flip();
}
private boolean ensureBytes(int needed) throws IOException {
if (buf.remaining() >= needed) {
return true;
}
filePosition += buf.position();
if (buf.capacity() < needed) {
ByteBuffer newBuf = ByteBuffer.allocateDirect(needed);
newBuf.put(buf);
buf = newBuf;
} else {
buf.compact();
}
while (ch.read(buf) > 0 && buf.position() < needed) {
// keep reading
}
buf.flip();
return buf.limit() > 0;
}
}