forked from ntt720/CloudSimPlus2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicCreationOfVmsAndCloudletsExample.java
220 lines (190 loc) · 9.28 KB
/
DynamicCreationOfVmsAndCloudletsExample.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
/*
* 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.dynamic;
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.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.VmSchedulerSpaceShared;
import org.cloudbus.cloudsim.schedulers.vm.VmSchedulerTimeShared;
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 org.cloudsimplus.listeners.CloudletVmEventInfo;
import org.cloudsimplus.listeners.EventListener;
import java.util.ArrayList;
import java.util.List;
/**
* An example showing how to dynamically create VMs and Cloudlets during simulation
* executing using the exclusive CloudSim Plus {@link DatacenterBroker} implementations
* and Listener features. Using such features, <b>it is not required to create DatacenterBrokers in runtime
* in order to allow dynamic submission of VMs and Cloudlets.</b>
*
* <p>This example uses CloudSim Plus Listener features to intercept when
* the first Cloudlet finishes its execution to then request
* the creation of new VMs and Cloudlets. It uses the Java 8 Lambda Functions features
* to pass a listener to the mentioned Cloudlet, by means of the
* {@link Cloudlet#addOnFinishListener(EventListener)} method.</p>
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 1.0
*
* @see Cloudlet#addOnFinishListener(EventListener)
* @see EventListener
*/
public class DynamicCreationOfVmsAndCloudletsExample {
private final CloudSim simulation;
private final DatacenterBrokerSimple broker0;
private List<Cloudlet> cloudletList;
private List<Vm> vmList;
private int numberOfCreatedCloudlets = 0;
private int numberOfCreatedVms = 0;
private int numberOfHosts = 1;
public static void main(String[] args) {
new DynamicCreationOfVmsAndCloudletsExample();
}
/**
* Default constructor that builds the simulation.
*/
private DynamicCreationOfVmsAndCloudletsExample() {
/*Enables just some level of log messages.
Make sure to import org.cloudsimplus.util.Log;*/
//Log.setLevel(ch.qos.logback.classic.Level.WARN);
System.out.println("Starting " + getClass().getSimpleName());
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).*/
broker0 = new DatacenterBrokerSimple(simulation);
final int vmsToCreate = 2;
final int cloudletsToCreateByVm = 2;
this.vmList = new ArrayList<>(vmsToCreate);
this.cloudletList = new ArrayList<>(vmsToCreate*cloudletsToCreateByVm);
createAndSubmitVmsAndCloudlets(vmsToCreate, cloudletsToCreateByVm);
/* Assigns an EventListener to be notified when the first Cloudlets finishes executing
* and then dynamically create a new list of VMs and Cloudlets to submit to the broker.*/
Cloudlet cloudlet0 = this.cloudletList.get(0);
cloudlet0.addOnFinishListener(this::submitNewVmsAndCloudletsToBroker);
/* Starts the simulation and waits all cloudlets to be executed. */
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)*/
List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();
System.out.println(getClass().getSimpleName() + " finished!");
}
private void createAndSubmitVmsAndCloudlets(int vmsToCreate, int cloudletsToCreateForEachVm) {
List<Vm> newVmList = new ArrayList<>(vmsToCreate);
List<Cloudlet> newCloudletList = new ArrayList<>(vmsToCreate*cloudletsToCreateForEachVm);
for (int i = 0; i < vmsToCreate; i++) {
Vm vm = createVm(broker0);
newVmList.add(vm);
for(int j = 0; j < cloudletsToCreateForEachVm; j++) {
Cloudlet cloudlet = createCloudlet(broker0, vm);
newCloudletList.add(cloudlet);
}
}
this.vmList.addAll(newVmList);
this.cloudletList.addAll(newCloudletList);
broker0.submitVmList(newVmList);
broker0.submitCloudletList(newCloudletList);
}
/**
* Dynamically creates and submits a set of VMs to the broker when
* the first cloudlet finishes.
* @param eventInfo information about the fired event
*/
private void submitNewVmsAndCloudletsToBroker(CloudletVmEventInfo eventInfo) {
final int numberOfNewVms = 2;
final int numberOfCloudletsByVm = 4;
System.out.printf(
"%n\t# Cloudlet %d finished. Submitting %d new VMs to the broker%n",
eventInfo.getCloudlet().getId(), numberOfNewVms);
createAndSubmitVmsAndCloudlets(numberOfNewVms, numberOfCloudletsByVm);
}
private DatacenterSimple createDatacenter() {
final int numberOfHosts = this.numberOfHosts;
List<Host> hostList = new ArrayList<>(numberOfHosts);
for (int i = 0; i < numberOfHosts; i++) {
Host host = createHost();
hostList.add(host);
}
return new DatacenterSimple(simulation, hostList, new VmAllocationPolicySimple());
}
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
final int numberOfPes = 8;
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 VmSchedulerSpaceShared());
}
private Vm createVm(DatacenterBroker broker) {
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(DatacenterBroker broker, Vm vm) {
long length = 10000; //in Million Instructions (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);
}
}