Commit a4078f12 authored by Lucas Cavalcante's avatar Lucas Cavalcante
Browse files

Merge branch 'sla-simulation' of...

Merge branch 'sla-simulation' of git.lsd.ufcg.edu.br:lenovo-autonomic/cloudsimplus into sla-simulation
parents 298bad17 be785f58
package org.cloudbus.cloudsim.examples.autonomic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicyInfoAware;
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.datacenters.Datacenter;
import org.cloudbus.cloudsim.datacenters.DatacenterSimple;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.HostSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.ResourceProvisioner;
import org.cloudbus.cloudsim.provisioners.ResourceProvisionerSimple;
import org.cloudbus.cloudsim.resources.Pe;
import org.cloudbus.cloudsim.resources.PeSimple;
import org.cloudbus.cloudsim.schedulers.cloudlet.CloudletSchedulerSpaceShared;
import org.cloudbus.cloudsim.schedulers.cloudlet.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.schedulers.vm.VmScheduler;
import org.cloudbus.cloudsim.schedulers.vm.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.util.Log;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModel;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModelFull;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.vms.VmWithMetadata;
import org.cloudbus.cloudsim.vms.VmWithMetadata.VmData;
import org.cloudbus.cloudsim.vms.VmWithMetadata.VmLabel;
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
import org.cloudsimplus.listeners.EventInfo;
/**
* A CloudSim Plus example which shows the Info-Aware placement
*
* Initially, we have a datacenter managing two hosts.
* After that two VMs, one that will be running a memory bound
* workload and the other one a network bound workload, will be
* allocated (using the VMAllocationPolicySimple), one in each of
* the hosts, and two cloudlets will be launched in each VM, one
* cloudlet with the double of the duration time in comparison with
* the another one. Finally, a third VM (with Memory Bound metadata)
* will be launched in the datacenter using the VmAllocationPolicyInfoAware,
* and it is expected that this VM will be allocated into the host
* with more memory available.
*
* @author Rafael Falcao
*/
public class InfoAwareAllocationExample {
private static final int SCHEDULING_INTERVAL = 0;
private static final int HOSTS = 2;
private static final int HOST_PES = 4;
private static final int VMS = 2;
private static final int VM1_PES = 1;
private static final int VM2_PES = 2;
private static final int VM3_PES = 2;
private static final int CLOUDLETS = 2;
private static final int CLOUDLET_PES = 1;
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;
public static void main(String[] args) {
new InfoAwareAllocationExample();
}
public InfoAwareAllocationExample() {
simulation = new CloudSim();
datacenter0 = createDatacenter();
//Creates a broker that is a software acting on behalf a cloud customer to manage his/her VMs and Cloudlets
broker0 = new DatacenterBrokerSimple(simulation);
vmList = createVms();
cloudletList = createCloudlets();
broker0.submitVmList(vmList);
broker0.submitCloudletList(cloudletList);
simulation.addOnClockTickListener(this::clockTickListener);
simulation.start();
//datacenter0 = ((DatacenterSimple) datacenter0).setVmAllocationPolicy(new VmAllocationPolicyInfoAware());
final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();
}
/**
* Event listener which is called every time the simulation clock advances.
* @param info information about the event happened.
*/
private void clockTickListener(final EventInfo info) {
final int time = (int)info.getTime();
/* Checks if the time specified in the scheduling interval has passed. */
if(time == SCHEDULING_INTERVAL) {
Log.printLine("\n" + info.getTime() + ": # Vm Allocation Policy changed to Info-Aware");
datacenter0 = ((DatacenterSimple) datacenter0).setVmAllocationPolicy(new VmAllocationPolicyInfoAware());
Vm vm2 =
new VmWithMetadata(2, 1000, VM3_PES, new HashMap<VmLabel,VmData>())
.setRam(512).setBw(5000).setSize(500000)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
((VmWithMetadata) vm2).putMetadata(VmLabel.TYPE, VmData.RAM);
Log.printLine(info.getTime() + ": # Created " + vm2 + "\n");
broker0.submitVm(vm2);
simulation.removeOnClockTickListener(info.getListener());
}
}
/**
* 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());
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
ResourceProvisioner ramProvisioner = new ResourceProvisionerSimple();
ResourceProvisioner bwProvisioner = new ResourceProvisionerSimple();
VmScheduler vmScheduler = new VmSchedulerTimeShared();
Host host = new HostSimple(ram, bw, storage, peList);
host
.setRamProvisioner(ramProvisioner)
.setBwProvisioner(bwProvisioner)
.setVmScheduler(vmScheduler);
return host;
}
/**
* Creates a list of VMs.
*/
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS);
Vm vm0 =
new VmWithMetadata(0, 1000, VM1_PES, new HashMap<VmLabel,VmData>())
.setRam(1526).setBw(2500).setSize(250000)
.setCloudletScheduler(new CloudletSchedulerSpaceShared());
((VmWithMetadata) vm0).putMetadata(VmLabel.TYPE, VmData.RAM);
list.add(vm0);
Vm vm1 =
new VmWithMetadata(1, 1000, VM2_PES, new HashMap<VmLabel,VmData>())
.setRam(1024).setBw(5000).setSize(500000)
.setCloudletScheduler(new CloudletSchedulerSpaceShared());
((VmWithMetadata) vm1).putMetadata(VmLabel.TYPE, VmData.BW);
list.add(vm1);
return list;
}
/**
* Creates a list of Cloudlets.
*/
private List<Cloudlet> createCloudlets() {
final List<Cloudlet> list = new ArrayList<>(CLOUDLETS);
UtilizationModel utilization = new UtilizationModelFull();
for (int i = 0; i < CLOUDLETS; i++) {
Cloudlet cloudlet =
new CloudletSimple(i, CLOUDLET_LENGTH, CLOUDLET_PES)
.setFileSize(1024)
.setOutputSize(1024)
.setUtilizationModel(utilization);
list.add(cloudlet);
}
for (int i = 0; i < CLOUDLETS; i++) {
Cloudlet cloudlet =
new CloudletSimple(i+2, 20000, CLOUDLET_PES)
.setFileSize(1024)
.setOutputSize(1024)
.setUtilizationModel(utilization);
list.add(cloudlet);
}
return list;
}
}
\ No newline at end of file
/*
* 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.cloudbus.cloudsim.examples.autonomics;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicyAbstract;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicyInfoAware;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.brokers.DatacenterBroker;
......@@ -32,201 +12,205 @@ import org.cloudbus.cloudsim.cloudlets.Cloudlet;
import org.cloudbus.cloudsim.cloudlets.CloudletSimple;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.datacenters.Datacenter;
import org.cloudbus.cloudsim.datacenters.DatacenterCharacteristics;
import org.cloudbus.cloudsim.datacenters.DatacenterCharacteristicsSimple;
import org.cloudbus.cloudsim.datacenters.DatacenterSimple;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.HostSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.ResourceProvisioner;
import org.cloudbus.cloudsim.provisioners.ResourceProvisionerSimple;
import org.cloudbus.cloudsim.resources.Pe;
import org.cloudbus.cloudsim.resources.PeSimple;
import org.cloudbus.cloudsim.schedulers.cloudlet.CloudletSchedulerSpaceShared;
import org.cloudbus.cloudsim.schedulers.cloudlet.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.schedulers.vm.VmScheduler;
import org.cloudbus.cloudsim.schedulers.vm.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.util.Log;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModel;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModelFull;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.cloudsimplus.listeners.CloudletVmEventInfo;
import org.cloudsimplus.listeners.EventListener;
import org.cloudbus.cloudsim.vms.VmWithMetadata;
import org.cloudbus.cloudsim.vms.VmWithMetadata.VmData;
import org.cloudbus.cloudsim.vms.VmWithMetadata.VmLabel;
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
import java.util.ArrayList;
import java.util.List;
import org.cloudsimplus.listeners.EventInfo;
/**
* An example showing how to dynamically create VMs and Cloudlets during simulation
* executing using the exclusive CloudSim Plus {@link DatacenterBroker} implementations
* and Listener features. Using such features, <b>it is not required to create DatacenterBrokers in runtime
* in order to allow dynamic submission of VMs and Cloudlets.</b>
*
* <p>This example uses CloudSim Plus Listener features to intercept when
* the first Cloudlet finishes its execution to then request
* the creation of new VMs and Cloudlets. It uses the Java 8 Lambda Functions features
* to pass a listener to the mentioned Cloudlet, by means of the
* {@link Cloudlet#addOnFinishListener(EventListener)} method.</p>
* A CloudSim Plus example which shows the Info-Aware placement
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 1.0
* Initially, we have a datacenter managing two hosts.
* After that two VMs, one that will be running a memory bound
* workload and the other one a network bound workload, will be
* allocated (using the VMAllocationPolicySimple), one in each of
* the hosts, and two cloudlets will be launched in each VM, one
* cloudlet with the double of the duration time in comparison with
* the another one. Finally, a third VM (with Memory Bound metadata)
* will be launched in the datacenter using the VmAllocationPolicyInfoAware,
* and it is expected that this VM will be allocated into the host
* with more memory available.
*
* @see Cloudlet#addOnFinishListener(EventListener)
* @see EventListener
* @author Rafael Falcao
*/
public class InfoAwareAllocationExample {
private final CloudSim simulation;
private final DatacenterBrokerSimple broker0;
private List<Cloudlet> cloudletList;
private List<Vm> vmList;
private int numberOfCreatedCloudlets = 0;
private int numberOfCreatedVms = 0;
/**
* Starts the simulation.
* @param args
*/
private static final int SCHEDULING_INTERVAL = 0;
private static final int HOSTS = 2;
private static final int HOST_PES = 4;
private static final int VMS = 2;
private static final int VM1_PES = 1;
private static final int VM2_PES = 2;
private static final int VM3_PES = 2;
private static final int CLOUDLETS = 2;
private static final int CLOUDLET_PES = 1;
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;
public static void main(String[] args) {
new InfoAwareAllocationExample();
}
/**
* Default constructor that builds the simulation.
*/
public InfoAwareAllocationExample() {
Log.printFormattedLine("Starting %s ...", getClass().getSimpleName());
this.simulation = new CloudSim();
List<Host> hosts = new ArrayList<Host>();
int id = 1;
long mips = 1000; // capacity of each CPU core (in Million Instructions per Second)
long ram = 2048; // host memory (MEGABYTE)
long iops = 1000; // writes and reads per second
long storage = 1000000; // host storage (MEGABYTE)
long bw = 10000; //in Megabits/s
long numberOfPes = 4;
hosts.add(createHost(id, mips, ram, iops, bw, storage, numberOfPes));
id = 2;
hosts.add(createHost(id, mips, ram, iops, bw, storage, numberOfPes));
VmAllocationPolicyAbstract allocationPolicy = new VmAllocationPolicyInfoAware();
Datacenter datacenter0 = createDatacenter(hosts,allocationPolicy);
/*Creates a Broker accountable for submission of VMs and Cloudlets
on behalf of a given cloud user (customer).*/
simulation = new CloudSim();
datacenter0 = createDatacenter();
//Creates a broker that is a software acting on behalf a cloud customer to manage his/her VMs and Cloudlets
broker0 = new DatacenterBrokerSimple(simulation);
vmList = createVms();
cloudletList = createCloudlets();
broker0.submitVmList(vmList);
broker0.submitCloudletList(cloudletList);
final int vmsToCreate = 1;
final int cloudletsToCreateByVm = 2;
this.vmList = new ArrayList<>(vmsToCreate);
this.cloudletList = new ArrayList<>(vmsToCreate*cloudletsToCreateByVm);
createAndSubmitVmsAndCloudlets(vmsToCreate, cloudletsToCreateByVm);
/* Assigns an EventListener to be notified when the first Cloudlets finishes executing
* and then dynamically create a new list of VMs and Cloudlets to submit to the broker.*/
Cloudlet cloudlet0 = this.cloudletList.get(0);
cloudlet0.addOnFinishListener(this::submitNewVmsAndCloudletsToBroker);
/* Starts the simulation and waits all cloudlets to be executed. */
simulation.addOnClockTickListener(this::clockTickListener);
simulation.start();
/*Prints results when the simulation is over
(you can use your own code here to print what you want from this cloudlet list)*/
List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
//datacenter0 = ((DatacenterSimple) datacenter0).setVmAllocationPolicy(new VmAllocationPolicyInfoAware());
final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();
Log.printConcatLine(getClass().getSimpleName(), " finished!");
}
private Host createHost(int id, long mips, long ram, long iops, long bw, long storage, long numberOfPes){
List<Pe> peList = new ArrayList<Pe>(); //List of CPU cores
for (int i = 0; i < numberOfPes; i++) {
peList.add(new PeSimple(mips, new PeProvisionerSimple()));
/**
* Event listener which is called every time the simulation clock advances.
* @param info information about the event happened.
*/
private void clockTickListener(final EventInfo info) {
final int time = (int)info.getTime();
/* Checks if the time specified in the scheduling interval has passed. */
if(time == SCHEDULING_INTERVAL) {
Log.printLine("\n" + info.getTime() + ": # Vm Allocation Policy changed to Info-Aware");
datacenter0 = ((DatacenterSimple) datacenter0).setVmAllocationPolicy(new VmAllocationPolicyInfoAware());
Vm vm2 =
new VmWithMetadata(2, 1000, VM3_PES, new HashMap<VmLabel,VmData>())
.setRam(512).setBw(5000).setSize(500000)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
((VmWithMetadata) vm2).putMetadata(VmLabel.TYPE, VmData.RAM);
Log.printLine(info.getTime() + ": # Created " + vm2 + "\n");
broker0.submitVm(vm2);
simulation.removeOnClockTickListener(info.getListener());
}
Host h = new HostSimple(ram,iops,bw,storage,peList);
h.setId(id);
h.setRamProvisioner(new ResourceProvisionerSimple());
h.setBwProvisioner(new ResourceProvisionerSimple());
//TODO implement a provisioner for Iops
h.setVmScheduler(new VmSchedulerTimeShared()); //TODO should it be Time shared? implement Iops sharing on Hypervisor
return h;
}
private DatacenterSimple createDatacenter(List<Host> hosts, VmAllocationPolicyAbstract allocationPollicy) {
return new DatacenterSimple(simulation, hosts, allocationPollicy);
}
}
private void createAndSubmitVmsAndCloudlets(int vmsToCreate, int cloudletsToCreateForEachVm) {
List<Vm> newVmList = new ArrayList<>(vmsToCreate);
List<Cloudlet> newCloudletList = new ArrayList<>(vmsToCreate*cloudletsToCreateForEachVm);
for (int i = 0; i < vmsToCreate; i++) {
Vm vm = createVm(broker0);
newVmList.add(vm);
for(int j = 0; j < cloudletsToCreateForEachVm; j++) {
Cloudlet cloudlet = createCloudlet(broker0, vm);
newCloudletList.add(cloudlet);
}
/**
* 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);
}
this.vmList.addAll(newVmList);
this.cloudletList.addAll(newCloudletList);
broker0.submitVmList(newVmList);
broker0.submitCloudletList(newCloudletList);
final Datacenter dc = new DatacenterSimple(simulation, hostList, new VmAllocationPolicySimple());
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
ResourceProvisioner ramProvisioner = new ResourceProvisionerSimple();
ResourceProvisioner bwProvisioner = new ResourceProvisionerSimple();
VmScheduler vmScheduler = new VmSchedulerTimeShared();
Host host = new HostSimple(ram, bw, storage, peList);
host
.setRamProvisioner(ramProvisioner)
.setBwProvisioner(bwProvisioner)
.setVmScheduler(vmScheduler);
return host;
}
/**
* Dynamically creates and submits a set of VMs to the broker when
* the first cloudlet finishes.
* @param eventInfo information about the fired event
* Creates a list of VMs.
*/
private void submitNewVmsAndCloudletsToBroker(CloudletVmEventInfo eventInfo) {
final int numberOfNewVms = 2;
final int numberOfCloudletsByVm = 4;
Log.printFormattedLine("\n\t#Cloudlet %d finished. Submitting %d new VMs to the broker\n",
eventInfo.getCloudlet().getId(), numberOfNewVms);
createAndSubmitVmsAndCloudlets(numberOfNewVms, numberOfCloudletsByVm);
}
private Vm createVm(DatacenterBroker broker) {
long mips = 1000;
long storage = 10000; // vm image size (MEGABYTE)
int ram = 512; // vm memory (MEGABYTE)
long bw = 1000; // vm bandwidth (Megabits/s)
int pesNumber = 1; // number of CPU cores
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS);
Vm vm0 =
new VmWithMetadata(0, 1000, VM1_PES, new HashMap<VmLabel,VmData>())
.setRam(1526).setBw(2500).setSize(250000)
.setCloudletScheduler(new CloudletSchedulerSpaceShared());
((VmWithMetadata) vm0).putMetadata(VmLabel.TYPE, VmData.RAM);
list.add(vm0);
Vm vm1 =
new VmWithMetadata(1, 1000, VM2_PES, new HashMap<VmLabel,VmData>())
.setRam(1024).setBw(5000).setSize(500000)
.setCloudletScheduler(new CloudletSchedulerSpaceShared());