jdmccalpin / low-overhead-timers

Very low-overhead timer/counter interfaces for C on Intel 64 processors.
BSD 3-Clause "New" or "Revised" License
116 stars 16 forks source link

Update base frequency detection for newer Intel processors #3

Open jdmccalpin opened 1 year ago

jdmccalpin commented 1 year ago

The function get_TSC_frequency() in low_overhead_timers.c is a hack (recommended by Intel) to obtain the base frequency of the processor from the CPUID Brand String. Newer processors support a CPUID-based mechanism to get the base frequency. I have not tested to see if the CPUID Brand String hack continues to work on newer processors (or on client processors, which I seldom have access to). I also don't know if the new CPUID-based base frequency mechanism is robust to features like "Configurable TDP".

If supported, CPUID Leaf 0x15 provides the TSC frequency, with no scary warnings about inaccuracy (such as are included in the CPUID Leaf 0x16 values for the Core Frequencies). Execute CPUID with EAX=0 and look at the returned value of EAX If it is greater than or equal to 0x15, then CPUID Leaf 0x15 is available. Looks like this corresponds to SKX or later processors.

I only need to get the TSC frequency programmatically -- the core frequencies should always be measured.