Unverified Commit fbf90ade authored by Manoel Campos's avatar Manoel Campos
Browse files

Redesign PowerModel classes



- Remove code duplication
- Provides a Functional implementation that
  enables passing a Function as parameter
  to defines how the power consumption increases
  along the time.
- Introduces the PowerModelSimple that enables
  the user to define the power consumption increment
  Function without requiring to create a new class.
Signed-off-by: default avatarManoel Campos <manoelcampos@gmail.com>
parent f298c7ff
......@@ -19,13 +19,13 @@ public abstract class PowerModelAbstract implements PowerModel {
}
@Override
public final void setHost(Host host) {
public final void setHost(final Host host) {
Objects.requireNonNull(host);
this.host = host;
}
@Override
public final double getPower(double utilization) throws IllegalArgumentException {
public final double getPower(final double utilization) throws IllegalArgumentException {
if (utilization < 0 || utilization > 1) {
throw new IllegalArgumentException(
String.format(
......@@ -58,5 +58,5 @@ public abstract class PowerModelAbstract implements PowerModel {
* @throws IllegalArgumentException when the utilization percentage is not
* between [0 and 1]
*/
protected abstract double getPowerInternal(double utilization) throws IllegalArgumentException;
protected abstract double getPowerInternal(final double utilization) throws IllegalArgumentException;
}
......@@ -25,93 +25,15 @@ package org.cloudbus.cloudsim.power.models;
* @author Manoel Campos da Silva Filho
* @since CloudSim Toolkit 2.0
*/
public class PowerModelCubic extends PowerModelAbstract {
/**
* The max power that can be consumed.
*/
private double maxPower;
/**
* The constant that represents the power consumption
* for each fraction of resource used.
*/
private double constant;
/**
* The static power consumption that is not dependent of resource usage.
* It is the amount of energy consumed even when the host is idle.
*/
private double staticPower;
public class PowerModelCubic extends PowerModelSimple {
/**
* Instantiates a new power model cubic.
*
* @param maxPower the max power
* @param staticPowerPercent the static power percent
* @param maxPower the max power that can be consumed (in Watts/second).
* @param staticPowerPercent the static power usage percentage between 0 and 1.
*/
public PowerModelCubic(double maxPower, double staticPowerPercent) {
setMaxPower(maxPower);
setStaticPower(staticPowerPercent * maxPower);
setConstant((maxPower - getStaticPower()) / Math.pow(100, 3));
public PowerModelCubic(final double maxPower, final double staticPowerPercent) {
super(maxPower, staticPowerPercent, utilizationPercent -> Math.pow(utilizationPercent, 3));
}
@Override
protected double getPowerInternal(double utilization) throws IllegalArgumentException {
return getStaticPower() + getConstant() * Math.pow(utilization * 100, 3);
}
/**
* Gets the max power.
*
* @return the max power
*/
protected double getMaxPower() {
return maxPower;
}
/**
* Sets the max power.
*
* @param maxPower the new max power
*/
protected final void setMaxPower(double maxPower) {
this.maxPower = maxPower;
}
/**
* Gets the constant.
*
* @return the constant
*/
protected double getConstant() {
return constant;
}
/**
* Sets the constant.
*
* @param constant the new constant
*/
protected final void setConstant(double constant) {
this.constant = constant;
}
/**
* Gets the static power.
*
* @return the static power
*/
protected final double getStaticPower() {
return staticPower;
}
/**
* Sets the static power.
*
* @param staticPower the new static power
*/
protected final void setStaticPower(double staticPower) {
this.staticPower = staticPower;
}
}
......@@ -25,95 +25,14 @@ package org.cloudbus.cloudsim.power.models;
* @author Manoel Campos da Silva Filho
* @since CloudSim Toolkit 2.0
*/
public class PowerModelLinear extends PowerModelAbstract {
/* @TODO the three first attributes are being repeated among several classes.
* Thus, a better class hierarchy should be provided, such as an abstract class
* implementing the PowerModel interface.
*/
/** @see #getMaxPower() */
private double maxPower;
/** @see #getConstant() */
private double constant;
/**
* The static power consumption that is not dependent of resource usage.
* It is the amount of energy consumed even when the host is idle.
*/
private double staticPower;
/**
* Instantiates a new linear power model.
public class PowerModelLinear extends PowerModelSimple {
/**
* Instantiates a linear power model.
*
* @param maxPower the max power that can be consumed (in Watts/second).
* @param staticPowerPercent the static power usage percentage
* @param staticPowerPercent the static power usage percentage between 0 and 1.
*/
public PowerModelLinear(final double maxPower, final double staticPowerPercent) {
setMaxPower(maxPower);
setStaticPower(staticPowerPercent * maxPower);
setConstant((maxPower - getStaticPower()) / 100.0);
}
@Override
protected double getPowerInternal(final double utilization) throws IllegalArgumentException {
return getStaticPower() + getConstant() * utilization * 100;
}
/**
* Gets the max power that can be consumed (in Watts/second).
*
* @return the max power
*/
protected double getMaxPower() {
return maxPower;
}
/**
* Sets The max power that can be consumed.
*
* @param maxPower the new max power
*/
protected final void setMaxPower(double maxPower) {
this.maxPower = maxPower;
super(maxPower, staticPowerPercent, utilizationPercent -> utilizationPercent);
}
/**
* Gets the constant which represents the power consumption
* for each fraction of resource used.
*
* @return the constant
*/
protected double getConstant() {
return constant;
}
/**
* Sets the constant which represents the power consumption
* for each fraction of resource used.
*
* @param constant the new constant
*/
protected final void setConstant(double constant) {
this.constant = constant;
}
/**
* Gets the static power usage percentage.
*
* @return the static power usage percentage
*/
protected final double getStaticPower() {
return staticPower;
}
/**
* Sets the static power usage percentage.
*
* @param staticPower the new static power usage percentage
*/
protected final void setStaticPower(double staticPower) {
this.staticPower = staticPower;
}
}
package org.cloudbus.cloudsim.power.models;
import java.util.Objects;
import java.util.function.UnaryOperator;
/**
* A power model where the power consumption is defined by a {@link UnaryOperator} function
* given as parameter to the constructor. This way, the user can define how the power consumption
* increases along the time without requiring to create a new class for it.
*
* <p>However, specific classes that implement well known models
* are provided, such as {@link PowerModelLinear},
* {@link PowerModelSquare}, {@link PowerModelCubic}
* and {@link PowerModelSqrt}.</p>
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 2.1.0
*/
public class PowerModelSimple extends PowerModelAbstract {
private final UnaryOperator<Double> powerIncrementFunction;
/**
* @see #getMaxPower()
*/
private double maxPower;
/**
* @see #getStaticPowerPercent()
*/
private double staticPowerPercent;
/**
* Instantiates a PowerModelSimple.
*
* @param maxPower the max power that can be consumed (in Watts/second).
* @param staticPowerPercent the static power usage percentage between 0 and 1.
* @param powerIncrementFunction a function that defines how the power consumption increases along the time.
* This function receives the utilization percentage in scale from 0 to 100
* and returns a factor representing how the power consumption will
* increase for the given utilization percentage.
* The function return is again a percentage value between [0 and 1].
*/
public PowerModelSimple(
final double maxPower,
final double staticPowerPercent,
final UnaryOperator<Double> powerIncrementFunction)
{
Objects.requireNonNull(powerIncrementFunction);
this.powerIncrementFunction = powerIncrementFunction;
setMaxPower(maxPower);
setStaticPowerPercent(staticPowerPercent);
}
/**
* Gets the max power that can be consumed (in Watts/second).
*
* @return the max power (in Watts/second)
*/
public double getMaxPower() {
return maxPower;
}
/**
* Sets the max power that can be consumed (in Watts/second).
*
* @param maxPower the new max power (in Watts/second)
*/
private void setMaxPower(final double maxPower) {
if(maxPower < 0){
throw new IllegalArgumentException("Maximum power consumption cannot be negative.");
}
this.maxPower = maxPower;
}
/**
* Gets the static power consumption percentage (between 0 and 1) that is not dependent of resource usage.
* It is the amount of energy consumed even when the host is idle.
* @return the static power consumption percentage (between 0 and 1)
*/
public double getStaticPowerPercent() {
return staticPowerPercent;
}
/**
* Sets the static power consumption percentage (between 0 and 1) that is not dependent of resource usage.
* It is the amount of energy consumed even when the host is idle.
*
* @param staticPowerPercent the value to set (between 0 and 1)
*/
private void setStaticPowerPercent(final double staticPowerPercent) {
if(staticPowerPercent < 0 || staticPowerPercent > 1){
throw new IllegalArgumentException("Static power percentage must be between 0 and 1.");
}
this.staticPowerPercent = staticPowerPercent;
}
/**
* Gets the static power consumption (in Watts/second) that is not dependent of resource usage,
* according to the {@link #getStaticPowerPercent()}.
* It is the amount of energy consumed even when the host is idle.
*
* @return the static power usage (in Watts/second)
*/
public final double getStaticPower() {
return staticPowerPercent * maxPower;
}
/**
* Gets the constant which represents the power consumption
* for each fraction of resource used (in Watts/second).
*
* @return the power consumption constant (in Watts/second)
*/
protected double getConstant() {
return (maxPower - getStaticPower()) / powerIncrementFunction.apply(100.0);
}
@Override
protected double getPowerInternal(final double utilization) throws IllegalArgumentException {
return getStaticPower() + getConstant() * powerIncrementFunction.apply(utilization*100.0);
}
}
......@@ -26,17 +26,19 @@ package org.cloudbus.cloudsim.power.models;
* @since CloudSim Toolkit 3.0
*/
public abstract class PowerModelSpecPower extends PowerModelAbstract {
@Override
protected double getPowerInternal(double utilization) throws IllegalArgumentException {
protected double getPowerInternal(final double utilization) throws IllegalArgumentException {
if (utilization % 0.1 == 0) {
return getPowerData((int) (utilization * 10));
}
final int utilization1 = (int) Math.floor(utilization * 10);
final int utilization2 = (int) Math.ceil(utilization * 10);
final double power1 = getPowerData(utilization1);
final double power2 = getPowerData(utilization2);
final double delta = (power2 - power1) / 10;
return power1 + delta * (utilization - (double) utilization1 / 10) * 100;
return power1 + delta * (utilization - (double) utilization1 / 10) * 100;
}
/**
......@@ -46,6 +48,6 @@ public abstract class PowerModelSpecPower extends PowerModelAbstract {
* where 10 means 100% of utilization.
* @return the power consumption for the given utilization percentage
*/
protected abstract double getPowerData(int index);
protected abstract double getPowerData(final int index);
}
......@@ -27,15 +27,16 @@ package org.cloudbus.cloudsim.power.models;
* @since CloudSim Toolkit 3.0
*/
public class PowerModelSpecPowerHpProLiantMl110G3PentiumD930 extends PowerModelSpecPower {
/**
* The power consumption according to the utilization percentage.
* @see #getPowerData(int)
*/
private final double[] power = { 105, 112, 118, 125, 131, 137, 147, 153, 157, 164, 169 };
/**
* The power consumption according to the utilization percentage.
*
* @see #getPowerData(int)
*/
private final double[] power = {105, 112, 118, 125, 131, 137, 147, 153, 157, 164, 169};
@Override
protected double getPowerData(int index) {
return power[index];
}
@Override
protected double getPowerData(final int index) {
return power[index];
}
}
......@@ -34,7 +34,7 @@ public class PowerModelSpecPowerHpProLiantMl110G4Xeon3040 extends PowerModelSpec
private final double[] power = { 86, 89.4, 92.6, 96, 99.5, 102, 106, 108, 112, 114, 117 };
@Override
protected double getPowerData(int index) {
protected double getPowerData(final int index) {
return power[index];
}
......
......@@ -34,7 +34,7 @@ public class PowerModelSpecPowerHpProLiantMl110G5Xeon3075 extends PowerModelSpec
private final double[] power = { 93.7, 97, 101, 105, 110, 116, 121, 125, 129, 133, 135 };
@Override
protected double getPowerData(int index) {
protected double getPowerData(final int index) {
return power[index];
}
......
......@@ -34,7 +34,7 @@ public class PowerModelSpecPowerIbmX3250XeonX3470 extends PowerModelSpecPower {
private final double[] power = { 41.6, 46.7, 52.3, 57.9, 65.4, 73, 80.7, 89.5, 99.6, 105, 113 };
@Override
protected double getPowerData(int index) {
protected double getPowerData(final int index) {
return power[index];
}
......
......@@ -34,7 +34,7 @@ public class PowerModelSpecPowerIbmX3250XeonX3480 extends PowerModelSpecPower {
private final double[] power = { 42.3, 46.7, 49.7, 55.4, 61.8, 69.3, 76.1, 87, 96.1, 106, 113 };
@Override
protected double getPowerData(int index) {
protected double getPowerData(final int index) {
return power[index];
}
......
......@@ -34,7 +34,7 @@ public class PowerModelSpecPowerIbmX3550XeonX5670 extends PowerModelSpecPower {
private final double[] power = { 66, 107, 120, 131, 143, 156, 173, 191, 211, 229, 247 };
@Override
protected double getPowerData(int index) {
protected double getPowerData(final int index) {
return power[index];
}
......
......@@ -34,7 +34,7 @@ public class PowerModelSpecPowerIbmX3550XeonX5675 extends PowerModelSpecPower {
private final double[] power = { 58.4, 98, 109, 118, 128, 140, 153, 170, 189, 205, 222 };
@Override
protected double getPowerData(int index) {
protected double getPowerData(final int index) {
return power[index];
}
......
......@@ -26,93 +26,15 @@ package org.cloudbus.cloudsim.power.models;
*
* @since CloudSim Toolkit 2.0
*/
public class PowerModelSqrt extends PowerModelAbstract {
/**
* The max power that can be consumed.
*/
private double maxPower;
/**
* The constant that represents the power consumption
* for each fraction of resource used.
*/
private double constant;
/**
* The static power consumption that is not dependent of resource usage.
* It is the amount of energy consumed even when the host is idle.
*/
private double staticPower;
public class PowerModelSqrt extends PowerModelSimple {
/**
* Instantiates a new power model sqrt.
*
* @param maxPower the max power
* @param staticPowerPercent the static power percent
* @param maxPower the max power that can be consumed (in Watts/second).
* @param staticPowerPercent the static power usage percentage between 0 and 1.
*/
public PowerModelSqrt(double maxPower, double staticPowerPercent) {
setMaxPower(maxPower);
setStaticPower(staticPowerPercent * maxPower);
setConstant((maxPower - getStaticPower()) / Math.sqrt(100));
public PowerModelSqrt(final double maxPower, final double staticPowerPercent) {
super(maxPower, staticPowerPercent, Math::sqrt);
}
@Override
protected double getPowerInternal(double utilization) throws IllegalArgumentException {
return getStaticPower() + getConstant() * Math.sqrt(utilization * 100);
}
/**
* Gets the max power.
*
* @return the max power
*/
protected double getMaxPower() {
return maxPower;
}
/**
* Sets the max power.
*
* @param maxPower the new max power
*/
protected final void setMaxPower(double maxPower) {
this.maxPower = maxPower;
}
/**
* Gets the constant.
*
* @return the constant
*/
protected double getConstant() {
return constant;
}
/**
* Sets the constant.
*
* @param constant the new constant
*/
protected final void setConstant(double constant) {
this.constant = constant;
}
/**
* Gets the static power.
*
* @return the static power
*/
protected final double getStaticPower() {
return staticPower;
}
/**
* Sets the static power.
*
* @param staticPower the new static power
*/
protected final void setStaticPower(double staticPower) {
this.staticPower = staticPower;
}
}
......@@ -25,94 +25,15 @@ package org.cloudbus.cloudsim.power.models;
* @author Manoel Campos da Silva Filho
* @since CloudSim Toolkit 2.0
*/
public class PowerModelSquare extends PowerModelAbstract {
/**