-
-
Save yzxlr/5866935 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package org.cloudbus.cloudsim.examples; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
import org.cloudbus.cloudsim.Host; | |
public final class CircularHostList implements Iterable<Host> { | |
private final List<Host> host_list = new LinkedList<Host>(); | |
private int ini; | |
public CircularHostList(List<? extends Host> hosts) { | |
this.host_list.addAll(hosts); | |
} | |
public boolean add(Host host) { | |
return this.host_list.add(host); | |
} | |
public boolean remove(Host host2Remove) { | |
return this.host_list.remove(host2Remove); | |
} | |
public Host next() { | |
Host host = null; | |
if (!host_list.isEmpty()) { | |
int index = (this.ini++ % this.host_list.size()); | |
host = this.host_list.get(index); | |
} | |
return host; | |
} | |
@Override | |
public Iterator<Host> iterator() { | |
return get().iterator(); | |
} | |
public List<Host> get() { | |
return Collections.unmodifiableList(this.host_list); | |
} | |
public Host getWithMinimumNumberOfPesEquals(int numberOfPes) { | |
List<Host> hosts = this.orderedAscByAvailablePes().get(); | |
for (int i = 0; i < hosts.size(); i++) { | |
if (hosts.get(i).getNumberOfFreePes() >= numberOfPes) { | |
return hosts.get(i); | |
} | |
} | |
return null; | |
} | |
public int size() { | |
return this.host_list.size(); | |
} | |
public CircularHostList orderedAscByAvailablePes() { | |
List<Host> list = new ArrayList<Host>(this.host_list); | |
Collections.sort(list, new Comparator<Host>() { | |
@Override | |
public int compare(Host o1, Host o2) { | |
return Integer.valueOf(o1.getNumberOfFreePes()).compareTo( | |
o2.getNumberOfFreePes()); | |
} | |
}); | |
return new CircularHostList(list); | |
} | |
} |
This file contains hidden or 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
package org.cloudbus.cloudsim.examples; | |
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.core.CloudSim; | |
/** | |
* Round-Robin {@link Vm} allocation policy. | |
* | |
* @author alessandro | |
*/ | |
public class RoudRobinVmAllocationPolicy extends org.cloudbus.cloudsim.VmAllocationPolicy { | |
private final Map<String, Host> vm_table = new HashMap<String, Host>(); | |
private final CircularHostList hosts; | |
public RoudRobinVmAllocationPolicy(List<? extends Host> list) { | |
super(list); | |
this.hosts = new CircularHostList(list); | |
} | |
@Override | |
public boolean allocateHostForVm(Vm vm) { | |
if (this.vm_table.containsKey(vm.getUid())) { | |
return true; | |
} | |
boolean vm_allocated = false; | |
Host host = this.hosts.next(); | |
if (host != null) { | |
vm_allocated = this.allocateHostForVm(vm, host); | |
} | |
return vm_allocated; | |
} | |
@Override | |
public boolean allocateHostForVm(Vm vm, Host host) | |
{ | |
if (host != null && host.vmCreate(vm)) | |
{ | |
vm_table.put(vm.getUid(), host); | |
Log.formatLine("%.4f: VM #" + vm.getId() + " has been allocated to the host#" + host.getId() + | |
" datacenter #" + host.getDatacenter().getId() + "(" + host.getDatacenter().getName() + ") #", | |
CloudSim.clock()); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) { | |
return null; | |
} | |
@Override | |
public void deallocateHostForVm(Vm vm) { | |
Host host = this.vm_table.remove(vm.getUid()); | |
if (host != null) { | |
host.vmDestroy(vm); | |
} | |
} | |
@Override | |
public Host getHost(Vm vm) { | |
return this.vm_table.get(vm.getUid()); | |
} | |
@Override | |
public Host getHost(int vmId, int userId) { | |
return this.vm_table.get(Vm.getUid(userId, vmId)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment