How can I detect CPU speed

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
Thunder

How can I detect CPU speed

Post by Thunder »

How can I detect CPU speed?
Can someone give me source in nasm?
Thanks
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How can I detect CPU speed

Post by Pype.Clicker »

basic trick is to use some "counting loop" using some code you can control the Cpu cycles needed together with a timer interrupt.

For instance, you have IRQ0@1KHz, and some code looking like
here:
inc [counter]
cmp [done],1
jne here
cpu_speed = [counter] * $x

using $x cpu cycles for each loop.

interrupt:
<pushall>
<read counter and compares it with previously stored>
<if counter=~old counter, set done=true>
<popall>

The key idea is that you can hardly start counting on an interrupt boundary, so you just go on counting until the amount of read cycles "stabilizes" itself (for instance, deviation < 1% of the measures, etc.) This will take automagically in account stuffs like branch prediction establishments, cache fills, etc.
Peter_Vigren

Re:How can I detect CPU speed

Post by Peter_Vigren »

Pype.Clicker wrote: basic trick is to use some "counting loop" using some code you can control the Cpu cycles needed together with a timer interrupt.

For instance, you have IRQ0@1KHz, and some code looking like
here:
inc [counter]
cmp [done],1
jne here
cpu_speed = [counter] * $x

using $x cpu cycles for each loop.

interrupt:
<pushall>
<read counter and compares it with previously stored>
<if counter=~old counter, set done=true>
<popall>

The key idea is that you can hardly start counting on an interrupt boundary, so you just go on counting until the amount of read cycles "stabilizes" itself (for instance, deviation < 1% of the measures, etc.) This will take automagically in account stuffs like branch prediction establishments, cache fills, etc.
But how do you turn the number of counts into MHz?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How can I detect CPU speed

Post by Pype.Clicker »

well to translate loops-count-per-millisecond, in cycles-per-seconds (Hz), all you should need to know is the amount of cycles the loop takes on the sampled CPU (that you should compute by hand using timings references, see this post for more infos)

If your cpu supports it, you can also try to use RDTSC instruction and read directly the cycles counter built in the CPU...
K.J.

Re:How can I detect CPU speed

Post by K.J. »

Peter_Vigren

Re:How can I detect CPU speed

Post by Peter_Vigren »

K.J. wrote: Get this:
http://alexfru.narod.ru/os/mhz.zip

K.J.
I got that one but the source would be nice, since I'm not up to going through the machine code...
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

hh

Post by Cmaranec »

This file is deleted. Have you got this file and can you send it to me please?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: hh

Post by Brynet-Inc »

Cmaranec wrote:This file is deleted. Have you got this file and can you send it to me please?
You do realize that the last post was in 2002? :roll:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

n

Post by Cmaranec »

And you know how i detect CPU Speed??? I am going to do it to my OS, but i don't know how i make it.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
Cmaranec wrote:And you know how i detect CPU Speed??? I am going to do it to my OS, but i don't know how i make it.
There's 2 common ways of detecting the current CPU core frequency - either using RDTSC to see how many cycles occured in a fixed amount of time (which won't work if the CPU doesn't support RDTSC), or measuring how long it takes for the CPU to execute a known number of cycles (which is a pain in the neck because different CPUs will execute the same intructions in a different number of cycles).

This has nothing to do with CPU speed though. For measuring current CPU speed, select some instructions and see how fast the CPU can execute them (or alternatively, how many times they can be executed in a fixed amount of time). This is how Linux's "BogoMips" works, but they use a very tight loop which is only really good for measuring how fast the CPU can do a very tight loop (and doesn't tell you anything about how fast the CPU executes normal instructions). For measuring current processor performance, you'd need to test a large variety of instructions.

Of course I've used the word "current" in all of the above for a reason. For most laptops the CPU's performance may be reduced to save battery power (unless it's plugged into mains power), and most CPUs have built in thermal throttling (so if the CPU gets too warm it slows down for a while to cool down). This means that the current CPU frequency or performance that you measure may not be the maximum frequency or performance, and may not be the average frequency or performance. It's also possible for the CPU frequency or performance to change while you're trying to measure it.

For multi-CPU, there's things like hyper-threading, where the work done on one logical CPU effects the performance of other logical CPUs in the same core. For performance measurement there's also variations caused by shared caches and bus traffic.

Lastly, things like interrupts (including SMM, which can't be disabled) and CPU caching can mess up your results, so it's best to do the testing mutliple times and take the fastest result or the average result (after ignoring the first result). For measuring CPU frequency it's also probably a good idea to round to the nearest sane value to remove some of the inaccuracies (e.g. a "123 MHz" CPU is probably a 133 MHz CPU).


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