Skip to content

Commit aa3b28b

Browse files
authored
Merge pull request #346 from jaypipes/issue-345
linux: refactor ProcessorCore retrieval
2 parents 36ff37e + 3699cf7 commit aa3b28b

File tree

4 files changed

+220
-98
lines changed

4 files changed

+220
-98
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,13 @@ A `ghw.ProcessorCore` has the following fields:
408408
core. Note that this does *not* necessarily equate to a zero-based index of
409409
the core within a physical package. For example, the core IDs for an Intel Core
410410
i7 are 0, 1, 2, 8, 9, and 10
411-
* `ghw.ProcessorCore.Index` is the zero-based index of the core on the physical
412-
processor package
413411
* `ghw.ProcessorCore.NumThreads` is the number of hardware threads associated
414412
with the core
415-
* `ghw.ProcessorCore.LogicalProcessors` is an array of logical processor IDs
416-
assigned to any processing unit for the core
413+
* `ghw.ProcessorCore.LogicalProcessors` is an array of ints representing the
414+
logical processor IDs assigned to any processing unit for the core. These are
415+
sometimes called the "thread siblings". Logical processor IDs are the
416+
*zero-based* index of the processor on the host and are *not* related to the
417+
core ID.
417418

418419
```go
419420
package main

pkg/block/block_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ func TestBlock(t *testing.T) {
5353
if d0.Name == "" {
5454
t.Fatalf("Expected disk name, but got \"\"")
5555
}
56-
if d0.SerialNumber == "unknown" {
57-
t.Fatalf("Got unknown serial number.")
58-
}
5956
if d0.SizeBytes <= 0 {
6057
t.Fatalf("Expected >0 disk size, but got %d", d0.SizeBytes)
6158
}

pkg/cpu/cpu.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ type ProcessorCore struct {
2323
// within a physical package. For example, the core IDs for an Intel Core
2424
// i7 are 0, 1, 2, 8, 9, and 10
2525
ID int `json:"id"`
26-
// Index is the zero-based index of the core on the physical processor
27-
// package
28-
Index int `json:"index"`
2926
// NumThreads is the number of hardware threads associated with the core
3027
NumThreads uint32 `json:"total_threads"`
3128
// LogicalProcessors is a slice of ints representing the logical processor
32-
// IDs assigned to any processing unit for the core
29+
// IDs assigned to any processing unit for the core. These are sometimes
30+
// called the "thread siblings". Logical processor IDs are the *zero-based*
31+
// index of the processor on the host and are *not* related to the core ID.
3332
LogicalProcessors []int `json:"logical_processors"`
3433
}
3534

@@ -38,7 +37,7 @@ type ProcessorCore struct {
3837
func (c *ProcessorCore) String() string {
3938
return fmt.Sprintf(
4039
"processor core #%d (%d threads), logical processors %v",
41-
c.Index,
40+
c.ID,
4241
c.NumThreads,
4342
c.LogicalProcessors,
4443
)
@@ -64,6 +63,16 @@ type Processor struct {
6463
Cores []*ProcessorCore `json:"cores"`
6564
}
6665

66+
// CoreByID returns the ProcessorCore having the supplied ID.
67+
func (p *Processor) CoreByID(coreID int) *ProcessorCore {
68+
for _, core := range p.Cores {
69+
if core.ID == coreID {
70+
return core
71+
}
72+
}
73+
return nil
74+
}
75+
6776
// HasCapability returns true if the Processor has the supplied cpuid
6877
// capability, false otherwise. Example of cpuid capabilities would be 'vmx' or
6978
// 'sse4_2'. To see a list of potential cpuid capabilitiies, see the section on

0 commit comments

Comments
 (0)