Skip to content

Commit 9a706d5

Browse files
committed
Update async-profiler to v4.2
1 parent 1dd7b89 commit 9a706d5

File tree

5 files changed

+152
-2
lines changed

5 files changed

+152
-2
lines changed

spark-common/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ license {
1212

1313
dependencies {
1414
api project(':spark-api')
15-
implementation 'tools.profiler:async-profiler:4.1' // spark native version: 5930966 (v4.1)
15+
implementation 'tools.profiler:async-profiler:4.2' // spark native version: 8f7e4e1 (v4.2)
1616
implementation 'org.ow2.asm:asm:9.7'
1717
implementation 'net.bytebuddy:byte-buddy-agent:1.14.17'
1818
implementation 'com.google.protobuf:protobuf-javalite:4.31.1'

spark-common/src/main/java/me/lucko/spark/common/sampler/async/jfr/JfrReader.java

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class JfrReader implements Closeable {
5555
public final Dictionary<JfrClass> types = new Dictionary<>();
5656
public final Map<String, JfrClass> typesByName = new HashMap<>();
5757
public final Dictionary<String> threads = new Dictionary<>();
58+
public final Dictionary<Long> javaThreads = new Dictionary<>();
5859
public final Dictionary<ClassRef> classes = new Dictionary<>();
5960
public final Dictionary<String> strings = new Dictionary<>();
6061
public final Dictionary<byte[]> symbols = new Dictionary<>();
@@ -69,6 +70,7 @@ public class JfrReader implements Closeable {
6970
private int executionSample;
7071
private int nativeMethodSample;
7172
private int wallClockSample;
73+
private int methodTrace;
7274
private int allocationInNewTLAB;
7375
private int allocationOutsideTLAB;
7476
private int allocationSample;
@@ -78,6 +80,8 @@ public class JfrReader implements Closeable {
7880
private int activeSetting;
7981
private int malloc;
8082
private int free;
83+
private int cpuTimeSample;
84+
private int nativeLock;
8185

8286
public JfrReader(Path path) throws IOException { // spark - Path instead of String
8387
this.ch = FileChannel.open(path, StandardOpenOption.READ); // spark - Path instead of String
@@ -179,10 +183,14 @@ public <E extends Event> E readEvent(Class<E> cls) throws IOException {
179183
if (cls == null || cls == ExecutionSample.class) return (E) readExecutionSample(false);
180184
} else if (type == wallClockSample) {
181185
if (cls == null || cls == ExecutionSample.class) return (E) readExecutionSample(true);
186+
} else if (type == methodTrace) {
187+
if (cls == null || cls == MethodTrace.class) return (E) readMethodTrace();
182188
} else if (type == allocationInNewTLAB) {
183189
if (cls == null || cls == AllocationSample.class) return (E) readAllocationSample(true);
184190
} else if (type == allocationOutsideTLAB || type == allocationSample) {
185191
if (cls == null || cls == AllocationSample.class) return (E) readAllocationSample(false);
192+
} else if (type == cpuTimeSample) {
193+
if (cls == null || cls == ExecutionSample.class) return (E) readCPUTimeSample();
186194
} else if (type == malloc) {
187195
if (cls == null || cls == MallocEvent.class) return (E) readMallocEvent(true);
188196
} else if (type == free) {
@@ -193,6 +201,8 @@ public <E extends Event> E readEvent(Class<E> cls) throws IOException {
193201
if (cls == null || cls == ContendedLock.class) return (E) readContendedLock(false);
194202
} else if (type == threadPark) {
195203
if (cls == null || cls == ContendedLock.class) return (E) readContendedLock(true);
204+
} else if (type == nativeLock) {
205+
if (cls == null || cls == NativeLockEvent.class) return (E) readNativeLockEvent();
196206
} else if (type == activeSetting) {
197207
readActiveSetting();
198208
} else {
@@ -224,6 +234,15 @@ private ExecutionSample readExecutionSample(boolean hasSamples) {
224234
return new ExecutionSample(time, tid, stackTraceId, threadState, samples);
225235
}
226236

237+
private MethodTrace readMethodTrace() {
238+
long startTime = getVarlong();
239+
long duration = getVarlong();
240+
int tid = getVarint();
241+
int stackTraceId = getVarint();
242+
int method = getVarint();
243+
return new MethodTrace(startTime, tid, stackTraceId, method, duration);
244+
}
245+
227246
private AllocationSample readAllocationSample(boolean tlab) {
228247
long time = getVarlong();
229248
int tid = getVarint();
@@ -234,6 +253,25 @@ private AllocationSample readAllocationSample(boolean tlab) {
234253
return new AllocationSample(time, tid, stackTraceId, classId, allocationSize, tlabSize);
235254
}
236255

256+
private ExecutionSample readCPUTimeSample() {
257+
long time = getVarlong();
258+
int stackTraceId = getVarint();
259+
int tid = getVarint();
260+
boolean failed = getBoolean();
261+
long samplingPeriod = getVarlong();
262+
boolean biased = getBoolean();
263+
return new ExecutionSample(time, tid, stackTraceId, ExecutionSample.CPU_TIME_SAMPLE, 1);
264+
}
265+
266+
private NativeLockEvent readNativeLockEvent() {
267+
long time = getVarlong();
268+
long duration = getVarlong();
269+
int tid = getVarint();
270+
int stackTraceId = getVarint();
271+
long address = getVarlong();
272+
return new NativeLockEvent(time, tid, stackTraceId, address, duration);
273+
}
274+
237275
private MallocEvent readMallocEvent(boolean hasSize) {
238276
long time = getVarlong();
239277
int tid = getVarint();
@@ -429,7 +467,9 @@ private void readConstants(JfrClass type) {
429467
}
430468

431469
private void readThreads(int fieldCount) {
432-
int count = threads.preallocate(getVarint());
470+
int count = getVarint();
471+
threads.preallocate(count);
472+
javaThreads.preallocate(count);
433473
for (int i = 0; i < count; i++) {
434474
long id = getVarlong();
435475
String osName = getString();
@@ -438,6 +478,7 @@ private void readThreads(int fieldCount) {
438478
long javaThreadId = getVarlong();
439479
readFields(fieldCount - 4);
440480
threads.put(id, javaName != null ? javaName : osName);
481+
javaThreads.put(id, javaThreadId);
441482
}
442483
}
443484

@@ -556,6 +597,7 @@ private void cacheEventTypes() {
556597
executionSample = getTypeId("jdk.ExecutionSample");
557598
nativeMethodSample = getTypeId("jdk.NativeMethodSample");
558599
wallClockSample = getTypeId("profiler.WallClockSample");
600+
methodTrace = getTypeId("jdk.MethodTrace");
559601
allocationInNewTLAB = getTypeId("jdk.ObjectAllocationInNewTLAB");
560602
allocationOutsideTLAB = getTypeId("jdk.ObjectAllocationOutsideTLAB");
561603
allocationSample = getTypeId("jdk.ObjectAllocationSample");
@@ -565,11 +607,14 @@ private void cacheEventTypes() {
565607
activeSetting = getTypeId("jdk.ActiveSetting");
566608
malloc = getTypeId("profiler.Malloc");
567609
free = getTypeId("profiler.Free");
610+
cpuTimeSample = getTypeId("jdk.CPUTimeSample");
611+
nativeLock = getTypeId("profiler.NativeLock");
568612

569613
registerEvent("jdk.CPULoad", CPULoad.class);
570614
registerEvent("jdk.GCHeapSummary", GCHeapSummary.class);
571615
registerEvent("jdk.ObjectCount", ObjectCount.class);
572616
registerEvent("jdk.ObjectCountAfterGC", ObjectCount.class);
617+
registerEvent("profiler.ProcessSample", ProcessSample.class);
573618
}
574619

575620
private int getTypeId(String typeName) {
@@ -624,6 +669,14 @@ public double getDouble() {
624669
return buf.getDouble();
625670
}
626671

672+
public byte getByte() {
673+
return buf.get();
674+
}
675+
676+
public boolean getBoolean() {
677+
return buf.get() != 0;
678+
}
679+
627680
public String getString() {
628681
switch (buf.get()) {
629682
case 0:
@@ -911,6 +964,10 @@ public long value() {
911964
}
912965

913966
public static class ExecutionSample extends Event {
967+
// Synthetic thread state to distinguish samples converted from jdk.CPUTimeSample event.
968+
// A small constant suitable for BitSet, does not clash with any existing thread state.
969+
public static final int CPU_TIME_SAMPLE = 254;
970+
914971
public final int threadState;
915972
public final int samples;
916973

@@ -1018,4 +1075,97 @@ public ObjectCount(JfrReader jfr) {
10181075
this.totalSize = jfr.getVarlong();
10191076
}
10201077
}
1078+
1079+
static class MethodTrace extends Event {
1080+
public final int method;
1081+
public final long duration;
1082+
1083+
public MethodTrace(long time, int tid, int stackTraceId, int method, long duration) {
1084+
super(time, tid, stackTraceId);
1085+
this.method = method;
1086+
this.duration = duration;
1087+
}
1088+
1089+
@Override
1090+
public int hashCode() {
1091+
return method * 127 + stackTraceId;
1092+
}
1093+
1094+
@Override
1095+
public boolean sameGroup(Event o) {
1096+
if (o instanceof MethodTrace) {
1097+
MethodTrace c = (MethodTrace) o;
1098+
return method == c.method;
1099+
}
1100+
return false;
1101+
}
1102+
1103+
@Override
1104+
public long value() {
1105+
return duration;
1106+
}
1107+
}
1108+
1109+
static class ProcessSample extends Event {
1110+
public final int pid;
1111+
public final int ppid;
1112+
public final String name;
1113+
public final String cmdLine;
1114+
public final int uid;
1115+
public final byte state;
1116+
public final long processStartTime;
1117+
public final float cpuUser;
1118+
public final float cpuSystem;
1119+
public final float cpuPercent;
1120+
public final int threads;
1121+
public final long vmSize;
1122+
public final long vmRss;
1123+
public final long rssAnon;
1124+
public final long rssFiles;
1125+
public final long rssShmem;
1126+
public final long minorFaults;
1127+
public final long majorFaults;
1128+
public final long ioRead;
1129+
public final long ioWrite;
1130+
1131+
public ProcessSample(JfrReader jfr) {
1132+
super(jfr.getVarlong(), 0, 0);
1133+
this.pid = jfr.getVarint();
1134+
this.ppid = jfr.getVarint();
1135+
this.name = jfr.getString();
1136+
this.cmdLine = jfr.getString();
1137+
this.uid = jfr.getVarint();
1138+
this.state = jfr.getByte();
1139+
this.processStartTime = jfr.getVarlong();
1140+
this.cpuUser = jfr.getFloat();
1141+
this.cpuSystem = jfr.getFloat();
1142+
this.cpuPercent = jfr.getFloat();
1143+
this.threads = jfr.getVarint();
1144+
this.vmSize = jfr.getVarlong();
1145+
this.vmRss = jfr.getVarlong();
1146+
this.rssAnon = jfr.getVarlong();
1147+
this.rssFiles = jfr.getVarlong();
1148+
this.rssShmem = jfr.getVarlong();
1149+
this.minorFaults = jfr.getVarlong();
1150+
this.majorFaults = jfr.getVarlong();
1151+
this.ioRead = jfr.getVarlong();
1152+
this.ioWrite = jfr.getVarlong();
1153+
}
1154+
}
1155+
1156+
static class NativeLockEvent extends Event {
1157+
public final long address;
1158+
public final long duration;
1159+
1160+
public NativeLockEvent(long time, int tid, int stackTraceId, long address, long duration) {
1161+
super(time, tid, stackTraceId);
1162+
this.address = address;
1163+
this.duration = duration;
1164+
}
1165+
1166+
@Override
1167+
public long value() {
1168+
return duration;
1169+
}
1170+
}
10211171
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)