Skip to content

Commit 57174b1

Browse files
authored
Merge pull request #1 from sarog/FreeBSD
Requested upstream changes
2 parents 5c5317e + 1106c15 commit 57174b1

File tree

4 files changed

+97
-79
lines changed

4 files changed

+97
-79
lines changed

providers/freebsd/host_freebsd_cgo.go

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ func (r *reader) addErr(err error) bool {
107107
}
108108

109109
func (r *reader) Err() error {
110-
if len(r.errs) > 0 {
111-
return errors.Join(r.errs...)
112-
}
113-
return nil
110+
return errors.Join(r.errs...)
114111
}
115112

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

131-
ps, err := pageSizeBytes()
128+
pageSize, err := pageSizeBytes()
132129
if r.addErr(err) {
133130
return
134131
}
135-
pageSize := uint64(ps)
136132

137-
m.Total, err = totalPhysicalMem()
138-
if r.addErr(err) {
139-
return
140-
}
133+
m.Total = totalPhysicalMem(r)
134+
activePages := activePageCount(r)
141135

142-
activePages, err := activePageCount()
143-
if r.addErr(err) {
144-
return
145-
}
146136
m.Metrics = make(map[string]uint64, 6)
147-
m.Metrics["active_bytes"] = uint64(activePages) * pageSize
137+
m.Metrics["active_bytes"] = activePages * pageSize
148138

149-
wirePages, err := wirePageCount()
150-
if r.addErr(err) {
151-
return
152-
}
153-
m.Metrics["wired_bytes"] = uint64(wirePages) * pageSize
139+
wirePages := wirePageCount(r)
140+
m.Metrics["wired_bytes"] = wirePages * pageSize
154141

155-
inactivePages, err := inactivePageCount()
156-
if r.addErr(err) {
157-
return
158-
}
159-
m.Metrics["inactive_bytes"] = uint64(inactivePages) * pageSize
142+
inactivePages := inactivePageCount(r)
143+
m.Metrics["inactive_bytes"] = inactivePages * pageSize
160144

161-
cachePages, err := cachePageCount()
162-
if r.addErr(err) {
163-
return
164-
}
165-
m.Metrics["cache_bytes"] = uint64(cachePages) * pageSize
145+
cachePages := cachePageCount(r)
146+
m.Metrics["cache_bytes"] = cachePages * pageSize
166147

167-
freePages, err := freePageCount()
168-
if r.addErr(err) {
169-
return
170-
}
171-
m.Metrics["free_bytes"] = uint64(freePages) * pageSize
148+
freePages := freePageCount(r)
149+
m.Metrics["free_bytes"] = freePages * pageSize
172150

173-
buffers, err := buffersUsedBytes()
174-
if r.addErr(err) {
175-
return
176-
}
151+
buffers := buffersUsedBytes(r)
177152
m.Metrics["buffer_bytes"] = buffers
178153

179-
m.Used = uint64(activePages+wirePages) * pageSize
180-
m.Free = uint64(freePages) * pageSize
181-
m.Available = uint64(inactivePages+cachePages+freePages)*pageSize + buffers
154+
m.Used = (activePages + wirePages) * pageSize
155+
m.Free = freePages * pageSize
156+
m.Available = (inactivePages+cachePages+freePages)*pageSize + buffers
182157

183158
// Virtual (swap) Memory
184159
swap, err := kvmGetSwapInfo()

providers/freebsd/process_freebsd_cgo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (p *process) Memory() (types.MemoryInfo, error) {
285285
p.kinfo = procs[0].kinfo
286286

287287
return types.MemoryInfo{
288-
Resident: uint64(p.kinfo.ki_rssize) * uint64(pageSize),
288+
Resident: uint64(p.kinfo.ki_rssize) * pageSize,
289289
Virtual: uint64(p.kinfo.ki_size),
290290
}, nil
291291
}

providers/freebsd/sysctl_freebsd.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,25 @@ var tickDuration = sync.OnceValues(func() (time.Duration, error) {
4242
return time.Duration(c.Tick) * time.Microsecond, nil
4343
})
4444

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

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

53-
return v, nil
53+
return uint64(v), nil
5454
})
5555

