public final class CPUSampler extends Object implements Closeable
iterateFrames().
The sampler samples the stack of each thread at regular intervals by using Truffle safepoints. The state of the stack is copied and saved into trees of nodes, which represent the profile of the execution.
Usage example:
Contextcontext =Context.create();CPUSamplersampler =CPUSampler.find(context.getEngine()); sampler.setCollecting(true); context.eval("...", "..."); sampler.setCollecting(false); sampler.close(); // Read information about the roots of the tree per thread. for (Collection<ProfilerNode<CPUSampler.Payload>> nodes : sampler.getData().values().iterator().next().threadData.values()) { for (ProfilerNode<CPUSampler.Payload> node : nodes) { finalStringrootName = node.getRootName(); final int selfHitCount = node.getPayload().getSelfHitCount(); // ... } }
| Modifier and Type | Class and Description |
|---|---|
static class |
CPUSampler.Payload
Wrapper for information on how many times an element was seen on the stack.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clearData()
Erases all the data gathered by the sampler and resets the sample count to 0.
|
void |
close()
Closes the sampler for further use, deleting all the gathered data.
|
static CPUSampler |
find(org.graalvm.polyglot.Engine engine)
Finds
CPUSampler associated with given engine. |
Map<com.oracle.truffle.api.TruffleContext,CPUSamplerData> |
getData()
Get per-context profiling data.
|
com.oracle.truffle.api.instrumentation.SourceSectionFilter |
getFilter() |
long |
getPeriod() |
int |
getStackLimit() |
boolean |
hasData() |
boolean |
hasStackOverflowed() |
boolean |
isCollecting() |
boolean |
isGatherSelfHitTimes() |
void |
setCollecting(boolean collecting)
Controls whether the sampler is collecting data or not.
|
void |
setDelay(long delay)
Sets the delay period i.e.
|
void |
setFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter filter)
Sets the
filter for the sampler. |
void |
setGatherSelfHitTimes(boolean gatherSelfHitTimes)
Sets whether or not to gather timestamp information for the element at the top of the stack
for each sample.
|
void |
setPeriod(long samplePeriod)
Sets the sampling period i.e.
|
void |
setSampleContextInitialization(boolean enabled)
Enables or disables the sampling of the time spent during context initialization.
|
void |
setStackLimit(int stackLimit)
Sets the maximum amount of stack frames that are sampled.
|
Map<Thread,List<StackTraceEntry>> |
takeSample()
Sample all threads and gather their current stack trace entries with a default time out.
|
Map<Thread,List<StackTraceEntry>> |
takeSample(long timeout,
TimeUnit timeoutUnit)
Sample all threads and gather their current stack trace entries.
|
public static CPUSampler find(org.graalvm.polyglot.Engine engine)
CPUSampler associated with given engine.public boolean isCollecting()
public void setCollecting(boolean collecting)
collecting - the new state of the sampler.public long getPeriod()
public void setPeriod(long samplePeriod)
samplePeriod - the new sampling period.public void setDelay(long delay)
delay - the delay period.public int getStackLimit()
public void setStackLimit(int stackLimit)
stackLimit - the maximum amount of stack frames that are sampledpublic void setSampleContextInitialization(boolean enabled)
true code executed during context initialization is included in the general
profile instead of grouping it into a single entry by default. If false a single
entry will be created that contains all time spent in initialization. This can be useful to
avoid polluting the general application profile with sampled stack frames that only run
during initialization.public com.oracle.truffle.api.instrumentation.SourceSectionFilter getFilter()
public void setFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter filter)
filter for the sampler. The sampler will only observe
parts of the executed source code that is specified by the filter.filter - The new filter describing which part of the source code to samplepublic boolean hasStackOverflowed()
public Map<com.oracle.truffle.api.TruffleContext,CPUSamplerData> getData()
TruffleContext to CPUSamplerData. It is collected based on the configuration
of the CPUSampler (e.g. CPUSampler.setFilter(SourceSectionFilter),
CPUSampler.setPeriod(long), etc.) and collecting can be controlled by the
CPUSampler.setCollecting(boolean). The collected data can be cleared from the cpusampler using
CPUSampler.clearData()TruffleContext to CPUSamplerData.public void clearData()
public boolean hasData()
public void close()
close in interface Closeableclose in interface AutoCloseablepublic boolean isGatherSelfHitTimes()
public void setGatherSelfHitTimes(boolean gatherSelfHitTimes)
gatherSelfHitTimes - new value for whether or not to gather timestampspublic Map<Thread,List<StackTraceEntry>> takeSample()
takeSample(this.getPeriod(),
TimeUnit.MILLISECONDS).CPUSampler.takeSample(long, TimeUnit)public Map<Thread,List<StackTraceEntry>> takeSample(long timeout, TimeUnit timeoutUnit)
If the given timeout is exceeded the sampling will be stopped. If a timeout occurs it may lead to an incomplete or empty result. For example, if only one thread times out the result of other threads may still be reported.