GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Keep native frames intact under --package-names (#5) · jonoffcpu/jonoffcpu@5f3cb64

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • docs/schema
    • jonoffcpu-profile.proto
  • jonoffcpu-correlator
    • OFFLINE.md
    • src
      • main/java/io/github/lhotari/jonoffcpu/offline
        • JfrDictionaries.java
        • StackProfile.java
        • StackProfileRenderer.java
      • test/java/io/github/lhotari/jonoffcpu/offline
        • PrimitiveStructuresTest.java
        • StackProfileTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

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 numberDiff line numberDiff line change
@@ -663,7 +663,17 @@ the whole time and ends each line in a `[sleeping]`, `[runqueue]` or
663663
needs a profile whose capture recorded the split. `--package-names abbreviate`
664664
shortens each Java frame's package to its initials (`i.n.c.e.Native.epollWait0`)
665665
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
667677
`--include`/`--exclude` still match the full names. Rendered with its defaults, a
668678
profile reproduces `jonoffcpu-offcpu-stacks.collapsed` byte for byte.
669679

Collapse file

‎docs/schema/jonoffcpu-profile.proto‎

Copy file name to clipboardExpand all lines: docs/schema/jonoffcpu-profile.proto
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ message Record {
3333
}
3434

3535
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.
3638
uint32 schema_version = 1;
3739
// The writing tool, for example "jonoffcpu-correlator".
3840
string producer = 2;
@@ -87,13 +89,17 @@ message FrameRecord {
8789
JAVA = 1;
8890
USER = 2;
8991
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;
9096
}
9197
uint32 id = 1;
9298
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.
95101
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.
97103
uint32 module_string = 4;
98104
}
99105

Collapse file

‎jonoffcpu-correlator/OFFLINE.md‎

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 numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ written before its first use and ids are dense from 1, so 0 always means absent.
227227
Entries are written in one canonical order, so the same analysis gives the same
228228
bytes whatever order its intervals matched in.
229229

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+
230243
An entry is keyed by the Java stack (at the collapsed file's class-and-method
231244
granularity) and the switch-out reason with its raw task state, and by default
232245
also by the kernel stack, the user stack and the thread name
@@ -278,8 +291,14 @@ a `sleeping` or `runqueue` slice leaves out.
278291

279292
`--package-names abbreviate` cuts each package segment of a Java frame to its
280293
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
283302
rendered text: lines that become identical are summed, filters match the full
284303
names, and `--summary` records the mode as `packageNames`.
285304

@@ -303,9 +322,14 @@ rendered to compute it. `merge`
303322
sums identical entries and keeps every input's provenance; it refuses profiles
304323
with different grouping, and thinned profiles, whose weights have no common scale.
305324
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.
309333

310334
## Degradation
311335

Collapse file

‎jonoffcpu-correlator/src/main/java/io/github/lhotari/jonoffcpu/offline/JfrDictionaries.java‎

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 numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.Map;
1010
import java.util.Objects;
11+
import java.util.Set;
1112

