Skip to content

Commit

Permalink
Merge pull request #362 from kishen-v/determine-cpu-online-sts
Browse files Browse the repository at this point in the history
Read core and physical package ID of the CPUs that are online
  • Loading branch information
jaypipes authored Apr 7, 2024
2 parents a2d4b25 + 122266f commit 633fd9f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

var (
regexForCpulCore = regexp.MustCompile("^cpu([0-9]+)$")
onlineFile = "online"
)

func (i *Info) load() error {
Expand Down Expand Up @@ -64,6 +65,10 @@ func processorsGet(ctx *context.Context) []*Processor {
continue
}

onlineFilePath := filepath.Join(paths.SysDevicesSystemCPU, fmt.Sprintf("cpu%d", lpID), onlineFile)
if util.SafeIntFromFile(ctx, onlineFilePath) == 0 {
continue
}
procID := processorIDFromLogicalProcessorID(ctx, lpID)
proc, found := procs[procID]
if !found {
Expand Down Expand Up @@ -201,6 +206,10 @@ func CoresForNode(ctx *context.Context, nodeID int) ([]*ProcessorCore, error) {
)
continue
}
onlineFilePath := filepath.Join(cpuPath, onlineFile)
if util.SafeIntFromFile(ctx, onlineFilePath) == 0 {
continue
}
coreIDPath := filepath.Join(cpuPath, "topology", "core_id")
coreID := util.SafeIntFromFile(ctx, coreIDPath)
core := findCoreByID(coreID)
Expand Down
89 changes: 89 additions & 0 deletions pkg/cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
package cpu_test

import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/jaypipes/ghw/pkg/cpu"
"github.com/jaypipes/ghw/pkg/option"
"github.com/jaypipes/ghw/pkg/topology"
"github.com/jaypipes/ghw/testdata"
)

Expand Down Expand Up @@ -71,3 +75,88 @@ func TestArmCPU(t *testing.T) {
}
}
}

func TestCheckCPUTopologyFilesForOfflineCPU(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_CPU"); ok {
t.Skip("Skipping CPU tests.")
}

testdataPath, err := testdata.SnapshotsDirectory()
if err != nil {
t.Fatalf("Expected nil err, but got %v", err)
}

offlineCPUSnapshot := filepath.Join(testdataPath, "linux-amd64-offlineCPUs.tar.gz")

// Capture stderr
rErr, wErr, err := os.Pipe()
if err != nil {
t.Fatalf("Cannot pipe StdErr. %v", err)
}
os.Stderr = wErr

info, err := cpu.New(option.WithSnapshot(option.SnapshotOptions{
Path: offlineCPUSnapshot,
}))
if err != nil {
t.Fatalf("Expected nil err, but got %v", err)
}
if info == nil {
t.Fatalf("Expected non-nil CPUInfo, but got nil")
}

if len(info.Processors) == 0 {
t.Fatalf("Expected >0 processors but got 0.")
}
wErr.Close()
var bufErr bytes.Buffer
if _, err := io.Copy(&bufErr, rErr); err != nil {
t.Fatalf("Failed to copy data to buffer: %v", err)
}
errorOutput := bufErr.String()
if strings.Contains(errorOutput, "WARNING: failed to read int from file:") {
t.Fatalf("Unexpected warning related to missing files under topology directory was reported")
}
}
func TestNumCoresAmongOfflineCPUs(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_CPU"); ok {
t.Skip("Skipping CPU tests.")
}

testdataPath, err := testdata.SnapshotsDirectory()
if err != nil {
t.Fatalf("Expected nil err, but got %v", err)
}

offlineCPUSnapshot := filepath.Join(testdataPath, "linux-amd64-offlineCPUs.tar.gz")

// Capture stderr
rErr, wErr, err := os.Pipe()
if err != nil {
t.Fatalf("Cannot pipe the StdErr. %v", err)
}
info, err := topology.New(option.WithSnapshot(option.SnapshotOptions{
Path: offlineCPUSnapshot,
}))
if err != nil {
t.Fatalf("Error determining node topology. %v", err)
}

if len(info.Nodes) < 1 {
t.Fatal("No nodes found. Must contain one or more nodes")
}
for _, node := range info.Nodes {
if len(node.Cores) < 1 {
t.Fatal("No cores found. Must contain one or more cores")
}
}
wErr.Close()
var bufErr bytes.Buffer
if _, err := io.Copy(&bufErr, rErr); err != nil {
t.Fatalf("Failed to copy data to buffer: %v", err)
}
errorOutput := bufErr.String()
if strings.Contains(errorOutput, "WARNING: failed to read int from file:") {
t.Fatalf("Unexpected warnings related to missing files under topology directory was raised")
}
}
Binary file added testdata/snapshots/linux-amd64-offlineCPUs.tar.gz
Binary file not shown.

0 comments on commit 633fd9f

Please sign in to comment.