Commit f61914d1 authored by Anton Beloglazov's avatar Anton Beloglazov

- Modified: energy for a time frame is calculated using linear interpolation

parent 42a8080d
...@@ -18,6 +18,7 @@ import org.cloudbus.cloudsim.lists.PeList; ...@@ -18,6 +18,7 @@ import org.cloudbus.cloudsim.lists.PeList;
import org.cloudbus.cloudsim.provisioners.BwProvisioner; import org.cloudbus.cloudsim.provisioners.BwProvisioner;
import org.cloudbus.cloudsim.provisioners.RamProvisioner; import org.cloudbus.cloudsim.provisioners.RamProvisioner;
// TODO: Auto-generated Javadoc
/** /**
* The Class HostDynamicWorkload. * The Class HostDynamicWorkload.
* *
...@@ -28,6 +29,9 @@ public class HostDynamicWorkload extends Host { ...@@ -28,6 +29,9 @@ public class HostDynamicWorkload extends Host {
/** The utilization mips. */ /** The utilization mips. */
private double utilizationMips; private double utilizationMips;
/** The previous utilization mips. */
private double previousUtilizationMips;
/** The under allocated mips. */ /** The under allocated mips. */
private Map<String, List<List<Double>>> underAllocatedMips; private Map<String, List<List<Double>>> underAllocatedMips;
...@@ -51,6 +55,7 @@ public class HostDynamicWorkload extends Host { ...@@ -51,6 +55,7 @@ public class HostDynamicWorkload extends Host {
VmScheduler vmScheduler) { VmScheduler vmScheduler) {
super(id, ramProvisioner, bwProvisioner, storage, peList, vmScheduler); super(id, ramProvisioner, bwProvisioner, storage, peList, vmScheduler);
setUtilizationMips(0); setUtilizationMips(0);
setPreviousUtilizationMips(0);
setUnderAllocatedMips(new HashMap<String, List<List<Double>>>()); setUnderAllocatedMips(new HashMap<String, List<List<Double>>>());
setVmsMigratingIn(new ArrayList<Vm>()); setVmsMigratingIn(new ArrayList<Vm>());
} }
...@@ -64,6 +69,7 @@ public class HostDynamicWorkload extends Host { ...@@ -64,6 +69,7 @@ public class HostDynamicWorkload extends Host {
Log.printLine(); Log.printLine();
} }
double smallerTime = super.updateVmsProcessing(currentTime); double smallerTime = super.updateVmsProcessing(currentTime);
setPreviousUtilizationMips(getUtilizationMips());
setUtilizationMips(0); setUtilizationMips(0);
for (Vm vm : getVmList()) { for (Vm vm : getVmList()) {
...@@ -197,7 +203,7 @@ public class HostDynamicWorkload extends Host { ...@@ -197,7 +203,7 @@ public class HostDynamicWorkload extends Host {
} }
/** /**
* Get current utilization of CPU in percents. * Get current utilization of CPU in percentage.
* *
* @return current utilization of CPU in percents * @return current utilization of CPU in percents
*/ */
...@@ -208,6 +214,19 @@ public class HostDynamicWorkload extends Host { ...@@ -208,6 +214,19 @@ public class HostDynamicWorkload extends Host {
} }
return utilization; return utilization;
} }
/**
* Gets the previous utilization of CPU in percentage.
*
* @return the previous utilization of cpu
*/
public double getPreviousUtilizationOfCpu() {
double utilization = getPreviousUtilizationMips() / getTotalMips();
if (utilization > 1 && utilization < 1.01) {
utilization = 1;
}
return utilization;
}
/** /**
* Get current utilization of CPU in MIPS. * Get current utilization of CPU in MIPS.
...@@ -223,7 +242,7 @@ public class HostDynamicWorkload extends Host { ...@@ -223,7 +242,7 @@ public class HostDynamicWorkload extends Host {
* *
* @return the utilization mips * @return the utilization mips
*/ */
protected double getUtilizationMips() { public double getUtilizationMips() {
return utilizationMips; return utilizationMips;
} }
...@@ -234,6 +253,24 @@ public class HostDynamicWorkload extends Host { ...@@ -234,6 +253,24 @@ public class HostDynamicWorkload extends Host {
*/ */
protected void setUtilizationMips(double utilizationMips) { protected void setUtilizationMips(double utilizationMips) {
this.utilizationMips = utilizationMips; this.utilizationMips = utilizationMips;
}
/**
* Gets the previous utilization mips.
*
* @return the previous utilization mips
*/
public double getPreviousUtilizationMips() {
return previousUtilizationMips;
}
/**
* Sets the previous utilization mips.
*
* @param previousUtilizationMips the new previous utilization mips
*/
protected void setPreviousUtilizationMips(double previousUtilizationMips) {
this.previousUtilizationMips = previousUtilizationMips;
} }
/** /**
......
...@@ -137,29 +137,21 @@ public class PowerDatacenter extends Datacenter { ...@@ -137,29 +137,21 @@ public class PowerDatacenter extends Datacenter {
double currentTime = CloudSim.clock(); double currentTime = CloudSim.clock();
double minTime = Double.MAX_VALUE; double minTime = Double.MAX_VALUE;
double timeDiff = currentTime - getLastProcessTime(); double timeDiff = currentTime - getLastProcessTime();
double timeframePower = 0.0; double timeFrameDatacenterEnergy = 0.0;
if (timeDiff > 0) { if (timeDiff > 0) {
Log.formatLine("\nEnergy consumption for the last time frame from %.2f to %.2f:", getLastProcessTime(), currentTime); Log.formatLine("\nEnergy consumption for the last time frame from %.2f to %.2f:", getLastProcessTime(), currentTime);
for (PowerHost host : this.<PowerHost>getHostList()) { for (PowerHost host : this.<PowerHost>getHostList()) {
Log.printLine(); double timeFrameHostEnergy = host.getEnergyLinearInterpolation(host.getPreviousUtilizationOfCpu(), host.getUtilizationOfCpu(), timeDiff);
double hostPower = 0.0; timeFrameDatacenterEnergy += timeFrameHostEnergy;
if (host.getUtilizationOfCpu() > 0) {
try {
hostPower = host.getPower() * timeDiff;
timeframePower += hostPower;
} catch (Exception e) {
e.printStackTrace();
}
}
Log.printLine();
Log.formatLine("%.2f: [Host #%d] utilization is %.2f%%", currentTime, host.getId(), host.getUtilizationOfCpu() * 100); Log.formatLine("%.2f: [Host #%d] utilization is %.2f%%", currentTime, host.getId(), host.getUtilizationOfCpu() * 100);
Log.formatLine("%.2f: [Host #%d] energy is %.2f W*sec", currentTime, host.getId(), hostPower); Log.formatLine("%.2f: [Host #%d] energy is %.2f W*sec", currentTime, host.getId(), timeFrameHostEnergy);
} }
Log.formatLine("\n%.2f: Consumed energy is %.2f W*sec\n", currentTime, timeframePower); Log.formatLine("\n%.2f: Consumed energy is %.2f W*sec\n", currentTime, timeFrameDatacenterEnergy);
} }
Log.printLine("\n\n--------------------------------------------------------------\n\n"); Log.printLine("\n\n--------------------------------------------------------------\n\n");
...@@ -176,7 +168,7 @@ public class PowerDatacenter extends Datacenter { ...@@ -176,7 +168,7 @@ public class PowerDatacenter extends Datacenter {
Log.formatLine("%.2f: [Host #%d] utilization is %.2f%%", currentTime, host.getId(), host.getUtilizationOfCpu() * 100); Log.formatLine("%.2f: [Host #%d] utilization is %.2f%%", currentTime, host.getId(), host.getUtilizationOfCpu() * 100);
} }
setPower(getPower() + timeframePower); setPower(getPower() + timeFrameDatacenterEnergy);
checkCloudletCompletion(); checkCloudletCompletion();
......
...@@ -56,9 +56,19 @@ public class PowerHost extends HostDynamicWorkload { ...@@ -56,9 +56,19 @@ public class PowerHost extends HostDynamicWorkload {
* @return the power * @return the power
*/ */
public double getPower() { public double getPower() {
return getPower(getUtilizationOfCpu());
}
/**
* Gets the power. For this moment only consumed by all PEs.
*
* @param utilization the utilization
* @return the power
*/
protected double getPower(double utilization) {
double power = 0; double power = 0;
try { try {
power = getPowerModel().getPower(getUtilizationOfCpu()); power = getPowerModel().getPower(utilization);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.exit(0); System.exit(0);
...@@ -82,6 +92,20 @@ public class PowerHost extends HostDynamicWorkload { ...@@ -82,6 +92,20 @@ public class PowerHost extends HostDynamicWorkload {
return power; return power;
} }
/**
* Gets the energy consumption using linear interpolation of the utilization change.
*
* @param fromUtilization the from utilization
* @param toUtilization the to utilization
* @param time the time
* @return the energy
*/
public double getEnergyLinearInterpolation(double fromUtilization, double toUtilization, double time) {
double fromPower = getPower(fromUtilization);
double toPower = getPower(toUtilization);
return (fromPower + (toPower - fromPower) / 2) * time;
}
/** /**
* Sets the power model. * Sets the power model.
* *
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
package org.cloudbus.cloudsim.power; package org.cloudbus.cloudsim.power;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -16,6 +17,7 @@ import java.util.List; ...@@ -16,6 +17,7 @@ import java.util.List;
import org.cloudbus.cloudsim.Pe; import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.power.models.PowerModelLinear; import org.cloudbus.cloudsim.power.models.PowerModelLinear;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/** /**
...@@ -27,13 +29,33 @@ public class PowerHostTest { ...@@ -27,13 +29,33 @@ public class PowerHostTest {
private static final double MIPS = 1000; private static final double MIPS = 1000;
private static final double MAX_POWER = 200; private static final double MAX_POWER = 200;
private static final double STATIC_POWER_PERCENT = 0.3; private static final double STATIC_POWER_PERCENT = 0.3;
private static final double TIME = 10;
private PowerHost host;
@Before
public void setUp() throws Exception {
List<Pe> peList = new ArrayList<Pe>();
peList.add(new Pe(0, new PeProvisionerSimple(MIPS)));
host = new PowerHost(0, null, null, 0, peList, null, new PowerModelLinear(MAX_POWER, STATIC_POWER_PERCENT));
}
@Test @Test
public void testGetMaxPower() { public void testGetMaxPower() {
List<Pe> peList = new ArrayList<Pe>();
peList.add(new Pe(0, new PeProvisionerSimple(MIPS)));
PowerHost host = new PowerHost(0, null, null, 0, peList, null, new PowerModelLinear(MAX_POWER, STATIC_POWER_PERCENT));
assertEquals(MAX_POWER, host.getMaxPower(), 0); assertEquals(MAX_POWER, host.getMaxPower(), 0);
} }
@Test
public void testGetEnergy() {
assertEquals(0, host.getEnergyLinearInterpolation(0, 0, TIME), 0);
double expectedEnergy = 0;
try {
expectedEnergy = (host.getPowerModel().getPower(0.2) + (host.getPowerModel().getPower(0.9) - host.getPowerModel().getPower(0.2)) / 2) * TIME;
} catch (Exception e) {
e.printStackTrace();
fail();
}
assertEquals(expectedEnergy, host.getEnergyLinearInterpolation(0.2, 0.9, TIME), 0);
}
} }
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