![]() |
| | #1 |
| Registered User Join Date: Mar 2008
Posts: 15
| Determining CPU frequency is there any way one can determine the current CPU frequency? I was trying to figure out a way to do this but couldn't find a way that works. My approach was to use the TSC (Time Stamp Counter), but with the tickless feature in the kernel the TSC gets unstable and can't be used for frequency calculation anymore. Here's a draft of this idea (though on my systems with tickless enabled the calculated frequency is absolutely wrong): Code: #include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
unsigned long long int rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
int main()
{
struct timezone tz;
struct timeval tvstart, tvstop;
unsigned long long int cycles[2];
unsigned long microseconds;
int mhz;
memset(&tz, 0, sizeof(tz));
gettimeofday(&tvstart, &tz);
cycles[0] = rdtsc();
gettimeofday(&tvstart, &tz);
usleep(250000);
gettimeofday(&tvstop, &tz);
cycles[1] = rdtsc();
gettimeofday(&tvstop, &tz);
microseconds = ((tvstop.tv_sec-tvstart.tv_sec)*1000000) + (tvstop.tv_usec-tvstart.tv_usec);
mhz = (int) (cycles[1]-cycles[0]) / microseconds;
printf("%i MHz\n",mhz);
return 0;
}
With tickless this gets all messed up and I can't come up with any other idea... Peter |
| pgzh is offline | |
| | #2 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| Quote:
If you're on Linux, just read the frequency from /proc/cpuinfo. Calculating it yourself on a live system is going to be impossible. I thought maybe the CPUID instruction would give you the processor frequency, but it doesn't seem to. You might also be able to query the BIOS somehow, but I doubt you can do that from a booted operating system. | |
| brewbuck is offline | |
| | #3 |
| Registered User Join Date: Mar 2008
Posts: 15
| Are you sure Linux reads the TSC for frequency calculation? The cpu mhz info in /proc/cpuinfo changes when using cpufreq with ondemand, but the TSC does not tick constant if tickless is enabled - and therefore it's impossible to calculate the frequency after booting up with the TSC. Reading /proc/cpuinfo seems like quite a cheap solution, but I really can't think of any better way to get the cpu frequency. I don't know if MS Windows implements something comparable to the tickless feature of Linux, because there are lots of tools for Windows that can calculate the cpu frequency quite accurately. Or maybe there is another way to get the current cpu mhz? Anyway - thanks for the tip! Peter |
| pgzh is offline | |
| | #4 | ||
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| Quote:
Quote:
| ||
| brewbuck is offline | |
| | #5 |
| Registered User Join Date: Mar 2008
Posts: 15
| Where exactly in the Linux source code did you look? Maybe I can look for the way this is handled in 2.6 myself. Peter |
| pgzh is offline | |
| | #6 |
| Registered User Join Date: Mar 2003
Posts: 3,903
| |
| Codeplug is offline | |
| | #7 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| |
| brewbuck is offline | |
| | #8 | ||
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Quote:
If TSC is varying, then it's most likely because you are running the power management and it detects idle CPU and stops/slows down the CPU. This SHOULD happen whether you are running with or without ticks, however. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | ||
| matsp is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| questions on multiple thread programming | lehe | C Programming | 11 | 03-27-2009 07:44 AM |
| Upgrading my old CPU (for another old one!) | foxman | Tech Board | 16 | 01-11-2008 05:41 PM |
| Can you still view the bios screen with a bad CPU? | HyperCreep | Tech Board | 4 | 12-31-2006 06:57 PM |
| CPU temp | PING | Tech Board | 5 | 01-28-2006 06:25 AM |
| determining frequency | lakai02 | C Programming | 4 | 10-03-2002 05:03 PM |