package org.cloudbus.cloudsim.sdn.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.cloudbus.cloudsim.Host; import org.cloudbus.cloudsim.Log; import org.cloudbus.cloudsim.Vm; import org.cloudbus.cloudsim.VmAllocationPolicy; import org.cloudbus.cloudsim.core.CloudSim; public class VmAllocationPolicyCombinedMostFullFirst extends VmAllocationPolicy { protected final double hostTotalMips; protected final double hostTotalBw; protected final int hostTotalPes; /** The vm table. */ private Map vmTable; /** The used pes. */ private Map usedPes; /** The free pes. */ private List freePes; private Map usedMips; private List freeMips; private Map usedBw; private List freeBw; /** * Creates the new VmAllocationPolicySimple object. * * @param list the list * @pre $none * @post $none */ public VmAllocationPolicyCombinedMostFullFirst(List list) { super(list); setFreePes(new ArrayList()); setFreeMips(new ArrayList()); setFreeBw(new ArrayList()); for (Host host : getHostList()) { getFreePes().add(host.getNumberOfPes()); getFreeMips().add((long) host.getTotalMips()); getFreeBw().add(host.getBw()); } hostTotalMips = getHostList().get(0).getTotalMips(); hostTotalBw = getHostList().get(0).getBw(); hostTotalPes = getHostList().get(0).getNumberOfPes(); setVmTable(new HashMap()); setUsedPes(new HashMap()); setUsedMips(new HashMap()); setUsedBw(new HashMap()); } protected double convertWeightedMetric(double mipsPercent, double bwPercent) { double ret = mipsPercent * bwPercent; return ret; } /** * Allocates a host for a given VM. * * @param vm VM specification * @return $true if the host could be allocated; $false otherwise * @pre $none * @post $none */ @Override public boolean allocateHostForVm(Vm vm) { if (getVmTable().containsKey(vm.getUid())) { // if this vm was not created return false; } int numHosts = getHostList().size(); // 1. Find/Order the best host for this VM by comparing a metric int requiredPes = vm.getNumberOfPes(); double requiredMips = vm.getCurrentRequestedTotalMips(); long requiredBw = vm.getCurrentRequestedBw(); boolean result = false; double[] freeResources = new double[numHosts]; for (int i = 0; i < numHosts; i++) { double mipsFreePercent = (double)getFreeMips().get(i) / this.hostTotalMips; double bwFreePercent = (double)getFreeBw().get(i) / this.hostTotalBw; freeResources[i] = this.convertWeightedMetric(mipsFreePercent, bwFreePercent); } for(int tries = 0; result == false && tries < numHosts; tries++) {// we still trying until we find a host or until we try all of them double lessFree = Double.POSITIVE_INFINITY; int idx = -1; // we want the host with less pes in use for (int i = 0; i < numHosts; i++) { if (freeResources[i] < lessFree) { lessFree = freeResources[i]; idx = i; } } freeResources[idx] = Double.POSITIVE_INFINITY; Host host = getHostList().get(idx); // Check whether the host can hold this VM or not. if(getFreeMips().get(idx) < requiredMips || getFreeBw().get(idx) < requiredBw || getFreePes().get(idx) < requiredPes) { //Cannot host the VM continue; } result = host.vmCreate(vm); if (result) { // if vm were succesfully created in the host getVmTable().put(vm.getUid(), host); getUsedPes().put(vm.getUid(), requiredPes); getFreePes().set(idx, getFreePes().get(idx) - requiredPes); getUsedMips().put(vm.getUid(), (long) requiredMips); getFreeMips().set(idx, (long) (getFreeMips().get(idx) - requiredMips)); getUsedBw().put(vm.getUid(), (long) requiredBw); getFreeBw().set(idx, (long) (getFreeBw().get(idx) - requiredBw)); break; } } logMaxNumHostsUsed(); return result; } protected int maxNumHostsUsed=0; protected void logMaxNumHostsUsed() { // Get how many are used int numHostsUsed=0; for(int freePes:getFreePes()) { if(freePes < hostTotalPes) { numHostsUsed++; } } if(maxNumHostsUsed < numHostsUsed) maxNumHostsUsed = numHostsUsed; System.err.println("Number of online hosts:"+numHostsUsed + ", max was ="+maxNumHostsUsed); } public int getMaxNumHostsUsed() { return maxNumHostsUsed;} /** * Releases the host used by a VM. * * @param vm the vm * @pre $none * @post none */ @Override public void deallocateHostForVm(Vm vm) { Host host = getVmTable().remove(vm.getUid()); if (host != null) { int idx = getHostList().indexOf(host); host.vmDestroy(vm); Integer pes = getUsedPes().remove(vm.getUid()); getFreePes().set(idx, getFreePes().get(idx) + pes); Long mips = getUsedMips().remove(vm.getUid()); getFreeMips().set(idx, getFreeMips().get(idx) + mips); Long bw = getUsedBw().remove(vm.getUid()); getFreeBw().set(idx, getFreeBw().get(idx) + bw); } } /** * Gets the host that is executing the given VM belonging to the given user. * * @param vm the vm * @return the Host with the given vmID and userID; $null if not found * @pre $none * @post $none */ @Override public Host getHost(Vm vm) { return getVmTable().get(vm.getUid()); } /** * Gets the host that is executing the given VM belonging to the given user. * * @param vmId the vm id * @param userId the user id * @return the Host with the given vmID and userID; $null if not found * @pre $none * @post $none */ @Override public Host getHost(int vmId, int userId) { return getVmTable().get(Vm.getUid(userId, vmId)); } /** * Gets the vm table. * * @return the vm table */ public Map getVmTable() { return vmTable; } /** * Sets the vm table. * * @param vmTable the vm table */ protected void setVmTable(Map vmTable) { this.vmTable = vmTable; } /** * Gets the used pes. * * @return the used pes */ protected Map getUsedPes() { return usedPes; } /** * Sets the used pes. * * @param usedPes the used pes */ protected void setUsedPes(Map usedPes) { this.usedPes = usedPes; } /** * Gets the free pes. * * @return the free pes */ protected List getFreePes() { return freePes; } /** * Sets the free pes. * * @param freePes the new free pes */ protected void setFreePes(List freePes) { this.freePes = freePes; } protected Map getUsedMips() { return usedMips; } protected void setUsedMips(Map usedMips) { this.usedMips = usedMips; } protected Map getUsedBw() { return usedBw; } protected void setUsedBw(Map usedBw) { this.usedBw = usedBw; } protected List getFreeMips() { return this.freeMips; } protected void setFreeMips(List freeMips) { this.freeMips = freeMips; } protected List getFreeBw() { return this.freeBw; } protected void setFreeBw(List freeBw) { this.freeBw = freeBw; } /* * (non-Javadoc) * @see cloudsim.VmAllocationPolicy#optimizeAllocation(double, cloudsim.VmList, double) */ @Override public List> optimizeAllocation(List vmList) { // TODO Auto-generated method stub return null; } /* * (non-Javadoc) * @see org.cloudbus.cloudsim.VmAllocationPolicy#allocateHostForVm(org.cloudbus.cloudsim.Vm, * org.cloudbus.cloudsim.Host) */ @Override public boolean allocateHostForVm(Vm vm, Host host) { if (host.vmCreate(vm)) { // if vm has been succesfully created in the host getVmTable().put(vm.getUid(), host); int requiredPes = vm.getNumberOfPes(); int idx = getHostList().indexOf(host); getUsedPes().put(vm.getUid(), requiredPes); getFreePes().set(idx, getFreePes().get(idx) - requiredPes); Log.formatLine( "%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(), CloudSim.clock()); return true; } return false; } }