forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.go
79 lines (70 loc) · 1.95 KB
/
topology.go
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
//
// 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"
"sort"
)
// Architecture describes the overall hardware architecture. It can be either
// Symmetric Multi-Processor (SMP) or Non-Uniform Memory Access (NUMA)
type Architecture int
const (
// SMP is a Symmetric Multi-Processor system
SMP Architecture = iota
// NUMA is a Non-Uniform Memory Access system
NUMA
)
// TopologyNode is an abstract construct representing a collection of
// processors and various levels of memory cache that those processors share.
// In a NUMA architecture, there are multiple NUMA nodes, abstracted here as
// multiple TopologyNode structs. In an SMP architecture, a single TopologyNode
// will be available in the TopologyInfo struct and this single struct can be
// used to describe the levels of memory caching available to the single
// physical processor package's physical processor cores
type TopologyNode struct {
// TODO(jaypipes): Deprecated in 0.2, remove in 1.0
Id int
ID int
Cores []*ProcessorCore
Caches []*MemoryCache
}
func (n *TopologyNode) String() string {
return fmt.Sprintf(
"node #%d (%d cores)",
n.ID,
len(n.Cores),
)
}
// TopologyInfo describes the system topology for the host hardware
type TopologyInfo struct {
Architecture Architecture
Nodes []*TopologyNode
}
// Topology returns a TopologyInfo struct that describes the system topology of
// the host hardware
func Topology() (*TopologyInfo, error) {
info := &TopologyInfo{}
err := topologyFillInfo(info)
for _, node := range info.Nodes {
sort.Sort(SortByMemoryCacheLevelTypeFirstProcessor(node.Caches))
}
if err != nil {
return nil, err
}
return info, nil
}
func (i *TopologyInfo) String() string {
archStr := "SMP"
if i.Architecture == NUMA {
archStr = "NUMA"
}
res := fmt.Sprintf(
"topology %s (%d nodes)",
archStr,
len(i.Nodes),
)
return res
}