1
- // Copyright 2018 The Prometheus Authors
1
+ // Copyright 2020 The Prometheus Authors
2
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
3
// you may not use this file except in compliance with the License.
4
4
// You may obtain a copy of the License at
11
11
// See the License for the specific language governing permissions and
12
12
// limitations under the License.
13
13
14
- //go:build openbsd && !amd64 && !nocpu
15
- // +build openbsd,!amd64, !nocpu
14
+ //go:build !nocpu
15
+ // +build !nocpu
16
16
17
17
package collector
18
18
@@ -25,11 +25,31 @@ import (
25
25
"golang.org/x/sys/unix"
26
26
)
27
27
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
+ )
33
53
34
54
type cpuCollector struct {
35
55
cpu typedDesc
@@ -52,32 +72,41 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) (err error) {
52
72
if err != nil {
53
73
return err
54
74
}
55
- clock := * (* C . struct_clockinfo )(unsafe .Pointer (& clockb [0 ]))
75
+ clock := * (* clockinfo )(unsafe .Pointer (& clockb [0 ]))
56
76
hz := float64 (clock .stathz )
57
77
58
78
ncpus , err := unix .SysctlUint32 ("hw.ncpu" )
59
79
if err != nil {
60
80
return err
61
81
}
62
82
63
- var cpTime [][C . CPUSTATES ]C. int64_t
83
+ var cpTime [][CPUSTATES ]uint64
64
84
for i := 0 ; i < int (ncpus ); i ++ {
65
85
cpb , err := unix .SysctlRaw ("kern.cp_time2" , i )
66
86
if err != nil && err != unix .ENODEV {
67
87
return err
68
88
}
69
89
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 )
71
99
}
72
100
}
73
101
74
102
for cpu , time := range cpTime {
75
103
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" )
81
110
}
82
111
return err
83
112
}
0 commit comments