github.com/Cloud-Foundations/Dominator@v0.3.4/hypervisor/manager/cpu.go (about)

     1  package manager
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  var (
     8  	errorInsufficientUnallocatedCPU = errors.New("insufficient unallocated CPU")
     9  )
    10  
    11  func (m *Manager) checkSufficientCPUWithLock(milliCPU uint) error {
    12  	if milliCPU > m.getAvailableMilliCPUWithLock() {
    13  		return errorInsufficientUnallocatedCPU
    14  	}
    15  	return nil
    16  }
    17  
    18  func (m *Manager) getAvailableMilliCPUWithLock() uint {
    19  	available := int(m.numCPUs) * 1000
    20  	for _, vm := range m.vms {
    21  		available -= int(vm.MilliCPUs)
    22  	}
    23  	if available < 0 {
    24  		return 0
    25  	}
    26  	return uint(available)
    27  }