|
| 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; |
| 25 | + |
| 26 | +import org.cloudsimplus.brokers.DatacenterBroker; |
| 27 | +import org.cloudsimplus.brokers.DatacenterBrokerSimple; |
| 28 | +import org.cloudsimplus.builders.tables.CloudletsTableBuilder; |
| 29 | +import org.cloudsimplus.builders.tables.MarkdownTableColumn; |
| 30 | +import org.cloudsimplus.cloudlets.Cloudlet; |
| 31 | +import org.cloudsimplus.cloudlets.CloudletSimple; |
| 32 | +import org.cloudsimplus.core.CloudSimPlus; |
| 33 | +import org.cloudsimplus.core.Lifetimed; |
| 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.resources.Pe; |
| 39 | +import org.cloudsimplus.resources.PeSimple; |
| 40 | +import org.cloudsimplus.schedulers.vm.VmSchedulerTimeShared; |
| 41 | +import org.cloudsimplus.util.TimeUtil; |
| 42 | +import org.cloudsimplus.utilizationmodels.UtilizationModelFull; |
| 43 | +import org.cloudsimplus.utilizationmodels.UtilizationModelStochastic; |
| 44 | +import org.cloudsimplus.vms.Vm; |
| 45 | +import org.cloudsimplus.vms.VmSimple; |
| 46 | + |
| 47 | +import java.time.LocalTime; |
| 48 | +import java.util.ArrayList; |
| 49 | +import java.util.Comparator; |
| 50 | +import java.util.List; |
| 51 | +import java.util.stream.IntStream; |
| 52 | + |
| 53 | +/** |
| 54 | + * An example showing how to use the new {@link Lifetimed#setLifeTime(double)} to |
| 55 | + * define the maximum time {@link Cloudlet} or {@link Vm} entity is allowed to execute. |
| 56 | + * After this time is reached, the entity is finished as soon as possible. |
| 57 | + * |
| 58 | + * <p> |
| 59 | + * If the VM has a lifeTime set, it's Cloudlets follow the same lifetime. |
| 60 | + * If both Cloudlet and Vm have a lifetime, the shorter defines the maximum |
| 61 | + * amount of time the Cloudlet will execute. |
| 62 | + * </p> |
| 63 | + * |
| 64 | + * <p>The example creates all Cloudlets and some VMs with a given lifetime. |
| 65 | + * Since VM lifetime is shorter than their Cloudlets' lifetime, |
| 66 | + * such Cloudlets finish sooner than their own lifetime.</p> |
| 67 | + * |
| 68 | + * @author Manoel Campos da Silva Filho |
| 69 | + * @since CloudSim Plus 8.2.0 |
| 70 | + * @see CloudletLifeTimeExample |
| 71 | + */ |
| 72 | +public class CloudletAndVmLifeTimeExample { |
| 73 | + private static final int HOSTS = 3; |
| 74 | + private static final int HOST_PES = 10; |
| 75 | + |
| 76 | + private static final int VMS = 4; |
| 77 | + private static final int VM_PES = 4; |
| 78 | + private static final int VM_MIPS = 1000; |
| 79 | + |
| 80 | + private static final int CLOUDLETS = 4; |
| 81 | + private static final int CLOUDLET_PES = 2; |
| 82 | + private static final int CLOUDLET_LENGTH = 10_000; |
| 83 | + |
| 84 | + /** |
| 85 | + * If the scheduling interval is not multiple of the VM/Cloudlet lifetime, |
| 86 | + * Cloudlets may execute more than you desire. |
| 87 | + * @see Datacenter#getSchedulingInterval() |
| 88 | + */ |
| 89 | + private static final int SCHEDULING_INTERVAL = 3; |
| 90 | + |
| 91 | + /** |
| 92 | + * Maximum time (in seconds) Cloudlets are allowed to execute. |
| 93 | + * Set -1 to disable lifeTime and execute the Cloudlet entirely. |
| 94 | + */ |
| 95 | + private static final double CLOUDLET_LIFE_TIME = 5; |
| 96 | + |
| 97 | + /** |
| 98 | + * Maximum time (in seconds) some VMs are allowed to execute. |
| 99 | + * @see Lifetimed#setLifeTime(double) |
| 100 | + */ |
| 101 | + private static final double VM_LIFE_TIME = 3; |
| 102 | + |
| 103 | + private final CloudSimPlus simulation; |
| 104 | + private final DatacenterBroker broker0; |
| 105 | + private List<Vm> vmList; |
| 106 | + private List<Cloudlet> cloudletList; |
| 107 | + private Datacenter datacenter0; |
| 108 | + |
| 109 | + public static void main(String[] args) { |
| 110 | + new CloudletAndVmLifeTimeExample(); |
| 111 | + } |
| 112 | + |
| 113 | + private CloudletAndVmLifeTimeExample() { |
| 114 | + /*Enables just some level of log messages. |
| 115 | + Make sure to import org.cloudsimplus.util.Log;*/ |
| 116 | + //Log.setLevel(ch.qos.logback.classic.Level.WARN); |
| 117 | + |
| 118 | + final double startSecs = TimeUtil.currentTimeSecs(); |
| 119 | + System.out.printf("Simulation started at %s%n%n", LocalTime.now()); |
| 120 | + simulation = new CloudSimPlus(); |
| 121 | + datacenter0 = createDatacenter(); |
| 122 | + |
| 123 | + //Creates a broker that is a software acting on behalf of a cloud customer to manage his/her VMs and Cloudlets |
| 124 | + broker0 = new DatacenterBrokerSimple(simulation); |
| 125 | + |
| 126 | + vmList = createVms(); |
| 127 | + setVmsLifeTime(); |
| 128 | + |
| 129 | + cloudletList = createCloudlets(); |
| 130 | + broker0.submitVmList(vmList); |
| 131 | + broker0.submitCloudletList(cloudletList); |
| 132 | + |
| 133 | + simulation.start(); |
| 134 | + |
| 135 | + final var cloudletFinishedList = broker0.getCloudletFinishedList(); |
| 136 | + cloudletFinishedList.sort(Comparator.comparingLong(c -> c.getVm().getId())); |
| 137 | + new CloudletsTableBuilder(cloudletFinishedList) |
| 138 | + .addColumn(new MarkdownTableColumn("Cloudlet", "LifeTime"), this::getLifeTimeStr) |
| 139 | + .addColumn(new MarkdownTableColumn("Vm ", "LifeTime"), c -> getLifeTimeStr(c.getVm())) |
| 140 | + .build(); |
| 141 | + System.out.printf("Simulation finished at %s. Execution time: %.2f seconds%n", LocalTime.now(), TimeUtil.elapsedSeconds(startSecs)); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Gets the lifetime as a String. |
| 146 | + * If the lifetime is {@link Double#MAX_VALUE} (the default value), |
| 147 | + * returns an empty string to indicate the attribute was not set. |
| 148 | + * |
| 149 | + * @param entity a Cloudlet of VM entity |
| 150 | + * @return a String lifetime |
| 151 | + */ |
| 152 | + private String getLifeTimeStr(final Lifetimed entity) { |
| 153 | + return entity.getLifeTime() == Double.MAX_VALUE ? "" : "%.2f".formatted(entity.getLifeTime()); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Sets a lifetime for half of the VMs. |
| 158 | + */ |
| 159 | + private void setVmsLifeTime() { |
| 160 | + vmList.stream() |
| 161 | + .filter(vm -> vm.getId() < VMS/2) |
| 162 | + .forEach(vm -> vm.setLifeTime(VM_LIFE_TIME)); |
| 163 | + } |
| 164 | + |
| 165 | + private Datacenter createDatacenter() { |
| 166 | + final var hostList = new ArrayList<Host>(HOSTS); |
| 167 | + for(int i = 0; i < HOSTS; i++) { |
| 168 | + final var host = createHost(); |
| 169 | + hostList.add(host); |
| 170 | + } |
| 171 | + |
| 172 | + final var datacenter = new DatacenterSimple(simulation, hostList); |
| 173 | + datacenter.setSchedulingInterval(SCHEDULING_INTERVAL); |
| 174 | + return datacenter; |
| 175 | + } |
| 176 | + |
| 177 | + private Host createHost() { |
| 178 | + final var peList = new ArrayList<Pe>(HOST_PES); |
| 179 | + //List of Host's CPUs (Processing Elements, PEs) |
| 180 | + IntStream.range(0, HOST_PES).forEach(i -> peList.add(new PeSimple(1000))); |
| 181 | + |
| 182 | + final long ram = 2048; //in Megabytes |
| 183 | + final long bw = 10000; //in Megabits/s |
| 184 | + final long storage = 1000000; //in Megabytes |
| 185 | + final var host = new HostSimple(ram, bw, storage, peList); |
| 186 | + host.setVmScheduler(new VmSchedulerTimeShared()); |
| 187 | + return host; |
| 188 | + } |
| 189 | + |
| 190 | + private List<Vm> createVms() { |
| 191 | + final var vmList = new ArrayList<Vm>(VMS); |
| 192 | + for (int i = 0; i < VMS; i++) { |
| 193 | + final var vm = new VmSimple(i, VM_MIPS, VM_PES); |
| 194 | + vmList.add(vm); |
| 195 | + } |
| 196 | + |
| 197 | + return vmList; |
| 198 | + } |
| 199 | + |
| 200 | + private List<Cloudlet> createCloudlets() { |
| 201 | + final var cloudletList = new ArrayList<Cloudlet>(CLOUDLETS); |
| 202 | + for (int i = 0; i < CLOUDLETS; i++) { |
| 203 | + final var cloudlet = new CloudletSimple(i, CLOUDLET_LENGTH, CLOUDLET_PES); |
| 204 | + cloudlet |
| 205 | + .setFileSize(1024) |
| 206 | + .setOutputSize(1024) |
| 207 | + .setUtilizationModelCpu(new UtilizationModelFull()) |
| 208 | + .setUtilizationModelBw(new UtilizationModelStochastic()) |
| 209 | + .setUtilizationModelRam(new UtilizationModelStochastic()) |
| 210 | + .setLifeTime(CLOUDLET_LIFE_TIME); |
| 211 | + cloudletList.add(cloudlet); |
| 212 | + } |
| 213 | + |
| 214 | + return cloudletList; |
| 215 | + } |
| 216 | +} |
0 commit comments