Class StacktraceModel
- java.lang.Object
-
- org.openjdk.jmc.flightrecorder.stacktrace.StacktraceModel
-
public class StacktraceModel extends java.lang.Object
A model for holding multiple stacktraces and their relations to each other.The model is stateful in two ways. It uses lazy evaluation to calculate the model, and it contains information about the currently selected path through the tree.
This class is not thread safe.
The typical way of using this class is to first decide on the
FrameSeparator
and then create the model. This is done in constant time. After this you get the root fork and use theStacktraceModel.Fork
andStacktraceModel.Branch
classes to traverse the tree of stacktraces. Getting the root fork or the end fork of any branch is roughly O(n) to the number of items in the branch.Opening a Java flight Recording and setting up the stacktrace model can be done like this:
IItemCollection items = JfrLoaderToolkit.loadEvents(file); IItemCollection filteredItems = items.apply(JdkFilters.EXECUTION_SAMPLE); FrameSeparator frameSeparator = new FrameSeparator(FrameCategorization.METHOD, false); StacktraceModel model = new StacktraceModel(true, frameSeparator, filteredItems); Fork root = model.getRootFork();
Traversing the stacktrace tree can be done like this:
void walkTree(Fork fork) { for (Branch branch : fork.getBranches()) { walkTree(branch.getEndFork()); } }
Examining the contents of a branch can be done by using
StacktraceModel.Branch.getFirstFrame()
andStacktraceModel.Branch.getTailFrames()
. These methods returnStacktraceFrame
entries that can be queried for more information.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
StacktraceModel.Branch
A branch is a sequence of frames without any forks.class
StacktraceModel.Fork
A fork is a collection of branches that share a common parent branch.private static class
StacktraceModel.FrameEntry
-
Field Summary
Fields Modifier and Type Field Description private IMemberAccessor<IMCStackTrace,IItem>
accessor
private static java.util.Comparator<StacktraceModel.FrameEntry>
COUNT_CMP
private FrameSeparator
frameSeparator
private IItemCollection
items
private StacktraceModel.Fork
rootFork
private boolean
threadRootAtTop
static IMCFrame
UNKNOWN_FRAME
A special marker object that indicates a frame that cannot be determined.
-
Constructor Summary
Constructors Constructor Description StacktraceModel(boolean threadRootAtTop, FrameSeparator frameSeparator, IItemCollection items)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static int
countFramesOnOrAbove(StacktraceModel.Branch branch)
boolean
equals(java.lang.Object obj)
private static StacktraceModel.FrameEntry
findEntryForFrame(SimpleArray<StacktraceModel.FrameEntry> entries, IMCFrame frame, FrameSeparator frameSeparator)
Find or create a matching FrameEntry for a frame.private java.util.List<StacktraceModel.FrameEntry>
getDistinctFrames(int frameIndex, java.lang.Iterable<? extends IItem> items)
Return a stream of frame entries that group the input items by distinct categories according to the frame separator.private IMCFrame
getFrame(IItem item, int frameIndex)
StacktraceModel.Fork
getRootFork()
Return the root fork which contains either top frames or thread roots, depending on the model configuration (threadRootAtTop
).int
hashCode()
-
-
-
Field Detail
-
accessor
private final IMemberAccessor<IMCStackTrace,IItem> accessor
-
threadRootAtTop
private final boolean threadRootAtTop
-
frameSeparator
private final FrameSeparator frameSeparator
-
items
private final IItemCollection items
-
rootFork
private StacktraceModel.Fork rootFork
-
UNKNOWN_FRAME
public static final IMCFrame UNKNOWN_FRAME
A special marker object that indicates a frame that cannot be determined.A typical case is when a stacktrace is truncated due to to Flight Recorder settings. We know that there is a frame because of a truncation flag, but there is no information about it.
-
COUNT_CMP
private static final java.util.Comparator<StacktraceModel.FrameEntry> COUNT_CMP
-
-
Constructor Detail
-
StacktraceModel
public StacktraceModel(boolean threadRootAtTop, FrameSeparator frameSeparator, IItemCollection items)
- Parameters:
threadRootAtTop
- If true, present the thread roots on the first fork. If false, present top frames on the first fork.frameSeparator
- Determines how different two frames must be to motivate a fork in the model.items
- Items containing stacktraces. Items in the collection that do not contain stacktraces are silently ignored.
-
-
Method Detail
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
getRootFork
public StacktraceModel.Fork getRootFork()
Return the root fork which contains either top frames or thread roots, depending on the model configuration (threadRootAtTop
).This is the entry point that you call when you want to access the model structure. After that you use the methods on the
StacktraceModel.Fork
andStacktraceModel.Branch
classes to navigate the model.The first call may take some time due to calculations, so it may be useful to call this in a background thread if used in a UI.
-
getDistinctFrames
private java.util.List<StacktraceModel.FrameEntry> getDistinctFrames(int frameIndex, java.lang.Iterable<? extends IItem> items)
Return a stream of frame entries that group the input items by distinct categories according to the frame separator.
-
findEntryForFrame
private static StacktraceModel.FrameEntry findEntryForFrame(SimpleArray<StacktraceModel.FrameEntry> entries, IMCFrame frame, FrameSeparator frameSeparator)
Find or create a matching FrameEntry for a frame.
-
countFramesOnOrAbove
private static int countFramesOnOrAbove(StacktraceModel.Branch branch)
- Returns:
- The number of frames in the selected branch and all its parent branches.
-
-