flat assembler
Message board for the users of flat assembler.

Index > Windows > Aproximating the CPU clock rate

Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20357
Location: In your JS exploiting you and your system
revolution 15 Feb 2015, 14:03
Here is a small utility for approximating the current CPU clock rate.

It is only accurate to about 2 significant digits so don't expect a perfect value. But for a quick check of how your CPU is clocking it is probably good enough.
Code:
include 'win32ax.inc'

.code

proc begin uses edi esi
        locals
                Hz      rd 2
                MHz     rd 1
                speed   rb 1024
        endl
        invoke  GetCurrentProcess
        invoke  SetProcessAffinityMask,eax,1
        rdtsc
        mov     esi,eax
        mov     edi,edx
        invoke  Sleep,500
        rdtsc
        sub     eax,esi
        sbb     edx,edi
        add     eax,eax
        adc     edx,edx
        mov     [Hz+0],eax
        mov     [Hz+4],edx
        mov     ecx,1000000
        div     ecx
        mov     [MHz],eax
        invoke  wsprintf,addr speed,<'Approximate Speed: %I64u Hz (%u MHz)'>,[Hz+0],[Hz+4],[MHz]
        invoke  MessageBox,0,addr speed,'Your current speed',MB_OK
        invoke  ExitProcess,0
endp

.end begin    
Post 15 Feb 2015, 14:03
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 15 Feb 2015, 14:34
revolution
On modern CPUs the TSC is unrelated to the current CPU clock rate, and in most cases is incremented according to the CPU base frequency.

_________________
Faith is a superposition of knowledge and fallacy
Post 15 Feb 2015, 14:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20357
Location: In your JS exploiting you and your system
revolution 15 Feb 2015, 16:15
Suggestion on how to fix it for these "modern" CPUs?
Post 15 Feb 2015, 16:15
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 15 Feb 2015, 17:03
revolution
Sure. You should read the performance monitor counter values such as the "unhalted core cycles" counter. Sure you need a driver at least for configuring the counters properly.

_________________
Faith is a superposition of knowledge and fallacy
Post 15 Feb 2015, 17:03
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 15 Feb 2015, 17:19
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 17:56; edited 1 time in total
Post 15 Feb 2015, 17:19
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 15 Feb 2015, 17:31
HaHaAnonymous
Quote:
Please define modern...

Pretty much all the Intel CPUs released during the last 10 years (since late P4).
Quote:
The code worked fine for my CPU

It is fine, if you accept any arbitrary value as fine.

_________________
Faith is a superposition of knowledge and fallacy
Post 15 Feb 2015, 17:31
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 15 Feb 2015, 21:11
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 17:55; edited 1 time in total
Post 15 Feb 2015, 21:11
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 15 Feb 2015, 22:53
HaHaAnonymous
Quote:
My CPU box says it runs at 2.6GHz, 2594MHz is precise enough.

As I said before, the TSC "in most cases is incremented according to the CPU base frequency". In your case 2.6GHz is the base frequency. But if you have Turbo Boost enabled, or if you try to change the actual CPU frequency by altering the CPU clock multiplier in the BIOS, you'll still get the 2.6GHz, even though this value wouldn't be correct. Try to compare the value you get from revolution's snippet to the value displayed by the CPU-Z.

_________________
Faith is a superposition of knowledge and fallacy
Post 15 Feb 2015, 22:53
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 16 Feb 2015, 00:53
[ Post removed by author. ]
Post 16 Feb 2015, 00:53
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.