Skip to content

Commit b8aac7c

Browse files
rexagoddiscordianfish
authored andcommitted
collector/cpu: Support CPU online status
Blocked by: prometheus/procfs#644. Signed-off-by: Pranshu Srivastava <[email protected]>
1 parent 2b5cef3 commit b8aac7c

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

collector/cpu_linux.go

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package collector
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"log/slog"
2223
"os"
@@ -26,15 +27,17 @@ import (
2627
"strconv"
2728
"sync"
2829

30+
"golang.org/x/exp/maps"
31+
2932
"github.com/alecthomas/kingpin/v2"
3033
"github.com/prometheus/client_golang/prometheus"
3134
"github.com/prometheus/procfs"
3235
"github.com/prometheus/procfs/sysfs"
33-
"golang.org/x/exp/maps"
3436
)
3537

3638
type cpuCollector struct {
37-
fs procfs.FS
39+
procfs procfs.FS
40+
sysfs sysfs.FS
3841
cpu *prometheus.Desc
3942
cpuInfo *prometheus.Desc
4043
cpuFrequencyHz *prometheus.Desc
@@ -45,6 +48,7 @@ type cpuCollector struct {
4548
cpuPackageThrottle *prometheus.Desc
4649
cpuIsolated *prometheus.Desc
4750
logger *slog.Logger
51+
cpuOnline *prometheus.Desc
4852
cpuStats map[int64]procfs.CPUStat
4953
cpuStatsMutex sync.Mutex
5054
isolatedCpus []uint16
@@ -70,17 +74,17 @@ func init() {
7074

7175
// NewCPUCollector returns a new Collector exposing kernel/system statistics.
7276
func NewCPUCollector(logger *slog.Logger) (Collector, error) {
73-
fs, err := procfs.NewFS(*procPath)
77+
pfs, err := procfs.NewFS(*procPath)
7478
if err != nil {
7579
return nil, fmt.Errorf("failed to open procfs: %w", err)
7680
}
7781

78-
sysfs, err := sysfs.NewFS(*sysPath)
82+
sfs, err := sysfs.NewFS(*sysPath)
7983
if err != nil {
8084
return nil, fmt.Errorf("failed to open sysfs: %w", err)
8185
}
8286

83-
isolcpus, err := sysfs.IsolatedCPUs()
87+
isolcpus, err := sfs.IsolatedCPUs()
8488
if err != nil {
8589
if !os.IsNotExist(err) {
8690
return nil, fmt.Errorf("Unable to get isolated cpus: %w", err)
@@ -89,8 +93,9 @@ func NewCPUCollector(logger *slog.Logger) (Collector, error) {
8993
}
9094

9195
c := &cpuCollector{
92-
fs: fs,
93-
cpu: nodeCPUSecondsDesc,
96+
procfs: pfs,
97+
sysfs: sfs,
98+
cpu: nodeCPUSecondsDesc,
9499
cpuInfo: prometheus.NewDesc(
95100
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "info"),
96101
"CPU information from /proc/cpuinfo.",
@@ -131,6 +136,11 @@ func NewCPUCollector(logger *slog.Logger) (Collector, error) {
131136
"Whether each core is isolated, information from /sys/devices/system/cpu/isolated.",
132137
[]string{"cpu"}, nil,
133138
),
139+
cpuOnline: prometheus.NewDesc(
140+
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "online"),
141+
"CPUs that are online and being scheduled.",
142+
[]string{"cpu"}, nil,
143+
),
134144
logger: logger,
135145
isolatedCpus: isolcpus,
136146
cpuStats: make(map[int64]procfs.CPUStat),
@@ -177,12 +187,21 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
177187
if c.isolatedCpus != nil {
178188
c.updateIsolated(ch)
179189
}
180-
return c.updateThermalThrottle(ch)
190+
err := c.updateThermalThrottle(ch)
191+
if err != nil {
192+
return err
193+
}
194+
err = c.updateOnline(ch)
195+
if err != nil {
196+
return err
197+
}
198+
199+
return nil
181200
}
182201

183202
// updateInfo reads /proc/cpuinfo
184203
func (c *cpuCollector) updateInfo(ch chan<- prometheus.Metric) error {
185-
info, err := c.fs.CPUInfo()
204+
info, err := c.procfs.CPUInfo()
186205
if err != nil {
187206
return err
188207
}
@@ -333,9 +352,31 @@ func (c *cpuCollector) updateIsolated(ch chan<- prometheus.Metric) {
333352
}
334353
}
335354

355+
// updateOnline reads /sys/devices/system/cpu/cpu*/online through sysfs and exports online status metrics.
356+
func (c *cpuCollector) updateOnline(ch chan<- prometheus.Metric) error {
357+
cpus, err := c.sysfs.CPUs()
358+
if err != nil {
359+
return err
360+
}
361+
// No-op if the system does not support CPU online stats.
362+
cpu0 := cpus[0]
363+
if _, err := cpu0.Online(); err != nil && errors.Is(err, os.ErrNotExist) {
364+
return nil
365+
}
366+
for _, cpu := range cpus {
367+
setOnline := float64(0)
368+
if online, _ := cpu.Online(); online {
369+
setOnline = 1
370+
}
371+
ch <- prometheus.MustNewConstMetric(c.cpuOnline, prometheus.GaugeValue, setOnline, cpu.Number())
372+
}
373+
374+
return nil
375+
}
376+
336377
// updateStat reads /proc/stat through procfs and exports CPU-related metrics.
337378
func (c *cpuCollector) updateStat(ch chan<- prometheus.Metric) error {
338-
stats, err := c.fs.Stat()
379+
stats, err := c.procfs.Stat()
339380
if err != nil {
340381
return err
341382
}

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/coreos/go-systemd/v22 v22.5.0
99
github.com/dennwc/btrfs v0.0.0-20240418142341-0167142bde7a
1010
github.com/ema/qdisc v1.0.0
11-
github.com/go-kit/log v0.2.1
1211
github.com/godbus/dbus/v5 v5.1.0
1312
github.com/hashicorp/go-envparse v0.1.0
1413
github.com/hodgesds/perf-utils v0.7.0
@@ -27,7 +26,7 @@ require (
2726
github.com/prometheus/client_model v0.6.1
2827
github.com/prometheus/common v0.61.0
2928
github.com/prometheus/exporter-toolkit v0.13.2
30-
github.com/prometheus/procfs v0.15.1
29+
github.com/prometheus/procfs v0.15.2-0.20240603130017-1754b780536b // == v0.15.1 + https://github.com/prometheus/procfs/commit/1754b780536bb81082baa913e04cc4fff4d2baea
3130
github.com/safchain/ethtool v0.5.9
3231
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
3332
golang.org/x/sys v0.28.0
@@ -39,7 +38,6 @@ require (
3938
github.com/beorn7/perks v1.0.1 // indirect
4039
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4140
github.com/dennwc/ioctl v1.0.0 // indirect
42-
github.com/go-logfmt/logfmt v0.5.1 // indirect
4341
github.com/google/go-cmp v0.6.0 // indirect
4442
github.com/jpillora/backoff v1.0.0 // indirect
4543
github.com/klauspost/compress v1.17.9 // indirect

go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ github.com/dennwc/ioctl v1.0.0 h1:DsWAAjIxRqNcLn9x6mwfuf2pet3iB7aK90K4tF16rLg=
2121
github.com/dennwc/ioctl v1.0.0/go.mod h1:ellh2YB5ldny99SBU/VX7Nq0xiZbHphf1DrtHxxjMk0=
2222
github.com/ema/qdisc v1.0.0 h1:EHLG08FVRbWLg8uRICa3xzC9Zm0m7HyMHfXobWFnXYg=
2323
github.com/ema/qdisc v1.0.0/go.mod h1:FhIc0fLYi7f+lK5maMsesDqwYojIOh3VfRs8EVd5YJQ=
24-
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
25-
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
26-
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
27-
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
2824
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
2925
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
3026
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
@@ -87,8 +83,8 @@ github.com/prometheus/common v0.61.0 h1:3gv/GThfX0cV2lpO7gkTUwZru38mxevy90Bj8YFS
8783
github.com/prometheus/common v0.61.0/go.mod h1:zr29OCN/2BsJRaFwG8QOBr41D6kkchKbpeNH7pAjb/s=
8884
github.com/prometheus/exporter-toolkit v0.13.2 h1:Z02fYtbqTMy2i/f+xZ+UK5jy/bl1Ex3ndzh06T/Q9DQ=
8985
github.com/prometheus/exporter-toolkit v0.13.2/go.mod h1:tCqnfx21q6qN1KA4U3Bfb8uWzXfijIrJz3/kTIqMV7g=
90-
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
91-
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
86+
github.com/prometheus/procfs v0.15.2-0.20240603130017-1754b780536b h1:4EJkx3vycI+n5JY5ht+bnSUGamkmmXkpcNeO/OBT/0A=
87+
github.com/prometheus/procfs v0.15.2-0.20240603130017-1754b780536b/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
9288
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
9389
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
9490
github.com/safchain/ethtool v0.5.9 h1://6RvaOKFf3nQ0rl5+8zBbE4/72455VC9Jq61pfq67E=

0 commit comments

Comments
 (0)