Commit 40781046 authored by Lucas Cavalcante's avatar Lucas Cavalcante
Browse files

new step credit strategy

parent c47e091b
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