Skip to content

Commit

Permalink
Merge branch 'main' into darwin_cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypipes committed Apr 7, 2024
2 parents f8b177a + 633fd9f commit 6c2f312
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/fmtcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ jobs:
disable-sudo: true
allowed-endpoints: >
github.com:443
api.github.com:443
proxy.github.com:443
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version: 1.19
go-version: 1.21
- name: check fmt
run: 'bash -c "diff -u <(echo -n) <(gofmt -d .)"'
4 changes: 3 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ jobs:
allowed-endpoints: >
github.com:443
api.github.com:443
proxy.github.com:443
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version: 1.19
go-version: 1.21
- name: lint
uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # v3.6.0
with:
Expand Down
31 changes: 28 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.19', '1.20']
go: [ '1.19', '1.20', '1.21']
steps:
- name: harden runner
uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1
Expand All @@ -26,6 +26,11 @@ jobs:
disable-sudo: true
allowed-endpoints: >
github.com:443
api.github.com:443
proxy.github.com:443
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
Expand All @@ -51,6 +56,11 @@ jobs:
disable-sudo: true
allowed-endpoints: >
github.com:443
api.github.com:443
proxy.github.com:443
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
Expand All @@ -67,7 +77,7 @@ jobs:
runs-on: windows-2022
strategy:
matrix:
go: [ '1.19' ]
go: [ '1.20', '1.21' ]
steps:
- name: harden runner
uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1
Expand All @@ -76,6 +86,11 @@ jobs:
disable-sudo: true
allowed-endpoints: >
github.com:443
api.github.com:443
proxy.github.com:443
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
Expand All @@ -98,7 +113,7 @@ jobs:
runs-on: windows-2019
strategy:
matrix:
go: [ '1.18' ]
go: [ '1.19' ]
steps:
- name: harden runner
uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1
Expand All @@ -107,6 +122,11 @@ jobs:
disable-sudo: true
allowed-endpoints: >
github.com:443
api.github.com:443
proxy.github.com:443
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
Expand Down Expand Up @@ -145,6 +165,11 @@ jobs:
disable-sudo: true
allowed-endpoints: >
github.com:443
api.github.com:443
proxy.github.com:443
raw.githubusercontent.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
- name: checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: setup go
Expand Down
6 changes: 5 additions & 1 deletion pkg/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ type Disk struct {
Model string `json:"model"`
// SerialNumber is the serial number of the disk.
SerialNumber string `json:"serial_number"`
// WWN is the World-wide Number of the disk.
// WWN is the World-wide Name of the disk.
// See: https://en.wikipedia.org/wiki/World_Wide_Name
WWN string `json:"wwn"`
// WWNNoExtension is the World-wide Name of the disk with any vendor
// extensions excluded.
// See: https://en.wikipedia.org/wiki/World_Wide_Name
WWNNoExtension string `json:"wwnNoExtension"`
// Partitions contains an array of pointers to `Partition` structs, one for
// each partition on the disk.
Partitions []*Partition `json:"partitions"`
Expand Down
1 change: 1 addition & 0 deletions pkg/block/block_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func (info *Info) load() error {
Model: ioregPlist.ModelNumber,
SerialNumber: ioregPlist.SerialNumber,
WWN: "",
WWNNoExtension: "",
Partitions: make([]*Partition, 0, len(disk.Partitions)+len(disk.APFSVolumes)),
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/block/block_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ func diskBusPath(paths *linuxpath.Paths, disk string) string {
return util.UNKNOWN
}

func diskWWNNoExtension(paths *linuxpath.Paths, disk string) string {
info, err := udevInfoDisk(paths, disk)
if err != nil {
return util.UNKNOWN
}

if wwn, ok := info["ID_WWN"]; ok {
return wwn
}
return util.UNKNOWN
}

func diskWWN(paths *linuxpath.Paths, disk string) string {
info, err := udevInfoDisk(paths, disk)
if err != nil {
Expand Down Expand Up @@ -326,6 +338,7 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
model := diskModel(paths, dname)
serialNo := diskSerialNumber(paths, dname)
wwn := diskWWN(paths, dname)
wwnNoExtension := diskWWNNoExtension(paths, dname)
removable := diskIsRemovable(paths, dname)

if storageController == STORAGE_CONTROLLER_LOOP && size == 0 {
Expand All @@ -345,6 +358,7 @@ func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
Model: model,
SerialNumber: serialNo,
WWN: wwn,
WWNNoExtension: wwnNoExtension,
}

parts := diskPartitions(ctx, paths, dname)
Expand Down
1 change: 1 addition & 0 deletions pkg/block/block_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (i *Info) load() error {
Model: strings.TrimSpace(*diskdrive.Caption),
SerialNumber: strings.TrimSpace(*diskdrive.SerialNumber),
WWN: util.UNKNOWN, // TODO: add information
WWNNoExtension: util.UNKNOWN, // TODO: add information
Partitions: make([]*Partition, 0),
}
for _, diskpartition := range win32DiskPartitionDescriptions {
Expand Down
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 6c2f312

Please sign in to comment.