Class CloudSim

  • All Implemented Interfaces:
    Simulation

    public class CloudSim
    extends java.lang.Object
    implements Simulation
    The main class of the simulation API, that manages Cloud Computing simulations providing all methods to start, pause and stop them. It sends and processes all discrete events during the simulation time.
    Since:
    CloudSim Toolkit 1.0
    Author:
    Rodrigo N. Calheiros, Anton Beloglazov, Manoel Campos da Silva Filho
    • Field Detail

      • VERSION

        public static final java.lang.String VERSION
        CloudSim Plus current version.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CloudSim

        public CloudSim​(double minTimeBetweenEvents)
        Creates a CloudSim simulation that tracks events happening in a time interval as little as the minTimeBetweenEvents parameter. Internally it creates a CloudInformationService.
        Parameters:
        minTimeBetweenEvents - the minimal period between events. Events within shorter periods after the last event are discarded.
        See Also:
        CloudInformationService
    • Method Detail

      • start

        public double start()
        Description copied from interface: Simulation
        Starts simulation execution and waits for all entities to finish, i.e. until all entities threads reach non-RUNNABLE state or there are no more events in the future event queue.

        Note: This method should be called just after all the entities have been setup and added. The method blocks until the simulation is ended.

        Specified by:
        start in interface Simulation
        Returns:
        the last clock time
      • terminate

        public boolean terminate()
        Description copied from interface: Simulation
        Forces the termination of the simulation before it ends.
        Specified by:
        terminate in interface Simulation
        Returns:
        true if the simulation was running and the termination request was accepted, false if the simulation was not started yet
      • terminateAt

        public boolean terminateAt​(double time)
        Description copied from interface: Simulation
        Schedules the termination of the simulation for a given time (in seconds).

        If a termination time is set, the simulation stays running even if there is no event to process. It keeps waiting for new dynamic events, such as the creation of Cloudlets and VMs at runtime. If no event happens, the clock is increased to simulate time passing. The clock increment is defined according to: (i) the lower Datacenter.getSchedulingInterval() between existing Datacenters; or (ii) Simulation.getMinTimeBetweenEvents() in case no Datacenter has its schedulingInterval set.

        Specified by:
        terminateAt in interface Simulation
        Parameters:
        time - the time at which the simulation has to be terminated (in seconds)
        Returns:
        true if the time given is greater than the current simulation time, false otherwise
      • getTerminationTime

        public double getTerminationTime()
        Gets the time defined to terminate the simulation or -1 if it was not set.
        Returns:
      • getMinTimeBetweenEvents

        public double getMinTimeBetweenEvents()
        Description copied from interface: Simulation
        Returns the minimum time between events (in seconds). Events within shorter periods after the last event are discarded.
        Specified by:
        getMinTimeBetweenEvents in interface Simulation
        Returns:
        the minimum time between events (in seconds).
      • getCalendar

        public java.util.Calendar getCalendar()
        Description copied from interface: Simulation
        Gets a new copy of initial simulation Calendar.
        Specified by:
        getCalendar in interface Simulation
        Returns:
        a new copy of Calendar object
      • getNumEntities

        public int getNumEntities()
        Description copied from interface: Simulation
        Get the current number of entities in the simulation.
        Specified by:
        getNumEntities in interface Simulation
        Returns:
        The number of entities
      • getEntityList

        public java.util.List<SimEntity> getEntityList()
        Description copied from interface: Simulation
        Returns a read-only list of entities created for the simulation.
        Specified by:
        getEntityList in interface Simulation
        Returns:
      • addEntity

        public void addEntity​(CloudSimEntity entity)
        Description copied from interface: Simulation
        Adds a new entity to the simulation. Each CloudSimEntity object register itself when it is instantiated.
        Specified by:
        addEntity in interface Simulation
        Parameters:
        entity - The new entity
      • sendNow

        public void sendNow​(SimEntity src,
                            SimEntity dest,
                            int tag,
                            java.lang.Object data)
        Description copied from interface: Simulation
        Sends an event from one entity to another without delaying the message.
        Specified by:
        sendNow in interface Simulation
        Parameters:
        src - entity that scheduled the event
        dest - entity that the event will be sent to
        tag - the tag that classifies the event
        data - the data to be sent inside the event
      • send

        public void send​(SimEntity src,
                         SimEntity dest,
                         double delay,
                         int tag,
                         java.lang.Object data)
        Description copied from interface: Simulation
        Sends an event from one entity to another.
        Specified by:
        send in interface Simulation
        Parameters:
        src - entity that scheduled the event
        dest - entity that the event will be sent to
        delay - How many seconds after the current simulation time the event should be sent
        tag - the tag that classifies the event
        data - the data to be sent inside the event
      • send

        public void send​(SimEvent evt)
        Description copied from interface: Simulation
        Sends an event where all data required is defined inside the event instance.
        Specified by:
        send in interface Simulation
        Parameters:
        evt - the event to send
      • sendFirst

        public void sendFirst​(SimEntity src,
                              SimEntity dest,
                              double delay,
                              int tag,
                              java.lang.Object data)
        Description copied from interface: Simulation
        Sends an event from one entity to another, adding it to the beginning of the queue in order to give priority to it.
        Specified by:
        sendFirst in interface Simulation
        Parameters:
        src - entity that scheduled the event
        dest - entity that the event will be sent to
        delay - How many seconds after the current simulation time the event should be sent
        tag - the tag that classifies the event
        data - the data to be sent inside the event
      • sendFirst

        public void sendFirst​(SimEvent evt)
        Description copied from interface: Simulation
        Sends an event where all data required is defined inside the event instance, adding it to the beginning of the queue in order to give priority to it.
        Specified by:
        sendFirst in interface Simulation
        Parameters:
        evt - the event to send
      • wait

        public void wait​(CloudSimEntity src,
                         java.util.function.Predicate<SimEvent> predicate)
        Description copied from interface: Simulation
        Sets the state of an entity to SimEntity.State.WAITING, making it to wait for events that satisfy a given predicate. Only such events will be passed to the entity. This is done to avoid unnecessary context Datacenter.
        Specified by:
        wait in interface Simulation
        Parameters:
        src - entity that scheduled the event
        predicate - the event selection predicate
      • waiting

        public long waiting​(SimEntity dest,
                            java.util.function.Predicate<SimEvent> predicate)
        Description copied from interface: Simulation
        Gets the number of events in the deferred event queue that are targeted to a given entity and match a given predicate.
        Specified by:
        waiting in interface Simulation
        Parameters:
        dest - Id of entity that the event has to be sent to
        predicate - the event selection predicate
        Returns:
      • select

        public SimEvent select​(SimEntity dest,
                               java.util.function.Predicate<SimEvent> predicate)
        Description copied from interface: Simulation
        Selects the first deferred event that matches a given predicate and removes it from the queue.
        Specified by:
        select in interface Simulation
        Parameters:
        dest - entity that the event has to be sent to
        predicate - the event selection predicate
        Returns:
        the removed event or SimEvent.NULL if not found
      • findFirstDeferred

        public SimEvent findFirstDeferred​(SimEntity dest,
                                          java.util.function.Predicate<SimEvent> predicate)
        Description copied from interface: Simulation
        Find first deferred event matching a predicate.
        Specified by:
        findFirstDeferred in interface Simulation
        Parameters:
        dest - Id of entity that the event has to be sent to
        predicate - the event selection predicate
        Returns:
        the first matched event or SimEvent.NULL if not found
      • cancel

        public SimEvent cancel​(SimEntity src,
                               java.util.function.Predicate<SimEvent> predicate)
        Description copied from interface: Simulation
        Cancels the first event from the future event queue that matches a given predicate and was sent by a given entity, then removes it from the queue.
        Specified by:
        cancel in interface Simulation
        Parameters:
        src - Id of entity that scheduled the event
        predicate - the event selection predicate
        Returns:
        the removed event or SimEvent.NULL if not found
      • cancelAll

        public boolean cancelAll​(SimEntity src,
                                 java.util.function.Predicate<SimEvent> predicate)
        Description copied from interface: Simulation
        Cancels all events from the future event queue that matches a given predicate and were sent by a given entity, then removes those ones from the queue.
        Specified by:
        cancelAll in interface Simulation
        Parameters:
        src - Id of entity that scheduled the event
        predicate - the event selection predicate
        Returns:
        true if at least one event has been cancelled; false otherwise
      • isRunning

        public boolean isRunning()
        Description copied from interface: Simulation
        Check if the simulation is still running. Even if the simulation is paused, the method returns true to indicate that the simulation is in fact active yet.

        This method should be used by entities to check if they should continue executing.

        Specified by:
        isRunning in interface Simulation
        Returns:
      • pause

        public boolean pause()
        Description copied from interface: Simulation
        Requests the simulation to be paused as soon as possible.
        Specified by:
        pause in interface Simulation
        Returns:
        true if the simulation was paused, false if it was already paused or has finished
      • pause

        public boolean pause​(double time)
        Description copied from interface: Simulation
        Requests the simulation to be paused at a given time. The method schedules the pause request and then returns immediately.
        Specified by:
        pause in interface Simulation
        Parameters:
        time - the time at which the simulation has to be paused
        Returns:
        true if pause request was successfully received (the given time is greater than or equal to the current simulation time), false otherwise.
      • resume

        public boolean resume()
        Description copied from interface: Simulation
        This method is called if one wants to resume the simulation that has previously been paused.
        Specified by:
        resume in interface Simulation
        Returns:
        true if the simulation has been restarted or false if it wasn't paused.
      • pauseEntity

        public void pauseEntity​(SimEntity src,
                                double delay)
        Description copied from interface: Simulation
        Pauses an entity for some time.
        Specified by:
        pauseEntity in interface Simulation
        Parameters:
        src - id of entity to be paused
        delay - the time period for which the entity will be inactive
      • holdEntity

        protected void holdEntity​(SimEntity src,
                                  long delay)
        Holds an entity for some time.
        Parameters:
        src - id of entity to be held
        delay - How many seconds after the current time the entity has to be held
      • getNumberOfFutureEvents

        public long getNumberOfFutureEvents​(java.util.function.Predicate<SimEvent> predicate)
        Description copied from interface: Simulation
        Gets the number of events in the future queue which match a given predicate.
        Specified by:
        getNumberOfFutureEvents in interface Simulation
        Parameters:
        predicate - the predicate to filter the list of future events.
        Returns:
        the number of future events which match the predicate
      • abort

        public void abort()
        Description copied from interface: Simulation
        Aborts the simulation without finishing the processing of entities in the entities list, what may give unexpected results.

        Use this method just if you want to abandon the simulation an usually ignore the results.

        Specified by:
        abort in interface Simulation
      • isPaused

        public boolean isPaused()
        Description copied from interface: Simulation
        Checks if the simulation is paused.
        Specified by:
        isPaused in interface Simulation
        Returns:
      • addOnSimulationPauseListener

        public final Simulation addOnSimulationPauseListener​(EventListener<EventInfo> listener)
        Description copied from interface: Simulation
        Adds an EventListener object that will be notified when the simulation is paused. When this Listener is notified, it will receive an EventInfo informing the time the pause occurred.

        This object is just information about the event that happened. In fact, it isn't generated an actual SimEvent for a pause event because there is not need for that.

        Specified by:
        addOnSimulationPauseListener in interface Simulation
        Parameters:
        listener - the event listener to add
        Returns:
      • removeOnSimulationPauseListener

        public boolean removeOnSimulationPauseListener​(EventListener<EventInfo> listener)
        Description copied from interface: Simulation
        Removes a listener from the onSimulationPausedListener List.
        Specified by:
        removeOnSimulationPauseListener in interface Simulation
        Parameters:
        listener - the listener to remove
        Returns:
        true if the listener was found and removed, false otherwise
      • removeOnEventProcessingListener

        public boolean removeOnEventProcessingListener​(EventListener<SimEvent> listener)
        Description copied from interface: Simulation
        Removes a listener from the onEventProcessingListener List.
        Specified by:
        removeOnEventProcessingListener in interface Simulation
        Parameters:
        listener - the listener to remove
        Returns:
        true if the listener was found and removed, false otherwise
      • addOnClockTickListener

        public Simulation addOnClockTickListener​(EventListener<EventInfo> listener)
        Description copied from interface: Simulation
        Adds a EventListener object that will be notified every time when the simulation clock advances. Notifications are sent in a second interval to avoid notification flood. Thus, if the clock changes, for instance, from 1.0, to 1.1, 2.0, 2.1, 2.2, 2.5 and then 3.2, notifications will just be sent for the times 1, 2 and 3 that represent the integer part of the simulation time.
        Specified by:
        addOnClockTickListener in interface Simulation
        Parameters:
        listener - the event listener to add
        Returns:
      • removeOnClockTickListener

        public boolean removeOnClockTickListener​(EventListener<? extends EventInfo> listener)
        Description copied from interface: Simulation
        Removes a listener from the onClockTickListener List.
        Specified by:
        removeOnClockTickListener in interface Simulation
        Parameters:
        listener - the listener to remove
        Returns:
        true if the listener was found and removed, false otherwise
      • setNetworkTopology

        public void setNetworkTopology​(NetworkTopology networkTopology)
        Description copied from interface: Simulation
        Sets the network topology used for Network simulations.
        Specified by:
        setNetworkTopology in interface Simulation
        Parameters:
        networkTopology - the network topology to set