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 20, 2025
1 parent f0f74e5 commit 95d34c0
Show file tree
Hide file tree
Showing 2 changed files with 53 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
47 changes: 47 additions & 0 deletions pkg/cache/tas_flavor_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 95d34c0

Please sign in to comment.