Detecting CPU Speed: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
m Bot: Replace deprecated source tag with syntaxhighlight
Line 74: Line 74:
It is possible to create code which is exceptionally pipeline hostile such as:
It is possible to create code which is exceptionally pipeline hostile such as:


<source lang="asm">
<syntaxhighlight lang="asm">
xor eax,edx
xor eax,edx
xor edx,eax
xor edx,eax
Line 80: Line 80:
xor edx,eax
xor edx,eax
...
...
</syntaxhighlight>
</source>


A simple xor instruction takes one cycle, which guarantees that the processor cannot pipeline this code as the current instructions operands depend on the results from the last calculation. One can check that, for a small count (tested from 16 to 64), RDTSC will show the instruction count is almost exactly (sometimes off by one) the cycles count. Unfortunately, when making the chain longer, code cache misses will occur, ruining the whole process.
A simple xor instruction takes one cycle, which guarantees that the processor cannot pipeline this code as the current instructions operands depend on the results from the last calculation. One can check that, for a small count (tested from 16 to 64), RDTSC will show the instruction count is almost exactly (sometimes off by one) the cycles count. Unfortunately, when making the chain longer, code cache misses will occur, ruining the whole process.
Line 157: Line 157:
I'd be tempted to say 'yes', though I haven't gave it a test nor heard of it elsewhere so far. Here is the trick:
I'd be tempted to say 'yes', though I haven't gave it a test nor heard of it elsewhere so far. Here is the trick:


<source lang="C">
<syntaxhighlight lang="C">
disable() // disable interrupts (if still not done)
disable() // disable interrupts (if still not done)
outb(0x43,0x34); // set PIT channel 0 to single-shot mode
outb(0x43,0x34); // set PIT channel 0 to single-shot mode
Line 168: Line 168:
byte lo=inb(0x40);
byte lo=inb(0x40);
byte hi=inb(0x40);
byte hi=inb(0x40);
</syntaxhighlight>
</source>


Now, we know that
Now, we know that