hu.mta.sztaki.lpds.cloud.simulator
Class Timed

java.lang.Object
  extended by hu.mta.sztaki.lpds.cloud.simulator.Timed
All Implemented Interfaces:
Comparable<Timed>
Direct Known Subclasses:
DeferredEvent.AggregatedEventDispatcher, EnergyMeter, MonitorConsumption, ResourceSpreader.FreqSyncer

public abstract class Timed
extends Object
implements Comparable<Timed>

This is the base class for the simulation, every class that should receive timing events should extend this and implement the function named "tick". Tick is also used as the smallest discrete increase in time and it is user defined. IMPORTANT: How long an actual tick takes is left for the simulator's user. The simulator expects every time dependent operation and constant to be based on ticks used here. E.g., if you define the outgoing network bandwidth of a networknode then you should set it in bytes/tick. Thus if your tick=ms you must set it in bytes/ms.

Author:
"Gabor Kecskemeti, Distributed and Parallel Systems Group, University of Innsbruck (c) 2013" "Gabor Kecskemeti, Laboratory of Parallel and Distributed Systems, MTA SZTAKI (c) 2012"

Field Summary
private  boolean activeSubscription
          Determines if the actual timed object is going to receive recurring events (through the tick() function).
private  boolean backPreference
          Should two timed events occur on the same time instance this marker allows Timed to determine which ones should be notified first.
private static long fireCounter
          The actual time in the system.
private  long frequency
          The number of ticks that should pass between two tick() calls.
private  long nextEvent
          Specifies the next time (in ticks) when the recurring event should be fired.
private static PriorityQueue<Timed> timedlist
          The main container for all recurring events in the system
private static Timed underProcessing
          the event that is currently processed by the event loop.
 
Constructor Summary
Timed()
           
 
Method Summary
static long calcTimeJump(long jump)
          A simple approach to calculate time advances in the system
 int compareTo(Timed o)
          a comparator for timed objects based on next events and back preference (those objects will be specified smaller that have an earlier next event - if nextevents are the same then backpreference decides betwen events)
static void fire()
          This function allows the manual operation of the event handling mechanism.
static long getFireCount()
          Determines the simulated time that has already passed since the beginning of the simulation (0).
 long getFrequency()
          Determines the time distance (in ticks) between two tick() calls.
 long getNextEvent()
          Allows the query of the next event at which the tick() function will be called for this object
static long getNextFire()
          Determines the earliest time instance when there is any event in the system to be performed.
 boolean isSubscribed()
          Allows to determine if a particular timed object is receiving notifications from the system
static long jumpTime(long desiredJump)
          Increases the time with a specific amount of ticks.
 long nextEventDistance()
          Determines the next event at which point this object will receive a tick() call.
private  void realSubscribe(long freq)
          The actual subscription function that is behind updateFreq or subcribe
static void resetTimed()
          Cancels all timed events and sets back the time to 0.
protected  void setBackPreference(boolean backPreference)
          Enables to set the back preference of a particular timed object.
static void simulateUntil(long time)
          Automatically advances the time in the simulation until the specific time instance.
static void simulateUntilLastEvent()
          Automatically advances the time in the simulation until there are no events remaining in the event queue.
static void skipEventsTill(long desiredTime)
          Jumps the time until the time given by the user.
protected  boolean subscribe(long freq)
          Allows Timed objects to subscribe for recurring events with a particular frequency.
abstract  void tick(long fires)
          This function will be called on all timed objects which asked for a recurring event notification at a given time instance.
 String toString()
          Prints out basic information about this timed object.
protected  boolean unsubscribe()
          Cancels the future recurrance of this event.
private  void updateEvent(long freq)
          A core function that actually manages the frequency and nextevent fields.
protected  long updateFrequency(long freq)
          Allows the alteration of the event frequency independently from subscription.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

timedlist

private static final PriorityQueue<Timed> timedlist
The main container for all recurring events in the system


underProcessing

private static Timed underProcessing
the event that is currently processed by the event loop.


fireCounter

private static long fireCounter
The actual time in the system. This is maintained in ticks!


activeSubscription

private boolean activeSubscription
Determines if the actual timed object is going to receive recurring events (through the tick() function).


nextEvent

private long nextEvent
Specifies the next time (in ticks) when the recurring event should be fired. This is used to order the elements in the timedList event list.


frequency

private long frequency
The number of ticks that should pass between two tick() calls. This field is usually a positive number, if it is -1 then the frequency is not yet initialized by the class.


backPreference

private boolean backPreference
Should two timed events occur on the same time instance this marker allows Timed to determine which ones should be notified first. if this is true then all timed events (except other backpreferred ones) on the same time instance will be fired first before this one is called. Just like regular events, the notification order of backpreferred events is not fixed!

Constructor Detail

Timed

public Timed()
Method Detail

isSubscribed

public final boolean isSubscribed()
Allows to determine if a particular timed object is receiving notifications from the system

Returns:
  • true if this object will receive recurrign events in the future
  • false otherwise

subscribe

protected final boolean subscribe(long freq)
Allows Timed objects to subscribe for recurring events with a particular frequency. This function is protected so no external entities should be able to modify the subscription for a timed object.

