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;
import org.cloudbus.cloudsim.provisioners.BwProvisioner;
import org.cloudbus.cloudsim.provisioners.RamProvisioner;
// TODO: Auto-generated Javadoc
/**
* The Class HostDynamicWorkload.
*
......@@ -28,6 +29,9 @@ public class HostDynamicWorkload extends Host {
/** The utilization mips. */
private double utilizationMips;
/** The previous utilization mips. */
private double previousUtilizationMips;
/** The under allocated mips. */
private Map<String, List<List<Double>>> underAllocatedMips;
......@@ -51,6 +55,7 @@ public class HostDynamicWorkload extends Host {
VmScheduler vmScheduler) {
super(id, ramProvisioner, bwProvisioner, storage, peList, vmScheduler);
setUtilizationMips(0);
setPreviousUtilizationMips(0);
setUnderAllocatedMips(new HashMap<String, List<List<Double>>>());
setVmsMigratingIn(new ArrayList<Vm>());
}
......@@ -64,6 +69,7 @@ public class HostDynamicWorkload extends Host {
Log.printLine();
}
double smallerTime = super.updateVmsProcessing(currentTime);
setPreviousUtilizationMips(getUtilizationMips());
setUtilizationMips(0);
for (Vm vm : getVmList()) {
......@@ -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
*/
......@@ -208,6 +214,19 @@ public class HostDynamicWorkload extends Host {
}
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.
......@@ -223,7 +242,7 @@ public class HostDynamicWorkload extends Host {
*
* @return the utilization mips
*/
protected double getUtilizationMips() {
public double getUtilizationMips() {
return utilizationMips;
}
......@@ -234,6 +253,24 @@ public class HostDynamicWorkload extends Host {
*/
protected void setUtilizationMips(double 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 {
double currentTime = CloudSim.clock();
double minTime = Double.MAX_VALUE;
double timeDiff = currentTime - getLastProcessTime();
double timeframePower = 0.0;
double timeFrameDatacenterEnergy = 0.0;
if (timeDiff > 0) {
Log.formatLine("\nEnergy consumption for the last time frame from %.2f to %.2f:", getLastProcessTime(), currentTime);
for (PowerHost host : this.<PowerHost>getHostList()) {
Log.printLine();
double hostPower = 0.0;
if (host.getUtilizationOfCpu() > 0) {
try {
hostPower = host.getPower() * timeDiff;
timeframePower += hostPower;
} catch (Exception e) {
e.printStackTrace();
}
}
double timeFrameHostEnergy = host.getEnergyLinearInterpolation(host.getPreviousUtilizationOfCpu(), host.getUtilizationOfCpu(), timeDiff);
timeFrameDatacenterEnergy += timeFrameHostEnergy;
Log.printLine();
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");
......@@ -176,7 +168,7 @@ public class PowerDatacenter extends Datacenter {
Log.formatLine("%.2f: [Host #%d] utilization is %.2f%%", currentTime, host.getId(), host.getUtilizationOfCpu() * 100);
}
setPower(getPower() + timeframePower);
setPower(getPower() + timeFrameDatacenterEnergy);
checkCloudletCompletion();
......
......@@ -56,9 +56,19 @@ public class PowerHost extends HostDynamicWorkload {
* @return the power
*/
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;
try {
power = getPowerModel().getPower(getUtilizationOfCpu());
power = getPowerModel().getPower(utilization);
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
......@@ -82,6 +92,20 @@ public class PowerHost extends HostDynamicWorkload {
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.
*
......
......@@ -9,6 +9,7 @@
package org.cloudbus.cloudsim.power;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
......@@ -16,6 +17,7 @@ import java.util.List;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.power.models.PowerModelLinear;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.junit.Before;
import org.junit.Test;
/**
......@@ -27,13 +29,33 @@ public class PowerHostTest {
private static final double MIPS = 1000;
private static final double MAX_POWER = 200;
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
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);
}
@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