Re: Computing cpu's clock in cycles per second
Did you look at using the RDTSC instruction to read the cycle counter? http://en.wikipedia.org/wiki/Time_Stamp_Counter Sample this over an interval to get an estimate of the clock frequency based on this counter. __inline__ unsigned long long int rdtsc() { unsigned long long int x; __asm__ volatile (.byte 0x0f, 0x31 : =A (x)); return(x); } I use gettimeofday() calls to check the wall-clock time, usleep(n) to sleep for a long time (second or more), and rdtsc to compute the cycles Seems to work well. Cheers, -- Wade Hampton -- users mailing list users@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Re: Computing cpu's clock in cycles per second
On 09/27/2010 07:34 AM, Wade Hampton wrote: Did you look at using the RDTSC instruction to read the cycle counter? http://en.wikipedia.org/wiki/Time_Stamp_Counter Sample this over an interval to get an estimate of the clock frequency based on this counter. __inline__ unsigned long long int rdtsc() { unsigned long long int x; __asm__ volatile (.byte 0x0f, 0x31 : =A (x)); return(x); } I use gettimeofday() calls to check the wall-clock time, usleep(n) to sleep for a long time (second or more), and rdtsc to compute the cycles Seems to work well. Cheers, -- Wade Hampton Thank you very much Wade. I confirm that using rdtsc, the delta in tsc values, divided by 60 (using nanosleep) is 798283074.98 ticks per second, which is almost iddentical to what is reported by cpuinfo: cpu MHz: 798.244 No matter what the load factor on the machine, even when the load reached 5 or 6, it is the same value. Well, that's what you get for buying a laptop from a crappy manufacturer that has put in it a fixed unprogrammable clock (oscillator), and put in a lame BIOS that provides no hooks for setting C2 C3 ...etc. I will certainly steer friends and family away from this manufacturer. -- users mailing list users@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Re: Computing cpu's clock in cycles per second
JD wrote: On 09/27/2010 07:34 AM, Wade Hampton wrote: Did you look at using the RDTSC instruction to read the cycle counter? http://en.wikipedia.org/wiki/Time_Stamp_Counter Sample this over an interval to get an estimate of the clock frequency based on this counter. __inline__ unsigned long long int rdtsc() { unsigned long long int x; __asm__ volatile (.byte 0x0f, 0x31 : =A (x)); return(x); } I use gettimeofday() calls to check the wall-clock time, usleep(n) to sleep for a long time (second or more), and rdtsc to compute the cycles Seems to work well. Cheers, -- Wade Hampton Thank you very much Wade. I confirm that using rdtsc, the delta in tsc values, divided by 60 (using nanosleep) is 798283074.98 ticks per second, which is almost iddentical to what is reported by cpuinfo: cpu MHz: 798.244 No matter what the load factor on the machine, even when the load reached 5 or 6, it is the same value. Well, that's what you get for buying a laptop from a crappy manufacturer that has put in it a fixed unprogrammable clock (oscillator), and put in a lame BIOS that provides no hooks for setting C2 C3 ...etc. I will certainly steer friends and family away from this manufacturer. I find the whole idea of a manufacturer purposely sabotaging a computer in this manner just disgraceful. This one really takes the cake since cool and quiet and powernow was a big marketing scheme back then. You would think that the bios would at least have something to say about it I wonder if the machine in question was on windows if the cool and quiet drivers would have something to enable/ disable them by software? But then again if Bios does not support then that would be a no show too... Very bad manufacturing screw up Michael -- users mailing list users@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Computing cpu's clock in cycles per second
Since I have not been successful to determine the runtime cpu frequency using the fedora tools: $ sudo /usr/bin/cpufreq-info -f $ sudo /usr/bin/cpufreq-info -w $ They come up empty. So, I wrote a simple program to determine an approximate value for the number of cycles per second. The algorithm is simple. Start an one shot interval timer for N seconds (can be an arg to the program), and M microseconds (can be an arg to the program). The interval timer can be virtual (-v arg to the program) or realtime (-r arg to the program). be sure to set up a proper handler routine for the SIGALRM or SIGVTALRM, depending on -v or -r. More on this later then simply run a loop that increments count (type unsigned long, initialized to 0) until the global var intr becomes none zero. Now, how many instructions were executed just for the loop. When I compiled the program with -S to see the assembler output, it turns out to be a total of 7 instructions for the loop. Now, let's assume each of the 7 instructions takes 2 or 3 cpu cycles. Lets say N cycles average per instruction. so now you have count * 7 * N total cycles in the loop. Divide that by the total number of seconds you set the timer for, and you will get a pretty close approximation of the actual runtime cpu clock (per second). Now, about the type of the interval timer. 1. I found that using the virtual interval timer yielded wrong values for count. Every time and each time it varies by thousand and sometimes millions. 2. That leaves the realtime interval timer. 3. In order to get a very close approximation to a realtime interval timer AND have a much more realistic value for the count, the program must be run as root, and niced to --20, like so: sudo nice --20 ./freq -s120 -u0 -r says run the program at the highest nice value, for 120 seconds and 0 microseconds with realtime interval timer. In my program I do not bother to do the division, because I have no idea what each different cpu's instructions cycles are for the specific 6 instruction in the loop. I use bc after the program prints the count to do the math with long precision (scale=20) The loop looks like this: while (intr == 0) count++; In assembly, it looks like this: jmp .L25 -+ .L26: | movlcount, %eax | addl$1, %eax | movl%eax, count | .L25: | movlintr, %eax| testl %eax, %eax| je .L26 -+ Here's the program. I hope some folks on this list more familiar with realtime programming can enhance it and share it with the rest of us. /* Program to compute the approximate cycles per second of a cpu clock */ #include stdio.h #include stdlib.h #include unistd.h #include signal.h #include getopt.h #include sys/time.h #include sys/timeb.h #define TWO_MINUTES (120) // seconds extern char *optarg; extern int optind, opterr, optopt; struct itimerval itv0; struct timeb t0, t1; long tv_sec = -1, tv_usec = -1; struct sigaction doit; unsigned count = 0; int intr = 0; void alarm_handler(int sig) { intr = 1; } void error(char *s) { fprintf(stderr, illegal option %s\n, (s == NULL) ? unknown : s); _exit(-1); } prt(int c) { fprintf(stderr, saw option %c\n, c); } main(int ac, char *av[]) { int opt; unsigned ms; unsigned itimer = -1; sleep(60); while ((opt = getopt(ac, av, rs:u:v)) != -1) { switch(opt) { case 's': prt('s'); tv_sec = atol(optarg); break; case 'u': prt('u'); tv_usec = atol(optarg); break; case 'r': prt('r'); itimer = ITIMER_REAL; break; case 'v': prt('v'); itimer = ITIMER_VIRTUAL; break; default: prt('?'); break; //error(optarg); } } if (tv_sec == -1) tv_sec = TWO_MINUTES; // 120 seconds. if (tv_usec == -1) tv_usec = 0; if (itimer == -1) itimer = ITIMER_REAL; itv0.it_value.tv_sec = tv_sec; itv0.it_value.tv_usec = tv_usec; itv0.it_interval.tv_sec = (long)0; itv0.it_interval.tv_usec = (long)0; doit.sa_handler = alarm_handler; if (itimer == ITIMER_VIRTUAL) sigaction(SIGVTALRM, doit, (struct sigaction *)NULL); elsesigaction(SIGALRM, doit, (struct sigaction *)NULL); ftime(t0); if (itimer == ITIMER_VIRTUAL) setitimer(ITIMER_VIRTUAL, itv0, (struct itimerval *)NULL); elsesetitimer(ITIMER_REAL, itv0, (struct itimerval *)NULL); while(intr == 0) count++; ftime(t1); ms = ((unsigned)t1.time * 1000 + (unsigned)t1.millitm) - ((unsigned)t0.time * 1000 + (unsigned)t0.millitm); fprintf(stderr, alarm after %u milliseconds \n, ms); // Above fprintf is just to show that the timer indeed ran for the
Re: Computing cpu's clock in cycles per second
On Sun, 26 Sep 2010 13:10:34 -0700 JD wrote: Since I have not been successful to determine the runtime cpu frequency using the fedora tools: How about cat /proc/cpuinfo | fgrep 'cpu MHz' Of course, I have no idea where that number comes from. I have some virtual machines which sometimes get a 0 for that number when they boot. -- users mailing list users@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Re: Computing cpu's clock in cycles per second
On 09/26/2010 01:21 PM, Tom Horsley wrote: On Sun, 26 Sep 2010 13:10:34 -0700 JD wrote: Since I have not been successful to determine the runtime cpu frequency using the fedora tools: How about cat /proc/cpuinfo | fgrep 'cpu MHz' Of course, I have no idea where that number comes from. I have some virtual machines which sometimes get a 0 for that number when they boot. That gives the bios reported value at bootup, which is not the AMD sstaated clock which is 2.4 GHz. Once it boots, and I run programs to load the cpu to 99%, couinfo still reports 790MHz. Which is totally bogus! -- users mailing list users@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Re: Computing cpu's clock in cycles per second
JD wrote: On 09/26/2010 01:21 PM, Tom Horsley wrote: On Sun, 26 Sep 2010 13:10:34 -0700 JD wrote: Since I have not been successful to determine the runtime cpu frequency using the fedora tools: How about cat /proc/cpuinfo | fgrep 'cpu MHz' Of course, I have no idea where that number comes from. I have some virtual machines which sometimes get a 0 for that number when they boot. That gives the bios reported value at bootup, which is not the AMD sstaated clock which is 2.4 GHz. Once it boots, and I run programs to load the cpu to 99%, couinfo still reports 790MHz. Which is totally bogus! What does your Bios say for speed of cpu? It sure sounds like cool and quiet is on or C2 - C3 is still on [ami...@localhost ~]$ cat /proc/cpuinfo | fgrep 'cpu MHz' cpu MHz: 3600.164 cpu MHz: 3600.164 cpu MHz: 3600.164 cpu MHz: 3600.164 [ami...@localhost ~]$ This is accurate and my Bios is reflecting this speed as well Michael -- users mailing list users@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines