com.sun.faban.driver.util
Class Timer

java.lang.Object
  extended by com.sun.faban.driver.util.Timer
All Implemented Interfaces:
Serializable

public class Timer
extends Object
implements Serializable

Timer for all benchmark runs.

See Also:
Serialized Form

Constructor Summary
Timer()
          Default Constructor which saves the current time as epochMillis and epochNanos (the start of the benchmark).
 
Method Summary
 void adjustBaseTime(int offset)
          Adjusts the base time based on the clock differences of this JVM to the master's JVM.
 void calibrate(String id, long endTime)
          Runs a timer sleep time calibration for a certain amount of time.
 long getCompensation()
          Reads the compensation value.
 double getDeviation()
          Reads the deviation value.
 int getTime()
          Obtains the current time relative to the base time, in milliseconds.
 long toAbsMillis(int relTimeMillis)
          Converts the millisec time relative to the run's epoch to absolute millisec comparable to System.currentTimeMillis().
 long toAbsMillis(long relTimeNanos)
          Converts the nanosecond time relative to the run's epoch to absolute millisec comparable to System.currentTimeMillis().
 long toAbsNanos(int relTimeMillis)
          Converts the millisec relative time to absolute nanosecs.
 long toAbsTime(long relNanos)
          Obtains the nano time comparable to System.nanoTime() from a given nanotime relative to the base time.
 long toRelTime(long nanoTime)
          Obtains the time relative to the base time, given a nanoTime with an unknown datum.
 void wakeupAt(long wakeupTime)
          Causes this thread to sleep until the wakeup time as referenced by this timer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Timer

public Timer()
Default Constructor which saves the current time as epochMillis and epochNanos (the start of the benchmark). The resolution is unknown. Note that this is only constructed on the master so the initial values before adjustments pertain to the master alone.

Method Detail

toAbsNanos

public long toAbsNanos(int relTimeMillis)
Converts the millisec relative time to absolute nanosecs.

Parameters:
relTimeMillis - The millisec relative time
Returns:
The corresponding nanosec time

toAbsMillis

public long toAbsMillis(long relTimeNanos)
Converts the nanosecond time relative to the run's epoch to absolute millisec comparable to System.currentTimeMillis().

Parameters:
relTimeNanos - The relative time in nanosecs
Returns:
The absolute time in millisecs

toAbsMillis

public long toAbsMillis(int relTimeMillis)
Converts the millisec time relative to the run's epoch to absolute millisec comparable to System.currentTimeMillis().

Parameters:
relTimeMillis - The relative time in nanosecs
Returns:
the absolute time in millisecs

getTime

public int getTime()
Obtains the current time relative to the base time, in milliseconds. This is mainly used to determine the current state of of the run. The base time is synchronized between the Faban master and all driver agents.

Returns:
The nanoseconds from the base time.

toRelTime

public long toRelTime(long nanoTime)
Obtains the time relative to the base time, given a nanoTime with an unknown datum. The base time is synchronized between the Faban master and all driver agents.

Parameters:
nanoTime - The nanotime obtained from System.nanoTime()
Returns:
The nanosecond time relative to our base time.

toAbsTime

public long toAbsTime(long relNanos)
Obtains the nano time comparable to System.nanoTime() from a given nanotime relative to the base time.

Parameters:
relNanos - The relative nanosecond time
Returns:
The nanoseconds comparable to System.nanoTime.

getCompensation

public long getCompensation()
Reads the compensation value.

Returns:
The compensation

getDeviation

public double getDeviation()
Reads the deviation value.

Returns:
The deviation

wakeupAt

public void wakeupAt(long wakeupTime)
Causes this thread to sleep until the wakeup time as referenced by this timer. Timer.sleep is not a minimum sleep time as in Thread.sleep, but rather a calibrated and compensated sleep time which gives the best statistical opportunity to wake up at the required time. The actual wakeup can be slightly before or slightly after the wakeup time but the average discrepancy should be close to zero.

Parameters:
wakeupTime - The time this thread is supposed to wakeup.

calibrate

public void calibrate(String id,
                      long endTime)
Runs a timer sleep time calibration for a certain amount of time.

Parameters:
id - The agent identifier - used for logging purposes.
endTime - The time to end the calibration, referencing this JVMs nanosec timer.

adjustBaseTime

public void adjustBaseTime(int offset)
Adjusts the base time based on the clock differences of this JVM to the master's JVM. This is done at the millisecond accuracy. Since remote calls are usually not much faster than a millisec, it does not make much sense to be too ideological about accuracy here.

Parameters:
offset - The millisec offset between systems