Skip to content

Commit

Permalink
Pack stack traces in pprof format
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Mar 26, 2024
1 parent 5da0a5f commit 978fe2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/converter/one/convert/JfrToPprof.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ public void dump(OutputStream out) throws IOException {
}

private Proto sample(Proto s, Event event, long value) {
int packedLocations = s.startField(1);

long classId = event.classId();
if (classId != 0) {
int function = functions.index(getClassName(classId));
s.field(1, locations.index((long) function << 16));
s.writeInt(locations.index((long) function << 16));
}

StackTrace stackTrace = jfr.stackTraces.get(event.stackTraceId);
Expand All @@ -95,10 +97,11 @@ private Proto sample(Proto s, Event event, long value) {
for (int i = 0; i < methods.length; i++) {
String methodName = getMethodName(methods[i], types[i]);
int function = functions.index(methodName);
s.field(1, locations.index((long) function << 16 | lines[i] >>> 16));
s.writeInt(locations.index((long) function << 16 | lines[i] >>> 16));
}
}

s.commitField(packedLocations);
s.field(2, value);

if (args.threads && event.tid != 0) {
Expand Down
17 changes: 17 additions & 0 deletions src/converter/one/proto/Proto.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public Proto field(int index, Proto proto) {
return this;
}

public int startField(int index) {
tag(index, 2);
ensureCapacity(3);
return pos += 3;
}

public void commitField(int mark) {
int length = pos - mark;
if (length >= 1 << (7 * 3)) {
throw new IllegalArgumentException("Field too large");
}

buf[mark - 3] = (byte) (0x80 | (length & 0x7f));
buf[mark - 2] = (byte) (0x80 | ((length >>> 7) & 0x7f));
buf[mark - 1] = (byte) (length >>> 14);
}

public void writeInt(int n) {
int length = n == 0 ? 1 : (38 - Integer.numberOfLeadingZeros(n)) / 7;
ensureCapacity(length);
Expand Down

0 comments on commit 978fe2c

Please sign in to comment.