From 6ad94ae4bc5527037ce0e6b8462f58cb3b8ccef3 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 18 Feb 2020 14:14:35 +0100 Subject: Implement loadavg on all BSDs without cgo (#1584) Reuse the Go-only implementation already in place for FreeBSD (#385) on Darwin, DragonflyBSD, NetBSD and OpenBSD. Tested on all affected platforms. Signed-off-by: Tobias Klauser --- collector/loadavg_bsd.go | 41 +++++++++++++++++++++++++++++++++++++++++ collector/loadavg_freebsd.go | 40 ---------------------------------------- collector/loadavg_unix.go | 33 --------------------------------- 3 files changed, 41 insertions(+), 73 deletions(-) create mode 100644 collector/loadavg_bsd.go delete mode 100644 collector/loadavg_freebsd.go delete mode 100644 collector/loadavg_unix.go (limited to 'collector') diff --git a/collector/loadavg_bsd.go b/collector/loadavg_bsd.go new file mode 100644 index 0000000..38215aa --- /dev/null +++ b/collector/loadavg_bsd.go @@ -0,0 +1,41 @@ +// Copyright 2016 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. + +// +build darwin dragonfly freebsd netbsd openbsd +// +build !noloadavg + +package collector + +import ( + "unsafe" + + "golang.org/x/sys/unix" +) + +func getLoad() ([]float64, error) { + type loadavg struct { + load [3]uint32 + scale int + } + b, err := unix.SysctlRaw("vm.loadavg") + if err != nil { + return nil, err + } + load := *(*loadavg)(unsafe.Pointer((&b[0]))) + scale := float64(load.scale) + return []float64{ + float64(load.load[0]) / scale, + float64(load.load[1]) / scale, + float64(load.load[2]) / scale, + }, nil +} diff --git a/collector/loadavg_freebsd.go b/collector/loadavg_freebsd.go deleted file mode 100644 index e919c50..0000000 --- a/collector/loadavg_freebsd.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2016 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. - -// +build !noloadavg - -package collector - -import ( - "unsafe" - - "golang.org/x/sys/unix" -) - -func getLoad() ([]float64, error) { - type loadavg struct { - load [3]uint32 - scale int - } - b, err := unix.SysctlRaw("vm.loadavg") - if err != nil { - return nil, err - } - load := *(*loadavg)(unsafe.Pointer((&b[0]))) - scale := float64(load.scale) - return []float64{ - float64(load.load[0]) / scale, - float64(load.load[1]) / scale, - float64(load.load[2]) / scale, - }, nil -} diff --git a/collector/loadavg_unix.go b/collector/loadavg_unix.go deleted file mode 100644 index 4d65885..0000000 --- a/collector/loadavg_unix.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2015 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. - -// +build darwin dragonfly netbsd openbsd -// +build !noloadavg - -package collector - -import ( - "errors" -) - -// #include -import "C" - -func getLoad() ([]float64, error) { - var loadavg [3]C.double - samples := C.getloadavg(&loadavg[0], 3) - if samples != 3 { - return nil, errors.New("failed to get load average") - } - return []float64{float64(loadavg[0]), float64(loadavg[1]), float64(loadavg[2])}, nil -} -- cgit v1.2.3