File: interrupts_openbsd_amd64.go

package info (click to toggle)
prometheus-node-exporter 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,692 kB
  • sloc: sh: 800; makefile: 175; ansic: 122
file content (120 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !nointerrupts
// +build !nointerrupts

package collector

import (
	"fmt"
	"strconv"
	"unsafe"

	"github.com/prometheus/client_golang/prometheus"
	"golang.org/x/sys/unix"
)

const (
	KERN_INTRCNT        = 63
	KERN_INTRCNT_NUM    = 1
	KERN_INTRCNT_CNT    = 2
	KERN_INTRCNT_NAME   = 3
	KERN_INTRCNT_VECTOR = 4
)

func nintr() _C_int {
	mib := [3]_C_int{unix.CTL_KERN, KERN_INTRCNT, KERN_INTRCNT_NUM}
	buf, err := sysctl(mib[:])
	if err != nil {
		return 0
	}
	return *(*_C_int)(unsafe.Pointer(&buf[0]))
}

func intr(idx _C_int) (itr interrupt, err error) {
	mib := [4]_C_int{unix.CTL_KERN, KERN_INTRCNT, KERN_INTRCNT_NAME, idx}
	buf, err := sysctl(mib[:])
	if err != nil {
		return
	}
	dev := *(*[128]byte)(unsafe.Pointer(&buf[0]))
	itr.device = string(dev[:])

	mib[2] = KERN_INTRCNT_VECTOR
	buf, err = sysctl(mib[:])
	if err != nil {
		return
	}
	itr.vector = *(*int)(unsafe.Pointer(&buf[0]))

	mib[2] = KERN_INTRCNT_CNT
	buf, err = sysctl(mib[:])
	if err != nil {
		return
	}
	count := *(*uint64)(unsafe.Pointer(&buf[0]))
	itr.values = []float64{float64(count)}
	return
}

var interruptLabelNames = []string{"cpu", "type", "devices"}

func (c *interruptsCollector) Update(ch chan<- prometheus.Metric) error {
	interrupts, err := getInterrupts()
	if err != nil {
		return fmt.Errorf("couldn't get interrupts: %s", err)
	}
	for dev, interrupt := range interrupts {
		for cpuNo, value := range interrupt.values {
			interruptType := fmt.Sprintf("%d", interrupt.vector)
			filterName := interruptType + ";" + dev
			if c.nameFilter.ignored(filterName) {
				c.logger.Debug("ignoring interrupt name", "filter_name", filterName)
				continue
			}
			if !c.includeZeros && value == 0.0 {
				c.logger.Debug("ignoring interrupt with zero value", "filter_name", filterName, "cpu", cpuNo)
				continue
			}
			ch <- c.desc.mustNewConstMetric(
				value,
				strconv.Itoa(cpuNo),
				interruptType,
				dev,
			)
		}
	}
	return nil
}

type interrupt struct {
	vector int
	device string
	values []float64
}

func getInterrupts() (map[string]interrupt, error) {
	var interrupts = map[string]interrupt{}
	n := nintr()

	for i := _C_int(0); i < n; i++ {
		itr, err := intr(i)
		if err != nil {
			return nil, err
		}
		interrupts[itr.device] = itr
	}

	return interrupts, nil
}