forked from ntt720/CloudSimPlus2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatacenterBrokerHeuristicExample.java
290 lines (251 loc) · 11.6 KB
/
DatacenterBrokerHeuristicExample.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
/*
* 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.brokers;
import ch.qos.logback.classic.Level;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.brokers.DatacenterBroker;
import org.cloudbus.cloudsim.brokers.DatacenterBrokerHeuristic;
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.distributions.UniformDistr;
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.UtilizationModel;
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.heuristics.CloudletToVmMappingHeuristic;
import org.cloudsimplus.heuristics.CloudletToVmMappingSimulatedAnnealing;
import org.cloudsimplus.heuristics.CloudletToVmMappingSolution;
import org.cloudsimplus.heuristics.HeuristicSolution;
import org.cloudsimplus.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* An example that uses a
* <a href="http://en.wikipedia.org/wiki/Simulated_annealing">Simulated Annealing</a>
* heuristic to find a suboptimal mapping between Cloudlets and Vm's submitted to a
* DatacenterBroker. The number of {@link Pe}s of Vm's and Cloudlets are defined
* randomly.
*
* <p>The {@link DatacenterBrokerHeuristic} is used
* with the {@link CloudletToVmMappingSimulatedAnnealing} class
* in order to find an acceptable solution with a high
* {@link HeuristicSolution#getFitness() fitness value}.</p>
*
* <p>Different {@link CloudletToVmMappingHeuristic} implementations can be used
* with the {@link DatacenterBrokerHeuristic} class.</p>
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 1.0
*/
public class DatacenterBrokerHeuristicExample {
private static final int HOSTS_TO_CREATE = 100;
private static final int VMS_TO_CREATE = 50;
private static final int CLOUDLETS_TO_CREATE = 100;
/**
* Simulated Annealing (SA) parameters.
*/
public static final double SA_INITIAL_TEMPERATURE = 1.0;
public static final double SA_COLD_TEMPERATURE = 0.0001;
public static final double SA_COOLING_RATE = 0.003;
public static final int SA_NUMBER_OF_NEIGHBORHOOD_SEARCHES = 50;
private final CloudSim simulation;
private final List<Cloudlet> cloudletList;
private List<Vm> vmList;
private CloudletToVmMappingSimulatedAnnealing heuristic;
/**
* Number of cloudlets created so far.
*/
private int createdCloudlets = 0;
/**
* Number of VMs created so far.
*/
private int createdVms = 0;
/**
* Number of hosts created so far.
*/
private int createdHosts = 0;
public static void main(String[] args) {
new DatacenterBrokerHeuristicExample();
}
/**
* Default constructor where the simulation is built.
*/
private DatacenterBrokerHeuristicExample() {
//Enables just some level of log messages.
Log.setLevel(Level.WARN);
System.out.println("Starting " + getClass().getSimpleName());
this.vmList = new ArrayList<>();
this.cloudletList = new ArrayList<>();
simulation = new CloudSim();
final Datacenter datacenter0 = createDatacenter();
DatacenterBrokerHeuristic broker0 = createBroker();
createAndSubmitVms(broker0);
createAndSubmitCloudlets(broker0);
simulation.start();
final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
new CloudletsTableBuilder(finishedCloudlets).build();
print(broker0);
}
private DatacenterBrokerHeuristic createBroker() {
createSimulatedAnnealingHeuristic();
final DatacenterBrokerHeuristic broker0 = new DatacenterBrokerHeuristic(simulation);
broker0.setHeuristic(heuristic);
return broker0;
}
private void createAndSubmitCloudlets(final DatacenterBrokerHeuristic broker0) {
for(int i = 0; i < CLOUDLETS_TO_CREATE; i++){
cloudletList.add(createCloudlet(broker0, getRandomPesNumber(4)));
}
broker0.submitCloudletList(cloudletList);
}
private void createAndSubmitVms(final DatacenterBrokerHeuristic broker0) {
vmList = new ArrayList<>(VMS_TO_CREATE);
for(int i = 0; i < VMS_TO_CREATE; i++){
vmList.add(createVm(broker0, getRandomPesNumber(4)));
}
broker0.submitVmList(vmList);
}
private void createSimulatedAnnealingHeuristic() {
heuristic =
new CloudletToVmMappingSimulatedAnnealing(SA_INITIAL_TEMPERATURE, new UniformDistr(0, 1));
heuristic.setColdTemperature(SA_COLD_TEMPERATURE);
heuristic.setCoolingRate(SA_COOLING_RATE);
heuristic.setSearchesByIteration(SA_NUMBER_OF_NEIGHBORHOOD_SEARCHES);
}
private void print(final DatacenterBrokerHeuristic broker0) {
final double roundRobinMappingCost = computeRoundRobinMappingCost();
printSolution(
"Heuristic solution for mapping cloudlets to Vm's ",
heuristic.getBestSolutionSoFar(), false);
System.out.printf(
"\tThe heuristic solution cost represents %.2f%% of the round robin mapping cost used by the DatacenterBrokerSimple%n",
heuristic.getBestSolutionSoFar().getCost()*100.0/roundRobinMappingCost);
System.out.printf("\tThe solution finding spend %.2f seconds to finish%n", broker0.getHeuristic().getSolveTime());
System.out.println("\tSimulated Annealing Parameters");
System.out.printf("\t\tNeighborhood searches by iteration: %d%n", SA_NUMBER_OF_NEIGHBORHOOD_SEARCHES);
System.out.printf("\t\tInitial Temperature: %18.6f%n", SA_INITIAL_TEMPERATURE);
System.out.printf("\t\tCooling Rate : %18.6f%n", SA_COOLING_RATE);
System.out.printf("\t\tCold Temperature : %18.6f%n%n", SA_COLD_TEMPERATURE);
System.out.println(getClass().getSimpleName() + " finished!");
}
/**
* Randomly gets a number of PEs (CPU cores).
*
* @param maxPesNumber the maximum value to get a random number of PEs
* @return the randomly generated PEs number
*/
private int getRandomPesNumber(final int maxPesNumber) {
return heuristic.getRandomValue(maxPesNumber)+1;
}
private DatacenterSimple createDatacenter() {
final List<Host> hostList = new ArrayList<>();
for(int i = 0; i < HOSTS_TO_CREATE; i++) {
hostList.add(createHost());
}
return new DatacenterSimple(simulation, hostList, new VmAllocationPolicySimple());
}
private Host createHost() {
final long mips = 1000; // capacity of each CPU core (in Million Instructions per Second)
final int ram = 2048; // host memory (Megabyte)
final long storage = 1000000; // host storage
final long bw = 10000;
final List<Pe> peList = new ArrayList<>();
/*Creates the Host's CPU cores and defines the provisioner
used to allocate each core for requesting VMs.*/
for(int i = 0; i < 8; 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 Vm createVm(final DatacenterBroker broker, final int pesNumber) {
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
return new VmSimple(createdVms++, mips, pesNumber)
.setRam(ram).setBw(bw).setSize(storage)
.setCloudletScheduler(new CloudletSchedulerTimeShared());
}
private Cloudlet createCloudlet(final DatacenterBroker broker, final int numberOfPes) {
final long length = 400000; //in Million Instructions (MI)
final long fileSize = 300; //Size (in bytes) before execution
final long outputSize = 300; //Size (in bytes) after execution
final UtilizationModel utilizationFull = new UtilizationModelFull();
final UtilizationModel utilizationDynamic = new UtilizationModelDynamic(0.1);
return new CloudletSimple(createdCloudlets++, length, numberOfPes)
.setFileSize(fileSize)
.setOutputSize(outputSize)
.setUtilizationModelCpu(utilizationFull)
.setUtilizationModelRam(utilizationDynamic)
.setUtilizationModelBw(utilizationDynamic);
}
private double computeRoundRobinMappingCost() {
final CloudletToVmMappingSolution roundRobinSolution = new CloudletToVmMappingSolution(heuristic);
int i = 0;
for (Cloudlet c : cloudletList) {
//cyclically selects a Vm (as in a circular queue)
roundRobinSolution.bindCloudletToVm(c, vmList.get(i));
i = (i+1) % vmList.size();
}
printSolution(
"Round robin solution used by DatacenterBrokerSimple class",
roundRobinSolution, false);
return roundRobinSolution.getCost();
}
private void printSolution(
final String title,
final CloudletToVmMappingSolution solution,
final boolean showIndividualCloudletFitness)
{
System.out.printf("%n%s (cost %.2f fitness %.6f)%n",
title, solution.getCost(), solution.getFitness());
if(!showIndividualCloudletFitness)
return;
for(Map.Entry<Cloudlet, Vm> e: solution.getResult().entrySet()){
System.out.printf(
"Cloudlet %3d (%d PEs, %6d MI) mapped to Vm %3d (%d PEs, %6.0f MIPS)%n",
e.getKey().getId(),
e.getKey().getNumberOfPes(), e.getKey().getLength(),
e.getValue().getId(),
e.getValue().getNumberOfPes(), e.getValue().getMips());
}
System.out.println();
}
}