56-
func activePageCount() (uint32, error) {
56+
func activePageCount(r *reader) uint64 {
5757
const mib = "vm.stats.vm.v_active_count"
5858

5959
v, err := unix.SysctlUint32(mib)
60-
if err != nil {
61-
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
60+
if r.addErr(err) {
61+
return 0
6262
}
63-
return v, nil
63+
return uint64(v)
6464
}
6565

6666
func architecture() (string, error) {
@@ -87,26 +87,26 @@ func bootTime() (time.Time, error) {
8787
}
8888

8989
// buffersUsedBytes returns the number memory bytes used as disk cache.
90-
func buffersUsedBytes() (uint64, error) {
90+
func buffersUsedBytes(r *reader) uint64 {
9191
const mib = "vfs.bufspace"
9292

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

98-
return v, nil
98+
return v
9999
}
100100

101-
func cachePageCount() (uint32, error) {
101+
func cachePageCount(r *reader) uint64 {
102102
const mib = "vm.stats.vm.v_cache_count"
103103

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

109-
return v, nil
109+
return uint64(v)
110110
}
111111

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

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

146-
func freePageCount() (uint32, error) {
146+
func freePageCount(r *reader) uint64 {
147147
const mib = "vm.stats.vm.v_free_count"
148148

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

154-
return v, nil
154+
return uint64(v)
155155
}
156156

157-
func inactivePageCount() (uint32, error) {
157+
func inactivePageCount(r *reader) uint64 {
158158
const mib = "vm.stats.vm.v_inactive_count"
159159

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

165-
return v, nil
165+
return uint64(v)
166166
}
167167

168168
func kernelVersion() (string, error) {
@@ -227,22 +227,22 @@ func operatingSystem() (*types.OSInfo, error) {
227227
return info, nil
228228
}
229229

230-
func totalPhysicalMem() (uint64, error) {
230+
func totalPhysicalMem(r *reader) uint64 {
231231
const mib = "hw.physmem"
232232

233233
v, err := unix.SysctlUint64(mib)
234-
if err != nil {
235-
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
234+
if r.addErr(err) {
235+
return 0
236236
}
237-
return v, nil
237+
return v
238238
}
239239

240-
func wirePageCount() (uint32, error) {
240+
func wirePageCount(r *reader) uint64 {
241241
const mib = "vm.stats.vm.v_wire_count"
242242

243243
v, err := unix.SysctlUint32(mib)
244-
if err != nil {
245-
return 0, fmt.Errorf("failed to get %s: %w", mib, err)
244+
if r.addErr(err) {
245+
return 0
246246
}
247-
return v, nil
247+
return uint64(v)
248248
}

providers/freebsd/sysctl_freebsd_test.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
package freebsd
2121

2222
import (
23-
"testing"
24-
"time"
25-
23+
"encoding/json"
2624
"github.com/stretchr/testify/assert"
2725
"github.com/stretchr/testify/require"
26+
"os/exec"
27+
"strings"
28+
"testing"
29+
"time"
2830
)
2931

3032
func TestArchitecture(t *testing.T) {
@@ -34,6 +36,7 @@ func TestArchitecture(t *testing.T) {
3436
}
3537

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

3942
func TestBootTime(t *testing.T) {
@@ -42,8 +45,36 @@ func TestBootTime(t *testing.T) {
4245
t.Fatal(err)
4346
}
4447

45-
// Apply a sanity check. This assumes the host has rebooted in the last year.
46-
assert.WithinDuration(t, time.Now().UTC(), bootTime, 365*24*time.Hour)
48+
bootDiff := time.Since(bootTime)
49+
// t.Logf("bootTime in seconds: %#v", int64(bootDiff.Seconds()))
50+
51+
cmd := exec.Command("/usr/bin/uptime", "--libxo=json")
52+
upcmd, err := cmd.Output()
53+
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
t.Logf(string(upcmd))
59+
60+
type UptimeOutput struct {
61+
UptimeInformation struct {
62+
Uptime int64 `json:"uptime"`
63+
} `json:"uptime-information"`
64+
}
65+
66+
var upInfo UptimeOutput
67+
err = json.Unmarshal(upcmd, &upInfo)
68+
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
73+
upsec := upInfo.UptimeInformation.Uptime
74+
uptime := time.Duration(upsec * int64(time.Second))
75+
// t.Logf("uptime in seconds: %#v", int64(uptime.Seconds()))
76+
77+
assert.InDelta(t, uptime, bootDiff, float64(5*time.Second))
4778
}
4879

4980
func TestCPUStateTimes(t *testing.T) {
@@ -62,7 +93,18 @@ func TestKernelVersion(t *testing.T) {
6293
t.Fatal(err)
6394
}
6495

96+
// Retrieve currently running kernel version
97+
cmd := exec.Command("/bin/freebsd-version", "-r")
98+
fbsdout, err := cmd.Output()
99+
100+
if err != nil {
101+
t.Fatal(err)
102+
}
103+
104+
fbsdver := strings.TrimSuffix(string(fbsdout), "\n")
105+
65106
assert.NotEmpty(t, kernel)
107+
assert.EqualValues(t, kernel, fbsdver)
66108
}
67109

68110
func TestMachineID(t *testing.T) {
@@ -72,6 +114,7 @@ func TestMachineID(t *testing.T) {
72114
}
73115

74116
assert.NotEmpty(t, machineID)
117+
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)
75118
}
76119

77120
func TestOperatingSystem(t *testing.T) {

0 commit comments

Comments
 (0)