Get hostUtilization every 600 seconds in CloudSim #467
-
Hi everyone; |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Try this, add an event listener to the simulation monitoring the clock tick, see package com.example;
import java.util.ArrayList;
import java.util.List;
import org.cloudsimplus.brokers.DatacenterBroker;
import org.cloudsimplus.brokers.DatacenterBrokerSimple;
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
import org.cloudsimplus.cloudlets.Cloudlet;
import org.cloudsimplus.cloudlets.CloudletSimple;
import org.cloudsimplus.core.CloudSimPlus;
import org.cloudsimplus.datacenters.Datacenter;
import org.cloudsimplus.datacenters.DatacenterSimple;
import org.cloudsimplus.hosts.Host;
import org.cloudsimplus.hosts.HostSimple;
import org.cloudsimplus.listeners.EventInfo;
import org.cloudsimplus.listeners.EventListener;
import org.cloudsimplus.resources.Pe;
import org.cloudsimplus.resources.PeSimple;
import org.cloudsimplus.utilizationmodels.UtilizationModelDynamic;
import org.cloudsimplus.vms.Vm;
import org.cloudsimplus.vms.VmSimple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HostLogExample {
private static final class Config {
// Datacenter
public static final class Datacenter {
public static final double SCHEDULING_INTERVAL = 1; // Interval indicates how often the datacenter handles
// events such as Vm allocating and Cloudlet processing
}
// Host
public static final class Host {
public static final int NUM = 1; // Number of physical hosts in the datacenter
public static final int PES = 64; // Number of CPU cores per host
public static final long MIPS = (long) (1 * 1e3); // CPU rate, 1GHz
public static final long RAM = Long.MAX_VALUE; // Infinite RAM (MB)
public static final long BW = Long.MAX_VALUE; // Infinite bandwidth (Mbps)
public static final long STORAGE = Long.MAX_VALUE;// Infinite storage (MB)
}
// Vm
public static final class Vm {
public static final int NUM = 1; // Create 1 Vm initially
public static final int PES = 1; // 1 CPU core per Vm
public static final long MIPS = (long) (1 * 1e3); // Same as host, assuming no performance loss
public static final long RAM = 1024; // 1G by default
public static final long BW = 100; // 100Mbps by default
public static final long STORAGE = (long) (10 * 1e3); // 10G by default
public static final int STARTUP_DELAY = 30; // Take 30s to bootup
public static final int SHUTDOWN_DELAY = 10; // Take 10s to shutdown
}
// Cloudlet
public static final class Cloudlet {
public static final int NUM = 2; // Create 2 cloudlet to run in first example
public static final int PES = 2; // Required CPU cores
/**
* ========= Important =========
* Since the number of requested CPU cores per Cloudlet is 2,
* while there is only 1 PE available on the vm,
* to run a cloudlet taking 1s per CPU (in maximum utilization model) will
* result in 2s execution time.
* ========= Important =========
*/
public static final long LENGTH = (long) (1 * Vm.MIPS); // 1s for 1 CPU core to run
public static final double CPU_USAGE_FACTOR = 1; // 100% CPU usage
public static final double RAM_USAGE_FACTOR = 1e-3; // 1MB RAM
public static final double BW_USAGE_FACTOR = 1e-2; // 1Mbps bandwidth
}
}
private CloudSimPlus simulation; // Simulation takes responsibility to run simulation globally, monitor events,
// etc., it's recommended to make it a class member
private Datacenter datacenter; // Datacenter to run simulation, must be a class member
private DatacenterBroker broker; // Represents a broker acting on behalf of a cloud customer.
// It hides VM management such as vm creation, submission of cloudlets to VMs
// and destruction of VMs.
// A broker implements the policies for selecting a VM to run a Cloudlet
// and a Datacenter to run the submitted VMs.
private long numVmCreated;
private long numCloudletCreated;
private void logHostStatus(EventInfo info) {
final var logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
final long LOG_INTERVAL = 1;
long time = (long) info.getTime();
final var hostList = datacenter.getHostList();
for (Host host : hostList) {
logger.info(String.format("%4.2f: Host %d, CPU MIPS Utilization %.2f MIPS", info.getTime(),host.getId(),host.getCpuMipsUtilization()));
}
}
private void createSimulation() {
this.simulation = new CloudSimPlus();
this.simulation.addOnClockTickListener(this::logHostStatus);
}
private void createDatacenter() {
assert this.simulation != null : "Simulation must be initialized before creating datacenter";
this.datacenter = new DatacenterSimple(this.simulation, this.createHostList());
this.datacenter.setSchedulingInterval(Config.Datacenter.SCHEDULING_INTERVAL);
}
private void createDatacenterBroker() {
assert this.simulation != null : "Simulation must be initialized before creating datacenterbroker";
this.broker = new DatacenterBrokerSimple(this.simulation);
}
private List<Host> createHostList() {
final var hostList = new ArrayList<Host>();
for (int i = 0; i < Config.Host.NUM; ++i) {
// Create CPU cores for physical host
final var peList = new ArrayList<Pe>();
for (int j = 0; j < Config.Host.PES; ++j) {
peList.add(new PeSimple(Config.Host.MIPS));
}
final var host = new HostSimple(Config.Host.RAM, Config.Host.BW, Config.Host.STORAGE, peList);
hostList.add(host);
}
return hostList;
}
private List<Vm> createVmList() {
final var vmList = new ArrayList<Vm>();
for (int i = 0; i < Config.Vm.NUM; ++i) {
final var vm = new VmSimple(this.numVmCreated, Config.Vm.MIPS, Config.Vm.PES);
vm.setRam(Config.Vm.RAM);
vm.setBw(Config.Vm.BW);
vm.setSize(Config.Vm.STORAGE);
vmList.add(vm);
++this.numVmCreated;
}
return vmList;
}
private List<Cloudlet> createCloudletList() {
final var cloudletList = new ArrayList<Cloudlet>();
for (int i = 0; i < Config.Cloudlet.NUM; ++i) {
final var cloudlet = new CloudletSimple(numCloudletCreated, Config.Cloudlet.LENGTH, Config.Cloudlet.PES);
cloudlet.setUtilizationModelCpu(new UtilizationModelDynamic(Config.Cloudlet.CPU_USAGE_FACTOR));
cloudlet.setUtilizationModelBw(new UtilizationModelDynamic(Config.Cloudlet.BW_USAGE_FACTOR));
cloudlet.setUtilizationModelRam(new UtilizationModelDynamic(Config.Cloudlet.RAM_USAGE_FACTOR));
cloudletList.add(cloudlet);
++this.numCloudletCreated;
}
return cloudletList;
}
private HostLogExample() {
createSimulation();
createDatacenter();
createDatacenterBroker();
this.broker.submitVmList(createVmList());
this.broker.submitCloudletList(createCloudletList());
this.simulation.start();
List<Cloudlet> cloudletList = this.broker.getCloudletSubmittedList();
new CloudletsTableBuilder(cloudletList).build();
}
public static void main(String[] args) {
new HostLogExample();
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for your response! I appreciate the help. |
Beta Was this translation helpful? Give feedback.
-
For your 1st question, try force case casting like private void logHostStatus(EventInfo info) {
Logger logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
final long LOG_INTERVAL = 1;
long time = (long) info.getTime();
if (time % LOG_INTERVAL == 0) {
List<Host> hostList = datacenter.getHostList();
for (Host host : hostList) {
logger.info(String.format("%4.2f: Host %d, CPU MIPS Utilization %.2f MIPS", info.getTime(),
host.getId(), host.getCpuMipsUtilization()));
}
}
} |
Beta Was this translation helpful? Give feedback.
Try this, add an event listener to the simulation monitoring the clock tick, see
createSimulation()
in the code