-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimulationRunner.java
235 lines (217 loc) · 10.2 KB
/
SimulationRunner.java
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
/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation
* of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.examples.SibSUTIS;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import com.sun.istack.internal.Nullable;
import javafx.util.Pair;
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.examples.SibSUTIS.utils.ExtendedDatacenter;
import org.cloudbus.cloudsim.examples.SibSUTIS.utils.ExtendedDatacenterBrocker;
import org.cloudbus.cloudsim.examples.SibSUTIS.utils.ExtendedHelper;
import org.cloudbus.cloudsim.examples.SibSUTIS.utils.MojosHelper;
import org.cloudbus.cloudsim.examples.power.Constants;
import org.cloudbus.cloudsim.examples.power.random.RandomHelper;
import org.cloudbus.cloudsim.power.*;
/*TODO
1.Random
2.First FIt
3.Round RObin
4. FFD Prod
5. FFD Sum
6. Dot-Product
7. Norm-based Greedy
*/
public class SimulationRunner {
static List<List<Pair<Double,Integer>>> totalResultList;
public static class SimulationConfiguration {
//Number of hosts in your simulation
public int numberOfHosts;
public int minVmsInSimulation;
public int maxVmsInSimulation;
public int simulationStep;
public boolean useMojosData = false;
public String outputPath = "/tmp/cloudsim/results";
public List <Class<? extends VmAllocationPolicy>> vmAllocationPolicies;
public List <String> inputFiles;
public boolean isValid() {
return (numberOfHosts > 0
&& minVmsInSimulation <= maxVmsInSimulation
&& maxVmsInSimulation > 0
&& simulationStep > 0
&& vmAllocationPolicies != null
&& vmAllocationPolicies.size() > 0
&& inputFiles != null
&& inputFiles.size() > 0
&& outputPath.length() > 0
);
}
}
public static void doSimulationStep(@Nullable String inputFile, int stepNumber, SimulationConfiguration config) {
File outputFolder = new File(config.outputPath + "/simulation_"+stepNumber);
if (!outputFolder.exists()) {
outputFolder.mkdirs();
}
int policyIndex = -1;
for (Class<? extends VmAllocationPolicy> vmallocationPolicyClass : config.vmAllocationPolicies) {
List<Pair<Double, Integer>> resultList = new ArrayList<Pair<Double, Integer>>();
PrintWriter logFileWriter = null;
policyIndex++;
try {
logFileWriter = new PrintWriter(
outputFolder.getAbsolutePath() + "/" +
vmallocationPolicyClass.getSimpleName() + ".txt");
} catch (FileNotFoundException e) {
new RuntimeException("Cannot open log file: " + e.getMessage());
}
for (int i = config.minVmsInSimulation, numbeOfStep = 0;
i <= config.maxVmsInSimulation;
i += config.simulationStep, numbeOfStep++) {
//Simulation body
VmAllocationPolicy policy = null;
List<PowerHost> hostList = null;
ExtendedDatacenterBrocker broker = null;
List<Cloudlet> cloudletList = null;
List<Vm> vmList = null;
try {
int numberOfVms = i;
CloudSim.init(1, Calendar.getInstance(), false);
if (vmallocationPolicyClass == VmAllocationPolicyFFDProd.class
|| vmallocationPolicyClass == VmAllocationPolicyFFDSum.class
|| vmallocationPolicyClass == VmAllocationPolicyNBG.class ) {
broker = ExtendedHelper.createExtendedBrocker(ExtendedDatacenterBrocker.VM_ALLOCATION_MODE_LIST);
} else {
broker = ExtendedHelper.createExtendedBrocker(ExtendedDatacenterBrocker.VM_ALLOCATION_MODE_STANDART);
}
int brokerId = broker.getId();
cloudletList = RandomHelper.createCloudletList(
brokerId,
numberOfVms);
if (inputFile == null) {
vmList = ExtendedHelper.createVmList(brokerId, cloudletList.size());
} else {
vmList = MojosHelper.createVmList(brokerId, cloudletList.size(), inputFile);
}
hostList = ExtendedHelper.createHostList(config.numberOfHosts);
Constructor<?> cons = vmallocationPolicyClass.getConstructor(List.class);
policy = (VmAllocationPolicy) cons.newInstance(hostList);
ExtendedDatacenter datacenter = (ExtendedDatacenter) ExtendedHelper.createDatacenter(
"Datacenter",
ExtendedDatacenter.class,
hostList,
policy);
datacenter.setDisableMigrations(true);
broker.submitVmList(vmList);
broker.submitCloudletList(cloudletList);
CloudSim.terminateSimulation(Constants.SIMULATION_LIMIT);
double lastClock = CloudSim.startSimulation();
double energy = datacenter.getPower() / (3600 * 1000);
resultList.add(new Pair<Double, Integer>(energy, datacenter.getMaximumUsedHostsCount()));
Pair<Double, Integer> pair = totalResultList.get(policyIndex).get(numbeOfStep);
totalResultList.get(policyIndex).set(numbeOfStep, new Pair<Double, Integer>(energy + pair.getKey(),
datacenter.getMaximumUsedHostsCount() + pair.getValue()));
} catch (Exception e) {
Log.printLine("Sumulation has been terminated due to unexpected exception: " +
e.getMessage());
e.printStackTrace();
System.exit(-1);
}
}
for (int i = config.minVmsInSimulation, j = 0;
i <= config.maxVmsInSimulation && j < resultList.size();
i += config.simulationStep, j++) {
Log.printLine("Res for vms[" + i + "]: \n" +
"Energy consumption: " + resultList.get(j).getKey() + " kWh\n" +
"Used hosts: " + resultList.get(j).getValue() + "\n");
logFileWriter.println("[" + i + "]: " + resultList.get(j).getKey() + " KWh;"
+ " hosts: " + resultList.get(j).getValue());
}
logFileWriter.close();
}
}
public static void startSimulation (SimulationConfiguration config) {
if (!config.isValid()) {
throw new RuntimeException("Simulation config isn't valid");
}
totalResultList = new ArrayList<List<Pair<Double,Integer>>>();
for (int i = 0 ;i < config.vmAllocationPolicies.size(); i++) {
List<Pair<Double, Integer>> list = new ArrayList<Pair<Double,Integer>>();
for(int j = config.minVmsInSimulation;
j <= config.maxVmsInSimulation;
j += config.simulationStep) {
list.add(new Pair<Double,Integer>(new Double(0), new Integer(0)));
}
totalResultList.add(list);
}
if (config.useMojosData) {
for (int i = 0; i < config.inputFiles.size(); i++)
doSimulationStep(config.inputFiles.get(i), i, config);
} else {
doSimulationStep(null, 0, config);
}
saveAverage(config);
}
public static void saveAverage(SimulationConfiguration config) {
File outputFolder = new File(config.outputPath + "/simulation_average");
if (!outputFolder.exists()) {
outputFolder.mkdirs();
}
int i = -1;
for (Class<? extends VmAllocationPolicy> vmallocationPolicyClass : config.vmAllocationPolicies) {
i++;
PrintWriter logFileWriter = null;
try {
logFileWriter = new PrintWriter(
outputFolder.getAbsolutePath() + "/" +
vmallocationPolicyClass.getSimpleName() + ".txt");
} catch (FileNotFoundException e) {
new RuntimeException("Cannot open log file: " + e.getMessage());
}
for (int j = config.minVmsInSimulation, numberOfStep = 0;
j <= config.maxVmsInSimulation;
j += config.simulationStep, numberOfStep++) {
logFileWriter.println("[" + i + "]: " + totalResultList.get(i).get(numberOfStep).getKey()/config.inputFiles.size() + " KWh;"
+ " hosts: " + 1.0*totalResultList.get(i).get(numberOfStep).getValue()/config.inputFiles.size());
}
logFileWriter.close();
}
}
public static void main(String[] args) throws IOException {
SimulationConfiguration config = new SimulationConfiguration();
config.numberOfHosts = 250;
config.minVmsInSimulation = 100;
config.simulationStep = 100;
config.maxVmsInSimulation = 1000;
//Specify your input files here
config.useMojosData = true;
config.inputFiles = Arrays.asList(
"/tmp/mojos/test1.xml",
"/tmp/mojos/test2.xml",
"/tmp/mojos/test3.xml"
);
//Specify vm allocation policies which you're want to use
config.vmAllocationPolicies = Arrays.asList(
VmAllocationPolicyRandom.class,
VmAllocationPolicyRoundRobin.class,
VmAllocationPolicyFirstFit.class,
VmAllocationPolicyNBG.class,
VmAllocationPolicyFFDProd.class,
VmAllocationPolicyFFDSum.class,
VmAllocationPolicyDotProduct.class
);
startSimulation(config);
}
}