Package org.wikidata.wdtk.util
Class Timer
java.lang.Object
org.wikidata.wdtk.util.Timer
Class for keeping CPU and system times. Timers measure wall clock and/or CPU
times (for specific threads). They can be started and stopped. Times between
these two methods will be recorded and (when starting and stopping more than
once) added up to total times. The number of start-stop measurements is
recorded, and one can also query the average times. Finally, a timer can be
reset.
There are two main ways of accessing timers: by creating a Timer object
directly or by using a global registry of timers. Registered timers are
identified by their string name and thread id. The global registry is useful
since it makes it much easier to re-integrate measurements taken in many
threads. They also free the caller of the burden of keeping a reference to
the Timer.
The code in this file was adapted from the ElkTimer class of the ELK reasoner, with contributions from Yevgeny
Kasakov and Pavel Klinov.
- Author:
- Markus Kroetzsch
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Flag for indicating that all supported times should be taken.static final int
Flag for indicating that CPU time should be taken.static final int
Flag for indicating that no times should be taken (just count runs).static final int
Flag for indicating that wall clock time should be taken. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
long
Return the average CPU time across all measurements.long
Return the average wall clock time across all measurements.getName()
Get the string name of the timer.static Timer
getNamedTimer
(String timerName) Get a timer of the given string name that takes all possible times (todos) for the current thread.static Timer
getNamedTimer
(String timerName, int todoFlags) Get a timer of the given string name and todos for the current thread.static Timer
getNamedTimer
(String timerName, int todoFlags, long threadId) Get a timer of the given string name for the given thread.static Timer
getNamedTotalTimer
(String timerName) Collect the total times measured by all known named timers of the given name.long
Get the ID of the thread for which this timer was created.long
Get the total recorded CPU time in nanoseconds.long
Get the total recorded wall clock time in nanoseconds.int
hashCode()
boolean
Return true if the timer is running.void
reset()
Stop the timer (if running) and reset all recorded values.static void
resetNamedTimer
(String timerName) Reset a timer of the given string name for all todos and the current thread.static void
resetNamedTimer
(String timerName, int todoFlags) Reset a timer of the given string name for the current thread.static void
resetNamedTimer
(String timerName, int todoFlags, long threadId) Reset a timer of the given string name for the given thread.void
start()
Start the timer.static void
startNamedTimer
(String timerName) Start a timer of the given string name for all todos and the current thread.static void
startNamedTimer
(String timerName, int todoFlags) Start a timer of the given string name for the current thread.static void
startNamedTimer
(String timerName, int todoFlags, long threadId) Start a timer of the given string name for the current thread.long
stop()
Stop the timer and record the times that have passed since its start.static long
stopNamedTimer
(String timerName) Stop a timer of the given string name for all todos and the current thread.static long
stopNamedTimer
(String timerName, int todoFlags) Stop a timer of the given string name for the current thread.static long
stopNamedTimer
(String timerName, int todoFlags, long threadId) Stop a timer of the given string name for the given thread.toString()
The implementation of toString() generates a summary of the times recorded so far.
-
Field Details
-
RECORD_NONE
public static final int RECORD_NONEFlag for indicating that no times should be taken (just count runs).- See Also:
-
RECORD_CPUTIME
public static final int RECORD_CPUTIMEFlag for indicating that CPU time should be taken.- See Also:
-
RECORD_WALLTIME
public static final int RECORD_WALLTIMEFlag for indicating that wall clock time should be taken.- See Also:
-
RECORD_ALL
public static final int RECORD_ALLFlag for indicating that all supported times should be taken.- See Also:
-
-
Constructor Details
-
Timer
Constructor. Every timer is identified by three things: a string name, an integer for flagging its tasks (todos), and a thread id (long). Tasks can be flagged by a disjunction of constants like RECORD_CPUTIME and RECORD_WALLTIME. Only times for which an according flag is set will be recorded. The thread id can be the actual id of the thread that is measured, or 0 (invalid id) to not assign the timer to any thread. In this case, no CPU time measurement is possible since Java does not allow us to measure the total CPU time across all threads.- Parameters:
name
- a string that identifies the timertodoFlags
- flags to define what the timer will measurethreadId
- the id of the thread for measuring CPU time or 0 if not measuring
-
Timer
Constructor. Same asTimer(String, int, long)
, but using the current thread instead of a freely specified thread.- Parameters:
name
- a string that identifies the timertodoFlags
- flags to define what the timer will measure
-
-
Method Details
-
getName
Get the string name of the timer.- Returns:
- string name
-
getThreadId
public long getThreadId()Get the ID of the thread for which this timer was created.- Returns:
- thread ID
-
isRunning
public boolean isRunning()Return true if the timer is running.- Returns:
- true if running
-
getTotalCpuTime
public long getTotalCpuTime()Get the total recorded CPU time in nanoseconds.- Returns:
- recorded CPU time in nanoseconds
-
getAvgCpuTime
public long getAvgCpuTime()Return the average CPU time across all measurements.- Returns:
- the average CPU time across all measurements
-
getTotalWallTime
public long getTotalWallTime()Get the total recorded wall clock time in nanoseconds.- Returns:
- recorded wall time in nanoseconds
-
getAvgWallTime
public long getAvgWallTime()Return the average wall clock time across all measurements.- Returns:
- the average wall clock time across all measurements
-
start
public void start()Start the timer. -
reset
public void reset()Stop the timer (if running) and reset all recorded values. -
stop
public long stop()Stop the timer and record the times that have passed since its start. The times that have passed are added to the internal state and can be retrieved withgetTotalCpuTime()
etc. If CPU times are recorded, then the method returns the CPU time that has passed since the timer was last started; otherwise -1 is returned.- Returns:
- CPU time that the timer was running, or -1 if timer not running or CPU time unavailable for other reasons
-
toString
The implementation of toString() generates a summary of the times recorded so far. If the timer is still running, then it will not be stopped to add the currently measured time to the output but a warning will added. -
startNamedTimer
Start a timer of the given string name for all todos and the current thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timer
-
startNamedTimer
Start a timer of the given string name for the current thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timertodoFlags
-
-
startNamedTimer
Start a timer of the given string name for the current thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timertodoFlags
-threadId
- of the thread to track, or 0 if only system clock should be tracked
-
stopNamedTimer
Stop a timer of the given string name for all todos and the current thread. If no such timer exists, -1 will be returned. Otherwise the return value is the CPU time that was measured.- Parameters:
timerName
- the name of the timer- Returns:
- CPU time if timer existed and was running, and -1 otherwise
-
stopNamedTimer
Stop a timer of the given string name for the current thread. If no such timer exists, -1 will be returned. Otherwise the return value is the CPU time that was measured.- Parameters:
timerName
- the name of the timertodoFlags
-- Returns:
- CPU time if timer existed and was running, and -1 otherwise
-
stopNamedTimer
Stop a timer of the given string name for the given thread. If no such timer exists, -1 will be returned. Otherwise the return value is the CPU time that was measured.- Parameters:
timerName
- the name of the timertodoFlags
-threadId
- of the thread to track, or 0 if only system clock should be tracked- Returns:
- CPU time if timer existed and was running, and -1 otherwise
-
resetNamedTimer
Reset a timer of the given string name for all todos and the current thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timer
-
resetNamedTimer
Reset a timer of the given string name for the current thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timertodoFlags
-
-
resetNamedTimer
Reset a timer of the given string name for the given thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timertodoFlags
-threadId
- of the thread to track, or 0 if only system clock should be tracked
-
getNamedTimer
Get a timer of the given string name that takes all possible times (todos) for the current thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timer- Returns:
- timer
-
getNamedTimer
Get a timer of the given string name and todos for the current thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timertodoFlags
-- Returns:
- timer
-
getNamedTimer
Get a timer of the given string name for the given thread. If no such timer exists yet, then it will be newly created.- Parameters:
timerName
- the name of the timertodoFlags
-threadId
- of the thread to track, or 0 if only system clock should be tracked- Returns:
- timer
-
getNamedTotalTimer
Collect the total times measured by all known named timers of the given name. This is useful to add up times that were collected across separate threads.- Parameters:
timerName
-- Returns:
- timer
-
hashCode
public int hashCode() -
equals
-