Skip to content

Commit

Permalink
cpu && host: fix compile time errors
Browse files Browse the repository at this point in the history
  • Loading branch information
darkgeek committed Sep 16, 2023
1 parent 734a7a6 commit 4a46201
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cpu/cpu_fallback.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9,!aix
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows,!dragonfly,!plan9,!aix

package cpu

Expand Down
8 changes: 0 additions & 8 deletions cpu/cpu_netbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import (
"golang.org/x/sys/unix"
)

type cpuTimes struct {
User uint64
Nice uint64
Sys uint64
Intr uint64
Idle uint64
}

const (
// sys/sysctl.h
ctlKern = 1 // "high kernel": proc, limits
Expand Down
5 changes: 2 additions & 3 deletions cpu/cpu_netbsd_arm64.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cpu

type cpuTimes struct {
User uint64
User uint64
Nice uint64
Sys uint64
Spin uint64
Intr uint64
Intr uint64
Idle uint64
}
4 changes: 2 additions & 2 deletions host/host_fallback.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows

package host

Expand Down
55 changes: 55 additions & 0 deletions host/host_netbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build netbsd
// +build netbsd

package host

import (
"context"
"strings"

"github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix"
)

func HostIDWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func numProcs(ctx context.Context) (uint64, error) {
return 0, common.ErrNotImplementedError
}

func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
platform := ""
family := ""
version := ""

p, err := unix.Sysctl("kern.ostype")
if err == nil {
platform = strings.ToLower(p)
}
v, err := unix.Sysctl("kern.osrelease")
if err == nil {
version = strings.ToLower(v)
}

return platform, family, version, nil
}

func VirtualizationWithContext(ctx context.Context) (string, string, error) {
return "", "", common.ErrNotImplementedError
}

func UsersWithContext(ctx context.Context) ([]UserStat, error) {
var ret []UserStat
return ret, common.ErrNotImplementedError
}

func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError
}

func KernelVersionWithContext(ctx context.Context) (string, error) {
_, _, version, err := PlatformInformationWithContext(ctx)
return version, err
}
4 changes: 2 additions & 2 deletions host/host_posix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux || freebsd || openbsd || darwin || solaris
// +build linux freebsd openbsd darwin solaris
//go:build linux || freebsd || openbsd || netbsd || darwin || solaris
// +build linux freebsd openbsd netbsd darwin solaris

package host

Expand Down
66 changes: 66 additions & 0 deletions internal/common/common_netbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build netbsd
// +build netbsd

package common

import (
"os"
"os/exec"
"strings"
"unsafe"

"golang.org/x/sys/unix"
)

func DoSysctrl(mib string) ([]string, error) {
cmd := exec.Command("sysctl", "-n", mib)
cmd.Env = getSysctrlEnv(os.Environ())
out, err := cmd.Output()
if err != nil {
return []string{}, err
}
v := strings.Replace(string(out), "{ ", "", 1)
v = strings.Replace(string(v), " }", "", 1)
values := strings.Fields(string(v))

return values, nil
}

func CallSyscall(mib []int32) ([]byte, uint64, error) {
mibptr := unsafe.Pointer(&mib[0])
miblen := uint64(len(mib))

// get required buffer size
length := uint64(0)
_, _, err := unix.Syscall6(
unix.SYS___SYSCTL,
uintptr(mibptr),
uintptr(miblen),
0,
uintptr(unsafe.Pointer(&length)),
0,
0)
if err != 0 {
var b []byte
return b, length, err
}
if length == 0 {
var b []byte
return b, length, err
}
// get proc info itself
buf := make([]byte, length)
_, _, err = unix.Syscall6(
unix.SYS___SYSCTL,
uintptr(mibptr),
uintptr(miblen),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&length)),
0,
0)
if err != 0 {
return buf, length, err
}

return buf, length, nil
}

0 comments on commit 4a46201

Please sign in to comment.