/** * Ensures the requested amount of resources are going to be available in the foreseeable future * on this physical machine. * * @param requested The amount of resources needed by the caller * @return With a time limited offer on the requested resources. If the requested * resourceconstraints cannot be met by the function then it returns with the maximum amount * of resources it can serve. If there are no available resources it returns with <i>null</i>! * If the requested resources are available then the original requested resourceconstraints * object is stored in the returned resource allocation. If the resourceconstraints only * specified a minimum resource limit, then a new resourceconstraints object is returned with * details of the maximum possible resource constraints that can fit into the machine. */ public ResourceAllocation allocateResources( final ResourceConstraints requested, final boolean strict, final int allocationValidityLength) throws VMManagementException { if (!currentState.equals(State.RUNNING)) { throw new VMManagementException("The PM is not running and thus cannot offer resources yet"); } // Basic tests for resource availability for the host if (internalReallyFreeCaps.getRequiredCPUs() == 0 || internalReallyFreeCaps.getRequiredMemory() == 0 || requested.getRequiredProcessingPower() > internalReallyFreeCaps.getRequiredProcessingPower()) { return null; } // Allocation type test (i.e. do we allow underprovisioning?) for (int i = 0; i < promisedResources.length; i++) { ResourceAllocation olderAllocation = promisedResources[i]; if (olderAllocation != null) { if (olderAllocation.allocated.isRequiredProcessingIsMinimum() == requested.isRequiredProcessingIsMinimum()) { break; } else { return null; } } } // Promised resources for the virtual machine double vmCPU = requested.getRequiredCPUs(); long vmMem = requested.getRequiredMemory(); final double vmPrPow = requested.isRequiredProcessingIsMinimum() ? totalCapacities.getRequiredProcessingPower() : requested.getRequiredProcessingPower(); // Actually allocated resources (memory is equivalent in both cases) final double allocPrPow = totalCapacities.getRequiredProcessingPower(); final double allocCPU = vmCPU * requested.getRequiredProcessingPower() / allocPrPow; if (0 <= internalReallyFreeCaps.getRequiredCPUs() - allocCPU) { if (0 <= internalReallyFreeCaps.getRequiredMemory() - requested.getRequiredMemory()) { return new ResourceAllocation( new ConstantConstraints(allocCPU, allocPrPow, vmMem), requested.isRequiredProcessingIsMinimum() ? new ConstantConstraints(vmCPU, vmPrPow, true, vmMem) : requested, allocationValidityLength); } else { vmMem = internalReallyFreeCaps.getRequiredMemory(); } } else if (0 <= internalReallyFreeCaps.getRequiredMemory() - requested.getRequiredMemory()) { vmCPU = internalReallyFreeCaps.getRequiredCPUs(); } else { vmCPU = internalReallyFreeCaps.getRequiredCPUs(); vmMem = internalReallyFreeCaps.getRequiredMemory(); } if (strict) { return null; } else { final ResourceConstraints updatedConstraints = new ConstantConstraints(vmCPU, vmPrPow, requested.isRequiredProcessingIsMinimum(), vmMem); return new ResourceAllocation( updatedConstraints, updatedConstraints, allocationValidityLength); } }