This repository was archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPSO_DatacenterBroker.java
More file actions
99 lines (79 loc) · 3.55 KB
/
PSO_DatacenterBroker.java
File metadata and controls
99 lines (79 loc) · 3.55 KB
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
package PSO;
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.CloudSimTags;
import org.cloudbus.cloudsim.core.SimEvent;
import org.cloudbus.cloudsim.lists.VmList;
import utils.Constants;
import java.util.Arrays;
import java.util.List;
public class PSO_DatacenterBroker extends DatacenterBroker {
private double[] mapping;
public PSO_DatacenterBroker(String name) throws Exception {
super(name);
}
public void setMapping(double[] mapping) {
this.mapping = mapping;
}
private List<Cloudlet> assignCloudletsToVms(List<Cloudlet> cloudlist) {
int idx = 0;
for (Cloudlet cl : cloudlist) {
cl.setVmId((int) mapping[(idx++)]);
System.out.println("VM id to cloudlet is " + cl.getCloudletId() + " " + cl.getVmId());
}
// Debug prints
System.out.println("Mapping array size: " + mapping.length);
System.out.println("Mapping array values: " + Arrays.toString(mapping));
return cloudlist;
}
@Override
protected void submitCloudlets() {
List<Cloudlet> tasks = assignCloudletsToVms(getCloudletList());
int vmIndex = 0;
for (Cloudlet cloudlet : tasks) {
Vm vm;
// if user didn't bind this cloudlet and it has not been executed yet
if (cloudlet.getVmId() == -1) {
vm = getVmsCreatedList().get(vmIndex);
} else { // submit to the specific vm
vm = VmList.getById(getVmsCreatedList(), cloudlet.getVmId());
if (vm == null) { // vm was not created
Log.printLine(CloudSim.clock() + ": " + getName() + ": Postponing execution of cloudlet "
+ cloudlet.getCloudletId() + ": bount VM not available");
continue;
}
}
Log.printLine(CloudSim.clock() + ": " + getName() + ": Sending cloudlet "
+ cloudlet.getCloudletId() + " to VM #" + vm.getId());
cloudlet.setVmId(vm.getId());
sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.CLOUDLET_SUBMIT, cloudlet);
cloudletsSubmitted++;
vmIndex = (vmIndex + 1) % getVmsCreatedList().size();
getCloudletSubmittedList().add(cloudlet);
}
}
@Override
protected void processResourceCharacteristics(SimEvent ev) {
DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData();
getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics);
if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) {
distributeRequestsForNewVmsAcrossDatacenters();
}
}
protected void distributeRequestsForNewVmsAcrossDatacenters() {
int numberOfVmsAllocated = 0;
int i = 0;
final List<Integer> availableDatacenters = getDatacenterIdsList();
for (Vm vm : getVmList()) {
int datacenterId = availableDatacenters.get(i++ % availableDatacenters.size());
String datacenterName = CloudSim.getEntityName(datacenterId);
if (!getVmsToDatacentersMap().containsKey(vm.getId())) {
Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() + " in " + datacenterName);
sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm);
numberOfVmsAllocated++;
}
}
setVmsRequested(numberOfVmsAllocated);
setVmsAcks(0);
}
}