GeezerVison is showing github.com — Keep native frames intact under --package-names (#5) · jonoffcpu/jonoffcpu@5f3cb64
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/github/lhotari/jonoffcpu/offline test/java/io/github/lhotari/jonoffcpu/offline
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +11-1Lines changed: 11 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -663,7 +663,17 @@ the whole time and ends each line in a `[sleeping]`, `[runqueue]` or | 663 | 663 | needs a profile whose capture recorded the split. `--package-names abbreviate` | 664 | 664 | shortens each Java frame's package to its initials (`i.n.c.e.Native.epollWait0`) | 665 | 665 | and `--package-names drop` removes it (`Native.epollWait0`); `full` is the | 666 | | -default. Stacks that become identical merge into one line, and | | 666 | +default. Only Java frames change: the native frames async-profiler records in | | 667 | +the Java stack (HotSpot, JNI libraries, libc, runtime stubs) keep their library | | 668 | +and symbol as written. | | 669 | + | | 670 | +| Frame | `abbreviate` | `drop` | | | 671 | +| --- | --- | --- | | | 672 | +| `io.netty.channel.epoll.Native.epollWait0` | `i.n.c.e.Native.epollWait0` | `Native.epollWait0` | | | 673 | +| `org.example.Cursor$$Lambda.0x0000000081a16ff8.run` | `o.e.Cursor$$Lambda.0x0000000081a16ff8.run` | `Cursor$$Lambda.0x0000000081a16ff8.run` | | | 674 | +| `libjvm.so.Unsafe_Park` | unchanged | unchanged | | | 675 | + | | 676 | +Stacks that become identical merge into one line, and | 667 | 677 | `--include`/`--exclude` still match the full names. Rendered with its defaults, a | 668 | 678 | profile reproduces `jonoffcpu-offcpu-stacks.collapsed` byte for byte. | 669 | 679 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/schema/jonoffcpu-profile.proto +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,8 @@ message Record { | 33 | 33 | } | 34 | 34 |
| 35 | 35 | message ProfileStart { | | 36 | + // 2 since FrameRecord.Kind JFR_NATIVE; a schema 1 profile tags every frame of its Java stacks JAVA. A | | 37 | + // reader refuses a schema it does not know rather than render frame kinds it cannot tell apart. | 36 | 38 | uint32 schema_version = 1; | 37 | 39 | // The writing tool, for example "jonoffcpu-correlator". | 38 | 40 | string producer = 2; | @@ -87,13 +89,17 @@ message FrameRecord { | 87 | 89 | JAVA = 1; | 88 | 90 | USER = 2; | 89 | 91 | KERNEL = 3; | | 92 | + // A frame of the Java stack that async-profiler did not record as Java code: frame type Native, C++ | | 93 | + // or Kernel, or no type. Its name is collapsed like a Java frame's ("libjvm.so.Unsafe_Park": the | | 94 | + // library, then the symbol) but is not a package-qualified Class.method. Since schema 2. | | 95 | + JFR_NATIVE = 4; | 90 | 96 | } | 91 | 97 | uint32 id = 1; | 92 | 98 | Kind kind = 2; | 93 | | - // A Java frame's collapsed name ("class.method"), or a native frame's symbol as the collector resolved | 94 | | - // it, including any "+0x" offset. | | 99 | + // A Java-stack frame's collapsed name ("class.method", or "library.symbol" for JFR_NATIVE), or a native | | 100 | + // frame's symbol as the collector resolved it, including any "+0x" offset. | 95 | 101 | uint32 name_string = 3; | 96 | | - // A native frame's module; 0 for Java frames and unresolved modules. | | 102 | + // A native frame's module; 0 for JAVA and JFR_NATIVE frames and unresolved modules. | 97 | 103 | uint32 module_string = 4; | 98 | 104 | } | 99 | 105 |
| |
Collapse file Copy file name to clipboardExpand all lines: jonoffcpu-correlator/OFFLINE.md +29-5Lines changed: 29 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -227,6 +227,19 @@ written before its first use and ids are dense from 1, so 0 always means absent. | 227 | 227 | Entries are written in one canonical order, so the same analysis gives the same | 228 | 228 | bytes whatever order its intervals matched in. | 229 | 229 |
| | 230 | +Each frame has a kind. Native stacks' frames are `USER` or `KERNEL`. A Java | | 231 | +stack's frame is `JAVA` when async-profiler recorded it as Java code (frame type | | 232 | +`Interpreted`, `JIT compiled`, `C1 compiled` or `Inlined`), and `JFR_NATIVE` | | 233 | +otherwise (`Native`, `C++`, `Kernel`, or no type): a frame such as | | 234 | +`libjvm.so.Unsafe_Park`, the library then the symbol, which only looks like a | | 235 | +package-qualified Java name. Stacks that differ only in a frame's type share a | | 236 | +collapsed key, so a frame position is `JAVA` only when every such stack agrees. | | 237 | +The kind changes no name, so the collapsed key and every rendered line stay the | | 238 | +same. `JFR_NATIVE` arrived with `schema_version` 2; a schema 1 profile tags every | | 239 | +Java-stack frame `JAVA` and still reads, and a reader that predates schema 2 | | 240 | +refuses a newer profile with `Unsupported stack profile schema` rather than | | 241 | +render it. | | 242 | + | 230 | 243 | An entry is keyed by the Java stack (at the collapsed file's class-and-method | 231 | 244 | granularity) and the switch-out reason with its raw task state, and by default | 232 | 245 | also by the kernel stack, the user stack and the thread name | @@ -278,8 +291,14 @@ a `sleeping` or `runqueue` slice leaves out. | 278 | 291 |
| 279 | 292 | `--package-names abbreviate` cuts each package segment of a Java frame to its | 280 | 293 | first letter (`io.netty.channel.epoll.Native.epollWait0` becomes | 281 | | -`i.n.c.e.Native.epollWait0`), and `drop` shows `Class.method`; a frame that is | 282 | | -not a package-qualified `Class.method` is left unchanged. It only changes the | | 294 | +`i.n.c.e.Native.epollWait0`), and `drop` shows `Class.method`; a hidden class's | | 295 | +`.0x…` suffix (`Cursor$$Lambda.0x0000000081a16ff8`) stays part of the class. Only | | 296 | +`JAVA` frames change: a `JFR_NATIVE` frame keeps its library and symbol | | 297 | +(`libjvm.so.Unsafe_Park`). As a fallback for schema 1 profiles, and as a second | | 298 | +line of defence, a name that reads as native is also left alone: one with a C++ | | 299 | +`::`, a shared-library segment (`.so.`, `.so.6.`), a leading `/` or `[`, or a | | 300 | +space. That rule can only prevent a rewrite, and a frame that is not a | | 301 | +package-qualified `Class.method` is left unchanged too. It only changes the | 283 | 302 | rendered text: lines that become identical are summed, filters match the full | 284 | 303 | names, and `--summary` records the mode as `packageNames`. | 285 | 304 |
| @@ -303,9 +322,14 @@ rendered to compute it. `merge` | 303 | 322 | sums identical entries and keeps every input's provenance; it refuses profiles | 304 | 323 | with different grouping, and thinned profiles, whose weights have no common scale. | 305 | 324 | It sums the split parts too, so an input without the split contributes its time | 306 | | -as unsplit and the merged profile has the split when any input has it. | 307 | | -`export` writes one row per entry with expanded stacks and the six split columns, | 308 | | -for tools such as DuckDB. | | 325 | +as unsplit and the merged profile has the split when any input has it. Java | | 326 | +stacks with the same frame names merge whatever kinds their inputs gave them, and | | 327 | +a frame any input calls `JFR_NATIVE` stays `JFR_NATIVE`, so a schema 1 input | | 328 | +cannot make a native frame rewritable. | | 329 | +`export` writes one row per entry with expanded stacks, the six split columns and, | | 330 | +last, `java_stack_kinds` (`javaStackKinds` in JSON Lines): each Java-stack | | 331 | +frame's kind, `java` or `native`, joined with `;` like `java_stack`. It is for | | 332 | +tools such as DuckDB. | 309 | 333 |
| 310 | 334 | ## Degradation | 311 | 335 |
| |
Collapse file Copy file name to clipboardExpand all lines: jonoffcpu-correlator/src/main/java/io/github/lhotari/jonoffcpu/offline/JfrDictionaries.java +46-4Lines changed: 46 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | import java.util.List; | 9 | 9 | import java.util.Map; | 10 | 10 | import java.util.Objects; | | 11 | +import java.util.Set; | 11 | 12 |
| 12 | 13 | /** | 13 | 14 | * The interned JFR side of a correlation: one entry per distinct resolved stack and per distinct | | 37 | 38 | * <p>Interning is done without materializing a key per sample: a 64-bit hash is folded over the raw | 38 | 39 | * frame list, and a hash hit is confirmed by comparing the raw list against the stored frames. Only | 39 | 40 | * a genuinely new stack allocates. | | 41 | + * | | 42 | + * <p>Each collapsed key also records which of its frames are Java frames, by async-profiler's frame type, since the | | 43 | + * key alone cannot tell {@code libjvm.so.Unsafe_Park} from {@code a.b.Class.method}. A frame position is Java only | | 44 | + * when every stack that collapses to the key has a Java type there: a frame that is sometimes native counts as | | 45 | + * native. | 40 | 46 | */ | 41 | 47 | final class JfrDictionaries { | 42 | 48 | private static final String UNAVAILABLE = "[stack unavailable]"; | 43 | 49 | private static final String UNRESOLVED = "[unresolved]"; | | 50 | + /** async-profiler's frame types for Java code; {@code Native}, {@code C++} and {@code Kernel} are not. */ | | 51 | + private static final Set<String> JAVA_TYPES = Set.of("Interpreted", "JIT compiled", "C1 compiled", "Inlined"); | 44 | 52 |
| 45 | 53 | record Frame( | 46 | 54 | String type, String className, String methodName, String descriptor, int lineNumber, int bytecodeIndex) {} | @@ -54,10 +62,12 @@ record Thread(long osThreadId, long javaThreadId, String name) {} | 54 | 62 | private final List<Integer> stackCollapsed = new ArrayList<>(); | 55 | 63 | private final Map<String, Integer> collapsedIds = new HashMap<>(); | 56 | 64 | private final List<String> collapsedKeys = new ArrayList<>(); | | 65 | + private final List<boolean[]> collapsedJava = new ArrayList<>(); | 57 | 66 | private final Map<Thread, Integer> threadIds = new HashMap<>(); | 58 | 67 | private final List<Thread> threads = new ArrayList<>(); | 59 | 68 | private long stringBytes; | 60 | 69 | private long frameBytes; | | 70 | + private long javaFlagBytes; | 61 | 71 |
| 62 | 72 | /** Interns the frame list of one sample and returns its stack id. */ | 63 | 73 | int internStack(List<?> rawFrames, boolean truncated, int maxFrames) throws IOException { | @@ -86,7 +96,7 @@ int internStack(List<?> rawFrames, boolean truncated, int maxFrames) throws IOEx | 86 | 96 | int id = stackFrames.size(); | 87 | 97 | stackFrames.add(frames); | 88 | 98 | stackTruncated.add(truncated); | 89 | | - stackCollapsed.add(internCollapsed(collapsed(frames))); | | 99 | + stackCollapsed.add(internCollapsed(collapsed(frames), javaFrames(frames))); | 90 | 100 | stacksByHash.merge(hash, new int[] {id}, (existing, added) -> { | 91 | 101 | int[] grown = Arrays.copyOf(existing, existing.length + 1); | 92 | 102 | grown[existing.length] = added[0]; | @@ -116,6 +126,16 @@ String collapsedKey(int collapsedId) { | 116 | 126 | return collapsedKeys.get(collapsedId); | 117 | 127 | } | 118 | 128 |
| | 129 | + /** Which frames of a collapsed key, root first like the key, are Java frames. */ | | 130 | + boolean[] collapsedJava(int collapsedId) { | | 131 | + return collapsedJava.get(collapsedId); | | 132 | + } | | 133 | + | | 134 | + /** Whether async-profiler's frame type names Java code; a missing or unknown type does not. */ | | 135 | + static boolean isJavaType(String type) { | | 136 | + return type != null && JAVA_TYPES.contains(type); | | 137 | + } | | 138 | + | 119 | 139 | int collapsedCount() { | 120 | 140 | return collapsedKeys.size(); | 121 | 141 | } | @@ -136,7 +156,12 @@ Thread thread(int threadId) { | 136 | 156 | } | 137 | 157 |
| 138 | 158 | long retainedBytes() { | 139 | | - return stringBytes + frameBytes + stacksByHash.size() * 64L + collapsedKeys.size() * 64L + threads.size() * 96L; | | 159 | + return stringBytes | | 160 | + + frameBytes | | 161 | + + stacksByHash.size() * 64L | | 162 | + + collapsedKeys.size() * 64L | | 163 | + + threads.size() * 96L | | 164 | + + javaFlagBytes; | 140 | 165 | } | 141 | 166 |
| 142 | 167 | /** | @@ -164,13 +189,30 @@ private static String collapsed(Frame[] frames) { | 164 | 189 | return key.toString(); | 165 | 190 | } | 166 | 191 |
| 167 | | - private int internCollapsed(String key) { | | 192 | + /** The Java flags of a stack, root first; the empty stack's single placeholder frame is not Java. */ | | 193 | + private static boolean[] javaFrames(Frame[] frames) { | | 194 | + if (frames.length == 0) return new boolean[1]; | | 195 | + boolean[] java = new boolean[frames.length]; | | 196 | + for (int index = 0; index < frames.length; index++) { | | 197 | + java[frames.length - 1 - index] = isJavaType(frames[index].type()); | | 198 | + } | | 199 | + return java; | | 200 | + } | | 201 | + | | 202 | + private int internCollapsed(String key, boolean[] java) { | 168 | 203 | Integer existing = collapsedIds.get(key); | 169 | | - if (existing != null) return existing; | | 204 | + if (existing != null) { | | 205 | + // Stacks that differ only in a frame's type share the key; they must agree for the frame to be Java. | | 206 | + boolean[] agreed = collapsedJava.get(existing); | | 207 | + for (int index = 0; index < agreed.length; index++) agreed[index] &= java[index]; | | 208 | + return existing; | | 209 | + } | 170 | 210 | int id = collapsedKeys.size(); | 171 | 211 | collapsedKeys.add(key); | | 212 | + collapsedJava.add(java); | 172 | 213 | collapsedIds.put(key, id); | 173 | 214 | stringBytes += 48L + 2L * key.length(); | | 215 | + javaFlagBytes += 16L + java.length; | 174 | 216 | return id; | 175 | 217 | } | 176 | 218 |
| |
Collapse file Copy file name to clipboardExpand all lines: jonoffcpu-correlator/src/main/java/io/github/lhotari/jonoffcpu/offline/StackProfile.java +65-9Lines changed: 65 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,10 @@ record StackProfile(Header header, List<Entry> entries) { | 34 | 34 | static final byte[] MAGIC = "JONOFFPRF\0".getBytes(StandardCharsets.US_ASCII); | 35 | 35 | static final int FORMAT_VERSION = 1; | 36 | 36 | static final int HEADER_BYTES = MAGIC.length + 2; | 37 | | - static final int SCHEMA_VERSION = 1; | | 37 | + /** Schema 2 added {@link Kind#JFR_NATIVE}; a schema 1 profile tags every frame of its Java stacks JAVA. */ | | 38 | + static final int SCHEMA_VERSION = 2; | | 39 | + | | 40 | + static final int OLDEST_SCHEMA_VERSION = 1; | 38 | 41 | static final String PRODUCER = "jonoffcpu-correlator"; | 39 | 42 | static final String WEIGHT_SEMANTICS = "selected-observed-offcpu-nanoseconds"; | 40 | 43 | /** The reason vocabulary written into every profile; {@code Entry.reason} indexes it. */ | @@ -43,10 +46,16 @@ record StackProfile(Header header, List<Entry> entries) { | 43 | 46 |
| 44 | 47 | private static final int MAX_RECORD_BYTES = 1 << 20; | 45 | 48 |
| | 49 | + /** A frame's kind; its ordinal plus one is the wire value. */ | 46 | 50 | enum Kind { | 47 | 51 | JAVA, | 48 | 52 | USER, | 49 | | - KERNEL | | 53 | + KERNEL, | | 54 | + /** | | 55 | + * A frame of the Java stack that async-profiler did not record as Java code ({@code Native}, {@code C++}, | | 56 | + * {@code Kernel}, or no type): rendered like a Java frame, but its name is never read as a package. | | 57 | + */ | | 58 | + JFR_NATIVE | 50 | 59 | } | 51 | 60 |
| 52 | 61 | /** One frame: a Java frame's collapsed name, or a native frame's symbol and module. */ | @@ -243,9 +252,11 @@ static StackProfile of( | 243 | 252 | ProfileAccumulator.Counters counters = item.getValue(); | 244 | 253 | List<Frame> java = javaStacks.computeIfAbsent(key.collapsed(), id -> { | 245 | 254 | List<Frame> frames = new ArrayList<>(); | | 255 | + boolean[] javaFrames = dictionaries.collapsedJava(id); | 246 | 256 | // A collapsed key's frames never contain ';', so splitting it is lossless. | 247 | | - for (String name : dictionaries.collapsedKey(id).split(";", -1)) { | 248 | | - frames.add(new Frame(Kind.JAVA, name, "")); | | 257 | + String[] names = dictionaries.collapsedKey(id).split(";", -1); | | 258 | + for (int index = 0; index < names.length; index++) { | | 259 | + frames.add(new Frame(javaFrames[index] ? Kind.JAVA : Kind.JFR_NATIVE, names[index], "")); | 249 | 260 | } | 250 | 261 | return List.copyOf(frames); | 251 | 262 | }); | @@ -314,6 +325,7 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException { | 314 | 325 | boolean timeSplitAvailable = false; | 315 | 326 | Map<Key, long[]> merged = new LinkedHashMap<>(); | 316 | 327 | Map<Key, Split> splits = new HashMap<>(); | | 328 | + Map<List<String>, List<Frame>> javaStacks = javaStacksByName(profiles); | 317 | 329 | for (StackProfile profile : profiles) { | 318 | 330 | Header header = profile.header(); | 319 | 331 | CaptureInput.require(header.dimensions().equals(dimensions), "Profiles with different grouping dimensions"); | @@ -329,12 +341,21 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException { | 329 | 341 | // An input without the split contributes its time as unsplit, which its entries already say. | 330 | 342 | timeSplitAvailable |= header.timeSplitAvailable(); | 331 | 343 | for (Entry entry : profile.entries()) { | 332 | | - long[] counters = merged.computeIfAbsent(entry.key(), ignored -> new long[3]); | | 344 | + Key key = entry.javaStack() == null | | 345 | + ? entry.key() | | 346 | + : new Key( | | 347 | + javaStacks.get(names(entry.javaStack())), | | 348 | + entry.kernelStack(), | | 349 | + entry.userStack(), | | 350 | + entry.reason(), | | 351 | + entry.taskState(), | | 352 | + entry.thread()); | | 353 | + long[] counters = merged.computeIfAbsent(key, ignored -> new long[3]); | 333 | 354 | counters[0] = Math.addExact(counters[0], entry.intervals()); | 334 | 355 | counters[1] = U64.add(counters[1], entry.observedNanos(), "Profile duration"); | 335 | 356 | counters[2] = U64.add(counters[2], entry.estimatedNanos(), "Profile estimate"); | 336 | | - Split split = splits.get(entry.key()); | 337 | | - splits.put(entry.key(), split == null ? entry.split() : split.plus(entry.split())); | | 357 | + Split split = splits.get(key); | | 358 | + splits.put(key, split == null ? entry.split() : split.plus(entry.split())); | 338 | 359 | } | 339 | 360 | } | 340 | 361 | List<Entry> entries = new ArrayList<>(merged.size()); | @@ -358,6 +379,36 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException { | 358 | 379 | entries); | 359 | 380 | } | 360 | 381 |
| | 382 | + /** | | 383 | + * One Java stack per distinct list of frame names across the inputs, so a stack merges whatever kinds its inputs | | 384 | + * gave it: a frame is JAVA only when every input says so. A schema 1 input calls every frame JAVA, which a newer | | 385 | + * input's JFR_NATIVE therefore overrules. | | 386 | + */ | | 387 | + private static Map<List<String>, List<Frame>> javaStacksByName(List<StackProfile> profiles) { | | 388 | + Map<List<String>, List<Frame>> stacks = new HashMap<>(); | | 389 | + for (StackProfile profile : profiles) { | | 390 | + for (Entry entry : profile.entries()) { | | 391 | + if (entry.javaStack() == null) continue; | | 392 | + stacks.merge(names(entry.javaStack()), entry.javaStack(), StackProfile::agreedKinds); | | 393 | + } | | 394 | + } | | 395 | + return stacks; | | 396 | + } | | 397 | + | | 398 | + private static List<Frame> agreedKinds(List<Frame> left, List<Frame> right) { | | 399 | + if (left.equals(right)) return left; | | 400 | + List<Frame> agreed = new ArrayList<>(left.size()); | | 401 | + for (int index = 0; index < left.size(); index++) { | | 402 | + Frame frame = left.get(index); | | 403 | + agreed.add(frame.kind() == Kind.JAVA ? right.get(index) : frame); | | 404 | + } | | 405 | + return List.copyOf(agreed); | | 406 | + } | | 407 | + | | 408 | + private static List<String> names(List<Frame> stack) { | | 409 | + return stack.stream().map(Frame::name).toList(); | | 410 | + } | | 411 | + | 361 | 412 | // ---- canonical order ----------------------------------------------------------------------- | 362 | 413 |
| 363 | 414 | private static final Comparator<List<Frame>> STACK_ORDER = (left, right) -> { | @@ -563,7 +614,12 @@ static StackProfile read(InputStream input) throws IOException { | 563 | 614 | first != null && first.getRecordCase() == ProfileProto.Record.RecordCase.PROFILE_START, | 564 | 615 | "Stack profile does not start with profile_start"); | 565 | 616 | ProfileProto.ProfileStart start = first.getProfileStart(); | 566 | | - CaptureInput.require(start.getSchemaVersion() == SCHEMA_VERSION, "Unsupported stack profile schema"); | | 617 | + int schema = start.getSchemaVersion(); | | 618 | + CaptureInput.require( | | 619 | + schema >= OLDEST_SCHEMA_VERSION && schema <= SCHEMA_VERSION, | | 620 | + "Unsupported stack profile schema " + schema); | | 621 | + // Kinds a profile's schema does not define are refused rather than guessed. | | 622 | + int kinds = schema == 1 ? Kind.KERNEL.ordinal() + 1 : Kind.values().length; | 567 | 623 | CaptureInput.require(start.getReasonList().equals(REASONS), "Unsupported stack profile reason vocabulary"); | 568 | 624 | CaptureInput.require( | 569 | 625 | start.getWeightSemantics().equals(WEIGHT_SEMANTICS), "Unsupported stack profile weight semantics"); | @@ -603,7 +659,7 @@ static StackProfile read(InputStream input) throws IOException { | 603 | 659 | ProfileProto.FrameRecord frame = record.getFrame(); | 604 | 660 | CaptureInput.require(frame.getId() == frames.size(), "Out-of-order frame id"); | 605 | 661 | int kind = frame.getKindValue(); | 606 | | - CaptureInput.require(kind >= 1 && kind <= Kind.values().length, "Invalid frame kind"); | | 662 | + CaptureInput.require(kind >= 1 && kind <= kinds, "Invalid frame kind"); | 607 | 663 | frames.add(new Frame( | 608 | 664 | Kind.values()[kind - 1], | 609 | 665 | reference(strings, frame.getNameString(), false), | |
|