forked from ntt720/CloudSimPlus2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelSimulationsExample.java
264 lines (229 loc) · 10.3 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
/*
* 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-2021 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 ch.qos.logback.classic.Level;
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.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.utilizationmodels.UtilizationModelDynamic;
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 org.cloudsimplus.util.Log;
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
* <a href="https://www.oracle.com/technical-resources/articles/java/ma14-java-se-8-streams.html">Java 8 Lambda Expressions and Parallel Streams</a>.
* 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 logging 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 final CloudSim simulation;
private DatacenterBroker broker;
private List<Cloudlet> cloudletList;
private List<Vm> vmList;
private int hostsNumber;
private int vmsNumber;
private int cloudletsNumber;
/**
* 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 TO AVOID RUNTIME EXCEPTIONS.*/
Log.setLevel(Level.OFF);
final var simulationList = new ArrayList<ParallelSimulationsExample>(2);
//Creates the first simulation scenario
simulationList.add(
new ParallelSimulationsExample("Simulation 1")
.setHostsNumber(4)
.setVmsNumber(4)
.setCloudletsNumber(8)
);
//Creates the second simulation scenario
simulationList.add(
new ParallelSimulationsExample("Simulation 2")
.setHostsNumber(4)
.setVmsNumber(4)
.setCloudletsNumber(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.setLevel(Level.INFO);
System.out.printf("Time to run %d simulations: %d milliseconds%n", simulationList.size(), finishTimeMilliSec);
//Prints the cloudlet list of all executed simulations
simulationList.forEach(ParallelSimulationsExample::printResults);
}
private void printResults(){
new CloudletsTableBuilder(broker.getCloudletFinishedList())
.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()
*/
private ParallelSimulationsExample(final String title) {
this.title = title;
this.cloudletList = new ArrayList<>();
this.vmList = new ArrayList<>();
this.simulation = new CloudSim();
}
private List<Host> createHosts() {
final var hostList = new ArrayList<Host>(hostsNumber);
for(int i = 0; i < hostsNumber; i++) {
hostList.add(createHost());
}
return hostList;
}
/**
* Creates a Hosts using a {@link ResourceProvisionerSimple}
* for RAM and BW.
* @return
*/
private Host createHost() {
final long mips = 1000; // capacity of each CPU core (in Million Instructions per Second)
final long ram = 2048; // host memory (Megabyte)
final long storage = 1000000; // host storage (Megabyte)
final long bw = 10000; // in Megabits/s
final int pesNumber = 4;
final var peList = new ArrayList<Pe>(pesNumber); // List of CPU cores
for(int i = 0; i < pesNumber; i++) {
peList.add(new PeSimple(mips, new PeProvisionerSimple()));
}
return new HostSimple(ram, bw, storage, peList).setVmScheduler(new VmSchedulerTimeShared());
}
private void createVms() {
this.vmList = new ArrayList<>(vmsNumber);
for(int i = 0; i < vmsNumber; i++) {
this.vmList.add(createVm(i));
}
broker.submitVmList(vmList);
}
/**
* Creates a VM using a {@link CloudletSchedulerTimeShared} by default.
* @param id
* @return
*/
private Vm createVm(final int id) {
final long mips = 1000;
final long storage = 10000; // vm image size (Megabyte)
final int ram = 512; // vm memory (Megabyte)
final long bw = 1000; // vm bandwidth (Megabits/s)
final int pesNumber = 2; // number of CPU cores
return new VmSimple(id, mips, pesNumber)
.setRam(ram)
.setBw(bw)
.setSize(storage);
}
private void createCloudlets() {
this.cloudletList = new ArrayList<>(cloudletsNumber);
for(int i = 0; i < cloudletsNumber; i++) {
this.cloudletList.add(createCloudlet(i));
}
}
private Cloudlet createCloudlet(final int id) {
final long length = 10000; //in Million Instructions (MI)
final long fileSize = 300; //Size (in bytes) before execution
final long outputSize = 300; //Size (in bytes) after execution
final int pesNumber = 1;
// Defines how CPU, RAM and Bandwidth resources are used.
// Sets the same utilization model for all these resources.
final var utilizationModelFull = new UtilizationModelFull();
/* A utilization model for RAM and BW that uses only a fraction of the resource capacity all the time.
* If there are 10 cloudlets for a VM, each Cloudlet will use 10% (0.1) of the VM resource capacity. */
final var utilizationModelDynamic = new UtilizationModelDynamic(1.0/cloudletsNumber);
return new CloudletSimple(id, length, pesNumber)
.setFileSize(fileSize)
.setOutputSize(outputSize)
.setUtilizationModelCpu(utilizationModelFull)
.setUtilizationModelBw(utilizationModelDynamic)
.setUtilizationModelRam(utilizationModelDynamic);
}
/**
* Builds the simulation scenario and starts the simulation.
*/
@Override
public void run() {
final Datacenter datacenter0 = new DatacenterSimple(simulation, createHosts());
/* 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();
}
public ParallelSimulationsExample setCloudletsNumber(final int cloudletsNumber) {
this.cloudletsNumber = cloudletsNumber;
return this;
}
public ParallelSimulationsExample setVmsNumber(final int vmsNumber) {
this.vmsNumber = vmsNumber;
return this;
}
public ParallelSimulationsExample setHostsNumber(final int hostsNumber) {
this.hostsNumber = hostsNumber;
return this;
}
}