Commit 7ccbd93b authored by Eduardo Falcão's avatar Eduardo Falcão
Browse files

Merge branch 'energy_monitor' into 'development'

Energy monitor

See merge request lenovo-autonomic/cloudsimplus!14
parents db752962 2f9bb721
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
package org.cloudbus.cloudsim.examples.autonomic.simulations;
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.cloudlets.Cloudlet.Status;
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.power.models.PowerModel;
import org.cloudbus.cloudsim.power.models.PowerModelSpecPowerLowPowerMode;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.ResourceProvisioner;
import org.cloudbus.cloudsim.provisioners.ResourceProvisionerShared;
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.VmScheduler;
import org.cloudbus.cloudsim.schedulers.vm.VmSchedulerSpaceShared;
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.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.CloudletVmEventInfo;
import org.cloudsimplus.listeners.EventInfo;
public class InfoAwareSimulation {
private static final int SCHEDULING_INTERVAL = 1;
/*
* Hosts (2) configuration
*/
private static final int HOSTS_PES = 12; // 12 VCPU's
private static final long HOSTS_RAM = 16384; // 16 GB of RAM
private static final long HOSTS_STORAGE = 572108; // 558 GB of Disk
private static final long HOSTS_BW = Integer.MAX_VALUE; // High value of bandwidth
private static final double HOSTS_MIPS = 100000; // 100k of MIPS per PE
private static final long HOSTS_IOPS = 500;
private static final int NUMBER_VMS = 2; // Initial Setup
/*
* Vm toy (Setted to finish at the time to launched the IO-Bound (grep) vms)
*/
private static final int VM_TOY_ID = 0;
private static final double VM_TOY_DELAY = 0;
private static final int VM_TOY_PES = 1; // 1 VCPU
private static final long VM_TOY_RAM = 0; // 1 GB of RAM
private static final long VM_TOY_STORAGE = 0; // 10 GB of Disk
private static final long VM_TOY_BW = 100; // High value of bandwidth
private static final long VM_TOY_MIPS = 1;
private static final long VM_TOY_IOPS = 0;
/*
* Cloudlet of the Vm Toy.
*/
private static final int CLOUDLET_TOY_ID = 0;
private static final double CLOUDLET_TOY_DELAY = 0;
private static final int CLOUDLET_TOY_LENGTH = 1; // Toy workload.
private static final int CLOUDLET_TOY_PES = 1;
private static final int CLOUDLET_TOY_IOPS = 0;
/*
* Vm CPU-Bound configuration
*/
private static final int VM_CPU_BOUND_ID = 1;
private static final double VM_CPU_BOUND_DELAY = 0.0;
private static final int VM_CPU_BOUND_PES = 1; // 1 VCPU
private static final long VM_CPU_BOUND_RAM = 1024; // 1 GB of RAM
private static final long VM_CPU_BOUND_STORAGE = 10240; // 10 GB of Disk
private static final long VM_CPU_BOUND_BW = 10000; // High value of bandwidth
private static final long VM_CPU_BOUND_MIPS = 100000;
private static final long VM_CPU_BOUND_IOPS = 500;
private static final int NUMBER_CLOUDLETS = 2; // Initial Setup
/*
* Cloudlet of the vm CPU-Bound
*/
private static final int CLOUDLET_CPU_BOUND_ID = 1;
private static final double CLOUDLET_CPU_BOUND_DELAY = 0.0;
private static final int CLOUDLET_CPU_BOUND_LENGTH = 1000000000; // CPU-Bound workload (Set a high duration time)
private static final int CLOUDLET_CPU_BOUND_PES = 1;
private static final int CLOUDLET_CPU_BOUND_IOPS = 3600; // 2*30*60
/*
* Vm IO-Bound configuration
*/
private static final int VM_IO_BOUND_ID = 2;
private static final double VM_IO_BOUND_DELAY = 0.0;
private static final int VM_IO_BOUND_PES = 1; // 1 VCPU
private static final int VM_IO_BOUND_RAM = 400; // 400 MB of RAM
private static final int VM_IO_BOUND_STORAGE = 10240; // 10 GB of Disk
private static final int VM_IO_BOUND_BW = 10000; // High value of bandwidth
private static final int VM_IO_BOUND_MIPS = 100000;
private static final int VM_IO_BOUND_IOPS = 500; // IO-Bound workload
/*
* Cloudlet of the vm IO-Bound
*/
private static final int CLOUDLET_IO_BOUND_ID = 2;
private static final double CLOUDLET_IO_BOUND_DELAY = 0;
private static final int CLOUDLET_IO_BOUND_LENGTH = 11250;
private static final int CLOUDLET_IO_BOUND_PES = 1;
private static final int CLOUDLET_IO_BOUND_IOPS = 5000000; // IO-Bound workload (Set a high duration time)
private static final int NUMBER_VMS_LAUNCHED = 4;
/*
* Vm about to be launched (Grep)
*/
private static final int VM_LAUNCHED_PES = 2; // 2 VCPU's
private static final double VM_LAUNCHED_DELAY = 0;
private static final int VM_LAUNCHED_RAM = 3072; // 3 GB of RAM
private static final int VM_LAUNCHED_DISK = 40960; // 40 GB of Disk
private static final int VM_LAUNCHED_BW = 10000; // High value of bandwidth
private static final int VM_LAUNCHED_MIPS = 100000; // Consuming a little MIPS: 11250
private static final int VM_LAUNCHED_IOPS = 500; // IO-Bound workload
private static final int NUMBER_CLOUDLET_LAUNCHED = 4;
/*
* Cloudlet of the grep vms
*/
private static final double CLOUDLET_LAUNCHED_DELAY = 0;
private static final int CLOUDLET_LAUNCHED_LENGTH = 11250;
private static final int CLOUDLET_LAUNCHED_PES = 2; // 4 ou 2? duvida lucas
private static final int CLOUDLET_LAUNCHED_IOPS = 900000; // IO-Bound workload
private final CloudSim simulation;
private Datacenter datacenter;
private DatacenterBroker broker;
private List<Vm> vmList;
private List<Cloudlet> cloudletList;
private List<Host> hostList;
public static void main(String[] args) {
new InfoAwareSimulation();
}
public InfoAwareSimulation() {
Log.printFormattedLine("Starting %s ...", getClass().getSimpleName());
simulation = new CloudSim();
datacenter = createDatacenter(2);
//Creates a broker that is a software acting on behalf a cloud customer to manage his/her VMs and Cloudlets
broker = new DatacenterBrokerSimple(simulation);
// Setting Initial Setup
vmList = new ArrayList<>(NUMBER_VMS);
vmList.add(createVmCPU(VM_CPU_BOUND_ID, VM_CPU_BOUND_DELAY, VM_CPU_BOUND_PES, VM_CPU_BOUND_MIPS, VM_CPU_BOUND_RAM,
VM_CPU_BOUND_BW, VM_CPU_BOUND_STORAGE, VM_CPU_BOUND_IOPS));
vmList.add(createVmIO(VM_IO_BOUND_ID, VM_IO_BOUND_DELAY, VM_IO_BOUND_PES, VM_IO_BOUND_MIPS, VM_IO_BOUND_RAM,
VM_IO_BOUND_BW, VM_IO_BOUND_STORAGE, VM_IO_BOUND_IOPS));
vmList.add(createVmSimple(VM_TOY_ID, VM_TOY_DELAY, VM_TOY_PES, VM_TOY_MIPS, VM_TOY_RAM,
VM_TOY_BW, VM_TOY_STORAGE, VM_TOY_IOPS));
broker.submitVmList(vmList);
cloudletList = new ArrayList<>(NUMBER_CLOUDLETS);
cloudletList.add(createCloudlets(CLOUDLET_CPU_BOUND_ID, CLOUDLET_CPU_BOUND_DELAY, CLOUDLET_CPU_BOUND_LENGTH,
CLOUDLET_CPU_BOUND_PES, CLOUDLET_CPU_BOUND_IOPS));
cloudletList.add(createCloudlets(CLOUDLET_IO_BOUND_ID, CLOUDLET_IO_BOUND_DELAY, CLOUDLET_IO_BOUND_LENGTH,
CLOUDLET_IO_BOUND_PES, CLOUDLET_IO_BOUND_IOPS));
cloudletList.add(createCloudlets(CLOUDLET_TOY_ID, CLOUDLET_TOY_DELAY, CLOUDLET_TOY_LENGTH,
CLOUDLET_TOY_PES, CLOUDLET_TOY_IOPS));
broker.submitCloudletList(cloudletList);
simulation.addOnClockTickListener(this::clockTickListener);
simulation.start();
final List<Cloudlet> finishedCloudlets = broker.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();
printHostCpuUtilizationAndPowerConsumption();
}
/**
* <p>
* The Host CPU Utilization History is only computed if VMs utilization history
* is enabled by calling {@code vm.getUtilizationHistory().enable()}
* </p>
* *
*/
private void printHostCpuUtilizationAndPowerConsumption() {
System.out.println();
for (Host host : hostList) {
System.out.printf("Host %d CPU utilization and power consumption\n", host.getId());
System.out.println(
"-------------------------------------------------------------------------------------------");
/*
* Since the utilization history are stored in the reverse chronological order,
* the values are presented in this way.
*/
final double[] utilizationPercentHistory = host.getUtilizationHistory();
double totalPower = 0;
double time = simulation.clock();
for (int i = 0; i < utilizationPercentHistory.length; i++) {
final double utilizationPercent = utilizationPercentHistory[i];
/**
* The power consumption is returned in Watt-second, but it's measured the
* continuous consumption before a given time, according to the time interval
* defined by {@link #SCHEDULING_INTERVAL} set to the Datacenter.
*/
final double wattsPerInterval = host.getPowerModel().getPower(utilizationPercent) * SCHEDULING_INTERVAL;
totalPower += wattsPerInterval;
System.out.printf(
"\tTime %6.0f | CPU Utilization %6.2f%% | Power Consumption: %8.2f Watt-Second in %d Seconds\n",
time, utilizationPercent * 100, wattsPerInterval, SCHEDULING_INTERVAL);
time -= SCHEDULING_INTERVAL;
}
System.out.printf(
"Total Host %d Power Consumption in %.0f seconds: %.2f Watt-Second (mean of %.2f Watt-Second) \n",
host.getId(), simulation.clock(), totalPower, totalPower / simulation.clock());
System.out.println(
"-------------------------------------------------------------------------------------------\n");
}
}
/**
* 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() + ": # Changing Vm Allocation Policy to: Info-Aware");
datacenter = ((DatacenterSimple) datacenter).setVmAllocationPolicy(new VmAllocationPolicyInfoAware());
List<Vm> listVmsLaunched = new ArrayList<>(NUMBER_VMS_LAUNCHED);
for (int i = 3; i < NUMBER_VMS_LAUNCHED + 3; i++) {
Vm vm = createVmCPU(i, VM_LAUNCHED_DELAY, VM_LAUNCHED_PES, VM_LAUNCHED_MIPS,
VM_LAUNCHED_RAM, VM_LAUNCHED_BW, VM_LAUNCHED_DISK, VM_LAUNCHED_IOPS);
vm.setSubmissionDelay(VM_LAUNCHED_DELAY);
listVmsLaunched.add(vm);
}
List<Cloudlet> listCloudletLaunched = new ArrayList<>(NUMBER_CLOUDLET_LAUNCHED);
for (int i = 3; i < NUMBER_CLOUDLET_LAUNCHED + 3; i++) {
Cloudlet cloudlet = createCloudlets(i, CLOUDLET_LAUNCHED_DELAY, CLOUDLET_LAUNCHED_LENGTH,
CLOUDLET_LAUNCHED_PES, CLOUDLET_LAUNCHED_IOPS);
listCloudletLaunched.add(cloudlet);
}
Log.printLine(info.getTime() + ": # Submiting Vms and Cloudlets Launched (Grep)\n");
broker.submitVmList(listVmsLaunched);
broker.submitCloudletList(listCloudletLaunched);
simulation.removeOnClockTickListener(info.getListener());
}
}
private Datacenter createDatacenter(int numberOfHosts) {
hostList = new ArrayList<>(numberOfHosts);
for(int i = 0; i < numberOfHosts; i++) {
Host host = createHost(HOSTS_PES, HOSTS_MIPS, HOSTS_RAM, HOSTS_IOPS, HOSTS_BW, HOSTS_STORAGE);
hostList.add(host);
}
final Datacenter dc = new DatacenterSimple(simulation, hostList, new VmAllocationPolicySimple());
dc.setSchedulingInterval(SCHEDULING_INTERVAL);
return dc;
}
private Host createHost(int hostPes, double mipsPerPe, long ram, long iops, long bw, long storage) {
List<Pe> peList = new ArrayList<>(hostPes);
//List of Host's CPUs (Processing Elements, PEs)
for (int i = 0; i < hostPes; i++) {
peList.add(new PeSimple(mipsPerPe, new PeProvisionerSimple()));
}
// final PowerModel powerModel = new PowerModelSpecPowerDvfsMode();
// final PowerModel powerModel = new PowerModelSpecPowerHighPowerMode();
final PowerModel powerModel = new PowerModelSpecPowerLowPowerMode();
ResourceProvisioner ramProvisioner = new ResourceProvisionerSimple();
ResourceProvisioner bwProvisioner = new ResourceProvisionerSimple();
ResourceProvisioner iopsProvisioner = new ResourceProvisionerShared();
VmScheduler vmScheduler = new VmSchedulerSpaceShared();
Host host = new HostSimple(ram, iops, bw, storage, peList);
host.setPowerModel(powerModel);
host
.setRamProvisioner(ramProvisioner)
.setBwProvisioner(bwProvisioner)
.setIopsProvisioner(iopsProvisioner)
.setVmScheduler(vmScheduler);
return host;
}
private Vm createVmIO(int id, double delayTime, int pes, long mipsCapacity, long ram, long bw, long storage, long iops) {
Vm vm =
new VmWithMetadata(id, mipsCapacity, pes, new HashMap<VmLabel,VmData>())
.setRam(ram).setBw(bw).setSize(storage).setIops(iops)