Skip to content

Commit

Permalink
PPC: unit: test sortTopology for expected output
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
shajmakh committed Nov 18, 2024
1 parent 1e9a589 commit a0114e4
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/performanceprofile/profilecreator/profilecreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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.
Expand Down
144 changes: 144 additions & 0 deletions pkg/performanceprofile/profilecreator/profilecreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package profilecreator
import (
"fmt"
"path/filepath"
"reflect"
"sort"

"github.com/jaypipes/ghw/pkg/cpu"
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a0114e4

Please sign in to comment.