Package org.apache.solr.util
Class ThreadCpuTimer
- java.lang.Object
-
- org.apache.solr.util.ThreadCpuTimer
-
@NotThreadSafe public class ThreadCpuTimer extends Object
Allows tracking information about the current thread using the JVM's built-in management beanThreadMXBean
.Calling code should create an instance of this class when starting the operation, and then can get the
getElapsedCpuMs()
at any time thereafter.This class is irrevocably not thread safe. Never allow instances of this class to be exposed to more than one thread. Acquiring an external lock will not be sufficient. This class can be considered "lock-hostile" due to its caching of timing information for a specific thread.
-
-
Field Summary
Fields Modifier and Type Field Description static String
CPU_TIME
static String
ENABLE_CPU_TIME
static String
LOCAL_CPU_TIME
-
Constructor Summary
Constructors Constructor Description ThreadCpuTimer()
Create an instance to track the current thread's usage of CPU.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<Long>
getElapsedCpuMs()
Get the CPU usage information for the current thread since it created thisThreadCpuTimer
.long
getElapsedCpuNs()
Return CPU time consumed by this thread since the construction of this timer object.static boolean
isSupported()
String
toString()
-
-
-
Field Detail
-
CPU_TIME
public static final String CPU_TIME
- See Also:
- Constant Field Values
-
LOCAL_CPU_TIME
public static final String LOCAL_CPU_TIME
- See Also:
- Constant Field Values
-
ENABLE_CPU_TIME
public static final String ENABLE_CPU_TIME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ThreadCpuTimer
public ThreadCpuTimer()
Create an instance to track the current thread's usage of CPU. Usage information can later be retrieved by callinggetElapsedCpuMs()
. Timing starts immediately upon construction.
-
-
Method Detail
-
isSupported
public static boolean isSupported()
-
getElapsedCpuNs
public long getElapsedCpuNs()
Return CPU time consumed by this thread since the construction of this timer object.- Returns:
- current value, or
UNSUPPORTED
if not supported.
-
getElapsedCpuMs
public Optional<Long> getElapsedCpuMs()
Get the CPU usage information for the current thread since it created thisThreadCpuTimer
. The result is undefined if called by any other thread.- Returns:
- the thread's cpu since the creation of this
ThreadCpuTimer
instance. If the VM's cpu tracking is disabled, returned value will beUNSUPPORTED
.
-
-