1213
/**
1314
* The interned JFR side of a correlation: one entry per distinct resolved stack and per distinct
@@ -37,10 +38,17 @@
3738
* <p>Interning is done without materializing a key per sample: a 64-bit hash is folded over the raw
3839
* frame list, and a hash hit is confirmed by comparing the raw list against the stored frames. Only
3940
* 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.
4046
*/
4147
final class JfrDictionaries {
4248
private static final String UNAVAILABLE = "[stack unavailable]";
4349
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");
4452

4553
record Frame(
4654
String type, String className, String methodName, String descriptor, int lineNumber, int bytecodeIndex) {}
@@ -54,10 +62,12 @@ record Thread(long osThreadId, long javaThreadId, String name) {}
5462
private final List<Integer> stackCollapsed = new ArrayList<>();
5563
private final Map<String, Integer> collapsedIds = new HashMap<>();
5664
private final List<String> collapsedKeys = new ArrayList<>();
65+
private final List<boolean[]> collapsedJava = new ArrayList<>();
5766
private final Map<Thread, Integer> threadIds = new HashMap<>();
5867
private final List<Thread> threads = new ArrayList<>();
5968
private long stringBytes;
6069
private long frameBytes;
70+
private long javaFlagBytes;
6171

6272
/** Interns the frame list of one sample and returns its stack id. */
6373
int internStack(List<?> rawFrames, boolean truncated, int maxFrames) throws IOException {
@@ -86,7 +96,7 @@ int internStack(List<?> rawFrames, boolean truncated, int maxFrames) throws IOEx
8696
int id = stackFrames.size();
8797
stackFrames.add(frames);
8898
stackTruncated.add(truncated);
89-
stackCollapsed.add(internCollapsed(collapsed(frames)));
99+
stackCollapsed.add(internCollapsed(collapsed(frames), javaFrames(frames)));
90100
stacksByHash.merge(hash, new int[] {id}, (existing, added) -> {
91101
int[] grown = Arrays.copyOf(existing, existing.length + 1);
92102
grown[existing.length] = added[0];
@@ -116,6 +126,16 @@ String collapsedKey(int collapsedId) {
116126
return collapsedKeys.get(collapsedId);
117127
}
118128

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+
119139
int collapsedCount() {
120140
return collapsedKeys.size();
121141
}
@@ -136,7 +156,12 @@ Thread thread(int threadId) {
136156
}
137157

138158
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;
140165
}
141166

142167
/**
@@ -164,13 +189,30 @@ private static String collapsed(Frame[] frames) {
164189
return key.toString();
165190
}
166191

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) {
168203
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+
}
170210
int id = collapsedKeys.size();
171211
collapsedKeys.add(key);
212+
collapsedJava.add(java);
172213
collapsedIds.put(key, id);
173214
stringBytes += 48L + 2L * key.length();
215+
javaFlagBytes += 16L + java.length;
174216
return id;
175217
}
176218

Collapse file

‎jonoffcpu-correlator/src/main/java/io/github/lhotari/jonoffcpu/offline/StackProfile.java‎

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 numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ record StackProfile(Header header, List<Entry> entries) {
3434
static final byte[] MAGIC = "JONOFFPRF\0".getBytes(StandardCharsets.US_ASCII);
3535
static final int FORMAT_VERSION = 1;
3636
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;
3841
static final String PRODUCER = "jonoffcpu-correlator";
3942
static final String WEIGHT_SEMANTICS = "selected-observed-offcpu-nanoseconds";
4043
/** The reason vocabulary written into every profile; {@code Entry.reason} indexes it. */
@@ -43,10 +46,16 @@ record StackProfile(Header header, List<Entry> entries) {
4346

4447
private static final int MAX_RECORD_BYTES = 1 << 20;
4548

49+
/** A frame's kind; its ordinal plus one is the wire value. */
4650
enum Kind {
4751
JAVA,
4852
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
5059
}
5160

5261
/** One frame: a Java frame's collapsed name, or a native frame's symbol and module. */
@@ -243,9 +252,11 @@ static StackProfile of(
243252
ProfileAccumulator.Counters counters = item.getValue();
244253
List<Frame> java = javaStacks.computeIfAbsent(key.collapsed(), id -> {
245254
List<Frame> frames = new ArrayList<>();
255+
boolean[] javaFrames = dictionaries.collapsedJava(id);
246256
// 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], ""));
249260
}
250261
return List.copyOf(frames);
251262
});
@@ -314,6 +325,7 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException {
314325
boolean timeSplitAvailable = false;
315326
Map<Key, long[]> merged = new LinkedHashMap<>();
316327
Map<Key, Split> splits = new HashMap<>();
328+
Map<List<String>, List<Frame>> javaStacks = javaStacksByName(profiles);
317329
for (StackProfile profile : profiles) {
318330
Header header = profile.header();
319331
CaptureInput.require(header.dimensions().equals(dimensions), "Profiles with different grouping dimensions");
@@ -329,12 +341,21 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException {
329341
// An input without the split contributes its time as unsplit, which its entries already say.
330342
timeSplitAvailable |= header.timeSplitAvailable();
331343
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]);
333354
counters[0] = Math.addExact(counters[0], entry.intervals());
334355
counters[1] = U64.add(counters[1], entry.observedNanos(), "Profile duration");
335356
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()));
338359
}
339360
}
340361
List<Entry> entries = new ArrayList<>(merged.size());
@@ -358,6 +379,36 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException {
358379
entries);
359380
}
360381

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+
361412
// ---- canonical order -----------------------------------------------------------------------
362413

363414
private static final Comparator<List<Frame>> STACK_ORDER = (left, right) -> {
@@ -563,7 +614,12 @@ static StackProfile read(InputStream input) throws IOException {
563614
first != null && first.getRecordCase() == ProfileProto.Record.RecordCase.PROFILE_START,
564615
"Stack profile does not start with profile_start");
565616
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;
567623
CaptureInput.require(start.getReasonList().equals(REASONS), "Unsupported stack profile reason vocabulary");
568624
CaptureInput.require(
569625
start.getWeightSemantics().equals(WEIGHT_SEMANTICS), "Unsupported stack profile weight semantics");
@@ -603,7 +659,7 @@ static StackProfile read(InputStream input) throws IOException {
603659
ProfileProto.FrameRecord frame = record.getFrame();
604660
CaptureInput.require(frame.getId() == frames.size(), "Out-of-order frame id");
605661
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");
607663
frames.add(new Frame(
608664
Kind.values()[kind - 1],
609665
reference(strings, frame.getNameString(), false),

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.