Skip to content

Commit 9b0e950

Browse files
committed
Expose s390x CPU Topology to Prometheus
1 parent 96c346e commit 9b0e950

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

info/v1/machine.go

+8
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,

machine/info.go

+2
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

+37
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import (
3737
var (
3838
coreRegExp = regexp.MustCompile(`(?m)^core id\s*:\s*([0-9]+)$`)
3939
nodeRegExp = regexp.MustCompile(`(?m)^physical id\s*:\s*([0-9]+)$`)
40+
bookRegExp = regexp.MustCompile(`(?m)^book id\s*:\s*([0-9]+)$`)
41+
drawerRegExp = regexp.MustCompile(`(?m)^drawer id\s*:\s*([0-9]+)$`)
4042
// Power systems have a different format so cater for both
4143
cpuClockSpeedMHz = regexp.MustCompile(`(?:cpu MHz|CPU MHz|clock)\s*:\s*([0-9]+\.[0-9]+)(?:MHz)?`)
4244
memoryCapacityRegexp = regexp.MustCompile(`MemTotal:\s*([0-9]+) kB`)
@@ -96,6 +98,41 @@ func GetSockets(procInfo []byte) int {
9698
return numSocket
9799
}
98100

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

metrics/prometheus_machine.go

+16
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.",

utils/sysfs/sysfs.go

+6
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)