Package com.datadoghq.profiler
Class JavaProfiler
- java.lang.Object
-
- com.datadoghq.profiler.JavaProfiler
-
public final class JavaProfiler extends java.lang.ObjectJava API for in-process profiling. Serves as a wrapper around java-profiler native library. This class is a singleton. The first call togetInstance()initiates loading of libjavaProfiler.so.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddThread()Add the given thread to the set of profiled threads.voidclearContext()Clears context identifier for current thread.voiddump(java.nio.file.Path recording)Dumps the JFR recording at the provided pathjava.lang.Stringexecute(java.lang.String command)Execute an agent-compatible profiling command - the comma-separated list of arguments described in arguments.cpplonggetCurrentTicks()Get the ticks for the current thread.java.util.Map<java.lang.String,java.lang.Long>getDebugCounters()If the profiler is built in debug mode, returns counters recorded during profile execution.static JavaProfilergetInstance()Get a JavaProfiler instance backed by the bundled native library and using the default temp directory as the scratch where the bundled library will be exploded before linking.static JavaProfilergetInstance(java.lang.String scratchDir)Get a JavaProfiler instance backed by the bundled native library and using the given directory as the scratch where the bundled library will be exploded before linking.static JavaProfilergetInstance(java.lang.String libLocation, java.lang.String scratchDir)Get a JavaProfiler instance backed by the given native library and using the given directory as the scratch where the bundled library will be exploded before linking.longgetSamples()Get the number of samples collected during the profiling sessionjava.lang.StringgetVersion()Get profiler agent version, e.g.booleanisThresholdExceeded(long thresholdMillis, long startTicks, long endTicks)Scales the ticks to milliseconds and applies a thresholdvoidrecordQueueTime(long startTicks, long endTicks, java.lang.Class<?> task, java.lang.Class<?> scheduler, java.lang.Thread origin)Records when queueing endedvoidrecordSetting(java.lang.String name, java.lang.String value)Records a datadog.ProfilerSetting event with no unitvoidrecordSetting(java.lang.String name, java.lang.String value, java.lang.String unit)Records a datadog.ProfilerSetting eventbooleanrecordTraceRoot(long rootSpanId, java.lang.String endpoint, int sizeLimit)Deprecated.booleanrecordTraceRoot(long rootSpanId, java.lang.String endpoint, java.lang.String operation, int sizeLimit)Records the completion of the trace rootvoidremoveThread()Remove the given thread to the set of profiled threads.voidsetContext(long spanId, long rootSpanId)Passing context identifier to a profiler.voidsetContextByteBuffer(int tid, int offset, int value)voidsetContextValue(int offset, int value)Sets a context valuevoidstop()Stop profiling (without dumping results)
-
-
-
Method Detail
-
getInstance
public static JavaProfiler getInstance() throws java.io.IOException
Get a JavaProfiler instance backed by the bundled native library and using the default temp directory as the scratch where the bundled library will be exploded before linking.- Throws:
java.io.IOException
-
getInstance
public static JavaProfiler getInstance(java.lang.String scratchDir) throws java.io.IOException
Get a JavaProfiler instance backed by the bundled native library and using the given directory as the scratch where the bundled library will be exploded before linking.- Parameters:
scratchDir- directory where the bundled library will be exploded before linking- Throws:
java.io.IOException
-
getInstance
public static JavaProfiler getInstance(java.lang.String libLocation, java.lang.String scratchDir) throws java.io.IOException
Get a JavaProfiler instance backed by the given native library and using the given directory as the scratch where the bundled library will be exploded before linking.- Parameters:
libLocation- the path to the native library to be used instead of the bundled onescratchDir- directory where the bundled library will be exploded before linking; ignored when 'libLocation' is null- Throws:
java.io.IOException
-
stop
public void stop() throws java.lang.IllegalStateExceptionStop profiling (without dumping results)- Throws:
java.lang.IllegalStateException- If profiler is not running
-
getSamples
public long getSamples()
Get the number of samples collected during the profiling session- Returns:
- Number of samples
-
getVersion
public java.lang.String getVersion()
Get profiler agent version, e.g. "1.0"- Returns:
- Version string
-
execute
public java.lang.String execute(java.lang.String command) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, java.io.IOExceptionExecute an agent-compatible profiling command - the comma-separated list of arguments described in arguments.cpp- Parameters:
command- Profiling command- Returns:
- The command result
- Throws:
java.lang.IllegalArgumentException- If failed to parse the commandjava.io.IOException- If failed to create output filejava.lang.IllegalStateException
-
recordTraceRoot
public boolean recordTraceRoot(long rootSpanId, java.lang.String endpoint, java.lang.String operation, int sizeLimit)Records the completion of the trace root
-
recordTraceRoot
@Deprecated public boolean recordTraceRoot(long rootSpanId, java.lang.String endpoint, int sizeLimit)Deprecated.Records the completion of the trace root
-
addThread
public void addThread()
Add the given thread to the set of profiled threads. 'filter' option must be enabled to use this method.
-
removeThread
public void removeThread()
Remove the given thread to the set of profiled threads. 'filter' option must be enabled to use this method.
-
setContext
public void setContext(long spanId, long rootSpanId)Passing context identifier to a profiler. This ID is thread-local and is dumped in the JFR output only. 0 is a reserved value for "no-context".- Parameters:
spanId- Span identifier that should be stored for current threadrootSpanId- Root Span identifier that should be stored for current thread
-
clearContext
public void clearContext()
Clears context identifier for current thread.
-
setContextValue
public void setContextValue(int offset, int value)Sets a context value- Parameters:
offset- the offsetvalue- the encoding of the value. Must have been encoded via @see JavaProfiler#registerConstant
-
setContextByteBuffer
public void setContextByteBuffer(int tid, int offset, int value)
-
dump
public void dump(java.nio.file.Path recording)
Dumps the JFR recording at the provided path- Parameters:
recording- the path to the recording- Throws:
java.lang.NullPointerException- if recording is null
-
recordSetting
public void recordSetting(java.lang.String name, java.lang.String value)Records a datadog.ProfilerSetting event with no unit- Parameters:
name- the namevalue- the value
-
recordSetting
public void recordSetting(java.lang.String name, java.lang.String value, java.lang.String unit)Records a datadog.ProfilerSetting event- Parameters:
name- the namevalue- the valueunit- the unit
-
isThresholdExceeded
public boolean isThresholdExceeded(long thresholdMillis, long startTicks, long endTicks)Scales the ticks to milliseconds and applies a threshold
-
recordQueueTime
public void recordQueueTime(long startTicks, long endTicks, java.lang.Class<?> task, java.lang.Class<?> scheduler, java.lang.Thread origin)Records when queueing ended- Parameters:
task- the name of the enqueue taskscheduler- the name of the thread-pool or executor scheduling the taskorigin- the thread the task was submitted on
-
getCurrentTicks
public long getCurrentTicks()
Get the ticks for the current thread.- Returns:
- ticks
-
getDebugCounters
public java.util.Map<java.lang.String,java.lang.Long> getDebugCounters()
If the profiler is built in debug mode, returns counters recorded during profile execution. These are for whitebox testing and not intended for production use.- Returns:
- a map of counters
-
-