Skip to content

Commit

Permalink
Merge pull request #381 from jaypipes/num-total
Browse files Browse the repository at this point in the history
use TotalXXX instead of NumXXX
  • Loading branch information
jaypipes authored Sep 25, 2024
2 parents 26a61af + 464a1c2 commit 2950821
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 30 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ information about the CPUs on the host system.

* `ghw.CPUInfo.TotalCores` has the total number of physical cores the host
system contains
* `ghw.CPUInfo.TotalThreads` has the total number of hardware threads the
host system contains
* `ghw.CPUInfo.TotalHardwareThreads` has the total number of hardware threads
the host system contains
* `ghw.CPUInfo.Processors` is an array of `ghw.Processor` structs, one for each
physical processor package contained in the host

Each `ghw.Processor` struct contains a number of fields:

* `ghw.Processor.ID` is the physical processor `uint32` ID according to the
system
* `ghw.Processor.NumCores` is the number of physical cores in the processor
package
* `ghw.Processor.NumThreads` is the number of hardware threads in the processor
* `ghw.Processor.TotalCores` is the number of physical cores in the processor
package
* `ghw.Processor.TotalHardwareThreads` is the number of hardware threads in the
processor package
* `ghw.Processor.Vendor` is a string containing the vendor name
* `ghw.Processor.Model` is a string containing the vendor's model name
* `ghw.Processor.Capabilities` (Linux only) is an array of strings indicating
Expand All @@ -102,8 +102,8 @@ A `ghw.ProcessorCore` has the following fields:
core. Note that this does *not* necessarily equate to a zero-based index of
the core within a physical package. For example, the core IDs for an Intel Core
i7 are 0, 1, 2, 8, 9, and 10
* `ghw.ProcessorCore.NumThreads` is the number of hardware threads associated
with the core
* `ghw.ProcessorCore.TotalHardwareThreads` is the number of hardware threads
associated with the core
* `ghw.ProcessorCore.LogicalProcessors` is an array of ints representing the
logical processor IDs assigned to any processing unit for the core. These are
sometimes called the "thread siblings". Logical processor IDs are the
Expand Down
29 changes: 22 additions & 7 deletions pkg/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ type ProcessorCore struct {
// within a physical package. For example, the core IDs for an Intel Core
// i7 are 0, 1, 2, 8, 9, and 10
ID int `json:"id"`
// NumThreads is the number of hardware threads associated with the core
// TotalHardwareThreads is the number of hardware threads associated with
// the core
TotalHardwareThreads uint32 `json:"total_hardware_threads"`
// NumThreads is the number of hardware threads associated with the core.
// DEPRECATED: Use `TotalHardwareThreads` instead.
NumThreads uint32 `json:"total_threads"`
// LogicalProcessors is a slice of ints representing the logical processor
// IDs assigned to any processing unit for the core. These are sometimes
Expand All @@ -38,7 +42,7 @@ func (c *ProcessorCore) String() string {
return fmt.Sprintf(
"processor core #%d (%d threads), logical processors %v",
c.ID,
c.NumThreads,
c.TotalHardwareThreads,
c.LogicalProcessors,
)
}
Expand All @@ -47,9 +51,16 @@ func (c *ProcessorCore) String() string {
type Processor struct {
// ID is the physical processor `uint32` ID according to the system
ID int `json:"id"`
// TotalCores is the number of physical cores in the processor package
TotalCores uint32 `json:"total_cores"`
// NumCores is the number of physical cores in the processor package
NumCores uint32 `json:"total_cores"`
// DEPRECATED: Use `TotalCores` instead.
NumCores uint32 `json:"-"`
// TotalHardwareThreads is the number of hardware threads associated with
// the processor package
TotalHardwareThreads uint32 `json:"total_hardware_threads"`
// NumThreads is the number of hardware threads in the processor package
// DEPRECATED: Use `TotalHardwareThreads` instead.
NumThreads uint32 `json:"total_threads"`
// Vendor is a string containing the vendor name
Vendor string `json:"vendor"`
Expand Down Expand Up @@ -91,19 +102,19 @@ func (p *Processor) HasCapability(find string) bool {
// String returns a short string describing the Processor
func (p *Processor) String() string {
ncs := "cores"
if p.NumCores == 1 {
if p.TotalCores == 1 {
ncs = "core"
}
nts := "threads"
if p.NumThreads == 1 {
if p.TotalHardwareThreads == 1 {
nts = "thread"
}
return fmt.Sprintf(
"physical package #%d (%d %s, %d hardware %s)",
p.ID,
p.NumCores,
p.TotalCores,
ncs,
p.NumThreads,
p.TotalHardwareThreads,
nts,
)
}
Expand All @@ -117,6 +128,10 @@ type Info struct {
TotalCores uint32 `json:"total_cores"`
// TotalThreads is the total number of hardware threads the host system
// contains
TotalHardwareThreads uint32 `json:"total_hardware_threads"`
// TotalThreads is the total number of hardware threads the host system
// contains
// DEPRECATED: Use `TotalHardwareThreads` instead
TotalThreads uint32 `json:"total_threads"`
// Processors is a slice of Processor struct pointers, one for each
// physical processor package contained in the host
Expand Down
23 changes: 19 additions & 4 deletions pkg/cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ func (i *Info) load() error {
var totCores uint32
var totThreads uint32
for _, p := range i.Processors {
totCores += p.NumCores
totThreads += p.NumThreads
totCores += p.TotalCores
totThreads += p.TotalHardwareThreads
}
i.TotalCores = totCores
i.TotalHardwareThreads = totThreads
// TODO(jaypipes): Remove TotalThreads before v1.0
i.TotalThreads = totThreads
return nil
}
Expand Down Expand Up @@ -113,12 +115,23 @@ func processorsGet(ctx *context.Context) []*Processor {
coreID := coreIDFromLogicalProcessorID(ctx, lpID)
core := proc.CoreByID(coreID)
if core == nil {
core = &ProcessorCore{ID: coreID, NumThreads: 1}
core = &ProcessorCore{
ID: coreID,
TotalHardwareThreads: 1,
// TODO(jaypipes): Remove NumThreads before v1.0
NumThreads: 1,
}
proc.Cores = append(proc.Cores, core)
proc.TotalCores += 1
// TODO(jaypipes): Remove NumCores before v1.0
proc.NumCores += 1
} else {
core.TotalHardwareThreads += 1
// TODO(jaypipes) Remove NumThreads before v1.0
core.NumThreads += 1
}
proc.TotalHardwareThreads += 1
// TODO(jaypipes) Remove NumThreads before v1.0
proc.NumThreads += 1
core.LogicalProcessors = append(core.LogicalProcessors, lpID)
}
Expand Down Expand Up @@ -231,7 +244,9 @@ func CoresForNode(ctx *context.Context, nodeID int) ([]*ProcessorCore, error) {
}

for _, c := range cores {
c.NumThreads = uint32(len(c.LogicalProcessors))
c.TotalHardwareThreads = uint32(len(c.LogicalProcessors))
// TODO(jaypipes): Remove NumThreads before v1.0
c.NumThreads = c.TotalHardwareThreads
}

return cores, nil
Expand Down
7 changes: 4 additions & 3 deletions pkg/cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func TestArmCPU(t *testing.T) {
}

for _, p := range info.Processors {
if p.NumCores == 0 {
if p.TotalCores == 0 {
t.Fatalf("Expected >0 cores but got 0.")
}
if p.NumThreads == 0 {
if p.TotalHardwareThreads == 0 {
t.Fatalf("Expected >0 threads but got 0.")
}
if len(p.Capabilities) == 0 {
Expand All @@ -66,7 +66,7 @@ func TestArmCPU(t *testing.T) {
t.Fatalf("Expected >0 cores in processor, but got 0.")
}
for _, c := range p.Cores {
if c.NumThreads == 0 {
if c.TotalHardwareThreads == 0 {
t.Fatalf("Expected >0 threads but got 0.")
}
if len(c.LogicalProcessors) == 0 {
Expand Down Expand Up @@ -118,6 +118,7 @@ func TestCheckCPUTopologyFilesForOfflineCPU(t *testing.T) {
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.")
Expand Down
6 changes: 3 additions & 3 deletions pkg/cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ func TestCPU(t *testing.T) {
}

for _, p := range info.Processors {
if p.NumCores == 0 {
if p.TotalCores == 0 {
t.Fatalf("Expected >0 cores but got 0.")
}
if p.NumThreads == 0 {
if p.TotalHardwareThreads == 0 {
t.Fatalf("Expected >0 threads but got 0.")
}
if len(p.Capabilities) == 0 {
Expand All @@ -50,7 +50,7 @@ func TestCPU(t *testing.T) {
t.Fatalf("Expected >0 cores in processor, but got 0.")
}
for _, c := range p.Cores {
if c.NumThreads == 0 {
if c.TotalHardwareThreads == 0 {
t.Fatalf("Expected >0 threads but got 0.")
}
if len(c.LogicalProcessors) == 0 {
Expand Down
12 changes: 9 additions & 3 deletions pkg/cpu/cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ func (i *Info) load() error {
var totCores uint32
var totThreads uint32
for _, p := range i.Processors {
totCores += p.NumCores
totThreads += p.NumThreads
totCores += p.TotalCores
totThreads += p.TotalHardwareThreads
}
i.TotalCores = totCores
i.TotalHardwareThreads = totThreads
// TODO(jaypipes): Remove TotalThreads by v1.0
i.TotalThreads = totThreads
return nil
}
Expand All @@ -48,7 +50,11 @@ func processorsGet(win32descriptions []win32Processor) []*Processor {
ID: index,
Model: *description.Name,
Vendor: *description.Manufacturer,
NumCores: description.NumberOfCores,
TotalCores: description.NumberOfCores,
// TODO(jaypipes): Remove NumCores before v1.0
NumCores: description.NumberOfCores,
TotalHardwareThreads: description.NumberOfLogicalProcessors,
// TODO(jaypipes): Remove NumThreads before v1.0
NumThreads: description.NumberOfLogicalProcessors,
}
procs = append(procs, p)
Expand Down
6 changes: 3 additions & 3 deletions pkg/topology/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func TestTopology(t *testing.T) {
if len(c.LogicalProcessors) == 0 {
t.Fatalf("Expected >0 logical processors but got 0.")
}
if uint32(len(c.LogicalProcessors)) != c.NumThreads {
if uint32(len(c.LogicalProcessors)) != c.TotalHardwareThreads {
t.Fatalf(
"Expected NumThreads == len(logical procs) but %d != %d",
c.NumThreads,
"Expected TotalHardwareThreads == len(logical procs) but %d != %d",
c.TotalHardwareThreads,
len(c.LogicalProcessors),
)
}
Expand Down

0 comments on commit 2950821

Please sign in to comment.