forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.go
More file actions
104 lines (94 loc) · 1.68 KB
/
cpu.go
File metadata and controls
104 lines (94 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"fmt"
)
type ProcessorCore struct {
Id uint32
Index int
NumThreads uint32
LogicalProcessors []uint32
}
func (c *ProcessorCore) String() string {
return fmt.Sprintf(
"processor core #%d (%d threads), logical processors %v",
c.Index,
c.NumThreads,
c.LogicalProcessors,
)
}
type Processor struct {
Id uint32
NumCores uint32
NumThreads uint32
Vendor string
Model string
Capabilities []string
Cores []*ProcessorCore
}
func (p *Processor) HasCapability(find string) bool {
for _, c := range p.Capabilities {
if c == find {
return true
}
}
return false
}
func (p *Processor) String() string {
ncs := "cores"
if p.NumCores == 1 {
ncs = "core"
}
nts := "threads"
if p.NumThreads == 1 {
nts = "thread"
}
return fmt.Sprintf(
"physical package #%d (%d %s, %d hardware %s)",
p.Id,
p.NumCores,
ncs,
p.NumThreads,
nts,
)
}
type CPUInfo struct {
TotalCores uint32
TotalThreads uint32
Processors []*Processor
}
func CPU() (*CPUInfo, error) {
info := &CPUInfo{}
err := cpuFillInfo(info)
if err != nil {
return nil, err
}
return info, nil
}
func (i *CPUInfo) String() string {
nps := "packages"
if len(i.Processors) == 1 {
nps = "package"
}
ncs := "cores"
if i.TotalCores == 1 {
ncs = "core"
}
nts := "threads"
if i.TotalThreads == 1 {
nts = "thread"
}
return fmt.Sprintf(
"cpu (%d physical %s, %d %s, %d hardware %s)",
len(i.Processors),
nps,
i.TotalCores,
ncs,
i.TotalThreads,
nts,
)
}