Skip to content

Commit

Permalink
Merge pull request #1 from sarog/FreeBSD
Browse files Browse the repository at this point in the history
Requested upstream changes
  • Loading branch information
redanthrax authored Jun 20, 2024
2 parents 5c5317e + 1106c15 commit 57174b1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 79 deletions.
59 changes: 17 additions & 42 deletions providers/freebsd/host_freebsd_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ func (r *reader) addErr(err error) bool {
}

func (r *reader) Err() error {
if len(r.errs) > 0 {
return errors.Join(r.errs...)
}
return nil
return errors.Join(r.errs...)
}

func (r *reader) cpuTime(cpu *types.CPUTimes) {
Expand All @@ -128,57 +125,35 @@ func (r *reader) memInfo(m *types.HostMemoryInfo) {
// free = free
// available = buffers + inactive + cache + free

ps, err := pageSizeBytes()
pageSize, err := pageSizeBytes()
if r.addErr(err) {
return
}
pageSize := uint64(ps)

m.Total, err = totalPhysicalMem()
if r.addErr(err) {
return
}
m.Total = totalPhysicalMem(r)
activePages := activePageCount(r)

activePages, err := activePageCount()
if r.addErr(err) {
return
}
m.Metrics = make(map[string]uint64, 6)
m.Metrics["active_bytes"] = uint64(activePages) * pageSize
m.Metrics["active_bytes"] = activePages * pageSize

wirePages, err := wirePageCount()
if r.addErr(err) {
return
}
m.Metrics["wired_bytes"] = uint64(wirePages) * pageSize
wirePages := wirePageCount(r)
m.Metrics["wired_bytes"] = wirePages * pageSize

inactivePages, err := inactivePageCount()
if r.addErr(err) {
return
}
m.Metrics["inactive_bytes"] = uint64(inactivePages) * pageSize
inactivePages := inactivePageCount(r)
m.Metrics["inactive_bytes"] = inactivePages * pageSize

cachePages, err := cachePageCount()
if r.addErr(err) {
return
}
m.Metrics["cache_bytes"] = uint64(cachePages) * pageSize
cachePages := cachePageCount(r)
m.Metrics["cache_bytes"] = cachePages * pageSize

freePages, err := freePageCount()
if r.addErr(err) {
return
}
m.Metrics["free_bytes"] = uint64(freePages) * pageSize
freePages := freePageCount(r)
m.Metrics["free_bytes"] = freePages * pageSize

buffers, err := buffersUsedBytes()
if r.addErr(err) {
return
}
buffers := buffersUsedBytes(r)
m.Metrics["buffer_bytes"] = buffers

m.Used = uint64(activePages+wirePages) * pageSize
m.Free = uint64(freePages) * pageSize
m.Available = uint64(inactivePages+cachePages+freePages)*pageSize + buffers
m.Used = (activePages + wirePages) * pageSize
m.Free = freePages * pageSize
m.Available = (inactivePages+cachePages+freePages)*pageSize + buffers

// Virtual (swap) Memory
swap, err := kvmGetSwapInfo()
Expand Down
2 changes: 1 addition & 1 deletion providers/freebsd/process_freebsd_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (p *process) Memory() (types.MemoryInfo, error) {
p.kinfo = procs[0].kinfo

return types.MemoryInfo{
Resident: uint64(p.kinfo.ki_rssize) * uint64(pageSize),
Resident: uint64(p.kinfo.ki_rssize) * pageSize,
Virtual: uint64(p.kinfo.ki_size),
}, nil
}
Expand Down
62 changes: 31 additions & 31 deletions providers/freebsd/sysctl_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ var tickDuration = sync.OnceValues(func() (time.Duration, error) {
return time.Duration(c.Tick) * time.Microsecond, nil
})

var pageSizeBytes = sync.OnceValues(func() (uint32, error) {
var pageSizeBytes = sync.OnceValues(func() (uint64, error) {
const mib = "vm.stats.vm.v_page_size"

v, err := unix.SysctlUint32(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
}

return v, nil
return uint64(v), nil
})

func activePageCount() (uint32, error) {
func activePageCount(r *reader) uint64 {

Check failure on line 56 in providers/freebsd/sysctl_freebsd.go

View workflow job for this annotation

GitHub Actions / check

undefined: reader
const mib = "vm.stats.vm.v_active_count"

v, err := unix.SysctlUint32(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
if r.addErr(err) {
return 0
}
return v, nil
return uint64(v)
}

func architecture() (string, error) {
Expand All @@ -87,26 +87,26 @@ func bootTime() (time.Time, error) {
}

// buffersUsedBytes returns the number memory bytes used as disk cache.
func buffersUsedBytes() (uint64, error) {
func buffersUsedBytes(r *reader) uint64 {

Check failure on line 90 in providers/freebsd/sysctl_freebsd.go

View workflow job for this annotation

GitHub Actions / check

undefined: reader
const mib = "vfs.bufspace"

v, err := unix.SysctlUint64(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
if r.addErr(err) {
return 0
}

return v, nil
return v
}

func cachePageCount() (uint32, error) {
func cachePageCount(r *reader) uint64 {

Check failure on line 101 in providers/freebsd/sysctl_freebsd.go

View workflow job for this annotation

GitHub Actions / check

undefined: reader
const mib = "vm.stats.vm.v_cache_count"

v, err := unix.SysctlUint32(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
if r.addErr(err) {
return 0
}

return v, nil
return uint64(v)
}

const sizeOfUint64 = int(unsafe.Sizeof(uint64(0)))
Expand All @@ -120,7 +120,7 @@ func cpuStateTimes() (*types.CPUTimes, error) {
}

const mib = "kern.cp_time"
buf, err := unix.SysctlRaw("kern.cp_time")
buf, err := unix.SysctlRaw(mib)
if err != nil {
return nil, fmt.Errorf("failed to get %s: %w", mib, err)
}
Expand All @@ -143,26 +143,26 @@ func cpuStateTimes() (*types.CPUTimes, error) {
}, nil
}

func freePageCount() (uint32, error) {
func freePageCount(r *reader) uint64 {

Check failure on line 146 in providers/freebsd/sysctl_freebsd.go

View workflow job for this annotation

GitHub Actions / check

undefined: reader
const mib = "vm.stats.vm.v_free_count"

v, err := unix.SysctlUint32(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
if r.addErr(err) {
return 0
}

return v, nil
return uint64(v)
}

func inactivePageCount() (uint32, error) {
func inactivePageCount(r *reader) uint64 {

Check failure on line 157 in providers/freebsd/sysctl_freebsd.go

View workflow job for this annotation

GitHub Actions / check

undefined: reader
const mib = "vm.stats.vm.v_inactive_count"

v, err := unix.SysctlUint32(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
if r.addErr(err) {
return 0
}

return v, nil
return uint64(v)
}

func kernelVersion() (string, error) {
Expand Down Expand Up @@ -227,22 +227,22 @@ func operatingSystem() (*types.OSInfo, error) {
return info, nil
}

func totalPhysicalMem() (uint64, error) {
func totalPhysicalMem(r *reader) uint64 {

Check failure on line 230 in providers/freebsd/sysctl_freebsd.go

View workflow job for this annotation

GitHub Actions / check

undefined: reader
const mib = "hw.physmem"

v, err := unix.SysctlUint64(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
if r.addErr(err) {
return 0
}
return v, nil
return v
}

func wirePageCount() (uint32, error) {
func wirePageCount(r *reader) uint64 {

Check failure on line 240 in providers/freebsd/sysctl_freebsd.go

View workflow job for this annotation

GitHub Actions / check

undefined: reader
const mib = "vm.stats.vm.v_wire_count"

v, err := unix.SysctlUint32(mib)
if err != nil {
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
if r.addErr(err) {
return 0
}
return v, nil
return uint64(v)
}
53 changes: 48 additions & 5 deletions providers/freebsd/sysctl_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
package freebsd

import (
"testing"
"time"

"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os/exec"
"strings"
"testing"
"time"
)

func TestArchitecture(t *testing.T) {
Expand All @@ -34,6 +36,7 @@ func TestArchitecture(t *testing.T) {
}

assert.NotEmpty(t, arch)
assert.Regexp(t, `(amd64|i386|powerpc|arm(64)?|riscv|mips|sparc64|pc98)`, arch)
}

func TestBootTime(t *testing.T) {
Expand All @@ -42,8 +45,36 @@ func TestBootTime(t *testing.T) {
t.Fatal(err)
}

// Apply a sanity check. This assumes the host has rebooted in the last year.
assert.WithinDuration(t, time.Now().UTC(), bootTime, 365*24*time.Hour)
bootDiff := time.Since(bootTime)
// t.Logf("bootTime in seconds: %#v", int64(bootDiff.Seconds()))

cmd := exec.Command("/usr/bin/uptime", "--libxo=json")
upcmd, err := cmd.Output()

if err != nil {
t.Fatal(err)
}

t.Logf(string(upcmd))

type UptimeOutput struct {
UptimeInformation struct {
Uptime int64 `json:"uptime"`
} `json:"uptime-information"`
}

var upInfo UptimeOutput
err = json.Unmarshal(upcmd, &upInfo)

if err != nil {
t.Fatal(err)
}

upsec := upInfo.UptimeInformation.Uptime
uptime := time.Duration(upsec * int64(time.Second))
// t.Logf("uptime in seconds: %#v", int64(uptime.Seconds()))

assert.InDelta(t, uptime, bootDiff, float64(5*time.Second))
}

func TestCPUStateTimes(t *testing.T) {
Expand All @@ -62,7 +93,18 @@ func TestKernelVersion(t *testing.T) {
t.Fatal(err)
}

// Retrieve currently running kernel version
cmd := exec.Command("/bin/freebsd-version", "-r")
fbsdout, err := cmd.Output()

if err != nil {
t.Fatal(err)
}

fbsdver := strings.TrimSuffix(string(fbsdout), "\n")

assert.NotEmpty(t, kernel)
assert.EqualValues(t, kernel, fbsdver)
}

func TestMachineID(t *testing.T) {
Expand All @@ -72,6 +114,7 @@ func TestMachineID(t *testing.T) {
}

assert.NotEmpty(t, machineID)
assert.Regexp(t, "^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$", machineID)
}

func TestOperatingSystem(t *testing.T) {
Expand Down

0 comments on commit 57174b1

Please sign in to comment.