forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology.go
116 lines (101 loc) · 2.06 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ghw
import (
"fmt"
"sort"
"strconv"
"strings"
)
type Architecture int
const (
SMP Architecture = iota
NUMA
)
type NodeId uint32
type Node struct {
Id NodeId
Cores []*ProcessorCore
Caches []*MemoryCache
}
func (n *Node) String() string {
return fmt.Sprintf(
"node #%d (%d cores)",
n.Id,
len(n.Cores),
)
}
type MemoryCacheType int
const (
UNIFIED MemoryCacheType = iota
INSTRUCTION
DATA
)
type ByCacheLevel []*MemoryCache
func (a ByCacheLevel) Len() int { return len(a) }
func (a ByCacheLevel) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCacheLevel) Less(i, j int) bool {
if a[i].Level < a[j].Level {
return true
} else if a[i].Level == a[j].Level {
return a[i].Type < a[j].Type
}
return false
}
type MemoryCache struct {
Level uint8
Type MemoryCacheType
SizeBytes uint64
// The set of logical processors (hardware threads) that have access to the
// cache
LogicalProcessors []ProcessorId
}
func (c *MemoryCache) String() string {
sizeKb := c.SizeBytes / uint64(KB)
typeStr := ""
if c.Type == INSTRUCTION {
typeStr = "i"
} else if c.Type == DATA {
typeStr = "d"
}
cacheIdStr := fmt.Sprintf("L%d%s", c.Level, typeStr)
processorMapStr := ""
if c.LogicalProcessors != nil {
lpStrings := make([]string, len(c.LogicalProcessors))
for x, lpid := range c.LogicalProcessors {
lpStrings[x] = strconv.Itoa(int(lpid))
}
processorMapStr = " shared with logical processors: " + strings.Join(lpStrings, ",")
}
return fmt.Sprintf(
"%s cache (%d KB)%s",
cacheIdStr,
sizeKb,
processorMapStr,
)
}
type TopologyInfo struct {
Architecture Architecture
Nodes []*Node
}
func Topology() (*TopologyInfo, error) {
info := &TopologyInfo{}
err := topologyFillInfo(info)
for _, node := range info.Nodes {
sort.Sort(ByCacheLevel(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
}