From a0114e4dc669ace7fbc08724236c62d866a8274d Mon Sep 17 00:00:00 2001 From: Shereen Haj Date: Thu, 14 Nov 2024 15:31:55 +0200 Subject: [PATCH] PPC: unit: test sortTopology for expected output The node topology contains several slices that later on are sorted and used to determine whether two nodes are equal. Add a unit test to verify this sorting is done as expected. Signed-off-by: Shereen Haj --- .../profilecreator/profilecreator.go | 6 +- .../profilecreator/profilecreator_test.go | 144 ++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) diff --git a/pkg/performanceprofile/profilecreator/profilecreator.go b/pkg/performanceprofile/profilecreator/profilecreator.go index 66d915cc1d..90689e18bc 100644 --- a/pkg/performanceprofile/profilecreator/profilecreator.go +++ b/pkg/performanceprofile/profilecreator/profilecreator.go @@ -270,6 +270,11 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) { if err != nil { return nil, fmt.Errorf("can't obtain topology info from GHW snapshot: %v", err) } + sortTopology(topologyInfo) + return topologyInfo, nil +} + +func sortTopology(topologyInfo *topology.Info) { sort.Slice(topologyInfo.Nodes, func(x, y int) bool { return topologyInfo.Nodes[x].ID < topologyInfo.Nodes[y].ID }) @@ -283,7 +288,6 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) { return node.Cores[i].LogicalProcessors[0] < node.Cores[j].LogicalProcessors[0] }) } - return topologyInfo, nil } // topologyHTDisabled returns topologyinfo in case Hyperthreading needs to be disabled. diff --git a/pkg/performanceprofile/profilecreator/profilecreator_test.go b/pkg/performanceprofile/profilecreator/profilecreator_test.go index 597b8cb827..0eb1879768 100644 --- a/pkg/performanceprofile/profilecreator/profilecreator_test.go +++ b/pkg/performanceprofile/profilecreator/profilecreator_test.go @@ -3,6 +3,7 @@ package profilecreator import ( "fmt" "path/filepath" + "reflect" "sort" "github.com/jaypipes/ghw/pkg/cpu" @@ -1477,6 +1478,149 @@ var _ = Describe("PerformanceProfileCreator: Test Helper Function ensureSameTopo }) }) +var _ = Describe("PerformanceProfileCreator: Test Helper Function sortTopology", func() { + It("should sort node topologies as expected", func() { + unsorted := topology.Info{ + Nodes: []*topology.Node{ + { + ID: 2, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{1, 0}, + }, + { + ID: 3, + LogicalProcessors: []int{6, 7}, + }, + { + ID: 2, + LogicalProcessors: []int{5, 4}, + }, + { + ID: 1, + LogicalProcessors: []int{3, 2}, + }, + }, + }, + { + ID: 1, + Cores: []*cpu.ProcessorCore{ + + { + ID: 2, + LogicalProcessors: []int{5, 4}, + }, + { + ID: 3, + LogicalProcessors: []int{7, 6}, + }, + { + ID: 0, + LogicalProcessors: []int{0, 1}, + }, + { + ID: 1, + LogicalProcessors: []int{2, 3}, + }, + }, + }, + { + ID: 0, + Cores: []*cpu.ProcessorCore{ + { + ID: 1, + LogicalProcessors: []int{2, 3}, + }, + { + ID: 0, + LogicalProcessors: []int{1, 0}, + }, + { + ID: 3, + LogicalProcessors: []int{7, 6}, + }, + { + ID: 2, + LogicalProcessors: []int{4, 5}, + }, + }, + }, + }, + } + expectedSorted := topology.Info{ + Nodes: []*topology.Node{ + { + ID: 0, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{0, 1}, + }, + { + ID: 1, + LogicalProcessors: []int{2, 3}, + }, + { + ID: 2, + LogicalProcessors: []int{4, 5}, + }, + { + ID: 3, + LogicalProcessors: []int{6, 7}, + }, + }, + }, + { + ID: 1, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{0, 1}, + }, + { + ID: 1, + LogicalProcessors: []int{2, 3}, + }, + { + ID: 2, + LogicalProcessors: []int{4, 5}, + }, + { + ID: 3, + LogicalProcessors: []int{6, 7}, + }, + }, + }, + { + ID: 2, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{0, 1}, + }, + { + ID: 1, + LogicalProcessors: []int{2, 3}, + }, + { + ID: 2, + LogicalProcessors: []int{4, 5}, + }, + { + ID: 3, + LogicalProcessors: []int{6, 7}, + }, + }, + }, + }, + } + + sortTopology(&unsorted) + Expect(reflect.DeepEqual(unsorted, expectedSorted)).To(BeTrue(), "expected %+v, got %+v", expectedSorted, unsorted) + }) +}) + var _ = Describe("PerformanceProfileCreator: Test Helper Function GetAdditionalKernelArgs", func() { var disableHT bool