forked from jgera/SibSUTIS-cloudsim-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VmAllocationPolicyFFDProd.java
132 lines (113 loc) · 4.3 KB
/
VmAllocationPolicyFFDProd.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.cloudbus.cloudsim.examples.SibSUTIS;
import javafx.util.Pair;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicy;
import java.util.*;
public class VmAllocationPolicyFFDProd extends VmAllocationPolicy implements ListAllocationPolicy{
/** The vm table. */
private Map<String, Host> vmTable;
public VmAllocationPolicyFFDProd(List<? extends Host> list) {
super(list);
setVmTable(new HashMap<String, Host>());
}
protected void setVmTable(Map<String, Host> vmTable) {
this.vmTable = vmTable;
}
public Map<String, Host> getVmTable() {
return vmTable;
}
private void printLogMsg(String msg) {
Log.print("FFDProd_Allocator: " + msg + "\n");
}
private Vector<Pair<Vm,Double>> calculateWeightsForVms(List<Vm> vmList) {
Vector<Pair<Vm,Double>> vector = new Vector<Pair<Vm, Double>>(vmList.size());
Double weight;
for(Vm vm: vmList) {
weight = 1.0d*vm.getNumberOfPes();
weight *= vm.getSize();
weight *= vm.getRam();
vector.add(new Pair<Vm, Double>(vm,weight));
}
return vector;
}
@Override
public boolean allocateHostForVm(Vm vm) {
//Do not even try to do this!
throw new RuntimeException("Not implemented!", null);
}
@Override
public boolean allocateHostForVmList(List<Vm> vmsToAllocate) {
//Bin-centric
printLogMsg("Allocate host for vmList: "+vmsToAllocate.size());
Vector<Pair<Vm, Double>> vmsVector = calculateWeightsForVms(vmsToAllocate);
Collections.sort(vmsVector, new Comparator<Pair<Vm, Double>>() {
@Override
public int compare(Pair<Vm, Double> o1, Pair<Vm, Double> o2) {
return (int)(o1.getValue() - o2.getValue());
}
});
Collections.reverse(vmsVector);
long iterationCount = 0;
long iterationThreshold = getHostList().size() * vmsVector.size();
for (Host host: getHostList()) {
//All items allocated
printLogMsg("Look at host: "+host.getId()+"Vms;");
if(vmsVector.size() <= 0) {
printLogMsg("We're allocate all requested vms");
break;
}
//first item is largest
for (int i = 0; i < vmsVector.size(); i++) {
//I want to avoid cycling
iterationCount++;
assert(iterationCount<=iterationThreshold);
Vm vm = vmsVector.get(i).getKey();
if (host.isSuitableForVm(vm)) {
host.vmCreate(vm);
getVmTable().put(vm.getUid(), host);
vmsVector.remove(i);
printLogMsg("Allocate vm: "+vm.getId()+" Ram: "+vm.getRam() + " on host: "+host.getId());
i -= 1;
} else {
//Log.printLine("Cannot allocate vm_ram:"+vm.getRam()+" mips:"+vm.getMips()+
//" On host:"+host.getRamProvisioner().getAvailableRam() +" pes:"+host.getNumberOfFreePes() );
}
}
Log.printLine("Left host: "+host.getId()+" With ram: "+host.getRamProvisioner().getAvailableRam() +" pes:"+host.getNumberOfFreePes());
}
if(vmsVector.size() == 0) {
printLogMsg("All vms successfully allocated!");
return true;
} else {
//TODO: deallocate vms
printLogMsg("ERROR! Vms doesn't allocated");
return false;
}
}
@Override
public boolean allocateHostForVm(Vm vm, Host host) {
//Do not even try to do this!
throw new RuntimeException("Not implemented!", null);
}
@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));
}
}