Skip to content

Commit 013c9e9

Browse files
committed
Expose s390x CPU Topology to Prometheus
1 parent 96c346e commit 013c9e9

File tree

7 files changed

+78
-0
lines changed

7 files changed

+78
-0
lines changed

info/v1/machine.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ type MachineInfo struct {
193193
// The number of cpu sockets in this machine.
194194
NumSockets int `json:"num_sockets"`
195195

196+
// The number of cpu books in this machine.
197+
NumBooks int `json:"num_books,omitempty"`
198+
199+
// The number of cpu drawers in this machine.
200+
NumDrawers int `json:"num_drawers,omitempty"`
201+
196202
// Maximum clock speed for the cores, in KHz.
197203
CpuFrequency uint64 `json:"cpu_frequency_khz"`
198204

@@ -263,6 +269,8 @@ func (m *MachineInfo) Clone() *MachineInfo {
263269
NumCores: m.NumCores,
264270
NumPhysicalCores: m.NumPhysicalCores,
265271
NumSockets: m.NumSockets,
272+
NumBooks: m.NumBooks,
273+
NumDrawers: m.NumDrawers,
266274
CpuFrequency: m.CpuFrequency,
267275
MemoryCapacity: m.MemoryCapacity,
268276
SwapCapacity: m.SwapCapacity,

info/v1/machine_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func getFakeMachineInfo() MachineInfo {
4242
NumCores: 1,
4343
NumPhysicalCores: 2,
4444
NumSockets: 3,
45+
NumBooks: 1,
46+
NumDrawers: 1,
4547
CpuFrequency: 4,
4648
MemoryCapacity: 5,
4749
SwapCapacity: 6,

machine/info.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ func Info(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (*info.Mach
130130
NumCores: numCores,
131131
NumPhysicalCores: GetPhysicalCores(cpuinfo),
132132
NumSockets: GetSockets(cpuinfo),
133+
NumBooks: GetBooks(cpuinfo),
134+
NumDrawers: GetDrawers(cpuinfo),
133135
CpuFrequency: clockSpeed,
134136
MemoryCapacity: memoryCapacity,
135137
MemoryByType: memoryByType,

machine/machine.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"path"
2222
"regexp"
23+
"runtime"
2324

2425
"strconv"
2526
"strings"
@@ -37,6 +38,8 @@ import (
3738
var (
3839
coreRegExp = regexp.MustCompile(`(?m)^core id\s*:\s*([0-9]+)$`)
3940
nodeRegExp = regexp.MustCompile(`(?m)^physical id\s*:\s*([0-9]+)$`)
41+
bookRegExp = regexp.MustCompile(`(?m)^book id\s*:\s*([0-9]+)$`)
42+
drawerRegExp = regexp.MustCompile(`(?m)^drawer id\s*:\s*([0-9]+)$`)
4043
// Power systems have a different format so cater for both
4144
cpuClockSpeedMHz = regexp.MustCompile(`(?:cpu MHz|CPU MHz|clock)\s*:\s*([0-9]+\.[0-9]+)(?:MHz)?`)
4245
memoryCapacityRegexp = regexp.MustCompile(`MemTotal:\s*([0-9]+) kB`)
@@ -96,6 +99,41 @@ func GetSockets(procInfo []byte) int {
9699
return numSocket
97100
}
98101

102+
// GetBooks returns number of CPU books reading from sysfs cpu path
103+
func GetBooks(procInfo []byte) int {
104+
if runtime.GOARCH != "s390x" {
105+
return 0
106+
}
107+
numBook := getUniqueMatchesCount(string(procInfo), bookRegExp)
108+
if numBook == 0 {
109+
// read number of books from /sys/bus/cpu/devices/cpu*/topology/book_id to deal with processors
110+
// for which 'book id' is not available in /proc/cpuinfo
111+
numBook = sysfs.GetUniqueCPUPropertyCount(cpuAttributesPath, sysfs.CPUBookID)
112+
}
113+
if numBook == 0 {
114+
klog.Errorf("Cannot read number of books correctly, number of books set to %d", numBook)
115+
}
116+
return numBook
117+
}
118+
119+
// GetDrawer returns number of CPU drawerss reading from sysfs cpu path
120+
func GetDrawers(procInfo []byte) int {
121+
if runtime.GOARCH != "s390x" {
122+
return 0
123+
}
124+
numDrawer := getUniqueMatchesCount(string(procInfo), drawerRegExp)
125+
if numDrawer == 0 {
126+
// read number of books from /sys/bus/cpu/devices/cpu*/topology/book_id to deal with processors
127+
// read number of drawers from /sys/bus/cpu/devices/cpu*/topology/drawer_id to deal with processors
128+
// for which 'drawer id' is not available in /proc/cpuinfo
129+
numDrawer = sysfs.GetUniqueCPUPropertyCount(cpuAttributesPath, sysfs.CPUDrawerID)
130+
}
131+
if numDrawer == 0 {
132+
klog.Errorf("Cannot read number of drawers correctly, number of drawers set to %d", numDrawer)
133+
}
134+
return numDrawer
135+
}
136+
99137
// GetClockSpeed returns the CPU clock speed, given a []byte formatted as the /proc/cpuinfo file.
100138
func GetClockSpeed(procInfo []byte) (uint64, error) {
101139
// First look through sys to find a max supported cpu frequency.

metrics/prometheus_machine.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ func NewPrometheusMachineCollector(i infoProvider, includedMetrics container.Met
103103
return metricValues{{value: float64(machineInfo.NumSockets), timestamp: machineInfo.Timestamp}}
104104
},
105105
},
106+
{
107+
name: "machine_cpu_books",
108+
help: "Number of CPU books.",
109+
valueType: prometheus.GaugeValue,
110+
getValues: func(machineInfo *info.MachineInfo) metricValues {
111+
return metricValues{{value: float64(machineInfo.NumBooks), timestamp: machineInfo.Timestamp}}
112+
},
113+
},
114+
{
115+
name: "machine_cpu_drawers",
116+
help: "Number of CPU drawers.",
117+
valueType: prometheus.GaugeValue,
118+
getValues: func(machineInfo *info.MachineInfo) metricValues {
119+
return metricValues{{value: float64(machineInfo.NumDrawers), timestamp: machineInfo.Timestamp}}
120+
},
121+
},
106122
{
107123
name: "machine_memory_bytes",
108124
help: "Amount of memory installed on the machine.",

metrics/testdata/prometheus_machine_metrics

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# HELP machine_cpu_books Number of CPU books.
2+
# TYPE machine_cpu_books gauge
3+
machine_cpu_books{boot_id="boot-id-test",machine_id="machine-id-test",system_uuid="system-uuid-test"} 0 1395066363000
14
# HELP machine_cpu_cache_capacity_bytes Cache size in bytes assigned to NUMA node and CPU core.
25
# TYPE machine_cpu_cache_capacity_bytes gauge
36
machine_cpu_cache_capacity_bytes{boot_id="boot-id-test",core_id="",level="3",machine_id="machine-id-test",node_id="1",system_uuid="system-uuid-test",type="Unified"} 8.388608e+06 1395066363000
@@ -28,6 +31,9 @@ machine_cpu_cache_capacity_bytes{boot_id="boot-id-test",core_id="7",level="2",ma
2831
# HELP machine_cpu_cores Number of logical CPU cores.
2932
# TYPE machine_cpu_cores gauge
3033
machine_cpu_cores{boot_id="boot-id-test",machine_id="machine-id-test",system_uuid="system-uuid-test"} 4 1395066363000
34+
# HELP machine_cpu_drawers Number of CPU drawers.
35+
# TYPE machine_cpu_drawers gauge
36+
machine_cpu_drawers{boot_id="boot-id-test",machine_id="machine-id-test",system_uuid="system-uuid-test"} 0 1395066363000
3137
# HELP machine_cpu_physical_cores Number of physical CPU cores.
3238
# TYPE machine_cpu_physical_cores gauge
3339
machine_cpu_physical_cores{boot_id="boot-id-test",machine_id="machine-id-test",system_uuid="system-uuid-test"} 1 1395066363000

utils/sysfs/sysfs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ const (
4949
// (rather than the kernel's). The actual value is architecture and platform dependent.
5050
CPUCoreID = "core_id"
5151

52+
// On some architecture there exists additional level of book and drawer id
53+
// CPUBookID is the book ID of cpu#. Typically corresponds to a physical book number.
54+
CPUBookID = "book_id"
55+
// CPUDrawerID is the drawer ID of cpu#. Typically corresponds to a physical drawer number.
56+
CPUDrawerID = "drawer_id"
57+
5258
coreIDFilePath = "/" + sysFsCPUTopology + "/core_id"
5359
packageIDFilePath = "/" + sysFsCPUTopology + "/physical_package_id"
5460
bookIDFilePath = "/" + sysFsCPUTopology + "/book_id"

0 commit comments

Comments
 (0)