Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make memInfo() less optimistic on collected values #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 16 additions & 38 deletions providers/freebsd/host_freebsd_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,57 +128,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 {
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 {
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 {
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 {
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 {
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 {
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 {
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)
}
39 changes: 34 additions & 5 deletions providers/freebsd/sysctl_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
package freebsd

import (
"testing"
"time"

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

func TestArchitecture(t *testing.T) {
Expand All @@ -42,8 +43,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 Down