lang: Use universal way to retrieve load
Sysinfo is not supported on macOS and results in a build error.
This commit is contained in:
committed by
James Shubin
parent
ba21554c5f
commit
d567dc3769
38
lang/funcs/facts/core/load_darwin.go
Normal file
38
lang/funcs/facts/core/load_darwin.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Mgmt
|
||||
// Copyright (C) 2013-2018+ James Shubin and the project contributors
|
||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// +build darwin
|
||||
|
||||
package core // TODO: should this be in its own individual package?
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
// macOS/Darwin specific implementation to get load.
|
||||
func load() (one, five, fifteen float64, err error) {
|
||||
avg := []C.double{0, 0, 0}
|
||||
|
||||
C.getloadavg(&avg[0], C.int(len(avg)))
|
||||
|
||||
one = float64(avg[0])
|
||||
five = float64(avg[1])
|
||||
fifteen = float64(avg[2])
|
||||
|
||||
return
|
||||
}
|
||||
45
lang/funcs/facts/core/load_posix.go
Normal file
45
lang/funcs/facts/core/load_posix.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// Mgmt
|
||||
// Copyright (C) 2013-2018+ James Shubin and the project contributors
|
||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// +build !darwin
|
||||
|
||||
package core // TODO: should this be in its own individual package?
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const (
|
||||
// LoadScale factor scales the output from sysinfo to the correct float
|
||||
// value.
|
||||
LoadScale = 65536 // XXX: is this correct or should it be 65535?
|
||||
)
|
||||
|
||||
// load returns the system load averages for the last minute, five minutes and
|
||||
// fifteen minutes. Calling this more often than once every five seconds seems
|
||||
// to be unnecessary, since the kernel only updates these values that often.
|
||||
// TODO: is the kernel update interval configurable?
|
||||
func load() (one, five, fifteen float64, err error) {
|
||||
var sysinfo syscall.Sysinfo_t
|
||||
if err = syscall.Sysinfo(&sysinfo); err != nil {
|
||||
return
|
||||
}
|
||||
one = float64(sysinfo.Loads[0]) / LoadScale
|
||||
five = float64(sysinfo.Loads[1]) / LoadScale
|
||||
fifteen = float64(sysinfo.Loads[2]) / LoadScale
|
||||
return
|
||||
}
|
||||
@@ -18,7 +18,6 @@
|
||||
package core // TODO: should this be in its own individual package?
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/purpleidea/mgmt/lang/funcs/facts"
|
||||
@@ -28,10 +27,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// LoadScale factor scales the output from sysinfo to the correct float
|
||||
// value.
|
||||
LoadScale = 65536 // XXX: is this correct or should it be 65535?
|
||||
|
||||
loadSignature = "struct{x1 float; x5 float; x15 float}"
|
||||
)
|
||||
|
||||
@@ -114,18 +109,3 @@ func (obj *LoadFact) Close() error {
|
||||
close(obj.closeChan)
|
||||
return nil
|
||||
}
|
||||
|
||||
// load returns the system load averages for the last minute, five minutes and
|
||||
// fifteen minutes. Calling this more often than once every five seconds seems
|
||||
// to be unnecessary, since the kernel only updates these values that often.
|
||||
// TODO: is the kernel update interval configurable?
|
||||
func load() (one, five, fifteen float64, err error) {
|
||||
var sysinfo syscall.Sysinfo_t
|
||||
if err = syscall.Sysinfo(&sysinfo); err != nil {
|
||||
return
|
||||
}
|
||||
one = float64(sysinfo.Loads[0]) / LoadScale
|
||||
five = float64(sysinfo.Loads[1]) / LoadScale
|
||||
fifteen = float64(sysinfo.Loads[2]) / LoadScale
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user