Skip to content
Merged
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
19 changes: 4 additions & 15 deletions collector/cpu_dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
#include <stdio.h>

int
getCPUTimes(uint64_t **cputime, size_t *cpu_times_len, long *freq) {
getCPUTimes(uint64_t **cputime, size_t *cpu_times_len) {
size_t len;

// Get number of cpu cores.
Expand All @@ -44,15 +44,6 @@ getCPUTimes(uint64_t **cputime, size_t *cpu_times_len, long *freq) {
return -1;
}

// The bump on each statclock is
// ((cur_systimer - prev_systimer) * systimer_freq) >> 32
// where
// systimer_freq = sysctl kern.cputimer.freq
len = sizeof(*freq);
if (sysctlbyname("kern.cputimer.freq", freq, &len, NULL, 0)) {
return -1;
}

// Get the cpu times.
struct kinfo_cputime cp_t[ncpu];
bzero(cp_t, sizeof(struct kinfo_cputime)*ncpu);
Expand Down Expand Up @@ -103,18 +94,16 @@ func getDragonFlyCPUTimes() ([]float64, error) {
// CPUSTATES (number of CPUSTATES) is defined as 5U.
// States: CP_USER | CP_NICE | CP_SYS | CP_IDLE | CP_INTR
//
// Each value is a counter incremented at frequency
// kern.cputimer.freq
// Each value is in microseconds
//
// Look into sys/kern/kern_clock.c for details.

var (
cpuTimesC *C.uint64_t
cpuTimerFreq C.long
cpuTimesLength C.size_t
)

if C.getCPUTimes(&cpuTimesC, &cpuTimesLength, &cpuTimerFreq) == -1 {
if C.getCPUTimes(&cpuTimesC, &cpuTimesLength) == -1 {
return nil, errors.New("could not retrieve CPU times")
}
defer C.free(unsafe.Pointer(cpuTimesC))
Expand All @@ -123,7 +112,7 @@ func getDragonFlyCPUTimes() ([]float64, error) {

cpuTimes := make([]float64, cpuTimesLength)
for i, value := range cput {
cpuTimes[i] = float64(value) / float64(cpuTimerFreq)
cpuTimes[i] = float64(value) / float64(1000000)
}
return cpuTimes, nil
}
Expand Down
9 changes: 8 additions & 1 deletion collector/memory_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ func NewMemoryCollector() (Collector, error) {
}
size := float64(tmp32)

mibSwapTotal := "vm.swap_total"
/* swap_total is FreeBSD specific. Fall back to Dfly specific mib if not present. */
_, err = unix.SysctlUint32(mibSwapTotal)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if vm.swap_size is not correct either? what about other BSDs?

would it make sense to check for the specific kind of error that switching to _size can fix (not found, I suppose?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this collector only supports Dragonfly and FreeBSD. They are relatively similar to each other comparing to other BSD variants, and in this case, that mib is the only difference. If there is _total, implicitly it is supposed to be FreeBSD otherwise Dragonfly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, meminfo for OpenBSD is supported by another code. https://github.com/prometheus/node_exporter/blob/master/collector/meminfo_openbsd.go

mibSwapTotal = "vm.swap_size"
}

fromPage := func(v float64) float64 {
return v * size
}
Expand Down Expand Up @@ -98,7 +105,7 @@ func NewMemoryCollector() (Collector, error) {
{
name: "swap_size_bytes",
description: "Total swap memory size",
mib: "vm.swap_total",
mib: mibSwapTotal,
dataType: bsdSysctlTypeUint64,
},
// Descriptions via: top(1)
Expand Down