Skip to content

Commit 0b82f40

Browse files
authored
Merge pull request #1971 from cjeker/openbsd_spin_time
Also track the CPU Spin time for OpenBSD systems.
2 parents 88a0315 + 2cf3db8 commit 0b82f40

File tree

2 files changed

+45
-112
lines changed

2 files changed

+45
-112
lines changed

collector/cpu_openbsd.go

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 The Prometheus Authors
1+
// Copyright 2020 The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -11,8 +11,8 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
//go:build openbsd && !amd64 && !nocpu
15-
// +build openbsd,!amd64,!nocpu
14+
//go:build !nocpu
15+
// +build !nocpu
1616

1717
package collector
1818

@@ -25,11 +25,31 @@ import (
2525
"golang.org/x/sys/unix"
2626
)
2727

28-
/*
29-
#include <sys/param.h>
30-
#include <sys/sched.h>
31-
*/
32-
import "C"
28+
type clockinfo struct {
29+
hz int32
30+
tick int32
31+
tickadj int32
32+
stathz int32
33+
profhz int32
34+
}
35+
36+
const (
37+
CP_USER = iota
38+
CP_NICE
39+
CP_SYS
40+
CP_SPIN
41+
CP_INTR
42+
CP_IDLE
43+
CPUSTATES
44+
)
45+
const (
46+
CP_USER_O63 = iota
47+
CP_NICE_O63
48+
CP_SYS_O63
49+
CP_INTR_O63
50+
CP_IDLE_O63
51+
CPUSTATES_O63
52+
)
3353

3454
type cpuCollector struct {
3555
cpu typedDesc
@@ -52,32 +72,41 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) (err error) {
5272
if err != nil {
5373
return err
5474
}
55-
clock := *(*C.struct_clockinfo)(unsafe.Pointer(&clockb[0]))
75+
clock := *(*clockinfo)(unsafe.Pointer(&clockb[0]))
5676
hz := float64(clock.stathz)
5777

5878
ncpus, err := unix.SysctlUint32("hw.ncpu")
5979
if err != nil {
6080
return err
6181
}
6282

63-
var cpTime [][C.CPUSTATES]C.int64_t
83+
var cpTime [][CPUSTATES]uint64
6484
for i := 0; i < int(ncpus); i++ {
6585
cpb, err := unix.SysctlRaw("kern.cp_time2", i)
6686
if err != nil && err != unix.ENODEV {
6787
return err
6888
}
6989
if err != unix.ENODEV {
70-
cpTime = append(cpTime, *(*[C.CPUSTATES]C.int64_t)(unsafe.Pointer(&cpb[0])))
90+
var times [CPUSTATES]uint64
91+
for n := 0; n < len(cpb); n += 8 {
92+
times[n/8] = *(*uint64)(unsafe.Pointer(&cpb[n]))
93+
}
94+
if len(cpb)/8 == CPUSTATES_O63 {
95+
copy(times[CP_INTR:], times[CP_INTR_O63:])
96+
times[CP_SPIN] = 0
97+
}
98+
cpTime = append(cpTime, times)
7199
}
72100
}
73101

74102
for cpu, time := range cpTime {
75103
lcpu := strconv.Itoa(cpu)
76-
ch <- c.cpu.mustNewConstMetric(float64(time[C.CP_USER])/hz, lcpu, "user")
77-
ch <- c.cpu.mustNewConstMetric(float64(time[C.CP_NICE])/hz, lcpu, "nice")
78-
ch <- c.cpu.mustNewConstMetric(float64(time[C.CP_SYS])/hz, lcpu, "system")
79-
ch <- c.cpu.mustNewConstMetric(float64(time[C.CP_INTR])/hz, lcpu, "interrupt")
80-
ch <- c.cpu.mustNewConstMetric(float64(time[C.CP_IDLE])/hz, lcpu, "idle")
104+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_USER])/hz, lcpu, "user")
105+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_NICE])/hz, lcpu, "nice")
106+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_SYS])/hz, lcpu, "system")
107+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_SPIN])/hz, lcpu, "spin")
108+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_INTR])/hz, lcpu, "interrupt")
109+
ch <- c.cpu.mustNewConstMetric(float64(time[CP_IDLE])/hz, lcpu, "idle")
81110
}
82111
return err
83112
}

collector/cpu_openbsd_amd64.go

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)