RDTSC on Corei5

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sentientnz
Posts: 13
Joined: Thu Nov 24, 2011 8:54 am

RDTSC on Corei5

Post by sentientnz »

2 quick questions regarding RDTSC :)

a) Does RDTSC return the number of CPU clock-ticks since boot?

b) If so -
On my Core i5 (520M), I have tried to use RDTSC to calculate the CPU speed.
I read the TSC, wait for 100ms, read again and take the difference.
For some reason, it is telling me the difference is ~2.1 billion ticks.

I know the timing loop is correct as I also use it to calculate the FSB (which is coming back as 1066Mhz).

I was about to multiply the TSC diff by 10 (to get seconds) when I noticed it was already huge :). I have checked the start and end TSC readings, and manually calculated the difference = 2 billion.

Am I getting 10 ticks per clock cycle or is something else weird going on?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: RDTSC on Corei5

Post by bluemoon »

sentientnz wrote:Does RDTSC return the number of CPU clock-ticks since boot?
No, the number may not related to ticks upon boot, btw it may be paused for inactive processors, or accordingly the the manual:
The processor monotonically increments the time-stamp counter MSR every clock cycle and resets it to 0 whenever the processor is reset.
sentientnz wrote:Am I getting 10 ticks per clock cycle or is something else weird going on?
There is always rounding errors and interrupts/SMM/hidden tasks. The only way to increase accuracy is enlarge sample.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: RDTSC on Corei5

Post by Brendan »

Hi,
sentientnz wrote:a) Does RDTSC return the number of CPU clock-ticks since boot?
For Core i5, TSC should measure "nominal clock ticks" since the CPU was started last (e.g. including reset, etc); as long as nobody messed with the MSR to change the TSC.

Note that for Core i5, "nominal clock ticks" has nothing to do with the speed that the CPU is actually running at, and may not have anything to do with any speed the CPU could ever run at. I'm not too sure exactly what the TSC in Core i5 measures - I'd guess the maximum turbo-boost speed (e.g. 2.933 GHz for a Core i5 520M), but that's only a guess, and it could just multiply the bus speed by an arbitrary "large enough" number (like 18).
sentientnz wrote:On my Core i5 (520M), I have tried to use RDTSC to calculate the CPU speed.
You can't estimate CPU speed using RDTSC on any recent CPU (you can only determine what speed the TSC runs at).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply