summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc
diff options
context:
space:
mode:
authorDante Niewenhuis <d.niewenhuis@hotmail.com>2025-02-06 10:49:12 +0100
committerGitHub <noreply@github.com>2025-02-06 10:49:12 +0100
commit8a3f26bfa51753c8b602985fc1965105629e5f37 (patch)
treeb9227e6d7d92103ee44812aea5fb70a749debbbd /opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc
parentdf1028c71cb6d50db886c8076c7139ec24feb6d7 (diff)
updated logging and added logging for batteries (#301)
* Updated logging * removed DoubleThresholdBatteryPolicy.java
Diffstat (limited to 'opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc')
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/SimPowerSource.java24
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/BatteryState.java6
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/SimBattery.java106
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/BatteryPolicy.java14
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/DoubleThresholdBatteryPolicy.java64
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/SingleThresholdBatteryPolicy.java12
6 files changed, 112 insertions, 114 deletions
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/SimPowerSource.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/SimPowerSource.java
index 3bc5ba70..718bc22a 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/SimPowerSource.java
+++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/SimPowerSource.java
@@ -43,10 +43,13 @@ public final class SimPowerSource extends FlowNode implements FlowSupplier, Carb
private FlowEdge distributorEdge;
- private double capacity;
+ private final double capacity;
private CarbonModel carbonModel = null;
+ private final String name;
+ private final String clusterName;
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Basic Getters and Setters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -96,16 +99,27 @@ public final class SimPowerSource extends FlowNode implements FlowSupplier, Carb
return this.capacity;
}
+ public String getName() {
+ return name;
+ }
+
+ public String getClusterName() {
+ return clusterName;
+ }
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public SimPowerSource(FlowGraph graph, double max_capacity) {
+ public SimPowerSource(FlowGraph graph, double max_capacity, String name, String clusterName) {
super(graph);
this.capacity = max_capacity;
lastUpdate = this.clock.millis();
+
+ this.name = name;
+ this.clusterName = clusterName;
}
public void close() {
@@ -136,9 +150,9 @@ public final class SimPowerSource extends FlowNode implements FlowSupplier, Carb
long lastUpdate = this.lastUpdate;
this.lastUpdate = now;
- long duration = now - lastUpdate;
- if (duration > 0) {
- double energyUsage = (this.powerSupplied * duration * 0.001);
+ long passedTime = now - lastUpdate;
+ if (passedTime > 0) {
+ double energyUsage = (this.powerSupplied * passedTime * 0.001);
// Compute the energy usage of the machine
this.totalEnergyUsage += energyUsage;
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/BatteryState.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/BatteryState.java
index 0f010864..ae87902d 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/BatteryState.java
+++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/BatteryState.java
@@ -23,7 +23,7 @@
package org.opendc.simulator.compute.power.batteries;
public enum BatteryState {
- Charging,
- Idle,
- Discharging
+ CHARGING,
+ IDLE,
+ DISCHARGING
}
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/SimBattery.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/SimBattery.java
index ad7e09e3..d229d4b5 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/SimBattery.java
+++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/SimBattery.java
@@ -32,22 +32,50 @@ import org.opendc.simulator.engine.graph.FlowSupplier;
public class SimBattery extends FlowNode implements FlowConsumer, FlowSupplier {
private final double capacity;
- private double chargingSpeed;
+ private final double chargingSpeed;
private FlowEdge distributorEdge;
private FlowEdge aggregatorEdge;
- private BatteryState batteryState = BatteryState.Idle;
+ private BatteryState batteryState = BatteryState.IDLE;
private double charge;
private long lastUpdate;
private double incomingSupply;
private double incomingDemand;
-
private double outgoingDemand;
private double outgoingSupply;
+ private final String name;
+ private final String clusterName;
+
+ public String getName() {
+ return name;
+ }
+
+ public String getClusterName() {
+ return clusterName;
+ }
+
+ public double getTotalEnergyUsage() {
+ return totalEnergyUsage;
+ }
+
+ public void setTotalEnergyUsage(double totalEnergyUsage) {
+ this.totalEnergyUsage = totalEnergyUsage;
+ }
+
+ public double getOutgoingSupply() {
+ return outgoingSupply;
+ }
+
+ public void setOutgoingSupply(double outgoingSupply) {
+ this.outgoingSupply = outgoingSupply;
+ }
+
+ private double totalEnergyUsage = 0.0f;
+
public BatteryPolicy getBatteryPolicy() {
return batteryPolicy;
}
@@ -78,24 +106,31 @@ public class SimBattery extends FlowNode implements FlowConsumer, FlowSupplier {
public boolean isFull() {
return (this.charge >= this.capacity);
}
- ;
public boolean isEmpty() {
return (this.charge <= 0.0);
}
- ;
/**
* Construct a new {@link FlowNode} instance.
*
* @param parentGraph The {@link FlowGraph} this stage belongs to.
*/
- public SimBattery(FlowGraph parentGraph, double capacity, double chargingSpeed, double initialCharge) {
+ public SimBattery(
+ FlowGraph parentGraph,
+ double capacity,
+ double chargingSpeed,
+ double initialCharge,
+ String name,
+ String clusterName) {
+
super(parentGraph);
this.capacity = capacity;
this.chargingSpeed = chargingSpeed;
this.charge = initialCharge;
+ this.name = name;
+ this.clusterName = clusterName;
}
public void close() {
@@ -112,17 +147,15 @@ public class SimBattery extends FlowNode implements FlowConsumer, FlowSupplier {
@Override
public long onUpdate(long now) {
- long passedTime = now - lastUpdate;
- this.lastUpdate = now;
+ this.updateCounters(now);
- if (this.batteryState == BatteryState.Idle) {
+ if (this.batteryState == BatteryState.IDLE) {
return Long.MAX_VALUE;
}
- this.updateCharge(passedTime);
long remainingTime = 0L;
- if (this.batteryState == BatteryState.Charging) {
+ if (this.batteryState == BatteryState.CHARGING) {
if (this.isFull()) {
this.batteryPolicy.invalidate();
return Long.MAX_VALUE;
@@ -131,7 +164,7 @@ public class SimBattery extends FlowNode implements FlowConsumer, FlowSupplier {
remainingTime = this.calculateRemainingTime();
}
- if (this.batteryState == BatteryState.Discharging) {
+ if (this.batteryState == BatteryState.DISCHARGING) {
if (this.isEmpty()) {
this.batteryPolicy.invalidate();
return Long.MAX_VALUE;
@@ -149,54 +182,69 @@ public class SimBattery extends FlowNode implements FlowConsumer, FlowSupplier {
return nextUpdate;
}
- private long calculateRemainingTime() {
- if ((this.batteryState == BatteryState.Charging) && (this.incomingSupply > 0.0)) {
- double remainingCharge = this.capacity - this.charge;
- return (long) Math.ceil((remainingCharge / this.incomingSupply) * 1000);
- }
+ public void updateCounters(long now) {
+ long lastUpdate = this.lastUpdate;
+ this.lastUpdate = now;
- if ((this.batteryState == BatteryState.Discharging) && (this.outgoingSupply > 0.0)) {
- return (long) Math.ceil((this.charge / this.outgoingSupply) * 1000);
+ long passedTime = now - lastUpdate;
+
+ this.updateCharge(passedTime);
+ if (passedTime > 0) {
+ double energyUsage = (this.outgoingSupply * passedTime * 0.001);
+
+ this.totalEnergyUsage += energyUsage;
}
+ }
- return Long.MAX_VALUE;
+ public void updateCounters() {
+ updateCounters(clock.millis());
}
private void updateCharge(long passedTime) {
- if (this.batteryState == BatteryState.Charging) {
+ if (this.batteryState == BatteryState.CHARGING) {
this.charge += this.incomingSupply * (passedTime / 1000.0);
}
- if (this.batteryState == BatteryState.Discharging) {
+ if (this.batteryState == BatteryState.DISCHARGING) {
this.charge -= this.outgoingSupply * (passedTime / 1000.0);
}
}
+ private long calculateRemainingTime() {
+ if ((this.batteryState == BatteryState.CHARGING) && (this.incomingSupply > 0.0)) {
+ double remainingCharge = this.capacity - this.charge;
+ return (long) Math.ceil((remainingCharge / this.incomingSupply) * 1000);
+ }
+
+ if ((this.batteryState == BatteryState.DISCHARGING) && (this.outgoingSupply > 0.0)) {
+ return (long) Math.ceil((this.charge / this.outgoingSupply) * 1000);
+ }
+
+ return Long.MAX_VALUE;
+ }
+
public void setBatteryState(BatteryState newBatteryState) {
if (newBatteryState == this.batteryState) {
return;
}
long now = this.clock.millis();
- long passedTime = now - lastUpdate;
- updateCharge(passedTime);
-
- this.lastUpdate = now;
+ updateCounters(now);
this.batteryState = newBatteryState;
- if (this.batteryState == BatteryState.Idle) {
+ if (this.batteryState == BatteryState.IDLE) {
this.pushOutgoingDemand(this.distributorEdge, 0.0f);
this.pushOutgoingSupply(this.distributorEdge, 0.0f);
}
- if (this.batteryState == BatteryState.Charging) {
+ if (this.batteryState == BatteryState.CHARGING) {
this.pushOutgoingDemand(this.distributorEdge, this.chargingSpeed);
this.pushOutgoingSupply(this.aggregatorEdge, 0.0f);
}
- if (this.batteryState == BatteryState.Discharging) {
+ if (this.batteryState == BatteryState.DISCHARGING) {
this.pushOutgoingDemand(this.distributorEdge, 0.0f);
}
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/BatteryPolicy.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/BatteryPolicy.java
index be2f49e0..5abbe861 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/BatteryPolicy.java
+++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/BatteryPolicy.java
@@ -38,7 +38,7 @@ public abstract class BatteryPolicy extends FlowNode implements CarbonReceiver {
protected double carbonIntensity;
- protected BatteryState batteryState = BatteryState.Idle;
+ protected BatteryState batteryState = BatteryState.IDLE;
/**
* Construct a new {@link FlowNode} instance.
@@ -68,20 +68,20 @@ public abstract class BatteryPolicy extends FlowNode implements CarbonReceiver {
this.batteryState = newBatteryState;
- if (newBatteryState == BatteryState.Charging) {
- this.battery.setBatteryState(BatteryState.Charging);
+ if (newBatteryState == BatteryState.CHARGING) {
+ this.battery.setBatteryState(BatteryState.CHARGING);
this.aggregator.setPowerSourceType(PowerSourceType.PowerSource);
return;
}
- if (newBatteryState == BatteryState.Idle) {
- this.battery.setBatteryState(BatteryState.Idle);
+ if (newBatteryState == BatteryState.IDLE) {
+ this.battery.setBatteryState(BatteryState.IDLE);
this.aggregator.setPowerSourceType(PowerSourceType.PowerSource);
return;
}
- if (newBatteryState == BatteryState.Discharging) {
- this.battery.setBatteryState(BatteryState.Discharging);
+ if (newBatteryState == BatteryState.DISCHARGING) {
+ this.battery.setBatteryState(BatteryState.DISCHARGING);
this.aggregator.setPowerSourceType(PowerSourceType.Battery);
}
}
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/DoubleThresholdBatteryPolicy.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/DoubleThresholdBatteryPolicy.java
deleted file mode 100644
index 18da75d0..00000000
--- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/DoubleThresholdBatteryPolicy.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (c) 2025 AtLarge Research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package org.opendc.simulator.compute.power.batteries.policy;
-
-import org.opendc.simulator.compute.power.batteries.BatteryAggregator;
-import org.opendc.simulator.compute.power.batteries.BatteryState;
-import org.opendc.simulator.compute.power.batteries.SimBattery;
-import org.opendc.simulator.engine.graph.FlowGraph;
-
-public class DoubleThresholdBatteryPolicy extends BatteryPolicy {
-
- private final double carbonThreshold;
-
- /**
- *
- * @param parentGraph The {@link FlowGraph} this stage belongs to.
- * @param battery
- * @param aggregator
- * @param carbonThreshold
- */
- public DoubleThresholdBatteryPolicy(
- FlowGraph parentGraph, SimBattery battery, BatteryAggregator aggregator, double carbonThreshold) {
- super(parentGraph, battery, aggregator);
-
- this.carbonThreshold = carbonThreshold;
- }
-
- @Override
- public long onUpdate(long now) {
-
- if (this.carbonIntensity >= this.carbonThreshold & !this.battery.isEmpty()) {
- this.setBatteryState(BatteryState.Discharging);
- return Long.MAX_VALUE;
- }
-
- if (this.carbonIntensity < this.carbonThreshold & !this.battery.isFull()) {
- this.setBatteryState(BatteryState.Charging);
- return Long.MAX_VALUE;
- }
-
- this.setBatteryState(BatteryState.Idle);
- return Long.MAX_VALUE;
- }
-}
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/SingleThresholdBatteryPolicy.java b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/SingleThresholdBatteryPolicy.java
index 4d71c096..e917a26f 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/SingleThresholdBatteryPolicy.java
+++ b/opendc-simulator/opendc-simulator-compute/src/main/java/org/opendc/simulator/compute/power/batteries/policy/SingleThresholdBatteryPolicy.java
@@ -33,9 +33,9 @@ public class SingleThresholdBatteryPolicy extends BatteryPolicy {
/**
*
* @param parentGraph The {@link FlowGraph} this stage belongs to.
- * @param battery
- * @param aggregator
- * @param carbonThreshold
+ * @param battery The {@link SimBattery} to control.
+ * @param aggregator The {@link BatteryAggregator} to use.
+ * @param carbonThreshold The carbon intensity threshold to trigger charging or discharging.
*/
public SingleThresholdBatteryPolicy(
FlowGraph parentGraph, SimBattery battery, BatteryAggregator aggregator, double carbonThreshold) {
@@ -48,16 +48,16 @@ public class SingleThresholdBatteryPolicy extends BatteryPolicy {
public long onUpdate(long now) {
if (this.carbonIntensity >= this.carbonThreshold & !this.battery.isEmpty()) {
- this.setBatteryState(BatteryState.Discharging);
+ this.setBatteryState(BatteryState.DISCHARGING);
return Long.MAX_VALUE;
}
if (this.carbonIntensity < this.carbonThreshold & !this.battery.isFull()) {
- this.setBatteryState(BatteryState.Charging);
+ this.setBatteryState(BatteryState.CHARGING);
return Long.MAX_VALUE;
}
- this.setBatteryState(BatteryState.Idle);
+ this.setBatteryState(BatteryState.IDLE);
return Long.MAX_VALUE;
}
}