Skip to content

Commit

Permalink
Closes #10
Browse files Browse the repository at this point in the history
- Several classes were refactored to stop working with IDs and starting providing a true object oriented framework.
  Lots of attributes that were defined as a List of Integer or as a Map where the key was Integer, to store
  the ID of entities, were changed to store the object itself not its ID.
  It makes the framework far less error prone, since the Lists and Maps will just accept
  the defined object.
- It makes even clear to understand the framework code and to use it, since
  that when a developer look at some of such Lists or Maps, he/she will immediately know
  what such Lists/Maps are storing.
- Storing actual objects instead of their IDs also improves the framework performance
  since it doesn't have to get the object ID and look for the actual object inside
  another List that in fact stored the objects (as it was common to be done).
- Message passing mechanisms was also improved, storing actual objects into the SimEvent.data attribute
  instead of the object ID. The source and destionation of a message sent is also performed internally
  using just the ID, because changing these behaviour would require far more work.
- The documentation of such Lists/Maps was reviewed and updated.
- Network related classes were those ones that received lots of changes, mainly NetworkPacket implementing classes.
  • Loading branch information
manoelcampos committed Dec 17, 2016
1 parent 56cad81 commit 60f00fd
Show file tree
Hide file tree
Showing 59 changed files with 1,494 additions and 1,301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ protected void createNetwork(NetworkDatacenter datacenter) {

for (NetworkHost host : datacenter.<NetworkHost>getHostList()) {
int switchNum = host.getId() / edgeSwitches[0].getPorts();
edgeSwitches[switchNum].getHostList().put(host.getId(), host);
edgeSwitches[switchNum].connectHost(host);
datacenter.addHostToSwitch(host, edgeSwitches[switchNum]);
host.setEdgeSwitch(edgeSwitches[switchNum]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void createNetwork(NetworkDatacenter datacenter) {

for (NetworkHost host : datacenter.<NetworkHost>getHostList()) {
int switchNum = host.getId() / edgeSwitches[0].getPorts();
edgeSwitches[switchNum].getHostList().put(host.getId(), host);
edgeSwitches[switchNum].connectHost(host);
datacenter.addHostToSwitch(host, edgeSwitches[switchNum]);
host.setEdgeSwitch(edgeSwitches[switchNum]);
}
Expand Down Expand Up @@ -267,7 +267,7 @@ private void addSendTask(
*/
private void addReceiveTask(NetworkCloudlet cloudlet, NetworkCloudlet sourceCloudlet) {
CloudletReceiveTask task = new CloudletReceiveTask(
cloudlet.getTasks().size(), sourceCloudlet.getVm().getId());
cloudlet.getTasks().size(), sourceCloudlet.getVm());
task.setMemory(TASK_RAM);
task.setNumberOfExpectedPacketsToReceive(NUMBER_OF_PACKETS_TO_SEND);
cloudlet.addTask(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public List<NetworkCloudlet> createNetworkCloudlets(DatacenterBroker broker){
//NetworkCloudlet 0 wait data from other cloudlets, while the other cloudlets send data
if (i==0){
for(int j=1; j < NETCLOUDLETS_FOR_EACH_APP; j++) {
task = new CloudletReceiveTask(taskStageId++, selectedVms.get(j+1).getId());
task = new CloudletReceiveTask(taskStageId++, selectedVms.get(j+1));
task.setMemory(memory);
cloudlet.addTask(task);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void addSendTask(
*/
private void addReceiveTask(NetworkCloudlet cloudlet, NetworkCloudlet sourceCloudlet) {
CloudletReceiveTask task = new CloudletReceiveTask(
cloudlet.getTasks().size(), sourceCloudlet.getVm().getId());
cloudlet.getTasks().size(), sourceCloudlet.getVm());
task.setMemory(NETCLOUDLET_RAM);
task.setNumberOfExpectedPacketsToReceive(NUMBER_OF_PACKETS_TO_SEND);
cloudlet.addTask(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ protected void createNetwork(NetworkDatacenter datacenter) {

for (NetworkHost host : datacenter.<NetworkHost>getHostList()) {
int switchNum = host.getId() / edgeSwitches[0].getPorts();
edgeSwitches[switchNum].getHostList().put(host.getId(), host);
edgeSwitches[switchNum].connectHost(host);
datacenter.addHostToSwitch(host, edgeSwitches[switchNum]);
host.setEdgeSwitch(edgeSwitches[switchNum]);
}
Expand Down
4 changes: 2 additions & 2 deletions cloudsim-plus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<!-- Switch OFF the doclint in Java 8 -->
<!-- AbstractSwitch OFF the doclint in Java 8 -->
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
Expand Down Expand Up @@ -133,7 +133,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<!-- Switch OFF the doclint in Java 8 -->
<!-- AbstractSwitch OFF the doclint in Java 8 -->
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.cloudbus.cloudsim.datacenters.Datacenter;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.power.PowerHost;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudsimplus.listeners.HostToVmEventInfo;

Expand All @@ -37,7 +36,7 @@ public abstract class VmAllocationPolicyAbstract implements VmAllocationPolicy {
/**
* @see #getVmHostMap()
*/
private Map<String, Host> vmTable;
private Map<Vm, Host> vmTable;

/**
* @see #getDatacenter()
Expand All @@ -50,7 +49,7 @@ public abstract class VmAllocationPolicyAbstract implements VmAllocationPolicy {
/**
* @see #getUsedPes()
*/
private Map<String, Integer> usedPes;
private Map<Vm, Integer> usedPes;

/**
* Creates a new VmAllocationPolicy object.
Expand All @@ -73,7 +72,7 @@ public <T extends Host> List<T> getHostList() {
*
* @return the VM map
*/
protected Map<String, Host> getVmHostMap() {
protected Map<Vm, Host> getVmHostMap() {
return vmTable;
}

Expand All @@ -82,7 +81,7 @@ protected Map<String, Host> getVmHostMap() {
*
* @param vmTable the vm table
*/
protected final void setVmTable(Map<String, Host> vmTable) {
protected final void setVmTable(Map<Vm, Host> vmTable) {
this.vmTable = vmTable;
}

Expand All @@ -98,7 +97,7 @@ protected void mapVmToPm(Vm vm, Host host) {
return;
}

getVmHostMap().put(vm.getUid(), host);
getVmHostMap().put(vm, host);
HostToVmEventInfo info =
new HostToVmEventInfo(host.getSimulation().clock(), host, vm);
vm.getOnHostAllocationListener().update(info);
Expand All @@ -118,7 +117,7 @@ protected Host unmapVmFromPm(Vm vm) {
return Host.NULL;
}

Host host = getVmHostMap().remove(vm.getUid());
Host host = getVmHostMap().remove(vm);
if(Objects.isNull(host)) {
return Host.NULL;
}
Expand Down Expand Up @@ -186,11 +185,11 @@ protected final VmAllocationPolicy setHostFreePesMap(Map<Host, Integer> hostFree

/**
* Gets the map between each VM and the number of PEs used. The map key is a
* VM UID and the value is the number of used Pes for that VM.
* VM and the value is the number of used Pes for that VM.
*
* @return the used PEs map
*/
protected Map<String, Integer> getUsedPes() {
protected Map<Vm, Integer> getUsedPes() {
return usedPes;
}

Expand All @@ -199,7 +198,7 @@ protected Map<String, Integer> getUsedPes() {
*
* @param usedPes the used pes
*/
protected final void setUsedPes(Map<String, Integer> usedPes) {
protected final void setUsedPes(Map<Vm, Integer> usedPes) {
this.usedPes = usedPes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

import java.util.*;

import org.cloudbus.cloudsim.core.UniquelyIdentificable;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.power.PowerHost;
import org.cloudbus.cloudsim.util.Log;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.vms.VmSimple;

/**
* A VmAllocationPolicy implementation that chooses, as
Expand Down Expand Up @@ -57,7 +56,7 @@ public boolean allocateHostForVm(Vm vm) {
}

// if this vm was already created
if (getVmHostMap().containsKey(vm.getUid())) {
if (getVmHostMap().containsKey(vm)) {
return false;
}

Expand All @@ -69,7 +68,7 @@ public boolean allocateHostForVm(Vm vm) {
final int hostFreePes = entry.getValue();
if (host.vmCreate(vm)) {
mapVmToPm(vm, host);
getUsedPes().put(vm.getUid(), vm.getNumberOfPes());
getUsedPes().put(vm, vm.getNumberOfPes());
getHostFreePesMap().put(host, hostFreePes - vm.getNumberOfPes());
if(!hostsWhereVmCreationFailed.isEmpty()){
Log.printFormattedLine("[VmAllocationPolicy] VM #%d was successfully allocated to Host #%d", vm.getId(), host.getId());
Expand Down Expand Up @@ -104,7 +103,7 @@ private Map.Entry<Host, Integer> getHostWithLessUsedPes(List<Host> ignoredHosts)
@Override
public void deallocateHostForVm(Vm vm) {
Host host = unmapVmFromPm(vm);
int pes = getUsedPes().remove(vm.getUid());
int pes = getUsedPes().remove(vm);
if (host != Host.NULL) {
host.destroyVm(vm);
getHostFreePesMap().put(host, getHostFreePesMap().get(host) + pes);
Expand All @@ -113,12 +112,12 @@ public void deallocateHostForVm(Vm vm) {

@Override
public Host getHost(Vm vm) {
return getVmHostMap().get(vm.getUid());
return getVmHostMap().get(vm);
}

@Override
public Host getHost(int vmId, int userId) {
return getVmHostMap().get(VmSimple.getUid(userId, vmId));
return getVmHostMap().get(UniquelyIdentificable.getUid(userId, vmId));
}

/**
Expand All @@ -141,7 +140,7 @@ public boolean allocateHostForVm(Vm vm, Host host) {

mapVmToPm(vm, host);
final int requiredPes = vm.getNumberOfPes();
getUsedPes().put(vm.getUid(), requiredPes);
getUsedPes().put(vm, requiredPes);
getHostFreePesMap().put(host, getHostFreePesMap().get(host) - requiredPes);

Log.printFormattedLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import java.util.Map;
import java.util.Objects;

import org.cloudbus.cloudsim.core.UniquelyIdentificable;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.power.PowerHost;
import org.cloudbus.cloudsim.util.Log;
import org.cloudbus.cloudsim.hosts.power.PowerHostSimple;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicyAbstract;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.cloudbus.cloudsim.core.Simulation;

/**
Expand All @@ -42,7 +41,7 @@ public abstract class PowerVmAllocationPolicyAbstract extends VmAllocationPolicy
/**
* @see #getVmHostMap()
*/
private final Map<String, Host> vmHostMap = new HashMap<>();
private final Map<Vm, Host> vmHostMap = new HashMap<>();

/**
* Creates a PowerVmAllocationPolicy.
Expand All @@ -65,7 +64,7 @@ public boolean allocateHostForVm(Vm vm, Host host) {
}

if (host.vmCreate(vm)) { // if vm has been successfully created in the host
getVmHostMap().put(vm.getUid(), host);
getVmHostMap().put(vm, host);
Log.printFormattedLine(
"%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(),
simulation.clock());
Expand All @@ -86,20 +85,20 @@ public PowerHost findHostForVm(Vm vm) {

@Override
public void deallocateHostForVm(Vm vm) {
Host host = getVmHostMap().remove(vm.getUid());
Host host = getVmHostMap().remove(vm);
if (!Objects.isNull(host)) {
host.destroyVm(vm);
}
}

@Override
public Host getHost(Vm vm) {
return getVmHostMap().get(vm.getUid());
return getVmHostMap().get(vm);
}

@Override
public Host getHost(int vmId, int userId) {
return getVmHostMap().get(VmSimple.getUid(userId, vmId));
return getVmHostMap().get(UniquelyIdentificable.getUid(userId, vmId));
}

/**
Expand All @@ -108,7 +107,7 @@ public Host getHost(int vmId, int userId) {
*
* @return
*/
public Map<String, Host> getVmHostMap() {
public Map<Vm, Host> getVmHostMap() {
return vmHostMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl

/**
* A map of CPU utilization history (in percentage) for each host, where
* each key is a host id and each value is the CPU utilization percentage history.
* each key is a hos and each value is the CPU utilization percentage history.
*/
private final Map<Integer, List<Double>> utilizationHistory = new HashMap<>();
private final Map<Host, List<Double>> utilizationHistory = new HashMap<>();

/**
* The metric history.
Expand All @@ -71,13 +71,13 @@ public abstract class PowerVmAllocationPolicyMigrationAbstract extends PowerVmAl
* threshold, other it stores utilization threshold or predicted
* utilization, that is very confusing.
*/
private final Map<Integer, List<Double>> metricHistory = new HashMap<>();
private final Map<Host, List<Double>> metricHistory = new HashMap<>();

/**
* The time when entries in each history list was added. All history lists
* are updated at the same time.
*/
private final Map<Integer, List<Double>> timeHistory = new HashMap<>();
private final Map<Host, List<Double>> timeHistory = new HashMap<>();

/**
* The history of time spent in VM selection every time the optimization of
Expand Down Expand Up @@ -505,7 +505,7 @@ protected void restoreAllocation() {
"Couldn't restore VM #%d on host #%d",
vm.getId(), host.getId()));
}
getVmHostMap().put(vm.getUid(), host);
getVmHostMap().put(vm, host);
}
}

Expand Down Expand Up @@ -576,15 +576,14 @@ protected double getUtilizationOfCpuMips(PowerHost host) {
*/
protected void addHistoryEntryIfAbsent(PowerHost host, double metric) {
Simulation simulation = host.getSimulation();
final int hostId = host.getId();
getTimeHistory().putIfAbsent(hostId, new LinkedList<>());
getUtilizationHistory().putIfAbsent(hostId, new LinkedList<>());
getMetricHistory().putIfAbsent(hostId, new LinkedList<>());

if (!getTimeHistory().get(hostId).contains(simulation.clock())) {
getTimeHistory().get(hostId).add(simulation.clock());
getUtilizationHistory().get(hostId).add(host.getUtilizationOfCpu());
getMetricHistory().get(hostId).add(metric);
getTimeHistory().putIfAbsent(host, new LinkedList<>());
getUtilizationHistory().putIfAbsent(host, new LinkedList<>());
getMetricHistory().putIfAbsent(host, new LinkedList<>());

if (!getTimeHistory().get(host).contains(simulation.clock())) {
getTimeHistory().get(host).add(simulation.clock());
getUtilizationHistory().get(host).add(host.getUtilizationOfCpu());
getMetricHistory().get(host).add(metric);
}
}

Expand Down Expand Up @@ -620,7 +619,7 @@ protected PowerVmSelectionPolicy getVmSelectionPolicy() {
*
* @return the utilization history
*/
public Map<Integer, List<Double>> getUtilizationHistory() {
public Map<Host, List<Double>> getUtilizationHistory() {
return utilizationHistory;
}

Expand All @@ -629,7 +628,7 @@ public Map<Integer, List<Double>> getUtilizationHistory() {
*
* @return the metric history
*/
public Map<Integer, List<Double>> getMetricHistory() {
public Map<Host, List<Double>> getMetricHistory() {
return metricHistory;
}

Expand All @@ -638,7 +637,7 @@ public Map<Integer, List<Double>> getMetricHistory() {
*
* @return the time history
*/
public Map<Integer, List<Double>> getTimeHistory() {
public Map<Host, List<Double>> getTimeHistory() {
return timeHistory;
}

Expand Down
Loading

0 comments on commit 60f00fd

Please sign in to comment.