Skip to content

Commit

Permalink
Includes RandomCloudletsArrivalExample and NetworkExample3
Browse files Browse the repository at this point in the history
Signed-off-by: Manoel Campos <manoelcampos@gmail.com>
  • Loading branch information
manoelcampos committed Nov 16, 2018
1 parent b570ac4 commit d534fe5
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ download the project source by cloning the repository issuing the command `git c
at a terminal.

The project has a [bash script](script/bootstrap.sh) you can use to build and run CloudSim Plus examples.
This is a script for Unix-like systems such as Linux, FreeBDS and macOS.
This is a script for Unix-like systems such as Linux, FreeBSD and macOS.

To run some example type the command: `sh script/bootstrap.sh package.ExampleClassName`.
For instance, to run the `CloudSimExample0` you can type: `sh script/bootstrap.sh org.cloudbus.cloudsim.examples.CloudSimExample0`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
* cloudlets of 2 users with network topology on them.
*/
public class NetworkExample3 {
private final Datacenter datacenter0;
private final Datacenter datacenter1;
private final DatacenterBroker broker1;
private final DatacenterBroker broker2;

private List<Cloudlet> cloudletList1;
private List<Cloudlet> cloudletList2;
private List<Vm> vmlist1;
Expand Down Expand Up @@ -67,12 +72,12 @@ public NetworkExample3() {

// Second step: Create Datacenters
//Datacenters are the resource providers in CloudSim. We need at list one of them to run a CloudSim simulation
Datacenter datacenter0 = createDatacenter();
Datacenter datacenter1 = createDatacenter();
datacenter0 = createDatacenter();
datacenter1 = createDatacenter();

//Third step: Create Brokers
DatacenterBroker broker1 = createBroker(1);
DatacenterBroker broker2 = createBroker(2);
broker1 = createBroker(1);
broker2 = createBroker(2);

//Fourth step: Create one virtual machine for each broker/user
vmlist1 = new ArrayList<>();
Expand Down Expand Up @@ -136,6 +141,28 @@ public NetworkExample3() {
broker2.submitCloudletList(cloudletList2);

//Sixth step: configure network
createNetwork();

// Sixth step: Starts the simulation
simulation.start();

// Final step: Print results when simulation is over
List<Cloudlet> newList1 = broker1.getCloudletFinishedList();
List<Cloudlet> newList2 = broker2.getCloudletFinishedList();

new CloudletsTableBuilder(newList1)
.setTitle("Broker " + broker1)
.build();
new CloudletsTableBuilder(newList2)
.setTitle("Broker " + broker2)
.build();
System.out.println(getClass().getSimpleName() + " finished!");
}

/**
* Creates the network topology from a brite file.
*/
private void createNetwork() {
//load the network topology file
NetworkTopology networkTopology = BriteNetworkTopology.getInstance("topology.brite");
simulation.setNetworkTopology(networkTopology);
Expand All @@ -156,21 +183,6 @@ public NetworkExample3() {
//Broker2 will correspond to BRITE node 4
briteNode = 4;
networkTopology.mapNode(broker2.getId(), briteNode);

// Sixth step: Starts the simulation
simulation.start();

// Final step: Print results when simulation is over
List<Cloudlet> newList1 = broker1.getCloudletFinishedList();
List<Cloudlet> newList2 = broker2.getCloudletFinishedList();

new CloudletsTableBuilder(newList1)
.setTitle("Broker " + broker1)
.build();
new CloudletsTableBuilder(newList2)
.setTitle("Broker " + broker2)
.build();
System.out.println(getClass().getSimpleName() + " finished!");
}

private Datacenter createDatacenter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ public class KeepSimulationRunningExample {
private static final int CLOUDLET_LENGTH = 10000;

private final CloudSim simulation;
private DatacenterBroker broker0;
private List<Vm> vmList;
private List<Cloudlet> cloudletList;
private Datacenter datacenter0;
private final DatacenterBroker broker0;
private final List<Vm> vmList;
private final List<Cloudlet> cloudletList;
private final Datacenter datacenter0;

public static void main(String[] args) {
new KeepSimulationRunningExample();
Expand All @@ -141,8 +141,7 @@ private KeepSimulationRunningExample() {

broker0 = new DatacenterBrokerSimple(simulation);

vmList = new ArrayList<>(VMS);
createVms();
vmList = createVms();
cloudletList = createCloudlets();
broker0.setVmDestructionDelayFunction(vm -> 0.0);
broker0.submitVmList(vmList);
Expand Down Expand Up @@ -192,14 +191,16 @@ private Host createHost() {
/**
* Creates a list of VMs.
*/
private void createVms() {
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS);
for (int i = 0; i < VMS; i++) {
vmList.add(createVm(VM_PES));
list.add(createVm(VM_PES));
}
return list;
}

private Vm createVm(final int pes) {
return new VmSimple(vmList.size(),1000, pes)
return new VmSimple(1000, pes)
.setRam(1000).setBw(1000).setSize(10000)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
}
Expand Down Expand Up @@ -230,7 +231,7 @@ private Cloudlet createCloudlet() {
* Simulates the dynamic arrival of a Cloudlet and a VM during simulation runtime.
* @param evt
*/
private void createDynamicCloudletAndVm(EventInfo evt) {
private void createDynamicCloudletAndVm(final EventInfo evt) {
if((int)evt.getTime() == TIME_TO_CREATE_NEW_CLOUDLET){
System.out.printf("\n# Dynamically creating 1 Cloudlet and 1 VM at time %.2f\n", evt.getTime());
Vm vm = createVm(VM_PES*2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
* CloudSim Plus: A modern, highly-extensible and easier-to-use Framework for
* Modeling and Simulation of Cloud Computing Infrastructures and Services.
* http://cloudsimplus.org
*
* Copyright (C) 2015-2018 Universidade da Beira Interior (UBI, Portugal) and
* the Instituto Federal de Educa莽茫o Ci锚ncia e Tecnologia do Tocantins (IFTO, Brazil).
*
* This file is part of CloudSim Plus.
*
* CloudSim Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CloudSim Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CloudSim Plus. If not, see <http://www.gnu.org/licenses/>.
*/
package org.cloudsimplus.examples.dynamic;

import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.brokers.DatacenterBroker;
import org.cloudbus.cloudsim.brokers.DatacenterBrokerSimple;
import org.cloudbus.cloudsim.cloudlets.Cloudlet;
import org.cloudbus.cloudsim.cloudlets.CloudletSimple;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.Simulation;
import org.cloudbus.cloudsim.datacenters.Datacenter;
import org.cloudbus.cloudsim.datacenters.DatacenterSimple;
import org.cloudbus.cloudsim.distributions.ContinuousDistribution;
import org.cloudbus.cloudsim.distributions.UniformDistr;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.HostSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.ResourceProvisionerSimple;
import org.cloudbus.cloudsim.resources.Pe;
import org.cloudbus.cloudsim.resources.PeSimple;
import org.cloudbus.cloudsim.schedulers.cloudlet.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.schedulers.vm.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModel;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModelDynamic;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModelFull;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
import org.cloudsimplus.listeners.EventInfo;
import org.cloudsimplus.listeners.EventListener;

import java.util.ArrayList;
import java.util.List;

/**
* Shows how to keep the simulation running, even
* when there is no event to be processed anymore.
* It calls the {@link Simulation#terminateAt(double)} to define
* the time the simulation must be terminated.
*
* <p>The example is useful when you want to run a simulation
* for a specific amount of time, for instance, to wait random arrival
* or requests (such as Cloudlets and VMs).
* For instance, if you want to run a simulation for 24 hours,
* you just need to call {@code simulation.terminateAt(60*60*24)} (realize the value is in seconds).</p>
*
* <p>It creates Cloudlets randomly, according to a pseudo random number generator (PRNG) following the
* {@link UniformDistr uniform distribution}. You can change the PRNG as you wich,
* for instance, to use a {@link org.cloudbus.cloudsim.distributions.PoissonDistr} arrival process.</p>
*
* <p>The example uses the CloudSim Plus {@link EventListener} feature
* to enable monitoring the simulation and dynamically creating Cloudlets and VMs at runtime.
* It relies on
* <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">Java 8 Method References</a>
* to set a method to be called for {@link Simulation#addOnClockTickListener(EventListener) onClockTick events}.
* It enables getting notifications when the simulation clock advances, then creating and submitting new cloudlets.
* </p>
*
* <p>Since the simulation was set to keep waiting for new events
* until a defined time, the clock will be updated
* even if no event arrives, to simulate time passing.
* Check the {@link Simulation#terminateAt(double)} for details.
* The simulation will just end at the specified time.
* </p>
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 4.0.4
*/
public class RandomCloudletsArrivalExample {
/**
* @see Simulation#terminateAt(double)
*/
private static final double TIME_TO_TERMINATE_SIMULATION = 30;

/**
* @see Datacenter#getSchedulingInterval()
*/
private static final int SCHEDULING_INTERVAL = 1;

private static final int HOSTS = 8;
private static final int HOST_PES = 8;

private static final int VMS = 10;
private static final int VM_PES = 4;

private static final int CLOUDLET_PES = 2;
private static final int CLOUDLET_LENGTH = 10000;
/**
* Number of Cloudlets to be statically created when the simulation starts.
*/
private static final int INITIAL_CLOUDLETS_NUMBER = 5;

private final CloudSim simulation;
private final DatacenterBroker broker0;
private final List<Vm> vmList;
private final List<Cloudlet> cloudletList;
private final Datacenter datacenter0;
private final ContinuousDistribution random;

public static void main(String[] args) {
new RandomCloudletsArrivalExample();
}

private RandomCloudletsArrivalExample() {
/*Enables just some level of log messages.
Make sure to import org.cloudsimplus.util.Log;*/
//Log.setLevel(ch.qos.logback.classic.Level.WARN);

simulation = new CloudSim();
random = new UniformDistr();
simulation.terminateAt(TIME_TO_TERMINATE_SIMULATION);
datacenter0 = createDatacenter();

broker0 = new DatacenterBrokerSimple(simulation);

vmList = createVms();
cloudletList = createCloudlets(INITIAL_CLOUDLETS_NUMBER);
broker0.submitVmList(vmList);
broker0.submitCloudletList(cloudletList);

simulation.addOnClockTickListener(this::createRandomCloudlets);

simulation.start();

final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();

final int randomCloudlets = cloudletList.size()-INITIAL_CLOUDLETS_NUMBER;
System.out.println(
"Number of Arrived Cloudlets: " +
cloudletList.size() + " ("+INITIAL_CLOUDLETS_NUMBER+" statically created and "+
randomCloudlets+" randomly created during simulation runtime)");
}

/**
* Creates a Datacenter and its Hosts.
*/
private Datacenter createDatacenter() {
final List<Host> hostList = new ArrayList<>(HOSTS);
for(int i = 0; i < HOSTS; i++) {
Host host = createHost();
hostList.add(host);
}

final Datacenter dc = new DatacenterSimple(simulation, hostList, new VmAllocationPolicySimple());
dc.setSchedulingInterval(SCHEDULING_INTERVAL);
return dc;
}

private Host createHost() {
List<Pe> peList = new ArrayList<>(HOST_PES);
//List of Host's CPUs (Processing Elements, PEs)
for (int i = 0; i < HOST_PES; i++) {
peList.add(new PeSimple(1000, new PeProvisionerSimple()));
}

final long ram = 2048; //in Megabytes
final long bw = 10000; //in Megabits/s
final long storage = 1000000; //in Megabytes
Host host = new HostSimple(ram, bw, storage, peList);
host
.setRamProvisioner(new ResourceProvisionerSimple())
.setBwProvisioner(new ResourceProvisionerSimple())
.setVmScheduler(new VmSchedulerTimeShared());
return host;
}

/**
* Creates a list of VMs.
*/
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS);
for (int i = 0; i < VMS; i++) {
list.add(createVm(VM_PES));
}
return list;
}

private Vm createVm(final int pes) {
return new VmSimple(1000, pes)
.setRam(1000).setBw(1000).setSize(10000)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
}

/**
* Creates a list of Cloudlets.
* @param count number of Cloudlets to create statically
*/
private List<Cloudlet> createCloudlets(final int count) {
final List<Cloudlet> list = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
list.add(createCloudlet());
}

return list;
}

private Cloudlet createCloudlet() {
UtilizationModel um = new UtilizationModelDynamic(0.2);
return new CloudletSimple(CLOUDLET_LENGTH, CLOUDLET_PES)
.setFileSize(1024)
.setOutputSize(1024)
.setUtilizationModelCpu(new UtilizationModelFull())
.setUtilizationModelRam(um)
.setUtilizationModelBw(um);
}

/**
* Simulates the dynamic arrival of Cloudlets, randomly during simulation runtime.
* At any time the simulation clock updates, a new Cloudlet will be
* created with a probability of 30%.
*
* @param evt
*/
private void createRandomCloudlets(final EventInfo evt) {
if(random.sample() <= 0.3){
System.out.printf("\n# Randomly creating 1 Cloudlet at time %.2f\n", evt.getTime());
Cloudlet cloudlet = createCloudlet();
cloudletList.add(cloudlet);
broker0.submitCloudlet(cloudlet);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
/**
* An experiment that tries to minimize task completion time
* by selecting as the VM to run a Cloudlet,
* that one that minimizes the Cloudlet completion time.
* one that minimizes the Task Completion Time (TCT): the time a cloudlet spends to finish.
*
* <p>
* It uses the {@link DatacenterBroker#setVmMapper(Function)}
Expand Down

0 comments on commit d534fe5

Please sign in to comment.