forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
topology.go
89 lines (79 loc) · 2.49 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
80
81
82
83
84
85
86
87
88
89
//
// 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"
)
// 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 `json:"-"`
ID int `json:"id"`
Cores []*ProcessorCore `json:"cores"`
Caches []*MemoryCache `json:"caches"`
}
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 `json:"architecture"`
Nodes []*TopologyNode `json:"nodes"`
}
// Topology returns a TopologyInfo struct that describes the system topology of
// the host hardware
func Topology(opts ...*WithOption) (*TopologyInfo, error) {
mergeOpts := mergeOptions(opts...)
ctx := &context{
chroot: *mergeOpts.Chroot,
}
info := &TopologyInfo{}
if err := ctx.topologyFillInfo(info); err != nil {
return nil, err
}
for _, node := range info.Nodes {
sort.Sort(SortByMemoryCacheLevelTypeFirstProcessor(node.Caches))
}
return info, nil
}
func (i *TopologyInfo) String() string {
archStr := "SMP"
if i.Architecture == ARCHITECTURE_NUMA {
archStr = "NUMA"
}
res := fmt.Sprintf(
"topology %s (%d nodes)",
archStr,
len(i.Nodes),
)
return res
}
// simple private struct used to encapsulate topology information in a
// top-level "topology" YAML/JSON map/object key
type topologyPrinter struct {
Info *TopologyInfo `json:"topology"`
}
// YAMLString returns a string with the topology information formatted as YAML
// under a top-level "topology:" key
func (i *TopologyInfo) YAMLString() string {
return safeYAML(topologyPrinter{i})
}
// JSONString returns a string with the topology information formatted as JSON
// under a top-level "topology:" key
func (i *TopologyInfo) JSONString(indent bool) string {
return safeJSON(topologyPrinter{i}, indent)
}