org.semanticweb.elk.util.logging
Class ElkTimer

java.lang.Object
  extended by org.semanticweb.elk.util.logging.ElkTimer

public class ElkTimer
extends Object

Class for keeping CPU and system times.

Author:
Markus Kroetzsch

Field Summary
protected  long currentStartCpuTime
           
protected  long currentStartWallTime
           
protected  boolean isRunning
           
protected  int measurements
           
protected  String name
           
static int RECORD_ALL
          Flag for indicating that all supported times should be taken.
static int RECORD_CPUTIME
          Flag for indicating that CPU time should be taken.
static int RECORD_NONE
          Flag for indicating that no times should be taken (just count runs).
static int RECORD_WALLTIME
          Flag for indicating that wall clock time should be taken.
protected  int threadCount
           
protected  long threadId
           
protected  int todoFlags
           
protected  long totalCpuTime
           
protected  long totalWallTime
           
 
Constructor Summary
ElkTimer(String name, int todoFlags, long threadId)
          Constructor.
 
Method Summary
 boolean equals(Object obj)
           
 long getAvgCpuTime()
           
 long getAvgWallTime()
           
 String getName()
          Get the string name of the timer.
static ElkTimer getNamedTimer(String timerName)
          Get a timer of the given string name that takes all possible times (todos) for the current thread.
static ElkTimer getNamedTimer(String timerName, int todoFlags)
          Get a timer of the given string name and todos for the current thread.
static ElkTimer getNamedTimer(String timerName, int todoFlags, long threadId)
          Get a timer of the given string name for the given thread.
static Iterable<ElkTimer> getNamedTimers()
          Returns all registered timers
static ElkTimer getNamedTotalTimer(String timerName)
          Collect the total times measured by all known named timers of the given name.
protected static long getThreadCpuTime(long threadId)
           
 long getThreadId()
          Get the ID of the thread for which this timer was created.
static ElkTimer getTimerForCurrentThread(String name, int todoFlags)
           
 long getTotalCpuTime()
          Get the total recorded CPU time in nanoseconds.
 long getTotalWallTime()
          Get the total recorded wall clock time in nanoseconds.
 int hashCode()
           
 boolean isRunning()
          Return true if the timer is running.
 void log(org.apache.log4j.Logger logger)
          Print logging information for the timer using debug priority.
 void log(org.apache.log4j.Logger logger, org.apache.log4j.Priority priority)
          Print logging information for the timer.
static void logAllNamedTimers(String timerName, org.apache.log4j.Logger logger)
           
static void logAllNamedTimers(String timerName, org.apache.log4j.Logger logger, org.apache.log4j.Priority priority)
           
 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 return the CPU time that has passed since it had last been started.
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.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RECORD_NONE

public static final int RECORD_NONE
Flag for indicating that no times should be taken (just count runs).

See Also:
Constant Field Values

RECORD_CPUTIME

public static final int RECORD_CPUTIME
Flag for indicating that CPU time should be taken.

See Also:
Constant Field Values

RECORD_WALLTIME

public static final int RECORD_WALLTIME
Flag for indicating that wall clock time should be taken.

See Also:
Constant Field Values

RECORD_ALL

public static final int RECORD_ALL
Flag for indicating that all supported times should be taken.

See Also:
Constant Field Values

name

protected final String name

threadId

protected final long threadId

todoFlags

protected final int todoFlags

currentStartCpuTime

protected long currentStartCpuTime

currentStartWallTime

protected long currentStartWallTime

isRunning

protected boolean isRunning

totalCpuTime

protected long totalCpuTime

totalWallTime

protected long totalWallTime

measurements

protected int measurements

threadCount

protected int threadCount
Constructor Detail

ElkTimer

public ElkTimer(String name,
                int todoFlags,
                long threadId)
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 -
todoFlags -
threadId -
Method Detail

getTimerForCurrentThread

public static ElkTimer getTimerForCurrentThread(String name,
                                                int todoFlags)
Parameters:
name -
todoFlags -
Returns:
a new ElkTimer for the current thread

getTotalCpuTime

public long getTotalCpuTime()
Get the total recorded CPU time in nanoseconds.

Returns:
recorded CPU time in nanoseconds

getAvgCpuTime

public long getAvgCpuTime()

getName

public String 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

getTotalWallTime

public long getTotalWallTime()
Get the total recorded wall clock time in nanoseconds.

Returns:
recorded wall time in nanoseconds

getAvgWallTime

public long getAvgWallTime()

isRunning

public boolean isRunning()
Return true if the timer is running.

Returns:
true if running

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 return the CPU time that has passed since it had last been started. The total time (both system and CPU) of all start-stop cycles is recorded with the timer.

Returns:
CPU time that the timer was running, or -1 if timer not running or CPU time unavailable for other reasons

log

public void log(org.apache.log4j.Logger logger)
Print logging information for the timer using debug priority.

Parameters:
logger -
See Also:
log()

log

public void log(org.apache.log4j.Logger logger,
                org.apache.log4j.Priority priority)
Print logging information for the timer. The log only shows the recorded time of the completed start-stop cycles. 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 be logged.

Parameters:
logger -
priority -

startNamedTimer

public static void startNamedTimer(String timerName)
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

public static void startNamedTimer(String timerName,
                                   int todoFlags)
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 timer
todoFlags -

startNamedTimer

public static void startNamedTimer(String timerName,
                                   int todoFlags,
                                   long threadId)
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 timer
todoFlags -
threadId - of the thread to track, or 0 if only system clock should be tracked

stopNamedTimer

public static long stopNamedTimer(String timerName)
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

public static long stopNamedTimer(String timerName,
                                  int todoFlags)
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 timer
todoFlags -
Returns:
CPU time if timer existed and was running, and -1 otherwise

stopNamedTimer

public static long stopNamedTimer(String timerName,
                                  int todoFlags,
                                  long threadId)
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 timer
todoFlags -
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

public static void resetNamedTimer(String timerName)
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

public static void resetNamedTimer(String timerName,
                                   int todoFlags)
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 timer
todoFlags -

resetNamedTimer

public static void resetNamedTimer(String timerName,
                                   int todoFlags,
                                   long threadId)
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 timer
todoFlags -
threadId - of the thread to track, or 0 if only system clock should be tracked

getNamedTimer

public static ElkTimer getNamedTimer(String timerName)
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

getNamedTimers

public static Iterable<ElkTimer> getNamedTimers()
Returns all registered timers

Returns:
an iterable collection of named timers

getNamedTimer

public static ElkTimer getNamedTimer(String timerName,
                                     int todoFlags)
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 timer
todoFlags -
Returns:
timer

getNamedTimer

public static ElkTimer getNamedTimer(String timerName,
                                     int todoFlags,
                                     long threadId)
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 timer
todoFlags -
threadId - of the thread to track, or 0 if only system clock should be tracked
Returns:
timer

getNamedTotalTimer

public static ElkTimer getNamedTotalTimer(String timerName)
Collect the total times measured by all known named timers of the given name.

Parameters:
timerName -
Returns:
timer

logAllNamedTimers

public static void logAllNamedTimers(String timerName,
                                     org.apache.log4j.Logger logger)

logAllNamedTimers

public static void logAllNamedTimers(String timerName,
                                     org.apache.log4j.Logger logger,
                                     org.apache.log4j.Priority priority)

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

getThreadCpuTime

protected static long getThreadCpuTime(long threadId)


Copyright © 2011-2013 Department of Computer Science, University of Oxford. All Rights Reserved.