/* * 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-2010, 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;/** * 虛擬機器分配策略,分配滿足條件的最少的pe的主機 * 欄位裡繼承了一個主機列表,vm和主機映射,主機usedpes,主機freepes * VmAllocationPolicySimple is an VmAllocationPolicy that * chooses, as the host for a VM, the host with * less PEs in use. * * @authorRodrigo N. Calheiros * @authorAnton Beloglazov * @sinceCloudSim Toolkit 1.0 */public class VmAllocationPolicySimple extends VmAllocationPolicy {/** The vm table. */private Map<String, Host> vmTable;/** The used pes. */private Map<String, Integer> usedPes;/** The free pes. 記錄了每一台主機閒置pe*/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.getPesNumber());}setVmTable(new HashMap<String, Host>());setUsedPes(new HashMap<String, Integer>());}/** * 對一個給定 的vm分配一個host * 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 */@Overridepublic boolean allocateHostForVm(Vm vm) {int requiredPes = vm.getPesNumber();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 createddo {//we still trying until we find a host or until we try all of themint moreFree = Integer.MIN_VALUE;int idx = -1;//we want the host with less pes in usefor (int i=0; i < freePesTmp.size(); i++) { //尋找具有最大空閑pe的hostif (freePesTmp.get(i) > moreFree) {moreFree = freePesTmp.get(i);idx = i;}}Host host = getHostList().get(idx);result = host.vmCreate(vm); //在此host上建立vmif (result) { //if vm were succesfully created in the host//Log.printLine("VmAllocationPolicy: VM #"+vm.getVmId()+ "Chosen host: #"+host.getMachineID()+" idx:"+idx);getVmTable().put(vm.getUid(), host); //vm和host的對應表getUsedPes().put(vm.getUid(), requiredPes); //vm和使用的pe對應表getFreePes().set(idx, getFreePes().get(idx) - requiredPes); //修改對應主機閒置peresult = true;break;} else {freePesTmp.set(idx, Integer.MIN_VALUE);}tries++;} while (!result && tries < getFreePes().size());}return result;}/** * 回收host * Releases the host used by a VM. * * @param vm the vm * * @pre $none * @post none */@Overridepublic 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 */@Overridepublic 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 */@Overridepublic 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;}/* (non-Javadoc) * @see cloudsim.VmAllocationPolicy#optimizeAllocation(double, cloudsim.VmList, double) */@Overridepublic List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {// TODO Auto-generated method stubreturn null;}/* (non-Javadoc) * 分配特定主機一個特定的vm * @see org.cloudbus.cloudsim.VmAllocationPolicy#allocateHostForVm(org.cloudbus.cloudsim.Vm, org.cloudbus.cloudsim.Host) */@Overridepublic boolean allocateHostForVm(Vm vm, Host host) {if (host.vmCreate(vm)) { //if vm has been succesfully created in the hostgetVmTable().put(vm.getUid(), host);Log.formatLine("%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(), CloudSim.clock());return true;}return false;}}