|
| 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.power; |
| 25 | + |
| 26 | +import org.cloudsimplus.brokers.DatacenterBroker; |
| 27 | +import org.cloudsimplus.brokers.DatacenterBrokerSimple; |
| 28 | +import org.cloudsimplus.builders.tables.CloudletsTableBuilder; |
| 29 | +import org.cloudsimplus.cloudlets.Cloudlet; |
| 30 | +import org.cloudsimplus.cloudlets.CloudletSimple; |
| 31 | +import org.cloudsimplus.core.CloudSimPlus; |
| 32 | +import org.cloudsimplus.datacenters.Datacenter; |
| 33 | +import org.cloudsimplus.datacenters.DatacenterSimple; |
| 34 | +import org.cloudsimplus.hosts.Host; |
| 35 | +import org.cloudsimplus.hosts.HostSimple; |
| 36 | +import org.cloudsimplus.power.models.PowerModelHostSpec; |
| 37 | +import org.cloudsimplus.resources.Pe; |
| 38 | +import org.cloudsimplus.resources.PeSimple; |
| 39 | +import org.cloudsimplus.schedulers.vm.VmSchedulerTimeShared; |
| 40 | +import org.cloudsimplus.utilizationmodels.UtilizationModelDynamic; |
| 41 | +import org.cloudsimplus.utilizationmodels.UtilizationModelFull; |
| 42 | +import org.cloudsimplus.vms.HostResourceStats; |
| 43 | +import org.cloudsimplus.vms.Vm; |
| 44 | +import org.cloudsimplus.vms.VmSimple; |
| 45 | + |
| 46 | +import java.util.ArrayList; |
| 47 | +import java.util.Arrays; |
| 48 | +import java.util.Comparator; |
| 49 | +import java.util.List; |
| 50 | + |
| 51 | +import static java.util.Comparator.comparingLong; |
| 52 | + |
| 53 | +/** |
| 54 | + * An example to show power consumption of Hosts |
| 55 | + * using the specifications from a file created based on |
| 56 | + * <a href="https://www.specs.org">www.specs.org</a> website public data. |
| 57 | + * Realize that for this goal, you define a {@link PowerModelHostSpec} |
| 58 | + * for each Host by calling {@code host.setPowerModel(powerModel)}. |
| 59 | + * |
| 60 | + * @author Manoel Campos da Silva Filho |
| 61 | + * @since CloudSim Plus 8.1.0 |
| 62 | + * |
| 63 | + * @see #POWER_SPEC_FILE |
| 64 | + * @see #createPowerHost(int) |
| 65 | + * @see PowerExample |
| 66 | + */ |
| 67 | +public class PowerSpecFileExample { |
| 68 | + /** |
| 69 | + * Defines, between other things, the time intervals |
| 70 | + * to keep Hosts CPU utilization history records. |
| 71 | + */ |
| 72 | + private static final int SCHEDULING_INTERVAL = 10; |
| 73 | + private static final int HOSTS = 2; |
| 74 | + private static final int HOST_PES = 8; |
| 75 | + |
| 76 | + /** Indicates the time (in seconds) the Host takes to start up. */ |
| 77 | + private static final double HOST_START_UP_DELAY = 5; |
| 78 | + |
| 79 | + /** Indicates the time (in seconds) the Host takes to shut down. */ |
| 80 | + private static final double HOST_SHUT_DOWN_DELAY = 3; |
| 81 | + |
| 82 | + /** Indicates Host power consumption (in Watts) during startup. */ |
| 83 | + private static final double HOST_START_UP_POWER = 5; |
| 84 | + |
| 85 | + /** Indicates Host power consumption (in Watts) during shutdown. */ |
| 86 | + private static final double HOST_SHUT_DOWN_POWER = 3; |
| 87 | + |
| 88 | + private static final int VMS = 4; |
| 89 | + private static final int VM_PES = 4; |
| 90 | + |
| 91 | + private static final int CLOUDLETS = 8; |
| 92 | + private static final int CLOUDLET_PES = 2; |
| 93 | + private static final int CLOUDLET_LENGTH = 50000; |
| 94 | + |
| 95 | + /** |
| 96 | + * The path to the file containing host power utilization specs. |
| 97 | + */ |
| 98 | + private static final String POWER_SPEC_FILE = "power-specs/PowerModelSpecPowerHpProLiantMl110G3PentiumD930.txt"; |
| 99 | + |
| 100 | + private final CloudSimPlus simulation; |
| 101 | + private final DatacenterBroker broker0; |
| 102 | + private List<Vm> vmList; |
| 103 | + private List<Cloudlet> cloudletList; |
| 104 | + private Datacenter datacenter0; |
| 105 | + private final List<Host> hostList; |
| 106 | + |
| 107 | + /** |
| 108 | + * Indicates how hosts consume power according to CPU utilization percentage |
| 109 | + * (based on data from a power spec file). |
| 110 | + * This attribute is used just to ensure the same data will be read just once |
| 111 | + * and used for all created Hosts. |
| 112 | + */ |
| 113 | + private final PowerModelHostSpec DEF_POWER_MODEL = PowerModelHostSpec.getInstance(POWER_SPEC_FILE); |
| 114 | + |
| 115 | + public static void main(String[] args) { |
| 116 | + new PowerSpecFileExample(); |
| 117 | + } |
| 118 | + |
| 119 | + private PowerSpecFileExample() { |
| 120 | + /*Enables just some level of log messages. |
| 121 | + Make sure to import org.cloudsimplus.util.Log;*/ |
| 122 | + //Log.setLevel(ch.qos.logback.classic.Level.WARN); |
| 123 | + |
| 124 | + simulation = new CloudSimPlus(); |
| 125 | + hostList = new ArrayList<>(HOSTS); |
| 126 | + datacenter0 = createDatacenter(); |
| 127 | + //Creates a broker that is a software acting on behalf of a cloud customer to manage his/her VMs and Cloudlets |
| 128 | + broker0 = new DatacenterBrokerSimple(simulation); |
| 129 | + |
| 130 | + vmList = createVms(); |
| 131 | + cloudletList = createCloudlets(); |
| 132 | + broker0.submitVmList(vmList); |
| 133 | + broker0.submitCloudletList(cloudletList); |
| 134 | + |
| 135 | + simulation.start(); |
| 136 | + |
| 137 | + System.out.println("------------------------------- SIMULATION FOR SCHEDULING INTERVAL = " + SCHEDULING_INTERVAL+" -------------------------------"); |
| 138 | + final var cloudletFinishedList = broker0.getCloudletFinishedList(); |
| 139 | + final Comparator<Cloudlet> hostComparator = comparingLong(cl -> cl.getVm().getHost().getId()); |
| 140 | + cloudletFinishedList.sort(hostComparator.thenComparing(cl -> cl.getVm().getId())); |
| 141 | + |
| 142 | + new CloudletsTableBuilder(cloudletFinishedList).build(); |
| 143 | + printHostsCpuUtilizationAndPowerConsumption(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * The Host CPU Utilization History is only computed |
| 148 | + * if VMs utilization history is enabled by calling |
| 149 | + * {@code vm.getUtilizationHistory().enable()}. |
| 150 | + */ |
| 151 | + private void printHostsCpuUtilizationAndPowerConsumption() { |
| 152 | + System.out.println(); |
| 153 | + for (final Host host : hostList) { |
| 154 | + printHostCpuUtilizationAndPowerConsumption(host); |
| 155 | + } |
| 156 | + System.out.println(); |
| 157 | + } |
| 158 | + |
| 159 | + private void printHostCpuUtilizationAndPowerConsumption(final Host host) { |
| 160 | + final HostResourceStats cpuStats = host.getCpuUtilizationStats(); |
| 161 | + |
| 162 | + //The total Host's CPU utilization for the time specified by the map key |
| 163 | + final double utilizationPercentMean = cpuStats.getMean(); |
| 164 | + final double watts = host.getPowerModel().getPower(utilizationPercentMean); |
| 165 | + System.out.printf( |
| 166 | + "Host %2d CPU Usage mean: %6.1f%% | Power Consumption mean: %8.0f W%n", |
| 167 | + host.getId(), utilizationPercentMean * 100, watts); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Creates a {@link Datacenter} and its {@link Host}s. |
| 172 | + */ |
| 173 | + private Datacenter createDatacenter() { |
| 174 | + for(int i = 0; i < HOSTS; i++) { |
| 175 | + final var host = createPowerHost(i); |
| 176 | + hostList.add(host); |
| 177 | + } |
| 178 | + |
| 179 | + final var dc = new DatacenterSimple(simulation, hostList); |
| 180 | + dc.setSchedulingInterval(SCHEDULING_INTERVAL); |
| 181 | + return dc; |
| 182 | + } |
| 183 | + |
| 184 | + private Host createPowerHost(final int id) { |
| 185 | + final var peList = new ArrayList<Pe>(HOST_PES); |
| 186 | + //List of Host's CPUs (Processing Elements, PEs) |
| 187 | + for (int i = 0; i < HOST_PES; i++) { |
| 188 | + peList.add(new PeSimple(1000)); |
| 189 | + } |
| 190 | + |
| 191 | + final long ram = 2048; //in Megabytes |
| 192 | + final long bw = 10000; //in Megabits/s |
| 193 | + final long storage = 1000000; //in Megabytes |
| 194 | + final var vmScheduler = new VmSchedulerTimeShared(); |
| 195 | + |
| 196 | + final var host = new HostSimple(ram, bw, storage, peList); |
| 197 | + |
| 198 | + final var powerModel = new PowerModelHostSpec(DEF_POWER_MODEL.getPowerSpecs()); |
| 199 | + powerModel.setStartupDelay(HOST_START_UP_DELAY) |
| 200 | + .setShutDownDelay(HOST_SHUT_DOWN_DELAY) |
| 201 | + .setStartupPower(HOST_START_UP_POWER) |
| 202 | + .setShutDownPower(HOST_SHUT_DOWN_POWER); |
| 203 | + System.out.println(Arrays.toString(powerModel.getPowerSpecs())); |
| 204 | + |
| 205 | + host.setId(id) |
| 206 | + .setVmScheduler(vmScheduler) |
| 207 | + .setPowerModel(powerModel); |
| 208 | + host.enableUtilizationStats(); |
| 209 | + |
| 210 | + return host; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Creates a list of VMs. |
| 215 | + */ |
| 216 | + private List<Vm> createVms() { |
| 217 | + final var list = new ArrayList<Vm>(VMS); |
| 218 | + for (int i = 0; i < VMS; i++) { |
| 219 | + final var vm = new VmSimple(i, 1000, VM_PES); |
| 220 | + vm.setRam(512).setBw(1000).setSize(10000); |
| 221 | + list.add(vm); |
| 222 | + } |
| 223 | + |
| 224 | + return list; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Creates a list of Cloudlets. |
| 229 | + */ |
| 230 | + private List<Cloudlet> createCloudlets() { |
| 231 | + final var cloudletList = new ArrayList<Cloudlet>(CLOUDLETS); |
| 232 | + final var utilization = new UtilizationModelDynamic(0.2); |
| 233 | + for (int i = 0; i < CLOUDLETS; i++) { |
| 234 | + //Sets half of the cloudlets with the defined length and the other half with the double of it |
| 235 | + final long length = i < CLOUDLETS / 2 ? CLOUDLET_LENGTH : CLOUDLET_LENGTH * 2; |
| 236 | + final var cloudlet = |
| 237 | + new CloudletSimple(i, length, CLOUDLET_PES) |
| 238 | + .setFileSize(1024) |
| 239 | + .setOutputSize(1024) |
| 240 | + .setUtilizationModelCpu(new UtilizationModelFull()) |
| 241 | + .setUtilizationModelRam(utilization) |
| 242 | + .setUtilizationModelBw(utilization); |
| 243 | + cloudletList.add(cloudlet); |
| 244 | + } |
| 245 | + |
| 246 | + return cloudletList; |
| 247 | + } |
| 248 | +} |
0 commit comments