mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
Eliminate call to uname on FreeBSD
Improve performance by eliminating the fork out to uname on FreeBSD which also helps prevent crashes / hangs due to the outstanding fork crash bug: golang/go#15658 Also added a test for PlatformInformation.
This commit is contained in:
parent
5776ff9c7c
commit
f846eda923
@ -8,7 +8,6 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -168,25 +167,17 @@ func PlatformInformation() (string, string, string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
||||||
platform := ""
|
platform, err := unix.Sysctl("kern.ostype")
|
||||||
family := ""
|
|
||||||
version := ""
|
|
||||||
uname, err := exec.LookPath("uname")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", "", err
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := invoke.Command(uname, "-s")
|
version, err := unix.Sysctl("kern.osrelease")
|
||||||
if err == nil {
|
if err != nil {
|
||||||
platform = strings.ToLower(strings.TrimSpace(string(out)))
|
return "", "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err = invoke.Command(uname, "-r")
|
return strings.ToLower(platform), "", strings.ToLower(version), nil
|
||||||
if err == nil {
|
|
||||||
version = strings.ToLower(strings.TrimSpace(string(out)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return platform, family, version, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Virtualization() (string, string, error) {
|
func Virtualization() (string, string, error) {
|
||||||
|
@ -148,3 +148,15 @@ func TestKernelVersion(t *testing.T) {
|
|||||||
|
|
||||||
t.Logf("KernelVersion(): %s", version)
|
t.Logf("KernelVersion(): %s", version)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPlatformInformation(t *testing.T) {
|
||||||
|
platform, family, version, err := PlatformInformation()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("PlatformInformation() failed, %v", err)
|
||||||
|
}
|
||||||
|
if platform == "" {
|
||||||
|
t.Errorf("PlatformInformation() retuns empty: %v", platform)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("PlatformInformation(): %v, %v, %v", platform, family, version)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user