From 45ac033d9ef416cf17a6b57244a47f4fab92ab17 Mon Sep 17 00:00:00 2001 From: stuart nelson Date: Mon, 19 Sep 2016 09:35:41 +0200 Subject: [PATCH] Use correct frequency for calculating cpu time The correct frequency is the systimer frequency, not the stathz. From one of the DragonFly developers: The bump upon each statclock is: ((cur_systimer - prev_systimer) * systimer_freq) >> 32 systimer_freq can be extracted from following sysctl in userspace: sysctl kern.cputimer.freq --- collector/cpu_dragonfly.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/collector/cpu_dragonfly.go b/collector/cpu_dragonfly.go index 2440f2a..b1a9785 100644 --- a/collector/cpu_dragonfly.go +++ b/collector/cpu_dragonfly.go @@ -63,16 +63,16 @@ getCPUTimes(char **cputime) { return -1; } - // Retrieve clockrate - struct clockinfo clockrate; - size_t clockrate_size = sizeof(clockrate); - if (sysctl(mib_kern_clockrate, mib_kern_clockrate_len, &clockrate, &clockrate_size, NULL, 0) == -1 || - sizeof(clockrate) != clockrate_size) { + // The bump on each statclock is + // ((cur_systimer - prev_systimer) * systimer_freq) >> 32 + // where + // systimer_freq = sysctl kern.cputimer.freq + long freq; + len = sizeof(freq); + if (sysctlbyname("kern.cputimer.freq", &freq, &len, NULL, 0)) { return -1; } - long freq = clockrate.stathz > 0 ? clockrate.stathz : clockrate.hz; - // Get the cpu times. struct kinfo_cputime cp_t[ncpu]; bzero(cp_t, sizeof(struct kinfo_cputime)*ncpu);