RDTSC use in C: | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

RDTSC use in C:

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-18-2008
GloriousDaisy GloriousDaisy is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 23 September 2008, 8:51 PM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
RDTSC use in C:

I have to use rdtsc on ubuntu for performance evaluation. But i am not getting the correct value. I am placing a sleep on 10 seconds, but value i get is some 1 sec... i dont know where im going wrong?


Code:
#include<sys/time.h>
#include<time.h>
#include<stdio.h>
typedef unsigned long long ticks;

static __inline__ ticks getticks(void)
{
     unsigned a, d;
     asm("cpuid");
     asm volatile("rdtsc" : "=a" (a), "=d" (d));

     return (((ticks)a) | (((ticks)d) << 32));
}
int main(){
     int r = 0;
     ticks tick,tick1,tickh;
     unsigned time =0;

     tick = getticks();
   
     sleep(10);
   
     tick1 = getticks();
   
     time = (unsigned)((tick1-tick)/1662543);
    printf("\ntime in MS%u\n",time);
return 0;
}

cat /proc/cpuinfo gives

Code:
pprocessor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 14
model name      : Genuine Intel(R) CPU           T2300  @ 1.66GHz
stepping        : 8
cpu MHz         : 1662.543
cache size      : 2048 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush dts acpi mmx fxsr sse sse2 ss tm pbe constant_tsc up arch_perfmon bts pni monitor vmx est tm2 xtpr
bogomips        : 3328.87
clflush size    : 64


Last edited by vino; 09-18-2008 at 02:45 AM.. Reason: added code tags
Sponsored Links
    #2  
Old 09-26-2008
otheus's Avatar
otheus otheus is offline Forum Advisor  
Smartass
 
Join Date: Feb 2007
Last Activity: 23 July 2014, 6:24 AM EDT
Location: Innsbruck, Austria
Posts: 2,151
Thanks: 12
Thanked 48 Times in 45 Posts
This works on my computer just fine, after adjusting the constant to the value I find in my own proc/cpuinfo. Place printf statements after each getticks() call. Be sure to use %Ld:
printf("%Ld\n",tick1);
Sponsored Links
    #3  
Old 09-30-2008
calv calv is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 8 December 2008, 9:53 AM EST
Location: Germany
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
rdtsc is not good for time measurement.

The problem is, that the CPU might throttle (with it does most of the time, when all processes are blocked), so a cpu clock might mean more time in that case. This is of course CPU and OS dependant.
    #4  
Old 06-08-2009
Sivaraman Sivaraman is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 13 June 2009, 5:19 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
CPU & Memory Finding CPU speed while CPU is in Throttling

Hi ,

I am trying to find out the Speed of the CPU while it is in 87.5 % Throttling. I am using RDTSC to get the Frequency of the Processor. Its reporting as 15 GHz while it is running in the lowest Speed. I need to validate whether CPU is running in the specified speed while it is in Throttling. As Calv notified i cannot use RDTSC, what could be the better way of doing. could you please any one help me.

Thanks
Siva
Sponsored Links
    #5  
Old 06-12-2009
otheus's Avatar
otheus otheus is offline Forum Advisor  
Smartass
 
Join Date: Feb 2007
Last Activity: 23 July 2014, 6:24 AM EDT
Location: Innsbruck, Austria
Posts: 2,151
Thanks: 12
Thanked 48 Times in 45 Posts
Sivaraman,

I need to re-phrase your question --- please tell me if this is correct: How does one find out (on Linux) whether the CPU is actually running at the speed specified in cpuinfo?

Instead of the sleep statement above, set an alarm for 10 seconds, do some CPU-intensive work, get the number of ticks, compare the clocks to find how long the CPU actually slept (setting an alarm does not guarantee anything) and calculate. Use gettimeofday() to get the actual time.
Sponsored Links
    #6  
Old 06-12-2009
Sivaraman Sivaraman is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 13 June 2009, 5:19 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your idea. But i am using the application in a EFI environment. To get the Ticks processed we can use RDTSC only. Anyway i will try your logic.
Sponsored Links
    #7  
Old 06-12-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 27 July 2014, 6:22 PM EDT
Location: /dev/ph
Posts: 4,772
Thanks: 59
Thanked 423 Times in 392 Posts
I am curious. Exactly what do you mean by "Speed of the CPU"? Frequency or have you some other measurement in mind?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 08:57 PM.