Skip to content

Commit 4a46201

Browse files
committed
cpu && host: fix compile time errors
1 parent 734a7a6 commit 4a46201

7 files changed

Lines changed: 129 additions & 17 deletions

File tree

cpu/cpu_fallback.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
2-
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9,!aix
1+
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
2+
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows,!dragonfly,!plan9,!aix
33

44
package cpu
55

cpu/cpu_netbsd.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ import (
1414
"golang.org/x/sys/unix"
1515
)
1616

17-
type cpuTimes struct {
18-
User uint64
19-
Nice uint64
20-
Sys uint64
21-
Intr uint64
22-
Idle uint64
23-
}
24-
2517
const (
2618
// sys/sysctl.h
2719
ctlKern = 1 // "high kernel": proc, limits

cpu/cpu_netbsd_arm64.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package cpu
22

33
type cpuTimes struct {
4-
User uint64
4+
User uint64
55
Nice uint64
66
Sys uint64
7-
Spin uint64
8-
Intr uint64
7+
Intr uint64
98
Idle uint64
109
}

host/host_fallback.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows
2-
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows
1+
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows
2+
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows
33

44
package host
55

host/host_netbsd.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//go:build netbsd
2+
// +build netbsd
3+
4+
package host
5+
6+
import (
7+
"context"
8+
"strings"
9+
10+
"github.com/shirou/gopsutil/v3/internal/common"
11+
"golang.org/x/sys/unix"
12+
)
13+
14+
func HostIDWithContext(ctx context.Context) (string, error) {
15+
return "", common.ErrNotImplementedError
16+
}
17+
18+
func numProcs(ctx context.Context) (uint64, error) {
19+
return 0, common.ErrNotImplementedError
20+
}
21+
22+
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
23+
platform := ""
24+
family := ""
25+
version := ""
26+
27+
p, err := unix.Sysctl("kern.ostype")
28+
if err == nil {
29+
platform = strings.ToLower(p)
30+
}
31+
v, err := unix.Sysctl("kern.osrelease")
32+
if err == nil {
33+
version = strings.ToLower(v)
34+
}
35+
36+
return platform, family, version, nil
37+
}
38+
39+
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
40+
return "", "", common.ErrNotImplementedError
41+
}
42+
43+
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
44+
var ret []UserStat
45+
return ret, common.ErrNotImplementedError
46+
}
47+
48+
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
49+
return []TemperatureStat{}, common.ErrNotImplementedError
50+
}
51+
52+
func KernelVersionWithContext(ctx context.Context) (string, error) {
53+
_, _, version, err := PlatformInformationWithContext(ctx)
54+
return version, err
55+
}

host/host_posix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build linux || freebsd || openbsd || darwin || solaris
2-
// +build linux freebsd openbsd darwin solaris
1+
//go:build linux || freebsd || openbsd || netbsd || darwin || solaris
2+
// +build linux freebsd openbsd netbsd darwin solaris
33

44
package host
55

internal/common/common_netbsd.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//go:build netbsd
2+
// +build netbsd
3+
4+
package common
5+
6+
import (
7+
"os"
8+
"os/exec"
9+
"strings"
10+
"unsafe"
11+
12+
"golang.org/x/sys/unix"
13+
)
14+
15+
func DoSysctrl(mib string) ([]string, error) {
16+
cmd := exec.Command("sysctl", "-n", mib)
17+
cmd.Env = getSysctrlEnv(os.Environ())
18+
out, err := cmd.Output()
19+
if err != nil {
20+
return []string{}, err
21+
}
22+
v := strings.Replace(string(out), "{ ", "", 1)
23+
v = strings.Replace(string(v), " }", "", 1)
24+
values := strings.Fields(string(v))
25+
26+
return values, nil
27+
}
28+
29+
func CallSyscall(mib []int32) ([]byte, uint64, error) {
30+
mibptr := unsafe.Pointer(&mib[0])
31+
miblen := uint64(len(mib))
32+
33+
// get required buffer size
34+
length := uint64(0)
35+
_, _, err := unix.Syscall6(
36+
unix.SYS___SYSCTL,
37+
uintptr(mibptr),
38+
uintptr(miblen),
39+
0,
40+
uintptr(unsafe.Pointer(&length)),
41+
0,
42+
0)
43+
if err != 0 {
44+
var b []byte
45+
return b, length, err
46+
}
47+
if length == 0 {
48+
var b []byte
49+
return b, length, err
50+
}
51+
// get proc info itself
52+
buf := make([]byte, length)
53+
_, _, err = unix.Syscall6(
54+
unix.SYS___SYSCTL,
55+
uintptr(mibptr),
56+
uintptr(miblen),
57+
uintptr(unsafe.Pointer(&buf[0])),
58+
uintptr(unsafe.Pointer(&length)),
59+
0,
60+
0)
61+
if err != 0 {
62+
return buf, length, err
63+
}
64+
65+
return buf, length, nil
66+
}

0 commit comments

Comments
 (0)