vc: utils: Rename CalculateMilliCPUs() to CalculateCPUsF()

With the change done in the last commit, instead of calculating milli
cpus, we're actually converting the CPUs to a fraction number, a float.

Let's update the function name (and associated vars) to represent that
change.

Signed-off-by: Fabiano Fidêncio <fabiano.fidencio@intel.com>
This commit is contained in:
Fabiano Fidêncio 2023-11-10 18:14:04 +01:00
parent e477ed0e86
commit 5e9cf75937
4 changed files with 12 additions and 12 deletions

View File

@ -1272,7 +1272,7 @@ func CalculateContainerSizing(spec *specs.Spec) (numCPU float32, memSizeMB uint3
} }
func calculateVMResources(period uint64, quota int64, memory int64) (numCPU float32, memSizeMB uint32) { func calculateVMResources(period uint64, quota int64, memory int64) (numCPU float32, memSizeMB uint32) {
numCPU = vcutils.CalculateMilliCPUs(quota, period) numCPU = vcutils.CalculateCPUsF(quota, period)
if memory < 0 { if memory < 0 {
// While spec allows for a negative value to indicate unconstrained, we don't // While spec allows for a negative value to indicate unconstrained, we don't

View File

@ -2394,7 +2394,7 @@ func (s *Sandbox) calculateSandboxMemory() (uint64, bool, int64) {
} }
func (s *Sandbox) calculateSandboxCPUs() (float32, error) { func (s *Sandbox) calculateSandboxCPUs() (float32, error) {
mCPU := float32(0) floatCPU := float32(0)
cpusetCount := int(0) cpusetCount := int(0)
for _, c := range s.config.Containers { for _, c := range s.config.Containers {
@ -2406,7 +2406,7 @@ func (s *Sandbox) calculateSandboxCPUs() (float32, error) {
if cpu := c.Resources.CPU; cpu != nil { if cpu := c.Resources.CPU; cpu != nil {
if cpu.Period != nil && cpu.Quota != nil { if cpu.Period != nil && cpu.Quota != nil {
mCPU += utils.CalculateMilliCPUs(*cpu.Quota, *cpu.Period) floatCPU += utils.CalculateCPUsF(*cpu.Quota, *cpu.Period)
} }
set, err := cpuset.Parse(cpu.Cpus) set, err := cpuset.Parse(cpu.Cpus)
@ -2420,11 +2420,11 @@ func (s *Sandbox) calculateSandboxCPUs() (float32, error) {
// If we aren't being constrained, then we could have two scenarios: // If we aren't being constrained, then we could have two scenarios:
// 1. BestEffort QoS: no proper support today in Kata. // 1. BestEffort QoS: no proper support today in Kata.
// 2. We could be constrained only by CPUSets. Check for this: // 2. We could be constrained only by CPUSets. Check for this:
if mCPU == 0 && cpusetCount > 0 { if floatCPU == 0 && cpusetCount > 0 {
return float32(cpusetCount), nil return float32(cpusetCount), nil
} }
return mCPU, nil return floatCPU, nil
} }
// GetHypervisorType is used for getting Hypervisor name currently used. // GetHypervisorType is used for getting Hypervisor name currently used.

View File

@ -121,8 +121,8 @@ func WriteToFile(path string, data []byte) error {
return nil return nil
} }
// CalculateMilliCPUs converts CPU quota and period to milli-CPUs // CalculateCPUsF converts CPU quota and period to a fraction number
func CalculateMilliCPUs(quota int64, period uint64) float32 { func CalculateCPUsF(quota int64, period uint64) float32 {
// If quota is -1, it means the CPU resource request is // If quota is -1, it means the CPU resource request is
// unconstrained. In that case, we don't currently assign // unconstrained. In that case, we don't currently assign
// additional CPUs. // additional CPUs.

View File

@ -147,22 +147,22 @@ func TestWriteToFile(t *testing.T) {
assert.True(reflect.DeepEqual(testData, data)) assert.True(reflect.DeepEqual(testData, data))
} }
func TestCalculateMilliCPUs(t *testing.T) { func TestCalculateCPUsF(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
n := CalculateMilliCPUs(1, 1) n := CalculateCPUsF(1, 1)
expected := float32(1) expected := float32(1)
assert.Equal(n, expected) assert.Equal(n, expected)
n = CalculateMilliCPUs(1, 0) n = CalculateCPUsF(1, 0)
expected = float32(0) expected = float32(0)
assert.Equal(n, expected) assert.Equal(n, expected)
n = CalculateMilliCPUs(-1, 1) n = CalculateCPUsF(-1, 1)
expected = float32(0) expected = float32(0)
assert.Equal(n, expected) assert.Equal(n, expected)
n = CalculateMilliCPUs(500, 1000) n = CalculateCPUsF(500, 1000)
expected = float32(0.5) expected = float32(0.5)
assert.Equal(n, expected) assert.Equal(n, expected)
} }