Commit e040d32d authored by Rafael Vieira Falcão's avatar Rafael Vieira Falcão
Browse files

Merge branch 'resource-provisioned-shared' into info-aware-simulation

parents 50c336b6 c74e1b67
......@@ -63,7 +63,7 @@ public class ResourceProvisionerShared extends ResourceProvisionerAbstract {
}
if(isPreemptionNeeded(vm, newTotalVmResourceCapacity)){
//allocate resources equally
allocateResourcesEqually(vm,newTotalVmResourceCapacity); //allocate resources equally
return true;
}
else{
......@@ -88,9 +88,7 @@ public class ResourceProvisionerShared extends ResourceProvisionerAbstract {
getResourceAllocationMap().put(vm, newTotalVmResourceCapacity);
vm.getResource(getResourceClass()).setAllocatedResource(prevVmResourceAllocation);
return true;
}
}
}
@Override
......@@ -212,14 +210,14 @@ public class ResourceProvisionerShared extends ResourceProvisionerAbstract {
Iterator<VmRequestedAllocated> it = vmRequestedAndAllocatedList.listIterator();
while (it.hasNext() && available > 0) {
VmRequestedAllocated vra = it.next();
long toBeAllocated = Math.min(vra.getRequested(), available); //gets the minimum between minimum requested value and available resources
if(toBeAllocated*vmRequestedAndAllocatedList.size() < available){ //then allocates it to each vm (if possible)
long toBeAllocated = Math.min(vra.getRequested(), available); //gets the minimum between minimum requested value and available resources
if(toBeAllocated*vmRequestedAndAllocatedList.size() < available){ //then allocates it to each vm (if possible)
Iterator<VmRequestedAllocated> it2 = vmRequestedAndAllocatedList.listIterator();
while (it2.hasNext()) {
VmRequestedAllocated vra2 = it2.next();
vra2.addAllocated(toBeAllocated); //increase the current allocation by the requested value
vra2.subtractRequested(toBeAllocated); //and subtract the current requested by the recently allocated value
if(vra2.getRequested() == 0){ //if this vm is satisfied
if(vra2.getRequested() == 0){ //if this vm is satisfied
finalList.add(vra2); //add it on final list
it2.remove(); //and remove from the main list
}
......@@ -233,20 +231,30 @@ public class ResourceProvisionerShared extends ResourceProvisionerAbstract {
VmRequestedAllocated vra2 = it2.next();
vra2.addAllocated(toBeAllocated); //increase the current allocation by the requested value
vra2.subtractRequested(toBeAllocated); //and subtract the current requested by the recently allocated value
finalList.add(vra2); //add it on final list because its the final allocation
it2.remove(); //and remove from the main list
finalList.add(vra2); //add it on final list because its the final allocation
it2.remove(); //and remove from the main list
allocated += toBeAllocated; //updates allocated and available
available -= toBeAllocated;
}
}
Collections.sort(vmRequestedAndAllocatedList); //is it necessary?
it = vmRequestedAndAllocatedList.listIterator(); //update iterator with new state of the list
}
}
deallocateResourceForAllVms();
for(VmRequestedAllocated vra : finalList){
//Allocates the requested resource from the physical resource
getResource().allocateResource(vra.getAllocated());
getResourceAllocationMap().put(vra.getVm(), vra.getAllocated());
vm.getResource(getResourceClass()).setAllocatedResource(vra.getAllocated());
}
}
@Override
public boolean isSuitableForVm(final Vm vm, final long newVmTotalAllocatedResource) {
return getResourceAllocationMap().size() < getResource().getCapacity();
int tam = getResourceAllocationMap().size();
long cap = getResource().getCapacity();
return tam < cap;
}
/**
......@@ -258,95 +266,7 @@ public class ResourceProvisionerShared extends ResourceProvisionerAbstract {
private boolean isPreemptionNeeded(final Vm vm, final long newVmTotalAllocatedResource) {
final long currentAllocatedResource = getAllocatedResourceForVm(vm);
final long allocationDifference = newVmTotalAllocatedResource - currentAllocatedResource;
return getResource().getAvailableResource() >= allocationDifference;
return getResource().getAvailableResource() < allocationDifference;
}
// private void allocateResourcesEquallyBKUP(final Vm vm, final long newTotalVmResourceCapacity){
//
// Map<Long,List<Vm>> requestedPerVm = new TreeMap<Long,List<Vm>>();
// Map<Vm,Long> resourcesToBeAllocated = new HashMap<Vm,Long>(); //the final allocation map
//
// for(Entry<Vm, Long> entry : getResourceAllocationMap().entrySet()){
// resourcesToBeAllocated.put(entry.getKey(), 0L); //initially the resourcesToBeAllocated of each VM is 0
// if(!requestedPerVm.containsKey(entry.getValue())){
// requestedPerVm.put(entry.getValue(), new ArrayList<Vm>());
// }
// requestedPerVm.get(entry.getValue()).add(entry.getKey()); //fulfill the requestedPerVm map with values from resourceAllocationMap
// }
//
// long capacity = getResource().getCapacity();
// long allocated = 0L;
// long available = capacity;
//
// int numVms = resourcesToBeAllocated.size();
//
// /**
// * here we iterate over requestedPerVm map distributing the available resources equally
// */
// Iterator<Map.Entry<Long,List<Vm>>> it = requestedPerVm.entrySet().iterator();
// while (it.hasNext()) {
// Map.Entry<Long,List<Vm>> entry = it.next();
// long requested = entry.getKey(); //gets the minimum requested value and
// if(requested*numVms<available){ //then allocates it to each vm (if possible)
// for (Map.Entry<Vm,Long> entryAllocation : resourcesToBeAllocated.entrySet()) {
// entryAllocation.setValue(entryAllocation.getValue()+requested); //increase the current allocation by the requested value
// Iterator<Map.Entry<Long,List<Vm>>> it2 = requestedPerVm.entrySet().iterator(); //and then subtract this value in requestedPerVm for all occurrences
// while (it2.hasNext()) { //as long as we have a map we need to use an iterator to remove
// Map.Entry<Long,List<Vm>> entry2 = it2.next();
// long entry2requested = entry2.getKey();
// if(entry2requested-requested==0){ //if this current allocation fulfills the requirements
// if(entry2.getValue().size()==1){ //and if there's only one VM with this requested
// it2.remove(); //then, remove this entry from requestedPerVm
// } else{ //otherwise, there is/are more VM(s) requesting this same amount
// Iterator<Vm> it3 = entry2.getValue().listIterator();
// while (it3.hasNext()) {
//
// }
// //entry2.getValue().remove(entryAllocation.getKey());
// }
// } else{
// if(!requestedPerVm.containsKey(entry2requested-requested)){ //if there is no other VM with this amount of request, then just put this new value in map
// requestedPerVm.put(entry2requested-requested, entry2.getValue());
// } else{ //if there is other VM with this amount of request, then add this VM on the list (inside map)
// requestedPerVm.get(entry.getKey()).add(e)
// }
// it2.remove();
// }
// }
// }
// allocated += requested*numVms;
// available = capacity - allocated;
// it.remove();
// } else{
//
// }
// }
//basically chooses the vm with less resources requested and give this amount to all Vms
//keep doing it until all you can not allocate resources equally to the VMs
//after this, split the remaining resources and give to th remaininh VMs
// int numberOfVms = getResourceAllocationMap().size();
// if(!getResourceAllocationMap().containsKey(vm))
// numberOfVms++;
//
// long resourcesPerVm = getResource().getCapacity()/numberOfVms;
//
//
//
//
// long resourcesMissingToAllocate = resourcesPerVm - (newTotalVmResourceCapacity - getResource().getAvailableResource());
//
//
// for(Entry<Vm, Long> entry : getResourceAllocationMap().entrySet()){
// if(entry.getValue().longValue() > resourcesPerVm){
// getResource().allocateResource(amountToAllocate)
// }
// }
// getResourceAllocationMap();
// int numberOfVms
// getResourceAllocationMap()
}
/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009-2012, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.provisioners;
import org.cloudbus.cloudsim.resources.Ram;
import static org.cloudbus.cloudsim.vms.VmSimpleTest.createVm;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.junit.Before;
import org.junit.Test;
import org.cloudbus.cloudsim.resources.ResourceManageable;
/**
* @author Eduardo de Lucena Falcao (@eduardolfalcao)
* @since CloudSim Toolkit 2.0
*/
public class ResourceProvisionerSharedTest {
private static final long CAPACITY = 1000;
private static final long HALF_CAPACITY = CAPACITY / 2;
private static final long QUARTER_OF_CAPACITY = CAPACITY / 4;
/** @see #createSharedProvisioner() */
private ResourceProvisionerShared provisioner;
@Before
public void setUp() throws Exception {
provisioner = createSharedProvisioner();
}
/**
* Creates a shared provisioner for any generic resource. It in fact doesn't matter
* if it is for HOST_RAM, CPU, HOST_BW or any other possible resource.
*/
private ResourceProvisionerShared createSharedProvisioner() {
return new ResourceProvisionerShared(new Ram(CAPACITY));
}
private ResourceProvisionerShared createSharedProvisioner(ResourceManageable resource) {
return new ResourceProvisionerShared(resource);
}
@Test(expected = NullPointerException.class)
public void testCreateProvisioner_null() {
createSharedProvisioner(null);
}
@Test
public void testGetCapacity() {
assertEquals(CAPACITY, provisioner.getCapacity());
}
@Test
public void testGetAvailableResource() {
assertEquals(CAPACITY, provisioner.getAvailableResource());
}
@Test
public void testGetTotalAllocatedResource() {
final ResourceProvisioner p = createSharedProvisioner();
assertEquals(p.getCapacity(), p.getAvailableResource());
assertEquals(0, p.getTotalAllocatedResource());
final VmSimple vm = createVm(1, CAPACITY);
final long allocatedResource = HALF_CAPACITY;
p.allocateResourceForVm(vm, allocatedResource);
assertEquals(allocatedResource, p.getTotalAllocatedResource());
}
@Test
public void testIsSuitableForVm() {
final VmSimple vm0 = createVm(0, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, QUARTER_OF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, HALF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm0, CAPACITY*2));
provisioner.allocateResourceForVm(vm0, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, QUARTER_OF_CAPACITY));
for(int i = 1; i <= CAPACITY-1; i++){
VmSimple vm = createVm(i, HALF_CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm, HALF_CAPACITY));
provisioner.allocateResourceForVm(vm, HALF_CAPACITY);
}
VmSimple vm = createVm(1001, HALF_CAPACITY);
assertFalse(provisioner.isSuitableForVm(vm, HALF_CAPACITY));
}
@Test
public void testAllocateResourceForVmScenario2() {
provisioner = new ResourceProvisionerShared(new Ram(2*CAPACITY));
final VmSimple vm0 = createVm(0, 200);
final VmSimple vm1 = createVm(1, 700);
final VmSimple vm2 = createVm(2, 701);
final VmSimple vm3 = createVm(3, 550);
assertTrue(provisioner.isSuitableForVm(vm0, 200));
assertTrue(provisioner.allocateResourceForVm(vm0, 200));
assertEquals(1800, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, 700));
assertTrue(provisioner.allocateResourceForVm(vm1, 700));
assertEquals(1100, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm2, 701));
assertTrue(provisioner.allocateResourceForVm(vm2, 701));
assertEquals(399, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm3, 550));
assertTrue(provisioner.allocateResourceForVm(vm3,550));
assertEquals(0, provisioner.getAvailableResource());
assertEquals(200, provisioner.getAllocatedResourceForVm(vm0));
assertEquals(625, provisioner.getAllocatedResourceForVm(vm1));
assertEquals(625, provisioner.getAllocatedResourceForVm(vm2));
assertEquals(550, provisioner.getAllocatedResourceForVm(vm3));
}
@Test
public void testAllocateResourceForVm() {
final VmSimple vm0 = createVm(0, HALF_CAPACITY);
final VmSimple vm1 = createVm(1, CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm0, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm0, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, QUARTER_OF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, QUARTER_OF_CAPACITY));
assertEquals(QUARTER_OF_CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
}
@Test
public void testGetAllocatedResourceForVm() {
final VmSimple vm1 = createVm(0, HALF_CAPACITY);
final VmSimple vm2 = createVm(1, CAPACITY);
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm1));
assertTrue(provisioner.isSuitableForVm(vm2, CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
assertTrue(provisioner.isSuitableForVm(vm2, QUARTER_OF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, QUARTER_OF_CAPACITY));
assertEquals(QUARTER_OF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
assertTrue(provisioner.isSuitableForVm(vm2, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAllocatedResourceForVm(vm2));
}
@Test
public void testDeallocateResourceForVm() {
final VmSimple vm1 = createVm(0, HALF_CAPACITY);
final VmSimple vm2 = createVm(1, HALF_CAPACITY);
assertEquals(0, vm1.getCurrentAllocatedRam());
assertEquals(0, vm2.getCurrentAllocatedRam());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertEquals(HALF_CAPACITY, provisioner.getAvailableResource());
provisioner.deallocateResourceForVm(vm1);
assertEquals(CAPACITY, provisioner.getAvailableResource());
assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY));
assertTrue(provisioner.isSuitableForVm(vm2, HALF_CAPACITY));
assertTrue(provisioner.allocateResourceForVm(vm2, HALF_CAPACITY));
assertEquals(0, provisioner.getAvailableResource());
provisioner.deallocateResourceForVm(vm1);
provisioner.deallocateResourceForVm(vm2);
assertEquals(CAPACITY, provisioner.getAvailableResource());
assertEquals(0, vm1.getCurrentAllocatedRam());
assertEquals(0, vm2.getCurrentAllocatedRam());
}
}
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