Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 9 Sep 2007 22:57:59 +0200
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        freebsd-questions@freebsd.org
Subject:   Re: Getting the CPU frequency in C
Message-ID:  <20070909205759.GA35519@owl.midgard.homeip.net>
In-Reply-To: <20070909205025.GA40102@glitch.rwxrwxrwx.net>
References:  <20070909205025.GA40102@glitch.rwxrwxrwx.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Sep 09, 2007 at 10:50:25PM +0200, Martin Tournoij wrote:
> I'm trying to get the CPU frequency in C:
> 
> #include <unistd.h>
> #include <time.h>
> #include <ctype.h>
> #include <sys/sysctl.h>
> #include <stdio.h>
> #include <sys/time.h>
> 
> int main()
> {
>     int mib[2];
>     size_t size;
>     struct clockinfo clockrate;
> 
>     mib[0] = CTL_KERN;
>     mib[1] = KERN_CLOCKRATE;
>     size = sizeof clockrate;
>     sysctl(mib, 2, &clockrate, &size, NULL, 0);
> 
>     fprintf(stdout, "hz: %i\n", clockrate.hz);
>     fprintf(stdout, "tick: %i\n", clockrate.tick);
>     fprintf(stdout, "spare: %i\n", clockrate.spare);
>     fprintf(stdout, "stathz: %i\n", clockrate.stathz);
>     fprintf(stdout, "profhz: %i\n", clockrate.profhz);
> 
>     return 0;
> }
> 
> I tried to run this on two machines (one machine with hw.clockrate: 1378 and
> the other 797) and it outputs the same on both:
> hz: 1000
> tick: 1000
> spare: 0
> stathz: 133
> profhz: 666
> 
> The profhz value suggest the devil is at work :D although it's probably a some
> stupid mistake on my part :/ Can anyone help?

None of the kern.clockrate entries has any particular relationship with the
CPU clock frequency, so it is not unexpected that you would get the same
output from both machines.

I think looking at hw.clockrate is the most portable you can get.
If your CPU is using Cool'n'Quiet or the Intel equivalent you can also
look at dev.cpu.N.freq for the current frequency.




-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070909205759.GA35519>