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 28, 2025
1 parent 136b749 commit 0313d39
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *Snapshot) Log(log logr.Logger) {
log.Info("TAS Snapshot Free Capacity",
"clusterQueue", cqName,
"resourceFlavor", tasFlavor,
"freeCapacityPerDomain", tasSnapshot.FreeCapacityPerDomain(),
"freeCapacityPerDomain", tasSnapshot.PrettyPrintFreeCapacityPerDomain(),
)
}
}
Expand Down
37 changes: 28 additions & 9 deletions pkg/cache/tas_flavor_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"maps"
"slices"
"strings"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -241,22 +242,40 @@ 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
freeCapacityPerDomain[string(domainID)] = leaf.freeCapacity.Clone()
}

return freeCapacityPerDomain
}

func (s *TASFlavorSnapshot) PrettyPrintFreeCapacityPerDomain() string {
freeCapacityPerDomain := s.freeCapacityPerDomain()

// Sort keys lexicographically
sortedFreeCapacity := make(map[string]resources.Requests, len(freeCapacityPerDomain))
for _, domain := range slices.Sorted(maps.Keys(freeCapacityPerDomain)) {
sortedFreeCapacity[domain] = freeCapacityPerDomain[domain]
}

// Pretty-print the free capacities
var prettyFreeCapacity []string
for _, domain := range slices.Sorted(maps.Keys(freeCapacityPerDomain)) {
rr := freeCapacityPerDomain[domain]
var resourceDetails []string
for _, resourceName := range slices.Sorted(maps.Keys(rr)) {
value := rr[resourceName]
prettyValue := resources.ResourceQuantityString(resourceName, value)
resourceDetails = append(resourceDetails, fmt.Sprintf("%s: %s", resourceName, prettyValue))
}
prettyFreeCapacity = append(prettyFreeCapacity, fmt.Sprintf("%s={%s}", domain, strings.Join(resourceDetails, ";")))
}
return strings.Join(prettyFreeCapacity, ", ")
}

type TASPodSetRequests struct {
PodSet *kueue.PodSet
SinglePodRequests resources.Requests
Expand Down
53 changes: 53 additions & 0 deletions pkg/cache/tas_flavor_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 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{
"domain2": &leafDomain{
freeCapacity: resources.Requests{
corev1.ResourceCPU: 1000,
corev1.ResourceMemory: 2 * 1024 * 1024 * 1024, // 2 GiB
},
},
"domain1": &leafDomain{
freeCapacity: resources.Requests{
corev1.ResourceCPU: 2000,
corev1.ResourceMemory: 4 * 1024 * 1024 * 1024, // 4 GiB
"nvidia.com/gpu": 1,
},
},
},
}

expected := "domain1={cpu: 2;memory: 4Gi;nvidia.com/gpu: 1}, domain2={cpu: 1;memory: 2Gi}"

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

0 comments on commit 0313d39

Please sign in to comment.