Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV

Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


KeQueryPerformanceCounter and RDTSC

daniel02daniel02 Member Posts: 15

Hello,

I've been experimenting with high-precision timing for gaming and have been using both KeQueryPerformanceCounter and RDTSC for this purpose. Both seem to deliver fantastic precision at the nanosecond level.

However, I heard that KeQueryPerformanceCounter may have slightly more overhead compared to RDTSC. Can anyone confirm or deny this from their own experience?

Also, I'm interested in implementing IA32_TSC_DEADLINE in Windows. I checked the CPUID and bit 24 is returning 1, indicating that this feature is available, but I am not sure if Windows supports it. Has anyone been able to utilize IA32_TSC_DEADLINE in a Windows environment, or can provide any insights about this?

Lastly, I have heard about a timer in AMD's RDPRU that's supposed to be faster than RDTSC. Is this correct, and has anyone used this timer for game timing?

Looking forward to your insights. thanks

Comments

  • Taed_WynnellTaed_Wynnell Member Posts: 111

    I've used it only sparingly, but here is a detailed article which may provide the answers you seek: [https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps]

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 14,718
    edited June 19

    The __rdtsc() intrinsic compiles to exactly one CPU instruction. It is not possible to find anything faster.

    The problem with RDTSC is and has always been that the register is not synchronized across multiple cores. If your process happens to change CPUs, you might get a discrepancy. The other problem is that you don't necessarily know at what frequency it is running.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 9,131

    Correct. Or, more precisely, you don’t know if rdtsc synchronizes properly among all the cores in your system.

    For a long time, rdtsc has worked “correctly” across all cores on one chip/socket. The problem now is that in systems with multiple sockets, the tsc is not synchronized.

    Also… if you need to do this right, you need to be very sure you know what is being timed. For example, whether prefetches are included. Google around… there is a ton of really interesting info on this question out there.

    Peter Viscarola
    OSR
    @OSRDrivers

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 14,718

    Also, I'm interested in implementing IA32_TSC_DEADLINE in Windows.

    You need to forget about that. The lowest-level timer interrupts are under the complete control of the operating system scheduler. If you dink with it, it's likely that scheduling will come crashing down.

    That bit is designed for people writing their own operating system.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • daniel02daniel02 Member Posts: 15

    @Tim_Roberts said:

    Also, I'm interested in implementing IA32_TSC_DEADLINE in Windows.

    You need to forget about that. The lowest-level timer interrupts are under the complete control of the operating system scheduler. If you dink with it, it's likely that scheduling will come crashing down.

    That bit is designed for people writing their own operating system.

    i see windows cant handle that scheduling thanks !

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 13-17 May 2024 Live, Online
Developing Minifilters 1-5 Apr 2024 Live, Online
Internals & Software Drivers 11-15 Mar 2024 Live, Online
Writing WDF Drivers 26 Feb - 1 Mar 2024 Live, Online