GeezerVison is showing github.com — Keep native frames intact under --package-names by lhotari · Pull Request #5 · jonoffcpu/jonoffcpu
Keep native frames intact under --package-names
#5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.java
(5)
.md
(2)
.proto
(1)
All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
Keep native frames intact under --package-names
stacks --package-names abbreviate|drop rewrote the native frames async-profiler
records inside the Java stack: libjvm.so.Unsafe_Park became l.so.Unsafe_Park or
so.Unsafe_Park, losing the library and merging symbols of different
libraries. The collapsed key drops each frame's type, the profile tagged every
Java-stack frame JAVA, and the package pattern read "libjvm." as a package.
- The correlator records which frames of each collapsed key async-profiler
typed as Java code (Interpreted, JIT compiled, C1 compiled, Inlined). Stacks
that share a key must agree for a frame to count as Java.
- The profile tags the others with a new frame kind, JFR_NATIVE (wire value
4), and moves to schema_version 2. Schema 1 profiles still read; an older
correlator refuses a new profile with "Unsupported stack profile schema".
merge combines stacks with the same names whatever kinds its inputs gave
them, and a frame any input calls native stays native.
- --package-names only rewrites JAVA frames. A name rule protects schema 1
profiles and backs up the kinds: a name with "::", a shared-library segment
(".so.", ".so.6."), a leading "/" or "[", or a space is never rewritten.
- A hidden class's ".0x..." suffix (Foo$$Lambda.0x0000000081a16ff8.run) now
stays part of the class, so drop removes its package too.
- export adds a last column, java_stack_kinds (javaStackKinds in JSON Lines),
with java or native per frame. Existing columns are unchanged.
Collapsed keys and frame names are unchanged, so the default rendering still
reproduces jonoffcpu-offcpu-stacks.collapsed byte for byte. On a Pulsar broker
profile, the old code produced 1,844 so./l.so. frame occurrences under
abbreviate and drop; now all 264 native frames keep their names, and filter
totals are identical in all three modes.
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit fe2c957f560b9c9d54ef0a7a46a5f2bb9f6fc57f
There are no files selected for viewing
12 changes: 11 additions & 1 deletion
12
README.md
Original file line number
Diff line number
Diff line change
Expand Up
@@ -663,7 +663,17 @@ the whole time and ends each line in a `[sleeping]`, `[runqueue]` or
needs a profile whose capture recorded the split. `--package-names abbreviate`
shortens each Java frame's package to its initials (`i.n.c.e.Native.epollWait0`)
and `--package-names drop` removes it (`Native.epollWait0`); `full` is the
default. Stacks that become identical merge into one line, and
default. Only Java frames change : the native frames async-profiler records in
the Java stack (HotSpot, JNI libraries, libc, runtime stubs) keep their library
and symbol as written.
| Frame | `abbreviate` | `drop` |
| --- | --- | --- |
| `io.netty.channel.epoll.Native.epollWait0` | `i.n.c.e.Native.epollWait0` | `Native.epollWait0` |
| `org.example.Cursor$$Lambda.0x0000000081a16ff8.run` | `o.e.Cursor$$Lambda.0x0000000081a16ff8.run` | `Cursor$$Lambda.0x0000000081a16ff8.run` |
| `libjvm.so.Unsafe_Park` | unchanged | unchanged |
Stacks that become identical merge into one line, and
` --include` /`--exclude` still match the full names. Rendered with its defaults, a
profile reproduces `jonoffcpu-offcpu-stacks.collapsed` byte for byte.
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -33,6 +33,8 @@ message Record {
}
message ProfileStart {
// 2 since FrameRecord.Kind JFR_NATIVE; a schema 1 profile tags every frame of its Java stacks JAVA. A
// reader refuses a schema it does not know rather than render frame kinds it cannot tell apart.
uint32 schema_version = 1 ;
// The writing tool, for example "jonoffcpu-correlator".
string producer = 2 ;
Expand Down
Expand Up
@@ -87,13 +89,17 @@ message FrameRecord {
JAVA = 1 ;
USER = 2 ;
KERNEL = 3 ;
// A frame of the Java stack that async-profiler did not record as Java code: frame type Native, C++
// or Kernel, or no type. Its name is collapsed like a Java frame's ("libjvm.so.Unsafe_Park": the
// library, then the symbol) but is not a package-qualified Class.method. Since schema 2.
JFR_NATIVE = 4 ;
}
uint32 id = 1 ;
Kind kind = 2 ;
// A Java frame's collapsed name ("class.method") , or a native frame's symbol as the collector resolved
// it, including any "+0x" offset.
// A Java-stack frame's collapsed name ("class.method", or "library. symbol" for JFR_NATIVE), or a native
// frame's symbol as the collector resolved it, including any "+0x" offset.
uint32 name_string = 3 ;
// A native frame's module; 0 for Java frames and unresolved modules.
// A native frame's module; 0 for JAVA and JFR_NATIVE frames and unresolved modules.
uint32 module_string = 4 ;
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -227,6 +227,19 @@ written before its first use and ids are dense from 1, so 0 always means absent.
Entries are written in one canonical order, so the same analysis gives the same
bytes whatever order its intervals matched in.
Each frame has a kind. Native stacks' frames are ` USER ` or ` KERNEL ` . A Java
stack's frame is ` JAVA ` when async-profiler recorded it as Java code (frame type
` Interpreted ` , ` JIT compiled ` , ` C1 compiled ` or ` Inlined ` ), and ` JFR_NATIVE `
otherwise (` Native ` , ` C++ ` , ` Kernel ` , or no type): a frame such as
` libjvm.so.Unsafe_Park ` , the library then the symbol, which only looks like a
package-qualified Java name. Stacks that differ only in a frame's type share a
collapsed key, so a frame position is ` JAVA ` only when every such stack agrees.
The kind changes no name, so the collapsed key and every rendered line stay the
same. ` JFR_NATIVE ` arrived with ` schema_version ` 2; a schema 1 profile tags every
Java-stack frame ` JAVA ` and still reads, and a reader that predates schema 2
refuses a newer profile with ` Unsupported stack profile schema ` rather than
render it.
An entry is keyed by the Java stack (at the collapsed file's class-and-method
granularity) and the switch-out reason with its raw task state, and by default
also by the kernel stack, the user stack and the thread name
Expand Down
Expand Up
@@ -278,8 +291,14 @@ a `sleeping` or `runqueue` slice leaves out.
` --package-names abbreviate ` cuts each package segment of a Java frame to its
first letter (` io.netty.channel.epoll.Native.epollWait0 ` becomes
` i.n.c.e.Native.epollWait0 ` ), and ` drop ` shows ` Class.method ` ; a frame that is
not a package-qualified ` Class.method ` is left unchanged. It only changes the
` i.n.c.e.Native.epollWait0 ` ), and ` drop ` shows ` Class.method ` ; a hidden class's
` .0x… ` suffix (` Cursor$$Lambda.0x0000000081a16ff8 ` ) stays part of the class. Only
` JAVA ` frames change: a ` JFR_NATIVE ` frame keeps its library and symbol
(` libjvm.so.Unsafe_Park ` ). As a fallback for schema 1 profiles, and as a second
line of defence, a name that reads as native is also left alone: one with a C++
` :: ` , a shared-library segment (` .so. ` , ` .so.6. ` ), a leading ` / ` or ` [ ` , or a
space. That rule can only prevent a rewrite, and a frame that is not a
package-qualified ` Class.method ` is left unchanged too. It only changes the
rendered text: lines that become identical are summed, filters match the full
names, and ` --summary ` records the mode as ` packageNames ` .
Expand All
@@ -303,9 +322,14 @@ rendered to compute it. `merge`
sums identical entries and keeps every input's provenance; it refuses profiles
with different grouping, and thinned profiles, whose weights have no common scale.
It sums the split parts too, so an input without the split contributes its time
as unsplit and the merged profile has the split when any input has it.
` export ` writes one row per entry with expanded stacks and the six split columns,
for tools such as DuckDB.
as unsplit and the merged profile has the split when any input has it. Java
stacks with the same frame names merge whatever kinds their inputs gave them, and
a frame any input calls ` JFR_NATIVE ` stays ` JFR_NATIVE ` , so a schema 1 input
cannot make a native frame rewritable.
` export ` writes one row per entry with expanded stacks, the six split columns and,
last, ` java_stack_kinds ` (` javaStackKinds ` in JSON Lines): each Java-stack
frame's kind, ` java ` or ` native ` , joined with ` ; ` like ` java_stack ` . It is for
tools such as DuckDB.
## Degradation
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -8,6 +8,7 @@
import java .util .List ;
import java .util .Map ;
import java .util .Objects ;
import java .util .Set ;
/**
* The interned JFR side of a correlation: one entry per distinct resolved stack and per distinct
Expand Down
Expand Up
@@ -37,10 +38,17 @@
* <p>Interning is done without materializing a key per sample: a 64-bit hash is folded over the raw
* frame list, and a hash hit is confirmed by comparing the raw list against the stored frames. Only
* a genuinely new stack allocates.
*
* <p>Each collapsed key also records which of its frames are Java frames, by async-profiler's frame type, since the
* key alone cannot tell {@code libjvm.so.Unsafe_Park} from {@code a.b.Class.method}. A frame position is Java only
* when every stack that collapses to the key has a Java type there: a frame that is sometimes native counts as
* native.
*/
final class JfrDictionaries {
private static final String UNAVAILABLE = "[stack unavailable]" ;
private static final String UNRESOLVED = "[unresolved]" ;
/** async-profiler's frame types for Java code; {@code Native}, {@code C++} and {@code Kernel} are not. */
private static final Set <String > JAVA_TYPES = Set .of ("Interpreted" , "JIT compiled" , "C1 compiled" , "Inlined" );
record Frame (
String type , String className , String methodName , String descriptor , int lineNumber , int bytecodeIndex ) {}
Expand All
@@ -54,10 +62,12 @@ record Thread(long osThreadId, long javaThreadId, String name) {}
private final List <Integer > stackCollapsed = new ArrayList <>();
private final Map <String , Integer > collapsedIds = new HashMap <>();
private final List <String > collapsedKeys = new ArrayList <>();
private final List <boolean []> collapsedJava = new ArrayList <>();
private final Map <Thread , Integer > threadIds = new HashMap <>();
private final List <Thread > threads = new ArrayList <>();
private long stringBytes ;
private long frameBytes ;
private long javaFlagBytes ;
/** Interns the frame list of one sample and returns its stack id. */
int internStack (List <?> rawFrames , boolean truncated , int maxFrames ) throws IOException {
Expand Down
Expand Up
@@ -86,7 +96,7 @@ int internStack(List<?> rawFrames, boolean truncated, int maxFrames) throws IOEx
int id = stackFrames .size ();
stackFrames .add (frames );
stackTruncated .add (truncated );
stackCollapsed .add (internCollapsed (collapsed (frames )));
stackCollapsed .add (internCollapsed (collapsed (frames ), javaFrames ( frames ) ));
stacksByHash .merge (hash , new int [] {id }, (existing , added ) -> {
int [] grown = Arrays .copyOf (existing , existing .length + 1 );
grown [existing .length ] = added [0 ];
Expand Down
Expand Up
@@ -116,6 +126,16 @@ String collapsedKey(int collapsedId) {
return collapsedKeys .get (collapsedId );
}
/** Which frames of a collapsed key, root first like the key, are Java frames. */
boolean [] collapsedJava (int collapsedId ) {
return collapsedJava .get (collapsedId );
}
/** Whether async-profiler's frame type names Java code; a missing or unknown type does not. */
static boolean isJavaType (String type ) {
return type != null && JAVA_TYPES .contains (type );
}
int collapsedCount () {
return collapsedKeys .size ();
}
Expand All
@@ -136,7 +156,12 @@ Thread thread(int threadId) {
}
long retainedBytes () {
return stringBytes + frameBytes + stacksByHash .size () * 64L + collapsedKeys .size () * 64L + threads .size () * 96L ;
return stringBytes
+ frameBytes
+ stacksByHash .size () * 64L
+ collapsedKeys .size () * 64L
+ threads .size () * 96L
+ javaFlagBytes ;
}
/**
Expand Down
Expand Up
@@ -164,13 +189,30 @@ private static String collapsed(Frame[] frames) {
return key .toString ();
}
private int internCollapsed (String key ) {
/** The Java flags of a stack, root first; the empty stack's single placeholder frame is not Java. */
private static boolean [] javaFrames (Frame [] frames ) {
if (frames .length == 0 ) return new boolean [1 ];
boolean [] java = new boolean [frames .length ];
for (int index = 0 ; index < frames .length ; index ++) {
java [frames .length - 1 - index ] = isJavaType (frames [index ].type ());
}
return java ;
}
private int internCollapsed (String key , boolean [] java ) {
Integer existing = collapsedIds .get (key );
if (existing != null ) return existing ;
if (existing != null ) {
// Stacks that differ only in a frame's type share the key; they must agree for the frame to be Java.
boolean [] agreed = collapsedJava .get (existing );
for (int index = 0 ; index < agreed .length ; index ++) agreed [index ] &= java [index ];
return existing ;
}
int id = collapsedKeys .size ();
collapsedKeys .add (key );
collapsedJava .add (java );
collapsedIds .put (key , id );
stringBytes += 48L + 2L * key .length ();
javaFlagBytes += 16L + java .length ;
return id ;
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -34,7 +34,10 @@ record StackProfile(Header header, List<Entry> entries) {
static final byte [] MAGIC = "JONOFFPRF\0 " .getBytes (StandardCharsets .US_ASCII );
static final int FORMAT_VERSION = 1 ;
static final int HEADER_BYTES = MAGIC .length + 2 ;
static final int SCHEMA_VERSION = 1 ;
/** Schema 2 added {@link Kind#JFR_NATIVE}; a schema 1 profile tags every frame of its Java stacks JAVA. */
static final int SCHEMA_VERSION = 2 ;
static final int OLDEST_SCHEMA_VERSION = 1 ;
static final String PRODUCER = "jonoffcpu-correlator" ;
static final String WEIGHT_SEMANTICS = "selected-observed-offcpu-nanoseconds" ;
/** The reason vocabulary written into every profile; {@code Entry.reason} indexes it. */
Expand All
@@ -43,10 +46,16 @@ record StackProfile(Header header, List<Entry> entries) {
private static final int MAX_RECORD_BYTES = 1 << 20 ;
/** A frame's kind; its ordinal plus one is the wire value. */
enum Kind {
JAVA ,
USER ,
KERNEL
KERNEL ,
/**
* A frame of the Java stack that async-profiler did not record as Java code ({@code Native}, {@code C++},
* {@code Kernel}, or no type): rendered like a Java frame, but its name is never read as a package.
*/
JFR_NATIVE
}
/** One frame: a Java frame's collapsed name, or a native frame's symbol and module. */
Expand Down
Expand Up
@@ -243,9 +252,11 @@ static StackProfile of(
ProfileAccumulator .Counters counters = item .getValue ();
List <Frame > java = javaStacks .computeIfAbsent (key .collapsed (), id -> {
List <Frame > frames = new ArrayList <>();
boolean [] javaFrames = dictionaries .collapsedJava (id );
// A collapsed key's frames never contain ';', so splitting it is lossless.
for (String name : dictionaries .collapsedKey (id ).split (";" , -1 )) {
frames .add (new Frame (Kind .JAVA , name , "" ));
String [] names = dictionaries .collapsedKey (id ).split (";" , -1 );
for (int index = 0 ; index < names .length ; index ++) {
frames .add (new Frame (javaFrames [index ] ? Kind .JAVA : Kind .JFR_NATIVE , names [index ], "" ));
}
return List .copyOf (frames );
});
Expand Down
Expand Up
@@ -314,6 +325,7 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException {
boolean timeSplitAvailable = false ;
Map <Key , long []> merged = new LinkedHashMap <>();
Map <Key , Split > splits = new HashMap <>();
Map <List <String >, List <Frame >> javaStacks = javaStacksByName (profiles );
for (StackProfile profile : profiles ) {
Header header = profile .header ();
CaptureInput .require (header .dimensions ().equals (dimensions ), "Profiles with different grouping dimensions" );
Expand All
@@ -329,12 +341,21 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException {
// An input without the split contributes its time as unsplit, which its entries already say.
timeSplitAvailable |= header .timeSplitAvailable ();
for (Entry entry : profile .entries ()) {
long [] counters = merged .computeIfAbsent (entry .key (), ignored -> new long [3 ]);
Key key = entry .javaStack () == null
? entry .key ()
: new Key (
javaStacks .get (names (entry .javaStack ())),
entry .kernelStack (),
entry .userStack (),
entry .reason (),
entry .taskState (),
entry .thread ());
long [] counters = merged .computeIfAbsent (key , ignored -> new long [3 ]);
counters [0 ] = Math .addExact (counters [0 ], entry .intervals ());
counters [1 ] = U64 .add (counters [1 ], entry .observedNanos (), "Profile duration" );
counters [2 ] = U64 .add (counters [2 ], entry .estimatedNanos (), "Profile estimate" );
Split split = splits .get (entry . key () );
splits .put (entry . key () , split == null ? entry .split () : split .plus (entry .split ()));
Split split = splits .get (key );
splits .put (key , split == null ? entry .split () : split .plus (entry .split ()));
}
}
List <Entry > entries = new ArrayList <>(merged .size ());
Expand All
@@ -358,6 +379,36 @@ static StackProfile merge(List<StackProfile> profiles) throws IOException {
entries );
}
/**
* One Java stack per distinct list of frame names across the inputs, so a stack merges whatever kinds its inputs
* gave it: a frame is JAVA only when every input says so. A schema 1 input calls every frame JAVA, which a newer
* input's JFR_NATIVE therefore overrules.
*/
private static Map <List <String >, List <Frame >> javaStacksByName (List <StackProfile > profiles ) {
Map <List <String >, List <Frame >> stacks = new HashMap <>();
for (StackProfile profile : profiles ) {
for (Entry entry : profile .entries ()) {
if (entry .javaStack () == null ) continue ;
stacks .merge (names (entry .javaStack ()), entry .javaStack (), StackProfile ::agreedKinds );
}
}
return stacks ;
}
private static List <Frame > agreedKinds (List <Frame > left , List <Frame > right ) {
if (left .equals (right )) return left ;
List <Frame > agreed = new ArrayList <>(left .size ());
for (int index = 0 ; index < left .size (); index ++) {
Frame frame = left .get (index );
agreed .add (frame .kind () == Kind .JAVA ? right .get (index ) : frame );
}
return List .copyOf (agreed );
}
private static List <String > names (List <Frame > stack ) {
return stack .stream ().map (Frame ::name ).toList ();
}
// ---- canonical order -----------------------------------------------------------------------
private static final Comparator <List <Frame >> STACK_ORDER = (left , right ) -> {
Expand Down
Expand Up
@@ -563,7 +614,12 @@ static StackProfile read(InputStream input) throws IOException {
first != null && first .getRecordCase () == ProfileProto .Record .RecordCase .PROFILE_START ,
"Stack profile does not start with profile_start" );
ProfileProto .ProfileStart start = first .getProfileStart ();
CaptureInput .require (start .getSchemaVersion () == SCHEMA_VERSION , "Unsupported stack profile schema" );
int schema = start .getSchemaVersion ();
CaptureInput .require (
schema >= OLDEST_SCHEMA_VERSION && schema <= SCHEMA_VERSION ,
"Unsupported stack profile schema " + schema );
// Kinds a profile's schema does not define are refused rather than guessed.
int kinds = schema == 1 ? Kind .KERNEL .ordinal () + 1 : Kind .values ().length ;
CaptureInput .require (start .getReasonList ().equals (REASONS ), "Unsupported stack profile reason vocabulary" );
CaptureInput .require (
start .getWeightSemantics ().equals (WEIGHT_SEMANTICS ), "Unsupported stack profile weight semantics" );
Expand Down
Expand Up
@@ -603,7 +659,7 @@ static StackProfile read(InputStream input) throws IOException {
ProfileProto .FrameRecord frame = record .getFrame ();
CaptureInput .require (frame .getId () == frames .size (), "Out-of-order frame id" );
int kind = frame .getKindValue ();
CaptureInput .require (kind >= 1 && kind <= Kind . values (). length , "Invalid frame kind" );
CaptureInput .require (kind >= 1 && kind <= kinds , "Invalid frame kind" );
frames .add (new Frame (
Kind .values ()[kind - 1 ],
reference (strings , frame .getNameString (), false ),
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.