LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2010, 09:03 PM   #1
lonelygoat
LQ Newbie
 
Registered: Nov 2009
Posts: 13

Rep: Reputation: 2
About Intel CPU rdtsc instruction


I played the following piece of code on both Windows and Linux

// Windows with Visual Studio 2008
unsigned __int64 GetCycleCount()
{
__asm RDTSC
}
...
t = GetCycleCount();
t2 = GetCycleCount();
wprintf(TEXT("cycle diff: %d\r\n"), t2 - t);
...

// Linux with gcc
unsigned long long int GetCycleCount()
{
asm ("rdtsc");
}
...
t = GetCycleCount();
t2 = GetCycleCount();
printf("cycle diff: %d\n", t2 - t);
...

while I always got "cycle diff: 88" under Linux, I got 308 under Windows XP (SP3) occationally (most time I got 297)..

I am wondering why there may be a very small difference under Windows ? just 11 cycle ? If there was a context switch or something, there would be much much bigger difference that 11.
 
Old 01-28-2010, 06:16 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Have you looked at the code generated by the compiler?
 
Old 01-28-2010, 08:14 PM   #3
vigol
Member
 
Registered: Feb 2008
Location: Uns Cigano from Vigol
Distribution: FreeBSD 8.0
Posts: 59

Rep: Reputation: 19
Microsoft recommends using QueryPerformanceCounter instead of RDTSC
http://www.strchr.com/performance_me...nts_with_rdtsc
An alternative solution with rdtsc
http://groups.google.com/group/micro...ce7086d2e7549e
 
Old 01-28-2010, 08:51 PM   #4
lonelygoat
LQ Newbie
 
Registered: Nov 2009
Posts: 13

Original Poster
Rep: Reputation: 2
Thanks guys,,

I didn't look at the code generated. I think whatever code was generated, the difference should be a constant unless it was interrupted between the two rdtsc instructions. In a case of interruption, I would expect much bigger difference than just 11 cycles. I didn't check Intel manual though..

The links are very useful..thx..
 
Old 01-28-2010, 09:20 PM   #5
lonelygoat
LQ Newbie
 
Registered: Nov 2009
Posts: 13

Original Poster
Rep: Reputation: 2
I guess it could be caused by cache warming up..
 
Old 01-28-2010, 11:47 PM   #6
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
I don't know if it is cache warming up. as if that were the issue, you should have seen the same numbers the first time the code was run. After all, code is not there in the caches when it runs the first time. In case it indeed is cache warming up, you could run the code in a loop to see if the numbers start matching.

Also, I am just thinking aloud here, could it be powersave features of the processor kicking in, perhaps, better implemented on Linux? If you are running the test on a laptop, you could set the CPU policy to performance to see if that changes anything.

Also, if possible, you could try running this code from a kernel module with interrupts disabled (I think on windows, cli/sti or something equivalent can be used). Although, context switches should be longer, but just to take them out of the equation altogether.

Last edited by shishir; 01-29-2010 at 12:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How instruction cpu works at limits.conf? Andys_sumy Linux - Newbie 8 12-06-2009 06:57 PM
What AMD CPU is comparable to P4 intel cpu? ngjunkie0011 Linux - Hardware 8 08-29-2007 12:47 AM
intel instruction set sci3ntist Programming 1 08-23-2007 05:37 AM
How is Intel SSSE3,x86-64 and all previous instruction set is used AbhishekSlam Programming 5 07-21-2007 07:53 PM
Which linux instruction is equivalent to windows' instruction "tracert"? backpacker Linux - Software 1 04-04-2006 10:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration