forked from cloudsimplus/cloudsimplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelSimulationsExample.java
299 lines (258 loc) · 11.4 KB
/
ParallelSimulationsExample.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* CloudSim Plus: A modern, highly-extensible and easier-to-use Framework for
* Modeling and Simulation of Cloud Computing Infrastructures and Services.
* http://cloudsimplus.org
*
* Copyright (C) 2015-2016 Universidade da Beira Interior (UBI, Portugal) and
* the Instituto Federal de Educação Ciência e Tecnologia do Tocantins (IFTO, Brazil).
*
* This file is part of CloudSim Plus.
*
* CloudSim Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CloudSim Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CloudSim Plus. If not, see <http://www.gnu.org/licenses/>.
*/
package org.cloudsimplus.examples;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.brokers.DatacenterBroker;
import org.cloudbus.cloudsim.brokers.DatacenterBrokerSimple;
import org.cloudbus.cloudsim.cloudlets.Cloudlet;
import org.cloudbus.cloudsim.cloudlets.CloudletSimple;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.datacenters.Datacenter;
import org.cloudbus.cloudsim.datacenters.DatacenterCharacteristics;
import org.cloudbus.cloudsim.datacenters.DatacenterCharacteristicsSimple;
import org.cloudbus.cloudsim.datacenters.DatacenterSimple;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.HostSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.ResourceProvisionerSimple;
import org.cloudbus.cloudsim.resources.Pe;
import org.cloudbus.cloudsim.resources.PeSimple;
import org.cloudbus.cloudsim.schedulers.cloudlet.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.schedulers.vm.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.util.Log;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModel;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModelFull;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
import java.util.ArrayList;
import java.util.List;
/**
* An example showing how to run two simulation scenarios with different configurations
* in parallel, using the CloudSim Plus exclusive features and Java 8 Lambda Expressions and Parallel Streams.
* The parallel execution of simulations doesn't ensure that the overall execution time will be reduced.
* It depends on diverse factors such as the number of simulations, the tasks that each simulation
* executes, the number of available physical CPUs at your machine, etc.
*
* <p>This is just a minimal example that runs 2 simulations with different number
* of Hosts, VMs and Cloudlets. However, all these objects will have the same configurations
* in the different simulations. There will be just a difference in the number of created
* objects.</p>
*
* <p>To enable such a parallel simulations execution, the {@link Log} must be
* disable (as shown in the first line of the main method) and any console
* output should be avoided during simulation execution.
* Further, usage of static mutable attributes must be avoided.
* All simulation attributes must be instance attributes and one
* simulation run (a simulation instance) should not share data with other ones.</p>
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 1.0
*/
public class ParallelSimulationsExample implements Runnable {
private final String title;
private CloudSim simulation;
private DatacenterBroker broker;
private List<Cloudlet> cloudletList;
private List<Vm> vmList;
private List<Cloudlet> finishedCloudletList;
private int hostsToCreate;
private int vmsToCreate;
private int cloudletsToCreate;
/**
* Creates the simulation scenarios with different configurations and execute them,
* printing the results for each one after all simulations have finished.
*
* @param args
*/
public static void main(String[] args) {
//IT IS MANDATORY TO DISABLE THE LOG WHEN EXECUTING PARALLEL SIMULATIONS.
Log.disable();
List<ParallelSimulationsExample> simulationList = new ArrayList<>(2);
//Creates the first simulation scenario
simulationList.add(
new ParallelSimulationsExample("Simulation 1")
.setHostsToCreate(4)
.setVmsToCreate(4)
.setCloudletsToCreate(8)
);
//Creates the second simulation scenario
simulationList.add(
new ParallelSimulationsExample("Simulation 2")
.setHostsToCreate(4)
.setVmsToCreate(4)
.setCloudletsToCreate(16)
);
final long startTimeMilliSec = System.currentTimeMillis();
//Uses Java 8 Streams to execute the simulation scenarios in parallel.
// tag::parallelExecution[]
simulationList.parallelStream().forEach(ParallelSimulationsExample::run);
// end::parallelExecution[]
final long finishTimeMilliSec = System.currentTimeMillis() - startTimeMilliSec;
Log.enable();
Log.printFormattedLine("Time to run %d simulations: %d milliseconds", simulationList.size(), finishTimeMilliSec);
//Prints the cloudlet list of all executed simulations
simulationList.forEach(ParallelSimulationsExample::printResults);
}
public void printResults(){
new CloudletsTableBuilder(getFinishedCloudletList())
.setTitle(this.title)
.build();
}
/**
* Default constructor where the simulation is initialized.
* @param title a title for the simulation scenario (just for log purposes)
* @see #run()
*/
public ParallelSimulationsExample(String title) {
//Number of cloud customers
int numberOfCloudUsers = 1;
this.title = title;
this.cloudletList = new ArrayList<>();
this.finishedCloudletList = new ArrayList<>();
this.vmList = new ArrayList<>();
this.simulation = new CloudSim();
}
private DatacenterSimple createDatacenter() {
List<Host> hostList = createHosts();
//Defines the characteristics of the data center
double cost = 3.0; // the cost of using processing in this Datacenter
double costPerMem = 0.05; // the cost of using memory in this Datacenter
double costPerStorage = 0.001; // the cost of using storage in this Datacenter
double costPerBw = 0.0; // the cost of using bw in this Datacenter
DatacenterCharacteristics characteristics =
new DatacenterCharacteristicsSimple(hostList)
.setCostPerSecond(cost)
.setCostPerMem(costPerMem)
.setCostPerStorage(costPerStorage)
.setCostPerBw(costPerBw);
return new DatacenterSimple(simulation, characteristics, new VmAllocationPolicySimple());
}
private List<Host> createHosts() {
List<Host> hostList = new ArrayList<>(hostsToCreate);
for(int i = 0; i < hostsToCreate; i++) {
Host host = createHost(i);
hostList.add(host);
}
return hostList;
}
private Host createHost(int hostId) {
long mips = 1000; // capacity of each CPU core (in Million Instructions per Second)
long ram = 2048; // host memory (MEGABYTE)
long storage = 1000000; // host storage (MEGABYTE)
long bw = 10000; //in Megabits/s
final int numberOfPes = 4;
List<Pe> peList = new ArrayList<>(numberOfPes); //List of CPU cores
for(int i = 0; i < numberOfPes; i++) {
peList.add(new PeSimple(mips, new PeProvisionerSimple()));
}
return new HostSimple(ram, bw, storage, peList)
.setRamProvisioner(new ResourceProvisionerSimple())
.setBwProvisioner(new ResourceProvisionerSimple())
.setVmScheduler(new VmSchedulerTimeShared());
}
private void createVms() {
this.vmList = new ArrayList<>(vmsToCreate);
for(int i = 0; i < vmsToCreate; i++) {
Vm vm0 = createVm(broker, i);
this.vmList.add(vm0);
}
broker.submitVmList(vmList);
}
private Vm createVm(DatacenterBroker broker, int vmId) {
long mips = 1000;
long storage = 10000; // vm image size (MEGABYTE)
int ram = 512; // vm memory (MEGABYTE)
long bw = 1000; // vm bandwidth (Megabits/s)
int pesNumber = 2; // number of CPU cores
return new VmSimple(vmId, mips, pesNumber)
.setRam(ram)
.setBw(bw)
.setSize(storage)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
}
private void createCloudlets() {
this.cloudletList = new ArrayList<>(cloudletsToCreate);
for(int i = 0; i < cloudletsToCreate; i++) {
Cloudlet cloudlet = createCloudlet(broker, i);
this.cloudletList.add(cloudlet);
}
}
private Cloudlet createCloudlet(DatacenterBroker broker, int cloudletId) {
long length = 10000; //in Million Structions (MI)
long fileSize = 300; //Size (in bytes) before execution
long outputSize = 300; //Size (in bytes) after execution
int numberOfCpuCores = 1;
//Defines how CPU, RAM and Bandwidth resources are used
//Sets the same utilization model for all these resources.
UtilizationModel utilization = new UtilizationModelFull();
return new CloudletSimple(cloudletId, length, numberOfCpuCores)
.setFileSize(fileSize)
.setOutputSize(outputSize)
.setUtilizationModel(utilization);
}
/**
* Builds the simulation scenario and starts the simulation.
*/
@Override
public void run() {
Datacenter datacenter0 = createDatacenter();
/*Creates a Broker accountable for submission of VMs and Cloudlets
on behalf of a given cloud user (customer).*/
broker = new DatacenterBrokerSimple(simulation);
createVms();
createCloudlets();
broker.submitCloudletList(cloudletList);
/*Starts the simulation and waits all cloudlets to be executed*/
simulation.start();
this.finishedCloudletList = broker.getCloudletFinishedList();
}
public int getCloudletsToCreate() {
return cloudletsToCreate;
}
public ParallelSimulationsExample setCloudletsToCreate(int cloudletsToCreate) {
this.cloudletsToCreate = cloudletsToCreate;
return this;
}
public int getVmsToCreate() {
return vmsToCreate;
}
public ParallelSimulationsExample setVmsToCreate(int vmsToCreate) {
this.vmsToCreate = vmsToCreate;
return this;
}
public int getHostsToCreate() {
return hostsToCreate;
}
public ParallelSimulationsExample setHostsToCreate(int hostsToCreate) {
this.hostsToCreate = hostsToCreate;
return this;
}
public List<Cloudlet> getFinishedCloudletList() {
return finishedCloudletList;
}
public String getTitle() {
return title;
}
}