1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
* Copyright (c) 2022 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.experiments.base.runner
import me.tongfei.progressbar.ProgressBarBuilder
import me.tongfei.progressbar.ProgressBarStyle
import org.opendc.compute.simulator.provisioner.Provisioner
import org.opendc.compute.simulator.provisioner.registerComputeMonitor
import org.opendc.compute.simulator.provisioner.setupComputeService
import org.opendc.compute.simulator.provisioner.setupHosts
import org.opendc.compute.simulator.scheduler.ComputeScheduler
import org.opendc.compute.simulator.service.ComputeService
import org.opendc.compute.simulator.telemetry.parquet.ComputeExportConfig
import org.opendc.compute.simulator.telemetry.parquet.ParquetComputeMonitor
import org.opendc.compute.simulator.telemetry.parquet.withGpuColumns
import org.opendc.compute.topology.clusterTopology
import org.opendc.experiments.base.experiment.Scenario
import org.opendc.experiments.base.experiment.specs.allocation.TimeShiftAllocationPolicySpec
import org.opendc.experiments.base.experiment.specs.allocation.createComputeScheduler
import org.opendc.experiments.base.experiment.specs.allocation.createTaskStopper
import org.opendc.experiments.base.experiment.specs.getScalingPolicy
import org.opendc.experiments.base.experiment.specs.getWorkloadLoader
import org.opendc.simulator.compute.power.CarbonModel
import org.opendc.simulator.compute.power.CarbonReceiver
import org.opendc.simulator.kotlin.runSimulation
import java.io.File
import java.time.Duration
import java.util.Random
import java.util.concurrent.ForkJoinPool
import java.util.stream.LongStream
/**
* Run scenario when a pool is available for parallel execution
* The scenario is run multiple times based on the user input
*
* @param scenario The scenario to run
*/
public fun runScenario(scenario: Scenario) {
val pb =
ProgressBarBuilder().setInitialMax(scenario.runs.toLong()).setStyle(ProgressBarStyle.ASCII)
.setTaskName("Simulating...").build()
val pool = ForkJoinPool(5)
pool.submit {
LongStream.range(0, scenario.runs.toLong()).parallel().forEach {
runScenario(scenario, scenario.initialSeed + it)
pb.step()
}
pb.close()
}.join()
}
/**
* Run a single scenario with a specific seed
*
* @param scenario The scenario to run
* @param seed The starting seed of the random generator.
*/
public fun runScenario(
scenario: Scenario,
seed: Long,
): Unit =
runSimulation {
val serviceDomain = "compute.opendc.org"
Provisioner(dispatcher, seed).use { provisioner ->
val checkpointInterval = scenario.checkpointModelSpec?.checkpointInterval ?: 0L
val checkpointDuration = scenario.checkpointModelSpec?.checkpointDuration ?: 0L
val checkpointIntervalScaling = scenario.checkpointModelSpec?.checkpointIntervalScaling ?: 1.0
val scalingPolicy = getScalingPolicy(scenario.workloadSpec.scalingPolicy)
val workloadLoader =
getWorkloadLoader(
scenario.workloadSpec.type,
File(scenario.workloadSpec.pathToFile),
scenario.workloadSpec.submissionTime,
checkpointInterval,
checkpointDuration,
checkpointIntervalScaling,
scalingPolicy,
scenario.workloadSpec.deferAll,
)
val workload = workloadLoader.sampleByLoad(scenario.workloadSpec.sampleFraction)
val startTimeLong = workload.minOf { it.submittedAt }
val startTime = Duration.ofMillis(startTimeLong)
val topology = clusterTopology(scenario.topologySpec.pathToFile)
val numHosts = topology.sumOf { it.hostSpecs.size }
provisioner.runSteps(
setupComputeService(
serviceDomain,
{
val computeScheduler =
createComputeScheduler(
scenario.allocationPolicySpec,
Random(it.seeder.nextLong()),
timeSource,
numHosts,
)
provisioner.registry.register(serviceDomain, ComputeScheduler::class.java, computeScheduler)
return@setupComputeService computeScheduler
},
maxNumFailures = scenario.maxNumFailures,
),
setupHosts(serviceDomain, topology, startTimeLong),
)
val gpuCount = topology.flatMap { it.hostSpecs }.maxOfOrNull { it.model.gpuModels.size } ?: 0
addExportModel(
provisioner,
serviceDomain,
scenario,
seed,
startTime,
scenario.id,
computeExportConfig =
scenario.exportModelSpec.computeExportConfig.withGpuColumns(gpuCount),
)
val service = provisioner.registry.resolve(serviceDomain, ComputeService::class.java)!!
service.setTasksExpected(workload.size)
service.setMetricReader(provisioner.getMonitor())
var carbonModel: CarbonModel?
if (provisioner.registry.hasService(serviceDomain, CarbonModel::class.java)) {
carbonModel = provisioner.registry.resolve(serviceDomain, CarbonModel::class.java)!!
val computeScheduler = provisioner.registry.resolve(serviceDomain, ComputeScheduler::class.java)!!
if (computeScheduler is CarbonReceiver) {
carbonModel.addReceiver(computeScheduler)
carbonModel.addReceiver(service)
}
if (scenario.allocationPolicySpec is TimeShiftAllocationPolicySpec) {
val taskStopper =
createTaskStopper(
scenario.allocationPolicySpec.taskStopper,
coroutineContext,
timeSource,
)
if (taskStopper != null) {
taskStopper.setService(service)
carbonModel.addReceiver(taskStopper)
}
}
}
service.replay(
timeSource,
workload,
failureModelSpec = scenario.failureModelSpec,
seed = seed,
)
}
}
/**
* Saves the simulation results into a specific output folder received from the input.A
*
* @param provisioner The provisioner used to setup and run the simulation.
* @param serviceDomain The domain of the compute service.
* @param scenario The scenario being run.
* @param seed The seed of the current run
* @param startTime The start time of the simulation given by the workload trace.
*/
public fun addExportModel(
provisioner: Provisioner,
serviceDomain: String,
scenario: Scenario,
seed: Long,
startTime: Duration,
index: Int,
computeExportConfig: ComputeExportConfig = scenario.exportModelSpec.computeExportConfig,
) {
provisioner.runStep(
registerComputeMonitor(
serviceDomain,
ParquetComputeMonitor(
File("${scenario.outputFolder}/raw-output/$index"),
"seed=$seed",
bufferSize = 4096,
scenario.exportModelSpec.filesToExportDict,
computeExportConfig = computeExportConfig,
),
Duration.ofSeconds(scenario.exportModelSpec.exportInterval),
startTime,
scenario.exportModelSpec.filesToExportDict,
scenario.exportModelSpec.printFrequency,
),
)
}
/**
* Utility function, in case we want to delete the previous simulation results.
* @param outputFolderPath The output folder to remove
*/
public fun clearOutputFolder(outputFolderPath: String) {
if (File(outputFolderPath).exists()) File(outputFolderPath).deleteRecursively()
}
/**
* Utility function to create the output folder structure for the simulation results.
* @param folderPath The path to the output folder
*/
public fun setupOutputFolderStructure(folderPath: String) {
val trackrPath = "$folderPath/trackr.json"
val simulationAnalysisPath = "$folderPath/simulation-analysis/"
val energyAnalysisPath = "$simulationAnalysisPath/power_draw/"
val emissionsAnalysisPath = "$simulationAnalysisPath/carbon_emission/"
File(folderPath).mkdir()
File(trackrPath).createNewFile()
File(simulationAnalysisPath).mkdir()
File(energyAnalysisPath).mkdir()
File(emissionsAnalysisPath).mkdir()
}
|