Created
May 28, 2020 03:55
-
-
Save ehzawad/478e26ed8458a4d34bb041c6265e417a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Title: CloudSim Toolkit | |
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds | |
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html | |
* | |
* Copyright (c) 2009-2012, The University of Melbourne, Australia | |
*/ | |
package org.cloudbus.cloudsim; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.cloudbus.cloudsim.core.CloudSim; | |
import examples.org.cloudbus.cloudsim.examples.CloudSimExampleInput1; | |
/** | |
* VmAllocationPolicySimple is an VmAllocationPolicy that chooses, as the host for a VM, the host | |
* with less PEs in use. | |
* | |
* @author Rodrigo N. Calheiros | |
* @author Anton Beloglazov | |
* @since CloudSim Toolkit 1.0 | |
*/ | |
public class VmAllocationPolicySimple extends VmAllocationPolicy { | |
/** The vm table. */ | |
private Map<String, Host> vmTable; | |
/** The used pes. */ | |
private Map<String, Integer> usedPes; | |
private Map<Vm, Host> vmAllocaton = new HashMap<Vm, Host>(); | |
/** The free pes. */ | |
private List<Integer> freePes; | |
/** | |
* Creates the new VmAllocationPolicySimple object. | |
* | |
* @param list the list | |
* @pre $none | |
* @post $none | |
*/ | |
public VmAllocationPolicySimple(List<? extends Host> list) { | |
super(list); | |
setFreePes(new ArrayList<Integer>()); | |
for (Host host : getHostList()) { | |
getFreePes().add(host.getNumberOfPes()); | |
} | |
setVmTable(new HashMap<String, Host>()); | |
setUsedPes(new HashMap<String, Integer>()); | |
} | |
/** | |
* 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) { | |
int requiredPes = vm.getNumberOfPes(); | |
boolean result = false; | |
int tries = 0; | |
List<Integer> freePesTmp = new ArrayList<Integer>(); | |
for (Integer freePes : getFreePes()) { | |
freePesTmp.add(freePes); | |
} | |
if (!getVmTable().containsKey(vm.getUid())) { // if this vm was not created | |
do {// we still trying until we find a host or until we try all of them | |
int moreFree = Integer.MIN_VALUE; | |
int lessFree = Integer.MAX_VALUE; | |
int idx = -1; | |
String allocationPolicy = CloudSimExampleInput1.allocationPolicy; | |
if(allocationPolicy.equals("default")) //Worst Fit { | |
// we want the host with less pes in use | |
for (int i = 0; i < freePesTmp.size(); i++) { | |
if (freePesTmp.get(i) > moreFree) { | |
moreFree = freePesTmp.get(i); | |
idx = i; | |
} | |
} | |
else if(allocationPolicy.equals("bestfit")){ | |
// we want the host with less pes in use | |
for (int i = 0; i < freePesTmp.size(); i++) { | |
if (freePesTmp.get(i) < lessFree && freePesTmp.get(i) > requiredPes){ | |
lessFree = freePesTmp.get(i); | |
idx = i; | |
} | |
} | |
} | |
Host host = getHostList().get(idx); | |
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); | |
result = true; | |
break; | |
} else { | |
freePesTmp.set(idx, Integer.MIN_VALUE); | |
} | |
tries++; | |
} while (!result && tries < getFreePes().size()); | |
} | |
return result; | |
} | |
private void printHostAllocation() { | |
List<Integer> pesTmp = new ArrayList<Integer>(); | |
int hostUnderU = 0; | |
int hostOverU = 0; | |
int hostFree = 0; | |
for (Integer freePes : getFreePes()) { | |
pesTmp.add(freePes); | |
} | |
int hostTotal = pesTmp.size(); | |
for (int i = 0; i < pesTmp.size(); i++) { | |
if (pesTmp.get(i) == 0) { | |
hostFree ++; | |
} | |
else if(pesTmp.get(i) < getHostList().get(i).getNumberOfPes()/2 ) | |
{ | |
hostUnderU ++; | |
} | |
else if(pesTmp.get(i) >= getHostList().get(i).getNumberOfPes()/2 ) | |
{ | |
hostOverU ++; | |
} | |
} | |
Log.printLine("================================="); | |
Log.printLine("Total host " + hostTotal); | |
Log.printLine("OverUtilized Host " + hostOverU); | |
Log.printLine("UnderUtilized Host " + hostUnderU); | |
Log.printLine("Free Host " + hostFree); | |
Log.printLine("================================="); | |
} | |
/** | |
* 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()); | |
int idx = getHostList().indexOf(host); | |
int pes = getUsedPes().remove(vm.getUid()); | |
if (host != null) { | |
host.vmDestroy(vm); | |
getFreePes().set(idx, getFreePes().get(idx) + pes); | |
} | |
} | |
/** | |
* 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; | |
} | |
public Map<Vm, Host>getVmAllocation() { | |
return vmAllocaton; | |
} | |
/** | |
* 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; | |
} | |
/* | |
* (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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment