From 1bf9bcdc7c19ef319e472c4c66a46588b246f6c2 Mon Sep 17 00:00:00 2001 From: Antti Kervinen Date: Mon, 21 Oct 2024 12:45:45 +0300 Subject: [PATCH] sysfs: fix order of cpu.cache levels in case of cache override Cache level functions, for instance CPU.GetCachesByLevel(), expect cpu.caches to be sorted by level, the lowest first. This change ensures that overridden caches follow this ordering, too, no matter in which order user has spesified them. Signed-off-by: Antti Kervinen --- pkg/sysfs/system.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/sysfs/system.go b/pkg/sysfs/system.go index 172873ccb..7dfe23181 100644 --- a/pkg/sysfs/system.go +++ b/pkg/sysfs/system.go @@ -1727,6 +1727,14 @@ func (sys *system) discoverCacheFromOverrides(cpu *cpu) (bool, error) { sys.Error("failed to parse cache overrides: %v", err) return false, err } + // cpu.caches must be ordered by cache level, the lowest first. + // Sort caches in each CPU by level, then by kind. + for _, caches := range ceo { + sort.Slice(caches, func(i, j int) bool { + return (caches[i].level < caches[j].level || + (caches[i].level == caches[j].level && caches[i].kind < caches[j].kind)) + }) + } cacheEnvOverrides = ceo } if caches, ok := cacheEnvOverrides[cpu.id]; ok {