Skip to content

Commit

Permalink
TAS: allow to dump the usage of domains for TAS ResourceFlavors
Browse files Browse the repository at this point in the history
* Add test
* Preserve resources
  • Loading branch information
vladikkuzn committed Feb 26, 2025
1 parent f0f74e5 commit 6da5f7c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
15 changes: 6 additions & 9 deletions pkg/cache/tas_flavor_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,14 @@ func (s *TASFlavorSnapshot) removeTASUsage(domainID utiltas.TopologyDomainID, us
s.leaves[domainID].tasUsage.Sub(usage)
}

func (s *TASFlavorSnapshot) FreeCapacityPerDomain() map[string]int {
freeCapacityPerDomain := make(map[string]int)
func (s *TASFlavorSnapshot) FreeCapacityPerDomain() map[string]resources.Requests {
freeCapacityPerDomain := make(map[string]resources.Requests)

for domainID, leaf := range s.leaves {
// Calculate the total free capacity for the domain
totalFreeCapacity := 0
for _, quantity := range leaf.freeCapacity {
totalFreeCapacity += int(quantity)
}

freeCapacityPerDomain[string(domainID)] = totalFreeCapacity
// CPU is represented in millicores
// Memory is represented in bytes
// For other resources, use their raw integer value
freeCapacityPerDomain[string(domainID)] = leaf.freeCapacity.Clone()
}

return freeCapacityPerDomain
Expand Down
63 changes: 63 additions & 0 deletions pkg/cache/tas_flavor_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cache

import (
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"

"sigs.k8s.io/kueue/pkg/resources"
)

func TestFreeCapacityPerDomain(t *testing.T) {
snapshot := &TASFlavorSnapshot{
leaves: leafDomainByID{
"domain1": &leafDomain{
freeCapacity: resources.Requests{
corev1.ResourceCPU: 2000,
corev1.ResourceMemory: 4 * 1024 * 1024 * 1024, // 4 GiB
"nvidia.com/gpu": 1,
},
},
"domain2": &leafDomain{
freeCapacity: resources.Requests{
corev1.ResourceCPU: 1000,
corev1.ResourceMemory: 2 * 1024 * 1024 * 1024, // 2 GiB
},
},
},
}

expected := map[string]resources.Requests{
"domain1": {
corev1.ResourceCPU: 2000,
corev1.ResourceMemory: 4 * 1024 * 1024 * 1024,
"nvidia.com/gpu": 1,
},
"domain2": {
corev1.ResourceCPU: 1000,
corev1.ResourceMemory: 2 * 1024 * 1024 * 1024,
},
}

actual := snapshot.FreeCapacityPerDomain()
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("FreeCapacityPerDomain() mismatch (-expected +actual):\n%s", diff)
}
}

0 comments on commit 6da5f7c

Please sign in to comment.