Class Timer


  • public class Timer
    extends Object
    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

      Fields 
      Modifier and Type Field Description
      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.
    • Constructor Summary

      Constructors 
      Constructor Description
      Timer​(String name, int todoFlags)
      Constructor.
      Timer​(String name, int todoFlags, long threadId)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(Object obj)  
      long getAvgCpuTime()
      Return the average CPU time across all measurements.
      long getAvgWallTime()
      Return the average wall clock time across all measurements.
      String 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 getThreadId()
      Get the ID of the thread for which this timer was created.
      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 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.
      String toString()
      The implementation of toString() generates a summary of the times recorded so far.
    • 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
    • Constructor Detail

      • Timer

        public Timer​(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 - a string that identifies the timer
        todoFlags - flags to define what the timer will measure
        threadId - the id of the thread for measuring CPU time or 0 if not measuring
      • Timer

        public Timer​(String name,
                     int todoFlags)
        Constructor. Same as Timer(String, int, long), but using the current thread instead of a freely specified thread.
        Parameters:
        name - a string that identifies the timer
        todoFlags - flags to define what the timer will measure
    • Method Detail

      • 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
      • 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 with getTotalCpuTime() 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

        public String 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.
        Overrides:
        toString in class Object
        Returns:
        string description of the timer results and state
      • 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 Timer 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
      • getNamedTimer

        public static Timer 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 Timer 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 Timer getNamedTotalTimer​(String timerName)
        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()
        Overrides:
        hashCode in class Object