Skip to content

Commit

Permalink
Merge pull request #346 from jaypipes/issue-345
Browse files Browse the repository at this point in the history
linux: refactor ProcessorCore retrieval
  • Loading branch information
jaypipes committed Jul 10, 2023
2 parents 36ff37e + 3699cf7 commit aa3b28b
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 98 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,13 @@ 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.Index` is the zero-based index of the core on the physical
processor package
* `ghw.ProcessorCore.NumThreads` is the number of hardware threads associated
with the core
* `ghw.ProcessorCore.LogicalProcessors` is an array of logical processor IDs
assigned to any processing unit for 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
*zero-based* index of the processor on the host and are *not* related to the
core ID.

```go
package main
Expand Down
3 changes: 0 additions & 3 deletions pkg/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ func TestBlock(t *testing.T) {
if d0.Name == "" {
t.Fatalf("Expected disk name, but got \"\"")
}
if d0.SerialNumber == "unknown" {
t.Fatalf("Got unknown serial number.")
}
if d0.SizeBytes <= 0 {
t.Fatalf("Expected >0 disk size, but got %d", d0.SizeBytes)
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ 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"`
// Index is the zero-based index of the core on the physical processor
// package
Index int `json:"index"`
// NumThreads is the number of hardware threads associated with the core
NumThreads uint32 `json:"total_threads"`
// LogicalProcessors is a slice of ints representing the logical processor
// IDs assigned to any processing unit for the core
// IDs assigned to any processing unit for the core. These are sometimes
// called the "thread siblings". Logical processor IDs are the *zero-based*
// index of the processor on the host and are *not* related to the core ID.
LogicalProcessors []int `json:"logical_processors"`
}

Expand All @@ -38,7 +37,7 @@ type ProcessorCore struct {
func (c *ProcessorCore) String() string {
return fmt.Sprintf(
"processor core #%d (%d threads), logical processors %v",
c.Index,
c.ID,
c.NumThreads,
c.LogicalProcessors,
)
Expand All @@ -64,6 +63,16 @@ type Processor struct {
Cores []*ProcessorCore `json:"cores"`
}

// CoreByID returns the ProcessorCore having the supplied ID.
func (p *Processor) CoreByID(coreID int) *ProcessorCore {
for _, core := range p.Cores {
if core.ID == coreID {
return core
}
}
return nil
}

// HasCapability returns true if the Processor has the supplied cpuid
// capability, false otherwise. Example of cpuid capabilities would be 'vmx' or
// 'sse4_2'. To see a list of potential cpuid capabilitiies, see the section on
Expand Down
Loading

0 comments on commit aa3b28b

Please sign in to comment.