Skip to content

Commit 89548db

Browse files
committed
Close #22 :: Upgrade to CloudSim Plus 8.5.0
Introduces DatacenterBrokerBatchVmCreationRequestExample Signed-off-by: Manoel Campos <[email protected]>
1 parent 955a503 commit 89548db

File tree

2 files changed

+212
-1
lines changed

2 files changed

+212
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
The module version defines the version of CloudSim Plus to be used.
99
This way, it cannot be a version number which doesn't exist for CloudSim Plus.
1010
-->
11-
<version>8.4.0</version>
11+
<version>8.5.0</version>
1212

1313
<name>CloudSim Plus Examples</name>
1414
<description>
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
* CloudSim Plus: A modern, highly-extensible and easier-to-use Framework for
3+
* Modeling and Simulation of Cloud Computing Infrastructures and Services.
4+
* http://cloudsimplus.org
5+
*
6+
* Copyright (C) 2015-2021 Universidade da Beira Interior (UBI, Portugal) and
7+
* the Instituto Federal de Educação Ciência e Tecnologia do Tocantins (IFTO, Brazil).
8+
*
9+
* This file is part of CloudSim Plus.
10+
*
11+
* CloudSim Plus is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* CloudSim Plus is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with CloudSim Plus. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
package org.cloudsimplus.examples.brokers;
25+
26+
import org.cloudsimplus.allocationpolicies.VmAllocationPolicyBatchPlacementUnderloadedHosts;
27+
import org.cloudsimplus.brokers.DatacenterBroker;
28+
import org.cloudsimplus.brokers.DatacenterBrokerSimple;
29+
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
30+
import org.cloudsimplus.cloudlets.Cloudlet;
31+
import org.cloudsimplus.cloudlets.CloudletSimple;
32+
import org.cloudsimplus.core.CloudSimPlus;
33+
import org.cloudsimplus.core.Identifiable;
34+
import org.cloudsimplus.datacenters.Datacenter;
35+
import org.cloudsimplus.datacenters.DatacenterSimple;
36+
import org.cloudsimplus.hosts.Host;
37+
import org.cloudsimplus.hosts.HostSimple;
38+
import org.cloudsimplus.listeners.EventInfo;
39+
import org.cloudsimplus.resources.Pe;
40+
import org.cloudsimplus.resources.PeSimple;
41+
import org.cloudsimplus.selectionpolicies.VmSelectionPolicyMinimumUtilization;
42+
import org.cloudsimplus.utilizationmodels.UtilizationModelDynamic;
43+
import org.cloudsimplus.vms.Vm;
44+
import org.cloudsimplus.vms.VmSimple;
45+
46+
import java.util.ArrayList;
47+
import java.util.List;
48+
49+
import static java.util.stream.Collectors.joining;
50+
51+
/**
52+
* An example showing how to enable a {@link DatacenterBroker} to
53+
* request creation of a Vm List in batch, instead of in a one-by-one basis.
54+
* It can use any {@link org.cloudsimplus.allocationpolicies.VmAllocationPolicy}
55+
* but it uses the {@link VmAllocationPolicyBatchPlacementUnderloadedHosts}
56+
* as an example.
57+
* Check {@link DatacenterBroker#setBatchVmCreation(boolean)}
58+
*
59+
* @author Manoel Campos da Silva Filho
60+
* @since CloudSim Plus 8.5.0
61+
*/
62+
public class DatacenterBrokerBatchVmCreationRequestExample {
63+
private static final int HOSTS = 2;
64+
private static final int HOST_PES = 8;
65+
private static final int HOST_MIPS = 1000; // Milion Instructions per Second (MIPS)
66+
private static final int HOST_RAM = 2048; //in Megabytes
67+
private static final long HOST_BW = 10_000; //in Megabits/s
68+
private static final long HOST_STORAGE = 1_000_000; //in Megabytes
69+
70+
private static final int VMS = 2;
71+
private static final int VM_PES = 4;
72+
73+
private static final int CLOUDLET_PES = 2;
74+
private static final int CLOUDLET_LENGTH = 10_000; // Milion Instructions (MI)
75+
76+
private final CloudSimPlus simulation;
77+
private final DatacenterBroker broker0;
78+
private List<Vm> vmList;
79+
private List<Cloudlet> cloudletList;
80+
private Datacenter datacenter0;
81+
82+
/** @see Datacenter#setSchedulingInterval(double) */
83+
private static final double SCHEDULING_INTERVAL = 1;
84+
85+
/**
86+
* Indicates wether no cloudlets were created during simulation runtime yet.
87+
*/
88+
private boolean noDynamicCloudletsCreated = true;
89+
90+
public static void main(String[] args) {
91+
new DatacenterBrokerBatchVmCreationRequestExample();
92+
}
93+
94+
private DatacenterBrokerBatchVmCreationRequestExample() {
95+
/*Enables just some level of log messages.
96+
Make sure to import org.cloudsimplus.util.Log;*/
97+
//Log.setLevel(ch.qos.logback.classic.Level.WARN);
98+
99+
simulation = new CloudSimPlus();
100+
datacenter0 = createDatacenter();
101+
102+
//Creates a broker that is a software acting on behalf of a cloud customer to manage his/her VMs and Cloudlets
103+
broker0 = createBroker();
104+
105+
vmList = new ArrayList<>();
106+
cloudletList = new ArrayList<>();
107+
createVmsAndCloudlets();
108+
109+
simulation.addOnClockTickListener(this::onClockTick);
110+
simulation.start();
111+
112+
final var cloudletFinishedList = broker0.getCloudletFinishedList();
113+
new CloudletsTableBuilder(cloudletFinishedList).build();
114+
}
115+
116+
/**
117+
* {@return a new broker} Enables batch VM creation.
118+
*/
119+
private DatacenterBroker createBroker() {
120+
return new DatacenterBrokerSimple(simulation).setBatchVmCreation(true);
121+
}
122+
123+
/**
124+
* @param info
125+
* @see Datacenter#setSchedulingInterval(double)
126+
*/
127+
private void onClockTick(final EventInfo info) {
128+
if(info.getTime() >= 10 && noDynamicCloudletsCreated) {
129+
noDynamicCloudletsCreated = false;
130+
final var newVmList = createAndSubmitVms();
131+
final var newCloudlets = createAndSubmitCloudlets(newVmList);
132+
final String vmIds = getEntityId(newVmList);
133+
final String cloudletIds = getEntityId(newCloudlets);
134+
System.out.printf(
135+
"%.2f: Submitting %d VMs and %d Cloudlets during simulation runtime. VM IDs: %s | Cloudlet IDs: %s %n%n",
136+
info.getTime(), newVmList.size(), newCloudlets.size(), vmIds, cloudletIds);
137+
}
138+
}
139+
140+
private static String getEntityId(final List<? extends Identifiable> entities) {
141+
return entities.stream().map(Identifiable::getId).map(String::valueOf).collect(joining(", "));
142+
}
143+
144+
private void createVmsAndCloudlets() {
145+
final var newVms = createAndSubmitVms();
146+
createAndSubmitCloudlets(newVms);
147+
}
148+
149+
/**
150+
* Creates a Datacenter and its Hosts.
151+
*/
152+
private Datacenter createDatacenter() {
153+
final var hostList = new ArrayList<Host>(HOSTS);
154+
for(int i = 0; i < HOSTS; i++) {
155+
final var host = createHost();
156+
hostList.add(host);
157+
}
158+
159+
final var vmAllocationPolicy = new VmAllocationPolicyBatchPlacementUnderloadedHosts(new VmSelectionPolicyMinimumUtilization());
160+
final var dc = new DatacenterSimple(simulation, hostList, vmAllocationPolicy);
161+
dc.setSchedulingInterval(SCHEDULING_INTERVAL);
162+
return dc;
163+
}
164+
165+
private Host createHost() {
166+
final var peList = new ArrayList<Pe>(HOST_PES);
167+
//List of Host's CPUs (Processing Elements, PEs)
168+
for (int i = 0; i < HOST_PES; i++) {
169+
//Uses a PeProvisionerSimple by default to provision PEs for VMs
170+
peList.add(new PeSimple(HOST_MIPS));
171+
}
172+
173+
/*
174+
Uses ResourceProvisionerSimple by default for RAM and BW provisioning
175+
and VmSchedulerSpaceShared for VM scheduling.
176+
*/
177+
return new HostSimple(HOST_RAM, HOST_BW, HOST_STORAGE, peList);
178+
}
179+
180+
private ArrayList<Vm> createAndSubmitVms() {
181+
final var newVms = new ArrayList<Vm>(VMS);
182+
for (int i = 0; i < VMS; i++) {
183+
//Uses a CloudletSchedulerTimeShared by default to schedule Cloudlets
184+
final var vm = new VmSimple(HOST_MIPS, VM_PES);
185+
vm.setRam(512).setBw(1000).setSize(10_000);
186+
newVms.add(vm);
187+
}
188+
189+
vmList.addAll(newVms);
190+
broker0.submitVmList(newVms);
191+
return newVms;
192+
}
193+
194+
private List<Cloudlet> createAndSubmitCloudlets(final List<Vm> vms) {
195+
final var newCloulets = new ArrayList<Cloudlet>(vms.size());
196+
197+
//UtilizationModel defining the Cloudlets use only 50% of any resource all the time
198+
final var utilizationModel = new UtilizationModelDynamic(0.5);
199+
200+
for (final var vm : vms) {
201+
final var cloudlet = new CloudletSimple(CLOUDLET_LENGTH, CLOUDLET_PES, utilizationModel);
202+
cloudlet.setSizes(1024);
203+
broker0.bindCloudletToVm(cloudlet, vm);
204+
newCloulets.add(cloudlet);
205+
}
206+
207+
cloudletList.addAll(newCloulets);
208+
broker0.submitCloudletList(newCloulets);
209+
return newCloulets;
210+
}
211+
}

0 commit comments

Comments
 (0)