Commit 195d4e5e authored by Manoel Campos's avatar Manoel Campos
Browse files

Refactored examples into the org.cloudbus.cloudsim.examples.power.planetlab

package to remove lots of duplicated code.
parent 05f7c182
......@@ -25,7 +25,6 @@ import java.io.IOException;
* @since Jan 5, 2012
*/
public class IqrMc {
/**
* The main method.
*
......@@ -33,11 +32,11 @@ public class IqrMc {
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void main(String[] args) throws IOException {
String inputFolder = ResourceLoader.getResourcePath(IqrMc.class,"workload/planetlab");
String outputFolder = "output";
String workload = "20110303"; // PlanetLab workload
String vmAllocationPolicy = "iqr"; // Inter Quartile Range (IQR) VM allocation policy
String vmSelectionPolicy = "mc"; // Maximum Correlation (MC) VM selection policy
final String inputFolder = ResourceLoader.getResourcePath(IqrMc.class,"workload/planetlab");
final String outputFolder = "output";
final String workload = "20110303"; // PlanetLab workload
final String vmAllocationPolicy = "iqr"; // Inter Quartile Range (IQR) VM allocation policy
final String vmSelectionPolicy = "mc"; // Maximum Correlation (MC) VM selection policy
double safetyParameter = 1.5;
new PlanetLabRunner(
......
......@@ -48,8 +48,8 @@ public class NonPowerAware {
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void main(String[] args) throws IOException {
String experimentName = "planetlab_npa";
String outputFolder = "output";
final String experimentName = "planetlab_npa";
final String outputFolder = "output";
String inputFolder = ResourceLoader.getResourcePath(NonPowerAware.class,"workload/planetlab/20110303");
if(Objects.isNull(inputFolder)){
inputFolder = "";
......@@ -60,15 +60,15 @@ public class NonPowerAware {
try {
simulation = new CloudSim();
Helper helper = new Helper(simulation, experimentName, Constants.OUTPUT_CSV, outputFolder);
DatacenterBroker broker = Helper.createBroker(simulation);
DatacenterBroker broker = helper.createBroker(simulation);
List<Cloudlet> cloudletList = PlanetLabHelper.createCloudletListPlanetLab(broker, inputFolder);
List<Vm> vmList = Helper.createVmList(broker, cloudletList.size());
List<PowerHost> hostList = Helper.createHostList(NUMBER_OF_HOSTS);
List<Vm> vmList = helper.createVmList(broker, cloudletList.size());
List<PowerHost> hostList = helper.createHostList(NUMBER_OF_HOSTS);
PowerDatacenterNonPowerAware datacenter = (PowerDatacenterNonPowerAware) Helper.createDatacenter(
simulation,
PowerDatacenterNonPowerAware datacenter = (PowerDatacenterNonPowerAware) helper.createDatacenter(
PowerDatacenterNonPowerAware.class, hostList,
new PowerVmAllocationPolicySimple());
......@@ -83,14 +83,7 @@ public class NonPowerAware {
List<Cloudlet> newList = broker.getCloudletsFinishedList();
Log.printLine("Received " + newList.size() + " cloudlets");
Helper.printResults(
datacenter,
vmList,
lastClock,
experimentName,
Constants.OUTPUT_CSV,
outputFolder);
helper.printResults(datacenter, vmList, lastClock);
} catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
......
......@@ -41,24 +41,24 @@ public class NonPowerAware {
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String experimentName = "random_npa";
String outputFolder = "output";
final String experimentName = "random_npa";
final String outputFolder = "output";
Log.setDisabled(!Constants.ENABLE_OUTPUT);
Log.printLine("Starting " + experimentName);
try {
CloudSim simulation = new CloudSim();
DatacenterBroker broker = Helper.createBroker(simulation);
Helper helper = new Helper(simulation, experimentName, Constants.OUTPUT_CSV, outputFolder);
DatacenterBroker broker = helper.createBroker(simulation);
List<Cloudlet> cloudletList = RandomHelper.createCloudletList(
broker, RandomConstants.NUMBER_OF_VMS);
List<Vm> vmList = Helper.createVmList(broker, cloudletList.size());
List<PowerHost> hostList = Helper.createHostList(RandomConstants.NUMBER_OF_HOSTS);
List<Vm> vmList = helper.createVmList(broker, cloudletList.size());
List<PowerHost> hostList = helper.createHostList(RandomConstants.NUMBER_OF_HOSTS);
PowerDatacenterNonPowerAware datacenter = (PowerDatacenterNonPowerAware) Helper.createDatacenter(
simulation, PowerDatacenterNonPowerAware.class, hostList,
PowerDatacenterNonPowerAware datacenter = (PowerDatacenterNonPowerAware) helper.createDatacenter(
PowerDatacenterNonPowerAware.class, hostList,
new PowerVmAllocationPolicySimple());
datacenter.setMigrationsEnabled(false);
......@@ -72,14 +72,7 @@ public class NonPowerAware {
List<Cloudlet> newList = broker.getCloudletsFinishedList();
Log.printLine("Received " + newList.size() + " cloudlets");
Helper.printResults(
datacenter,
vmList,
lastClock,
experimentName,
Constants.OUTPUT_CSV,
outputFolder);
helper.printResults(datacenter, vmList, lastClock);
} catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
......
......@@ -41,7 +41,8 @@ public final class PlanetLabRunner extends RunnerAbstract {
String workload,
String vmAllocationPolicy,
String vmSelectionPolicy,
double safetyParameterOrUtilizationThreshold) {
double safetyParameterOrUtilizationThreshold)
{
super(
enableOutput,
outputToFile,
......@@ -54,7 +55,7 @@ public final class PlanetLabRunner extends RunnerAbstract {
}
@Override
protected void init(String inputFolder) {
protected void init(final String inputFolder) {
try {
super.init(inputFolder);
broker = Helper.createBroker(getSimulation());
......
......@@ -51,7 +51,7 @@ public final class RandomRunner extends RunnerAbstract {
}
@Override
protected void init(String inputFolder) {
protected void init(final String inputFolder) {
try {
super.init(inputFolder);
......
......@@ -70,7 +70,8 @@ public abstract class RunnerAbstract {
String workload,
String vmAllocationPolicy,
String vmSelectionPolicy,
double safetyParameterOrUtilizationThreshold) {
double safetyParameterOrUtilizationThreshold)
{
try {
initLogOutput(
enableOutput,
......@@ -115,7 +116,8 @@ public abstract class RunnerAbstract {
String workload,
String vmAllocationPolicy,
String vmSelectionPolicy,
double safetyParameterOrUtilizationThreshold) throws IOException {
double safetyParameterOrUtilizationThreshold) throws IOException
{
setEnableOutput(enableOutput);
Log.setDisabled(!isEnableOutput());
if (isEnableOutput() && outputToFile) {
......@@ -142,7 +144,7 @@ public abstract class RunnerAbstract {
*
* @param inputFolder the input folder
*/
protected void init(String inputFolder){
protected void init(final String inputFolder){
this.simulation = new CloudSim();
}
......@@ -153,12 +155,12 @@ public abstract class RunnerAbstract {
* @param outputFolder the output folder
* @param vmAllocationPolicy the vm allocation policy
*/
protected void start(String experimentName, String outputFolder, VmAllocationPolicy vmAllocationPolicy) {
protected void start(final String experimentName, final String outputFolder, VmAllocationPolicy vmAllocationPolicy) {
System.out.println("Starting " + experimentName);
try {
PowerDatacenter datacenter = (PowerDatacenter) Helper.createDatacenter(
simulation,
Helper helper = new Helper(simulation, experimentName, Constants.OUTPUT_CSV, outputFolder);
PowerDatacenter datacenter = (PowerDatacenter) helper.createDatacenter(
PowerDatacenter.class,
hostList,
vmAllocationPolicy);
......@@ -174,14 +176,7 @@ public abstract class RunnerAbstract {
List<Cloudlet> newList = broker.getCloudletsFinishedList();
Log.printLine("Received " + newList.size() + " cloudlets");
Helper.printResults(
datacenter,
vmList,
lastClock,
experimentName,
Constants.OUTPUT_CSV,
outputFolder);
helper.printResults(datacenter, vmList, lastClock);
} catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
......
......@@ -118,7 +118,7 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
final String hostSelectionStr = "optimizeAllocationHostSelection";
ExecutionTimeMeasurer.start(hostSelectionStr);
final List<PowerHostUtilizationHistory> overUtilizedHosts = getOverUtilizedHosts();
getExecutionTimeHistoryHostSelection().add(
executionTimeHistoryHostSelection.add(
ExecutionTimeMeasurer.end(hostSelectionStr));
printOverUtilizedHosts(overUtilizedHosts);
......@@ -128,7 +128,7 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
final String vmSelectionStr = "optimizeAllocationVmSelection";
ExecutionTimeMeasurer.start(vmSelectionStr);
final List<Vm> vmsToMigrate = getVmsToMigrateFromHosts(overUtilizedHosts);
getExecutionTimeHistoryVmSelection().add(ExecutionTimeMeasurer.end(vmSelectionStr));
executionTimeHistoryVmSelection.add(ExecutionTimeMeasurer.end(vmSelectionStr));
Map<Vm, Host> migrationMap = new HashMap<>();
if(!overUtilizedHosts.isEmpty()){
......@@ -137,14 +137,14 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
ExecutionTimeMeasurer.start(vmReallocationStr);
migrationMap =
getNewVmPlacement(vmsToMigrate, new HashSet<>(overUtilizedHosts));
getExecutionTimeHistoryVmReallocation().add(
executionTimeHistoryVmReallocation.add(
ExecutionTimeMeasurer.end(vmReallocationStr));
Log.printLine();
}
migrationMap.putAll(getMigrationMapFromUnderUtilizedHosts(overUtilizedHosts));
restoreAllocation();
getExecutionTimeHistoryTotal().add(ExecutionTimeMeasurer.end(allocationTotalStr));
executionTimeHistoryTotal.add(ExecutionTimeMeasurer.end(allocationTotalStr));
return migrationMap;
}
......@@ -564,15 +564,15 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
* @param metric the metric to be added to the metric history map
*/
protected void addHistoryEntryIfAbsent(PowerHost host, double metric) {
getTimeHistory().putIfAbsent(host, new LinkedList<>());
getUtilizationHistory().putIfAbsent(host, new LinkedList<>());
getMetricHistory().putIfAbsent(host, new LinkedList<>());
timeHistory.putIfAbsent(host, new LinkedList<>());
utilizationHistory.putIfAbsent(host, new LinkedList<>());
metricHistory.putIfAbsent(host, new LinkedList<>());
final Simulation simulation = host.getSimulation();
if (!getTimeHistory().get(host).contains(simulation.clock())) {
getTimeHistory().get(host).add(simulation.clock());
getUtilizationHistory().get(host).add(host.getUtilizationOfCpu());
getMetricHistory().get(host).add(metric);
if (!timeHistory.get(host).contains(simulation.clock())) {
timeHistory.get(host).add(simulation.clock());
utilizationHistory.get(host).add(host.getUtilizationOfCpu());
metricHistory.get(host).add(metric);
}
}
......
......@@ -222,7 +222,7 @@ public abstract class VmSchedulerAbstract implements VmScheduler {
* considering the {@link #getVmMigrationCpuOverhead() CPU migration overhead}.
* @return the max percentage of CPU usage during migration (in scale from [0 to 1], where 1 is 100%)
*/
private double getMaxCpuUsagePercentDuringMigration() {
public double getMaxCpuUsagePercentDuringMigration() {
return 1 - getVmMigrationCpuOverhead();
}
......
......@@ -78,16 +78,10 @@ public class VmSchedulerTimeSharedOverSubscription extends VmSchedulerTimeShared
*/
protected void redistributeMipsDueToOverSubscription() {
// First, we calculate the scaling factor - the MIPS allocation for all VMs will be scaled proportionally
final Map<Vm, List<Double>> mipsMapRequestedReduced = newTotalRequiredMipsByAllVms();
final double totalRequiredMipsByAllVms =
mipsMapRequestedReduced.values()
.stream()
.flatMap(list -> list.stream())
.mapToDouble(mips -> mips)
.sum();
final Map<Vm, List<Double>> mipsMapRequestedReduced = getNewTotalRequestedMipsByAllVms();
//the factor that will be used to reduce the amount of MIPS allocated to each vPE
final double scalingFactor = getHost().getTotalMipsCapacity() / totalRequiredMipsByAllVms;
final double scalingFactor = getHost().getTotalMipsCapacity() / getTotalMipsToAllocateForAllVms(mipsMapRequestedReduced);
getMipsMapAllocated().clear();
for (final Entry<Vm, List<Double>> entry : mipsMapRequestedReduced.entrySet()) {
......@@ -99,14 +93,14 @@ public class VmSchedulerTimeSharedOverSubscription extends VmSchedulerTimeShared
}
/**
* Generate a new Map containing the list of required MIPS by all VMs,
* Generate a new Map containing the list of requested MIPS by all VMs,
* ensuring the MIPS requested for each vPE doesn't exceeds
* the capacity of each Physical PE.
*
* @return the new map of requested MIPS for all VMs
* @see #getMipsMapRequested()
*/
private Map<Vm, List<Double>> newTotalRequiredMipsByAllVms() {
private Map<Vm, List<Double>> getNewTotalRequestedMipsByAllVms() {
final Map<Vm, List<Double>> mipsMapRequestedReduced = new HashMap<>(getMipsMapRequested().entrySet().size());
for (final Entry<Vm, List<Double>> entry : getMipsMapRequested().entrySet()) {
final Vm vm = entry.getKey();
......@@ -116,4 +110,48 @@ public class VmSchedulerTimeSharedOverSubscription extends VmSchedulerTimeShared
return mipsMapRequestedReduced;
}
/**
* Gets the total MIPS that will be allocated to all VMs.
* For VMs that are migrating into the Host,
* just the {@link #getVmMigrationCpuOverhead()}
* will be allocated, representing just the CPU migration overhead
* while the VM is in migration process.
*
* @param mipsMapRequestedReduced the map of MIPS requested by each VM, after being
* adjusted to avoid allocating more MIPS for a vPE
* than there is in the physical PE
* @return the total MIPS to be allocated for all VMs, considering the
* VMs migrating into the Host.
* @see #getMipsShareRequestedReduced(List)
*/
private double getTotalMipsToAllocateForAllVms(final Map<Vm, List<Double>> mipsMapRequestedReduced){
return mipsMapRequestedReduced.entrySet()
.stream()
.mapToDouble(this::getMipsToBeAllocatedForVmPes)
.sum();
}
/**
* Gets the total MIPS to be allocated to a VM (across all vPEs),
* considering if the VM is migrating into the Host.
* In this case, just a percentage of the total required MIPS will
* be in fact allocated to representing the CPU migration overhead.
* @param entry a Map entry containing a VM and the List of MIPS required by its vPEs
* @return the sum of required MIPS by all vPEs, considering the VMs
* in migration process to the Host.
*/
private double getMipsToBeAllocatedForVmPes(final Map.Entry<Vm, List<Double>> entry){
final double requiredMipsByThisVm = entry.getValue().stream().reduce(0.0, Double::sum);
if (getHost().getVmsMigratingIn().contains(entry.getKey())) {
/*
the destination host only experiences a percentage of the migrating VM's MIPS
which is the migration CPU overhead.
*/
return requiredMipsByThisVm * getVmMigrationCpuOverhead();
}
return requiredMipsByThisVm;
}
}
......@@ -291,7 +291,7 @@ public interface Vm extends Machine, UniquelyIdentificable, Comparable<Vm>, Cust
Resource getStorage();
/**
* Gets the history of MIPS capacity allocated to the VM.
* Gets a <b>read-only</b> list with the history of requests and allocation of MIPS for this VM.
*
* @return the state history
*/
......
......@@ -541,31 +541,27 @@ public class VmSimple implements Vm {
this.created = created;
}
/**
* Gets the history of MIPS capacity allocated to the VM.
*
* @todo Instead of using a list, this attribute would be a map, where the
* key can be the history time and the value the history itself. By this
* way, if one wants to get the history for a given time, he/she doesn't
* have to iterate over the entire list to find the desired entry.
*
* @return the state history
*/
@Override
public List<VmStateHistoryEntry> getStateHistory() {
return stateHistory;
/*
* @todo Instead of using a list, this attribute would be a map, where the
* key can be the history time and the value the history itself. This
* way, if one wants to get the history for a given time, he/she doesn't
* have to iterate over the entire list to find the desired entry.
*/
return Collections.unmodifiableList(stateHistory);
}
@Override
public void addStateHistoryEntry(VmStateHistoryEntry entry) {
if (!getStateHistory().isEmpty()) {
final VmStateHistoryEntry previousState = getStateHistory().get(getStateHistory().size() - 1);
if (!stateHistory.isEmpty()) {
final VmStateHistoryEntry previousState = stateHistory.get(stateHistory.size() - 1);
if (previousState.getTime() == entry.getTime()) {
getStateHistory().set(getStateHistory().size() - 1, entry);
stateHistory.set(stateHistory.size() - 1, entry);
return;
}
}
getStateHistory().add(entry);
stateHistory.add(entry);
}
@Override
......
......@@ -8,7 +8,7 @@
package org.cloudbus.cloudsim.vms;
/**
* Stores historic data about a VM.
* Historic data about requests and allocation of MIPS for a given VM over the time.
*
* @author Anton Beloglazov
* @since CloudSim Toolkit 2.1.2
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment