Skip to content

Commit

Permalink
Redesign PowerModel classes
Browse files Browse the repository at this point in the history
- 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: Manoel Campos <manoelcampos@gmail.com>
  • Loading branch information
manoelcampos committed Apr 21, 2018
1 parent f298c7f commit fbf90ad
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public Host getHost() {
}

@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(
Expand Down Expand Up @@ -58,5 +58,5 @@ public final double getPower(double utilization) throws IllegalArgumentException
* @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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,93 +25,15 @@
* @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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,95 +25,14 @@
* @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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@
* @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;
}

/**
Expand All @@ -46,6 +48,6 @@ protected double getPowerInternal(double utilization) throws IllegalArgumentExce
* 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);

}
Loading

0 comments on commit fbf90ad

Please sign in to comment.