This commit is contained in:
2021-04-06 00:45:28 +02:00
commit 17fabc368e
836 changed files with 3042963 additions and 0 deletions

View File

@@ -0,0 +1,324 @@
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<String, Host> vmTable;
/** The used pes. */
private Map<String, Integer> usedPes;
/** The free pes. */
private List<Integer> freePes;
private Map<String, Long> usedMips;
private List<Long> freeMips;
private Map<String, Long> usedBw;
private List<Long> freeBw;
/**
* Creates the new VmAllocationPolicySimple object.
*
* @param list the list
* @pre $none
* @post $none
*/
public VmAllocationPolicyCombinedMostFullFirst(List<? extends Host> list) {
super(list);
setFreePes(new ArrayList<Integer>());
setFreeMips(new ArrayList<Long>());
setFreeBw(new ArrayList<Long>());
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<String, Host>());
setUsedPes(new HashMap<String, Integer>());
setUsedMips(new HashMap<String, Long>());
setUsedBw(new HashMap<String, Long>());
}
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<String, Host> getVmTable() {
return vmTable;
}
/**
* Sets the vm table.
*
* @param vmTable the vm table
*/
protected void setVmTable(Map<String, Host> vmTable) {
this.vmTable = vmTable;
}
/**
* Gets the used pes.
*
* @return the used pes
*/
protected Map<String, Integer> getUsedPes() {
return usedPes;
}
/**
* Sets the used pes.
*
* @param usedPes the used pes
*/
protected void setUsedPes(Map<String, Integer> usedPes) {
this.usedPes = usedPes;
}
/**
* Gets the free pes.
*
* @return the free pes
*/
protected List<Integer> getFreePes() {
return freePes;
}
/**
* Sets the free pes.
*
* @param freePes the new free pes
*/
protected void setFreePes(List<Integer> freePes) {
this.freePes = freePes;
}
protected Map<String, Long> getUsedMips() {
return usedMips;
}
protected void setUsedMips(Map<String, Long> usedMips) {
this.usedMips = usedMips;
}
protected Map<String, Long> getUsedBw() {
return usedBw;
}
protected void setUsedBw(Map<String, Long> usedBw) {
this.usedBw = usedBw;
}
protected List<Long> getFreeMips() {
return this.freeMips;
}
protected void setFreeMips(List<Long> freeMips) {
this.freeMips = freeMips;
}
protected List<Long> getFreeBw() {
return this.freeBw;
}
protected void setFreeBw(List<Long> freeBw) {
this.freeBw = freeBw;
}
/*
* (non-Javadoc)
* @see cloudsim.VmAllocationPolicy#optimizeAllocation(double, cloudsim.VmList, double)
*/
@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> 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;
}
}