forked from cloudsimplus/cloudsimplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PauseSimulationAtGivenTimeExample1.java
271 lines (238 loc) · 11.7 KB
/
PauseSimulationAtGivenTimeExample1.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
/*
* 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.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.CloudletSchedulerSpaceShared;
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.listeners.EventInfo;
import org.cloudsimplus.listeners.EventListener;
import org.cloudsimplus.listeners.CloudletVmEventInfo;
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
import java.util.ArrayList;
import java.util.List;
/**
* An example showing how to pause the simulation at a given time in order to collect
* some partial results. In this example, such results are the cloudlets that have finished so far.
* The example creates 4 Cloudlets that will run sequentially using a {@link CloudletSchedulerSpaceShared}.
* The pause is scheduled before the simulation starts, that is the easier way to perform
* such an action.
*
* <p>This example uses CloudSim Plus Listener features to intercept when
* the simulation was paused, allowing to collect the desired data.
* This example uses the Java 8 Lambda Functions features
* to pass a listener to a {@link CloudSim} instance, by means of the
* {@link CloudSim#addOnSimulationPausedListener(EventListener)} method.
* However, the same feature can be used for Java 7 passing an anonymous class
* that implements {@code EventListener<EventInfo>}.</p>
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 1.0
*
* @see CloudSim#pause(double)
* @see CloudSim#addOnSimulationPausedListener(EventListener)
* @see EventListener
*/
public class PauseSimulationAtGivenTimeExample1 {
/**
* The interval in which the Datacenter will schedule events.
* As lower is this interval, sooner the processing of VMs and Cloudlets
* is updated and you will get more notifications about the simulation execution.
* However, as higher is this value, it can affect the simulation performance.
*
* <p>For this example, a large schedule interval such as 15 will make that just
* at every 15 seconds the processing of Cloudlets is updated.
* Consider that each Cloudlet takes 10 seconds to finish and that
* the simulation is paused at time 22. With a scheduling interval of 15 seconds,
* at the time 22, the Cloudlets execution will be updated just 1 time, that means
* if we get the list of finished cloudlets at time 22, it will not be updated yet with
* the second Cloudlet that finished at time 20.</p>
*
* <p>Considering this characteristics, the scheduling interval
* was set to a lower value to get updates as soon as possible.
* For more details, see {@link Datacenter#getSchedulingInterval()}.</p>
*/
public static final int SCHEDULING_INTERVAL = 1;
private final CloudSim simulation;
private final DatacenterBrokerSimple broker;
private List<Cloudlet> cloudletList;
private List<Vm> vmList;
private int numberOfCreatedCloudlets = 0;
private int numberOfCreatedVms = 0;
private int numberOfCreatedHosts = 0;
/**
* Starts the simulation.
* @param args
*/
public static void main(String[] args) {
new PauseSimulationAtGivenTimeExample1();
}
/**
* Default constructor that builds the simulation.
*/
public PauseSimulationAtGivenTimeExample1() {
Log.printFormattedLine("Starting %s ...", getClass().getSimpleName());
this.vmList = new ArrayList<>();
this.cloudletList = new ArrayList<>();
this.simulation = new CloudSim();
Datacenter datacenter0 = createDatacenter();
/*
Creates a Broker accountable for submission of VMs and Cloudlets
on behalf of a given cloud user (customer).
*/
this.broker = new DatacenterBrokerSimple(simulation);
Vm vm0 = createVm();
this.vmList.add(vm0);
this.broker.submitVmList(vmList);
for(int i = 0; i < 4; i++) {
Cloudlet cloudlet = createCloudlet(vm0);
this.cloudletList.add(cloudlet);
}
this.broker.submitCloudletList(cloudletList);
/*
Requests the simulation to be paused at the 22nd second.
The method schedules the simulation pause for a future time and returns immediately.
Since the start() method blocks untiil the simulation is finished,
the request to pause the simulation at a given time has to be scheduled
before the simulation start.
Otherwise, more complex mechanisms have to be used
to schedule a pause when the simulation is already running.
*/
this.simulation.pause(22);
/*
* Sets a Listener that will be notified when the simulation is paused.
* Realise that it is being used Java 8 Lambda Expressions to define a Listener
* that will be executed only when the simulation is paused.
* */
this.simulation
.addOnSimulationPausedListener(pauseInfo -> printCloudletsFinishedSoFarAndResumeSimulation(pauseInfo));
/* Starts the simulation and waits all cloudlets to be executed. */
this.simulation.start();
/*Prints results when the simulation is over
(you can use your own code here to print what you want from this cloudlet list)*/
printsListOfFinishedCloudlets("Finished cloudlets after simulation is complete");
Log.printConcatLine(getClass().getSimpleName(), " finished!");
}
private void printCloudletsFinishedSoFarAndResumeSimulation(EventInfo pauseInfo) {
Log.printFormattedLine("\n#Simulation paused at %.2f second", pauseInfo.getTime());
printsListOfFinishedCloudlets("Cloudlets Finished So Far");
this.simulation.resume();
}
private void printsListOfFinishedCloudlets(String title) {
//Gets the list of cloudlets finished so far a prints
new CloudletsTableBuilder(broker.getCloudletFinishedList())
.setTitle(title)
.build();
}
/**
* Checks if the Cloudlet that had its processing updated reached 50% of execution.
* If so, request the simulation interruption.
* @param event object containing data about the happened event
*/
private void onClouletProcessingUpdate(CloudletVmEventInfo event) {
if(event.getCloudlet().getFinishedLengthSoFar() >= event.getCloudlet().getLength()/2.0){
Log.printFormattedLine("Cloudlet %d reached 50% of execution. Intentionally requesting termination of the simulation at time %.2f",
event.getCloudlet().getId(), simulation.clock());
simulation.terminate();
}
}
private DatacenterSimple createDatacenter() {
List<Host> hostList = new ArrayList<>();
Host host0 = createHost();
hostList.add(host0);
//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);
DatacenterSimple dc = new DatacenterSimple(simulation, characteristics, new VmAllocationPolicySimple());
dc.setSchedulingInterval(SCHEDULING_INTERVAL);
return dc;
}
private Host createHost() {
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
List<Pe> peList = new ArrayList<>(); //List of CPU cores
/*Creates the Host's CPU cores and defines the provisioner
used to allocate each core for requesting VMs.*/
peList.add(new PeSimple(mips, new PeProvisionerSimple()));
return new HostSimple(ram, bw, storage, peList)
.setRamProvisioner(new ResourceProvisionerSimple())
.setBwProvisioner(new ResourceProvisionerSimple())
.setVmScheduler(new VmSchedulerTimeShared());
}
private Vm createVm() {
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 = 1; // number of CPU cores
return new VmSimple(numberOfCreatedVms++, mips, pesNumber)
.setRam(ram)
.setBw(bw)
.setSize(storage)
.setCloudletScheduler(new CloudletSchedulerSpaceShared());
}
private Cloudlet createCloudlet(Vm vm) {
long length = 10000; //in Million Structions (MI)
long fileSize = 300; //Size (in bytes) before execution
long outputSize = 300; //Size (in bytes) after execution
long numberOfCpuCores = vm.getNumberOfPes(); //cloudlet will use all the VM's CPU cores
//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(
numberOfCreatedCloudlets++, length, numberOfCpuCores)
.setFileSize(fileSize)
.setOutputSize(outputSize)
.setUtilizationModel(utilization)
.setVm(vm);
}
}