Commit d9f30a5d authored by Kaio Oliveira's avatar Kaio Oliveira
Browse files

Merge branch 'sla-simulation' of...

Merge branch 'sla-simulation' of https://git.lsd.ufcg.edu.br/lenovo-autonomic/cloudsimplus into sla-simulation
parents 5c2b4f84 2d127d23
......@@ -33,6 +33,7 @@ import org.cloudbus.cloudsim.vms.Vm;
* @author Kaio Kassiano Moura Oliveira (@kaiokmo)
* @author Eduardo de Lucena Falcao (@eduardolfalcao)
*/
public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
private static final String LOOSE = "LOOSE";
......@@ -116,6 +117,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
}
List<Vm> sufferingVms = new ArrayList<Vm>();
//FIXME sort to prioritize the VMs that are suffering more
sufferingVms.addAll(getVmsAboutToViolateSla(tightSlaHosts));
//for each heaven host
......@@ -192,6 +194,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
* @param h the host
* @return a list of VMs
*/
//TODO This method needs to get the looseSlaHosts as a parameter?
protected List<Vm> getVmsWithEnoughCredit(List<Host> looseSlaHosts){
List<Vm> candidateVmsToMigrate = new ArrayList<Vm>();
......@@ -199,7 +202,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
for (Host h : looseSlaHosts) {
for (Vm vm : h.getVmList()) {
// TODO (IF) Create method with code bellow
if (vm.getMonitor().getCumulativeVmViolationTime(clock) + MIGRATION_TIME <=
vm.getMonitor().getMaximumAllowedSlaViolationTime()) {
candidateVmsToMigrate.add(vm);
......@@ -285,6 +288,7 @@ public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
return allocationPolicy.findHostForVm(vm);
}
//TODO Rename method name
public int getMIGRATION_TIME() {
return MIGRATION_TIME;
}
......
package org.cloudbus.cloudsim.allocationpolicies.migration;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.allocationpolicies.VmAllocationPolicy;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.provisioners.ResourceProvisionerShared;
import org.cloudbus.cloudsim.vms.Vm;
/**
* A VM allocation policy that tries to exploit a VM's SLA to
* better use the resources of a host, reducing fragmentation.
*
* To accomplish this task, the allocation policy continually
* keeps track of all VMs and migrate them from hosts in which
* the SLA contracted is delivered (SL Loose) to hosts in which
* the instantaneous SLA may de be violated (SL Tight), or vice-
* versa.
*
* The creation of the 'Tight' hosts is dynamic. Although,
* the maximum number of 'Tight' hosts is static
*
* For more information, please refer to this paper:
*
* @author Rafael Vieira Falcao (@rafaelvfalc)
*/
public class VmAllocationPolicyMigrationSlaFixedTightHost extends VmAllocationPolicyMigrationSla {
private int vmsThreshold = 0;
public VmAllocationPolicyMigrationSlaFixedTightHost(VmAllocationPolicy allocationPolicy, int migrationTime, int vmTresshold) {
super(allocationPolicy, migrationTime);
setVmsThreshold(vmTresshold);
}
@Override
public Map<Vm, Host> getOptimizedAllocationMap(
final List<? extends Vm> vmList) {
int numberCreditVms = getVmsWithEnoughCredit(getHostsWithLooseSla()).size();
if(numberCreditVms >= getVmsThreshold()) {
// The Tight host can be created
// Select the Loose host with more credit Vms
Host bestCandidateHost = getHostCandidate();
// Make it a Tight host
bestCandidateHost.setIopsProvisioner(new ResourceProvisionerShared());
}
// Run Migration Sla Algorithm
return super.getOptimizedAllocationMap(vmList);
}
/*
* Gets the best candidate host to be the new Tight host
* in order to optimize the number of migrations to consolidate all
* Credit Vms in the Tight host.
*/
public Host getHostCandidate() {
List<Host> looseHosts = getHostsWithLooseSla();
final double clock = getDatacenter().getSimulation().clock();
int actualHighestCreditVms = 0;
Host bestCandidateHost = null;
for (Host host : looseHosts) {
int creditVms = 0;
for (Vm vm : host.getVmList()) {
if (vm.getMonitor().getVmCredit(clock) - getMIGRATION_TIME() > 0) {
creditVms++;
}
}
if(creditVms > actualHighestCreditVms) {
actualHighestCreditVms = creditVms;
bestCandidateHost = host;
}
}
return bestCandidateHost;
}
private int getVmsThreshold() {
return vmsThreshold;
}
private void setVmsThreshold(int newThreshold) {
this.vmsThreshold = newThreshold;
}
}
package org.cloudsimplus.slametrics;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.cloudbus.cloudsim.vms.Vm;
......@@ -10,6 +12,7 @@ import org.cloudbus.cloudsim.vms.VmStateHistoryEntry;
* A class that uses the history and SLA of a VM to monitor
* its credit and/or SLA violation time.
* @author Eduardo de Lucena Falcao (@eduardolfalcao)
* @author Lucas Medeiros Cavalcante (@lcavalcante)
*/
public class SlaMonitor {
......@@ -19,10 +22,117 @@ public class SlaMonitor {
private Vm vm;
private Map<Integer,Boolean> violations;
private Map<Integer,Double> credits;
private int lastElement;
public SlaMonitor(Vm vm) {
this.vm = vm;
this.violations = new HashMap<Integer,Boolean>();
this.credits = new HashMap<Integer,Double>();
this.lastElement = 0;
}
public double getCumulativeCredit(double time) {
double availability = vm.getContract().getAvailabilityMetric().getMinDimension().getValue();
List<VmStateHistoryEntry> stateList = vm.getStateHistory();
double cumulativeCredit = 0;
ListIterator<VmStateHistoryEntry> it = vm.getStateHistory().listIterator(lastElement);
if((int) time <= 0) {
return 0;
}
while(it.hasNext()) {
VmStateHistoryEntry state = it.next();
double provisioned = vm.getContract().getIopsProvisioned().getMinDimension().getValue();
if((int) state.getTime() < (int) time) {
if(credits.containsKey((int) time)) {
cumulativeCredit = credits.get((int) time);
} else {
double dif = calculateMissingCredit((int) state.getTime(), availability, provisioned);
cumulativeCredit = putCredits(state, provisioned, dif, availability);
lastElement = it.nextIndex();
}
}
if ((int) state.getTime() == (int) time){
if(credits.containsKey((int) time)) {
cumulativeCredit = credits.get((int) time);
} else {
double dif = calculateMissingCredit((int) state.getTime(), availability, provisioned);
cumulativeCredit = putCredits(state, provisioned, dif, availability);
lastElement = it.nextIndex();
}
}
}
if(stateList.get(lastElement - 1).getTime() < time) {
Double previous = credits.get((int) stateList.get(lastElement - 1).getTime());
Double preprevious = credits.get((int) stateList.get(lastElement - 2).getTime());;
if (previous > preprevious) {
cumulativeCredit = previous - (((int) time - stateList.get(lastElement - 1).getTime())) * (1 / availability);
} else {
cumulativeCredit = previous + (((int) time - stateList.get(lastElement - 1).getTime())) * (1 / availability);
}
}
return cumulativeCredit;
}
private Double putCredits(VmStateHistoryEntry state, double provisioned, double dif, double availability) {
Double cumulativeCredit;
if(state.getAllocatedIops() < provisioned) {
credits.put((int) state.getTime(), dif - (1 / availability));
}
else {
credits.put((int) state.getTime(), dif + (1 / availability));
}
cumulativeCredit = credits.get((int) state.getTime());
return cumulativeCredit;
}
private double calculateMissingCredit(int time, double availability, double provisioned) {
double prevTime;
VmStateHistoryEntry state;
if(lastElement - 1 > 0) {
if(lastElement - 1 == 0) {
state = vm.getStateHistory().get(lastElement);
} else {
state = vm.getStateHistory().get(lastElement - 1);
}
}
else
state = vm.getStateHistory().get(0);
prevTime = state.getTime();
double previous = previousCredit(time);
double preprevious = previousCredit((int) prevTime);
Double dif;
if (previous == preprevious) {
return previous;
}
if(state.getAllocatedIops() > provisioned)
dif = previous + (1 / availability) * (int )(time - prevTime - 1);
else
dif = previous - (1 / availability) * (int )(time - prevTime - 1);
return dif;
}
private Double previousCredit(int time) {
Double credit = 0.0;
while(time >= 0) {
time--;
if(this.credits.containsKey(time)) {
credit = this.credits.get(time);
break;
}
}
return credit;
}
public boolean isTheIopsSlaBeingViolated(double time){
......
......@@ -18,6 +18,7 @@ public class SlaMonitorTest {
private static final long MINIMUM_IOPS = 500;
private static final double MINIMUM_AVAILABILITY = 99.9;
private static final double STEP_CREDIT = 1 / MINIMUM_AVAILABILITY;
private static final long INITIAL_CREDIT = (long) ((100-MINIMUM_AVAILABILITY)*SlaMonitor.SECONDS_OF_A_MONTH);
private static SlaContract contract;
......@@ -74,6 +75,30 @@ public class SlaMonitorTest {
assertFalse(vm.getMonitor().isTheIopsSlaBeingViolated(10));
}
@Test
public void getVmCredit_test1() {
Vm vm = new VmSimple(0, 0);
vm.addStateHistoryEntry(new VmStateHistoryEntry(1,0,0,400,0,false));
vm.addStateHistoryEntry(new VmStateHistoryEntry(2,0,0,500,0,false));
vm.addStateHistoryEntry(new VmStateHistoryEntry(3,0,0,500,0,false));
vm.addStateHistoryEntry(new VmStateHistoryEntry(4,0,0,600,0,false));
vm.addStateHistoryEntry(new VmStateHistoryEntry(5,0,0,600,0,false));
vm.addStateHistoryEntry(new VmStateHistoryEntry(11,0,0,400,0,false));
vm.addStateHistoryEntry(new VmStateHistoryEntry(16,0,0,500,0,false));
vm.setContract(contract);
assertEquals(0, vm.getMonitor().getCumulativeCredit(0), imprecision);
assertEquals(-1 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(1), imprecision);
assertEquals(0, vm.getMonitor().getCumulativeCredit(2), imprecision);
assertEquals(1 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(3), imprecision);
assertEquals(2 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(4), imprecision);
assertEquals(3 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(5), imprecision);
assertEquals(7 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(11), imprecision);
assertEquals(3 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(15), imprecision);
assertEquals(4 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(16), imprecision);
assertEquals(1184 * STEP_CREDIT, vm.getMonitor().getCumulativeCredit(1196), imprecision);
}
@Test
public void getVmCredit_test() {
Vm vm = new VmSimple(0, 0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment