Getting the CPU frequency in C
Martin Tournoij
carpetsmoker at xs4all.nl
Sun Sep 9 13:46:05 PDT 2007
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?
Thanks,
Martin Tournoij
More information about the freebsd-questions
mailing list