From 72c405ad1500b35415ab7dd2133e0ec4c5ea6e34 Mon Sep 17 00:00:00 2001 From: iori-yja Date: Fri, 2 Nov 2018 17:35:41 +0900 Subject: [PATCH 1/4] Change Dfly's CPU counting frequency, see: https://github.com/prometheus/node_exporter/issues/1129 Signed-off-by: iori-yja --- collector/cpu_dragonfly.go | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/collector/cpu_dragonfly.go b/collector/cpu_dragonfly.go index eb7f8f8af3..61a45dfacf 100644 --- a/collector/cpu_dragonfly.go +++ b/collector/cpu_dragonfly.go @@ -31,7 +31,7 @@ import ( #include 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. @@ -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); @@ -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)) @@ -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) } return cpuTimes, nil } From dcdbf3e0bfed0ab776a2b6e793dc20e4690eed0c Mon Sep 17 00:00:00 2001 From: iori-yja Date: Fri, 2 Nov 2018 17:47:02 +0900 Subject: [PATCH 2/4] Convert Dfly's CPU unit into second Signed-off-by: iori-yja --- collector/cpu_dragonfly.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collector/cpu_dragonfly.go b/collector/cpu_dragonfly.go index 61a45dfacf..b8c4c06d59 100644 --- a/collector/cpu_dragonfly.go +++ b/collector/cpu_dragonfly.go @@ -112,7 +112,7 @@ func getDragonFlyCPUTimes() ([]float64, error) { cpuTimes := make([]float64, cpuTimesLength) for i, value := range cput { - cpuTimes[i] = float64(value) + cpuTimes[i] = float64(value) / float64(1000000) } return cpuTimes, nil } From d0b0c7a57d4ecaa92e40f7351a3d2d57d58f4c94 Mon Sep 17 00:00:00 2001 From: iori-yja Date: Fri, 9 Nov 2018 17:29:33 +0900 Subject: [PATCH 3/4] Check BSD's mib which accounts for swap size; see #1127 Signed-off-by: iori-yja --- collector/memory_bsd.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/collector/memory_bsd.go b/collector/memory_bsd.go index 8c7265e362..22436e8da5 100644 --- a/collector/memory_bsd.go +++ b/collector/memory_bsd.go @@ -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 { + mibSwapTotal = "vm.swap_size" + } + fromPage := func(v float64) float64 { return v * size } @@ -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) From d6e61dfa996a439a9c54cf9365497e5f98445697 Mon Sep 17 00:00:00 2001 From: iori-yja Date: Fri, 9 Nov 2018 17:36:03 +0900 Subject: [PATCH 4/4] fix swap check code Signed-off-by: iori-yja --- collector/memory_bsd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collector/memory_bsd.go b/collector/memory_bsd.go index 22436e8da5..00bf3e55e2 100644 --- a/collector/memory_bsd.go +++ b/collector/memory_bsd.go @@ -47,7 +47,7 @@ func NewMemoryCollector() (Collector, error) { mibSwapTotal := "vm.swap_total" /* swap_total is FreeBSD specific. Fall back to Dfly specific mib if not present. */ - _, err := unix.SysctlUint32(mibSwapTotal) + _, err = unix.SysctlUint32(mibSwapTotal) if err != nil { mibSwapTotal = "vm.swap_size" }