forked from jgera/SibSUTIS-cloudsim-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VmAllocationPolicyFirstFit.java
102 lines (89 loc) · 2.52 KB
/
VmAllocationPolicyFirstFit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.cloudbus.cloudsim.examples.SibSUTIS;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by andrey on 1/8/15.
*/
public class VmAllocationPolicyFirstFit extends VmAllocationPolicy {
/** The vm table. */
private Map<String, Host> vmTable;
/**
* Creates the new VmAllocationPolicySimple object.
*
* @param list the list
* @pre $none
* @post $none
*/
public VmAllocationPolicyFirstFit(List<? extends Host> list) {
super(list);
setVmTable(new HashMap<String, Host>());
}
/**
* Sets the vm table.
*
* @param vmTable the vm table
*/
protected void setVmTable(Map<String, Host> vmTable) {
this.vmTable = vmTable;
}
/**
* Gets the vm table.
*
* @return the vm table
*/
public Map<String, Host> getVmTable() {
return vmTable;
}
private void printLogMsg(String msg) {
Log.print("FF_Allocator: " + msg + "\n");
}
@Override
public boolean allocateHostForVm(Vm vm) {
int idx = 0;
for (Host host : getHostList()) {
idx++;
if(host.isSuitableForVm(vm)) {
boolean result = host.vmCreate(vm);
if(result) {
printLogMsg("Vm:"+vm.getId()+ "Ram: "+vm.getRam() +" Allocated on " + host.getId());
getVmTable().put(vm.getUid(), host);
return true;
} else {
printLogMsg("Vm creation failed on " + idx + " Lets try another host");
continue;
}
}
}
return false;
}
@Override
public boolean allocateHostForVm(Vm vm, Host host) {
printLogMsg("Allocate specified host for vm");
return false;
}
@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
return null;
}
@Override
public void deallocateHostForVm(Vm vm) {
Host host = getVmTable().remove(vm.getUid());
if (host != null) {
host.vmDestroy(vm);
}
}
@Override
public Host getHost(Vm vm){
return getVmTable().get(vm.getUid());
}
@Override
public Host getHost(int vmId, int userId) {
return getVmTable().get(Vm.getUid(userId, vmId));
}
}