Detecting CPU Speed: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
(Major formatting changes, except for "Without Interrupts" section.)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(3 intermediate revisions by 2 users not shown)
Line 74:
It is possible to create code which is exceptionally pipeline hostile such as:
 
<sourcesyntaxhighlight lang="asm">
xor eax,edx
xor edx,eax
Line 80:
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.
Line 111:
* The code assumes that the command CPUID is supported.
 
<sourcesyntaxhighlight lang = "asm">
;__get_speed__:
;first do a cpuid command, with eax=1
Line 146:
; ax contains measured speed in MHz
mov ~[mhz], ax
</syntaxhighlight>
</source>
 
See the Intel manual (see links) for more information.
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:
 
<sourcesyntaxhighlight lang="C">
disable() // disable interrupts (if still not done)
outb(0x43,0x34); // set PIT channel 0 to single-shot mode
Line 168:
byte lo=inb(0x40);
byte hi=inb(0x40);
</syntaxhighlight>
</source>
 
Now, we know that
Line 229:
 
[[Category:X86 CPU]]
[[Category:Hardware Detection]]