From f3e578a2a43c99997dbf35e09debfde255a4ae22 Mon Sep 17 00:00:00 2001 From: Dante Niewenhuis Date: Sun, 3 Nov 2024 20:00:26 +0100 Subject: Rewritten the Carbon model (#260) --- .../org/opendc/compute/carbon/CarbonTrace.kt | 113 --------------------- .../org/opendc/compute/carbon/CarbonTraceLoader.kt | 15 +-- .../org/opendc/compute/carbon/CarbonTraceReader.kt | 15 ++- 3 files changed, 15 insertions(+), 128 deletions(-) delete mode 100644 opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTrace.kt (limited to 'opendc-compute/opendc-compute-carbon/src/main') diff --git a/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTrace.kt b/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTrace.kt deleted file mode 100644 index 2ba3e4e3..00000000 --- a/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTrace.kt +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2021 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.compute.carbon - -import java.time.Instant - -/** - * A virtual machine workload. - * - * @param uid The unique identifier of the virtual machine. - * @param name The name of the virtual machine. - * @param cpuCapacity The required CPU capacity for the VM in MHz. - * @param cpuCount The number of vCPUs in the VM. - * @param memCapacity The provisioned memory for the VM in MB. - * @param startTime The start time of the VM. - * @param stopTime The stop time of the VM. - * @param trace The trace that belong to this VM. - * @param interferenceProfile The interference profile of this virtual machine. - */ -public data class CarbonFragment( - var startTime: Long, - var endTime: Long, - var carbonIntensity: Double, -) { - init { - require(endTime > startTime) { - "The end time of a report should be higher than the start time -> start time: $startTime, end time: $endTime" - } - require(carbonIntensity >= 0.0) { "carbon intensity cannot be negative" } - } -} - -public class CarbonTrace(reports: List? = null) { - private var index: Int = 0 - private val numberOfReports = reports?.size - private val reports = reports?.sortedBy { it.startTime } - - private fun hasPreviousReport(): Boolean { - return index > 0 - } - - private fun hasNextReport(): Boolean { - if (numberOfReports == null) { - return false - } - - return index < numberOfReports - } - - public fun getCarbonIntensity(timestamp: Instant): Double { - return getCarbonIntensity(timestamp.toEpochMilli()) - } - - /** - * Get the carbon intensity of the energy at a given timestamp - * Returns the carbon intensity of the first or last [CarbonFragment] - * if the given timestamp is outside the information - * - * @param timestamp - * @return The carbon intensity at the given timestamp in gCO2/kWh - */ - public fun getCarbonIntensity(timestamp: Long): Double { - if (reports == null) { - return 0.0 - } - - var currentFragment: CarbonFragment - - while (true) { - currentFragment = reports[index] - - if (currentFragment.startTime > timestamp) { - if (hasPreviousReport()) { - index-- - continue - } - break - } - - if (currentFragment.endTime <= timestamp) { - if (hasNextReport()) { - index++ - continue - } - break - } - - break - } - - return currentFragment.carbonIntensity - } -} diff --git a/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceLoader.kt b/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceLoader.kt index b66aedf9..ccf1d81c 100644 --- a/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceLoader.kt +++ b/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceLoader.kt @@ -22,6 +22,7 @@ package org.opendc.compute.carbon +import org.opendc.simulator.compute.power.CarbonFragmentNew import org.opendc.trace.Trace import org.opendc.trace.conv.CARBON_INTENSITY_TIMESTAMP import org.opendc.trace.conv.CARBON_INTENSITY_VALUE @@ -40,14 +41,14 @@ public class CarbonTraceLoader { /** * The cache of workloads. */ - private val cache = ConcurrentHashMap>>() + private val cache = ConcurrentHashMap>>() - private val builder = CarbonFragmentBuilder() + private val builder = CarbonFragmentNewBuilder() /** * Read the metadata into a workload. */ - private fun parseCarbon(trace: Trace): List { + private fun parseCarbon(trace: Trace): List { val reader = checkNotNull(trace.getTable(TABLE_CARBON_INTENSITIES)).newReader() val startTimeCol = reader.resolve(CARBON_INTENSITY_TIMESTAMP) @@ -76,7 +77,7 @@ public class CarbonTraceLoader { /** * Load the trace with the specified [name] and [format]. */ - public fun get(pathToFile: File): List { + public fun get(pathToFile: File): List { val trace = Trace.open(pathToFile, "carbon") return parseCarbon(trace) @@ -92,11 +93,11 @@ public class CarbonTraceLoader { /** * A builder for a VM trace. */ - private class CarbonFragmentBuilder { + private class CarbonFragmentNewBuilder { /** * The total load of the trace. */ - public val fragments: MutableList = mutableListOf() + public val fragments: MutableList = mutableListOf() /** * Add a fragment to the trace. @@ -109,7 +110,7 @@ public class CarbonTraceLoader { carbonIntensity: Double, ) { fragments.add( - CarbonFragment(startTime.toEpochMilli(), Long.MAX_VALUE, carbonIntensity), + CarbonFragmentNew(startTime.toEpochMilli(), Long.MAX_VALUE, carbonIntensity), ) } diff --git a/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceReader.kt b/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceReader.kt index 3e0269f8..0b2b07a1 100644 --- a/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceReader.kt +++ b/opendc-compute/opendc-compute-carbon/src/main/kotlin/org/opendc/compute/carbon/CarbonTraceReader.kt @@ -20,33 +20,32 @@ * SOFTWARE. */ -@file:JvmName("ComputeWorkloads") +@file:JvmName("ComputeWorkloadsNew") package org.opendc.compute.carbon +import org.opendc.simulator.compute.power.CarbonFragmentNew import java.io.File import javax.management.InvalidAttributeValueException /** * Construct a workload from a trace. */ -public fun getCarbonTrace(pathToFile: String?): CarbonTrace { +public fun getCarbonFragments(pathToFile: String?): List? { if (pathToFile == null) { - return CarbonTrace(null) + return null } - return getCarbonTrace(File(pathToFile)) + return getCarbonFragments(File(pathToFile)) } /** * Construct a workload from a trace. */ -public fun getCarbonTrace(file: File): CarbonTrace { +public fun getCarbonFragments(file: File): List { if (!file.exists()) { throw InvalidAttributeValueException("The carbon trace cannot be found") } - val fragments = CarbonTraceLoader().get(file) - - return CarbonTrace(fragments) + return CarbonTraceLoader().get(file) } -- cgit v1.2.3