github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/hypervisor/manager/cpu.go (about)

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