Parameters:
freq - the event frequency with which the tick() function should be called on the particular implementation of timed.
Returns:
  • true if the subscription succeeded
  • false otherwise (i.e. when there was already a subscription). Please note that if you receive false, then the tick() function will not be called with the frequency defined here!

realSubscribe

private void realSubscribe(long freq)
The actual subscription function that is behind updateFreq or subcribe

Parameters:
freq - the event frequency with which the tick() function should be called on the particular implementation of timed.

unsubscribe

protected final boolean unsubscribe()
Cancels the future recurrance of this event.

Returns:
  • true if the unsubscription succeeded
  • false otherwise (i.e., this timed object was already cancelled)

updateFrequency

protected final long updateFrequency(long freq)
Allows the alteration of the event frequency independently from subscription. If the Timed object is not subscribed then the update function will ensure the subscription happens

Parameters:
freq - the event frequency with which the tick() function should be called on the particular implementation of timed.
Returns:
the earilest time instance (in ticks) when the tick() function will be called.

updateEvent

private void updateEvent(long freq)
A core function that actually manages the frequency and nextevent fields. It contains several checks to reveal inproper handling of the Timed object.

Parameters:
freq - the event frequency with which the tick() function should be called on the particular implementation of timed.
Throws:
IllegalStateException - if the frequency specified is negative, or if the next event would be in the indefinite future

getNextEvent

public long getNextEvent()
Allows the query of the next event at which the tick() function will be called for this object

Returns:
the next event's time instance in ticks.

getFrequency

public long getFrequency()
Determines the time distance (in ticks) between two tick() calls. If this object is unsubscribed then this call returns with the last frequency.

Returns:
the frequency in ticks.

nextEventDistance

public long nextEventDistance()
Determines the next event at which point this object will receive a tick() call.

Returns:
  • if subscribed the number of ticks till the next tick() call arrives
  • if not subscribed Long.MAX_VALUE.

compareTo

public int compareTo(Timed o)
a comparator for timed objects based on next events and back preference (those objects will be specified smaller that have an earlier next event - if nextevents are the same then backpreference decides betwen events)

Specified by:
compareTo in interface Comparable<Timed>

setBackPreference

protected void setBackPreference(boolean backPreference)
Enables to set the back preference of a particular timed object.

Parameters:
backPreference -
  • true if this event should be processed amongst the last events at any given time instance
  • false if the event should be processed before the backpreferred events - this is the default case for all events before calling this function!

fire

public static final void fire()
This function allows the manual operation of the event handling mechanism. It is used to send out events that should occur at a particular time instance. After the events are sent out the time will be advanced by 1 tick. If there are no events due at the particular time instance then this function just advances the time by one tick.


calcTimeJump

public static long calcTimeJump(long jump)
A simple approach to calculate time advances in the system

Parameters:
jump - the time (in ticks) to be advanced with
Returns:
the time (in ticks) at which point the particular jump will be complete

jumpTime

public static final long jumpTime(long desiredJump)
Increases the time with a specific amount of ticks. If there are some events that would need to be called before the specific amount of ticks happen, then the time is advanced to just the time instance before the next event should be fired. This function allows a more manual handling of the simulation. But it is also used by the simulateUntil* functions.

Parameters:
desiredJump - the amount of time to be jumped ahead.
Returns:
the amount of time that still remains until desiredjump.

skipEventsTill

public static final void skipEventsTill(long desiredTime)
Jumps the time until the time given by the user. If some events supposed to happen during the jumped time period, then this function cancels them. If some events should be recurring during the period, then the first recurrence of the event will be after the given time instance. If the given time instance has already occurred then this function does nothing!

Parameters:
desiredTime - the time at which the simulation should continue after this call. If the time given here already happened then this function will have no effect.

getFireCount

public static final long getFireCount()
Determines the simulated time that has already passed since the beginning of the simulation (0).

Returns:
The number of ticks that has passed since the beginning of time.

getNextFire

public static final long getNextFire()
Determines the earliest time instance when there is any event in the system to be performed.

Returns:
the time instance in ticks

simulateUntilLastEvent

public static final void simulateUntilLastEvent()
Automatically advances the time in the simulation until there are no events remaining in the event queue. This function is useful when the simulation is completely set up and there is no user interaction expected before the simulation completes. The function is ensuring that all events are fired during its operation. WARNING: Please note calling this function could lead to infinite loops if at least one of the timed objects in the system does not call its unsubscribe() function.


simulateUntil

public static final void simulateUntil(long time)
Automatically advances the time in the simulation until the specific time instance. The function is ensuring that all events are fired during its operation.

Parameters:
time - the time instance that should not happen but the time should advance to this point.

resetTimed

public static final void resetTimed()
Cancels all timed events and sets back the time to 0.


toString

public String toString()
Prints out basic information about this timed object. Enables easy debugging of complex simulations.

Overrides:
toString in class Object

tick

public abstract void tick(long fires)
This function will be called on all timed objects which asked for a recurring event notification at a given time instance.

Parameters:
fires - The particular time instance when the function was called. The time instance is passed so the tick functions will not need to call getFireCount() if they need to operate on the actual time.


Copyright © 2012–2015 University of Innsbruck & MTA SZTAKI. All rights reserved.