63 points · by artsandsci · 8y ago · 32 comments
TSC had some problems historically with SMP and for a time HPET was pretty much the only high resolution timer available, but nowadays modern CPUs from both Intel/AMD support invariant TSC or constant TSC, which if supported, will be preferred by operating systems.
This was a good article explaining their findings, but it missed the industry shift back toward favouring TSC.
http://www.agner.org/optimize/blog/read.php?i=838
> The varying clock frequency was a big problem for my performance tests because it was impossible to get precise and reproducible measurements of computation times. It helps to warm up the processor with a long sequence of dummy calculations, but the clock counts were still somewhat inaccurate. The Time Stamp Counter (TSC), which is used for measuring the execution time of small pieces of code, is counting at the nominal frequency. The Ryzen processor has another counter called Actual Performance Frequency Clock Counter (APERF) which is similar to the Core Clock Counter in Intel processors. Unfortunately, the APERF counter can only be read in kernel mode, unlike the TSC which is accessible to the test program running in user mode. I had to calculate the actual clock counts in the following way: The TSC and APERF counters are both read in a device driver immediately before and after a run of the test sequence. The ratio between the TSC count and the APERF count obtained in this way is then used as a correction factor which is applied to all TSC counts obtained during the running of the test sequence. This method is awkward, but the results appear to be quite precise, except in the cases where the frequency is varying considerably during the test sequence. My test program is available at www.agner.org/optimize/#testp
---------------
EDIT: Odd. AMD Documentation of 17h (aka Zen) suggests that the TSC is invariant: https://support.amd.com/TechDocs/54945_PPR_Family_17h_Models...
See bottom of page 82.
I can't say I've run tests as thoroughly as Agner Fog has done however, or even have access to Zen cores.
https://software.intel.com/sites/default/files/managed/39/c5... (24MB mobile warning):
"17.17.1 Invariant TSC"
"The invariant TSC will run at a constant rate in all ACPI P-, C-, and T-states. This is an architectural behavior moving forward. On processors with invariant TSC support, the OS may use the TSC for wall clock timer services (instead of ACPI or HPET timers). TSC reads are much more efficient and do not incur the overhead associated with a ring transition [system call, u<=>k context switch] or access to a platform resource. [Device I/O]"
If overclockers methods are breaking this promise, then operating systems have very little recourse. I suspect they are not, however. The point is that the TSC is not derived from the CPU clock.
https://www.tweaktown.com/guides/7481/tweaktowns-ultimate-in...
Its not a "common" practice, but its certainly an overclocking practice that enough overclockers do that its a concern.
I guess the real concern is where does software get its information about the ratio of the TSC's rate to the progression of actual wall time. If it's derived from a constant multiplier of the CPU base clock or something like that, then adjusting the base clock could be quite problematic for benchmarking even if that base clock speed remains constant throughout a benchmark run. On the other hand, if the TSC to wall time ratio is measured using an external clock (like HPET), then most overclocking situations are probably fine (since it's pretty rare to overclock peripheral busses on modern platforms).
And since RDTSC is quite deliberately an unprivileged instruction, it's not just the OS we have to worry about on the software side. But hopefully most application authors that directly use the TSC instead of portable APIs know what they're doing. (I wouldn't bet on it.)
Clear and simple.
I wish more media could be so straight forward and honest as this.
That doesn’t surprise me at all. The HPET is awful. Reading it takes thousands of cycles. Reading it concurrently on multiple cores is even worse. Even using it as a timer (to fire an interrupt at a given time) is highly dubious — IIRC it only supports “interrupt of time == X” and not “interrupt the first time that time >= X”, which means that setting a timer without races requires manually checking that the timeout hasn’t already expired after programming the timer, which is extra bad given how incredibly slow the HPET is.
(I maintain the Linux vDSO, and the HPET has so many problems that I disabled vDSO HPET support entirely a while back.)
For those who don't know: the vDSO is a "dynamic library" provided by the Linux kernel to every process. System calls in modern Linux are made by calling a function exported by the vDSO, which then does the real system call, using the fastest method available. There are a few special cases in the vDSO for time-related system calls: depending on the currently active clock source, it can do a few calculations and return, without having to actually do a system call. This makes getting the current time much faster.
I'm interested in first-generation Ryzen numbers. Especially considering AMD explicitly advised turning HPET off while anandtech turned them on.
Read this for details and try the timer benchmark in there: https://www.overclockers.at/articles/the-hpet-bug-what-it-is...
I am wondering where did they get the Intel uses 24mhz timer from tho...
hpet0: 4 comparators, 64-bit 14.318180 MHz counter
The subsequent question I haven't seen clarified is:
In any of these situations, are Intel's results actually accurate FPS or could it be showing 'accelerated' FPS?
I.e. is Intel taking a performance hit, or are their 'non-HPET' numbers cosmetically showing faster performance that isn't there?
At least one of the factors seems to be the Meltdown workarounds that are required on Intel but not AMD processors. My intuition is that this explanation is sufficient to account for the entire discrepancy between vendors, but I can't rule out other factors. (Querying the HPET incurs all the Meltdown workaround overhead of accessing peripherals like storage, but with none of the actual waiting on a peripheral device, so on a percentage basis this is probably the most severely affected task.)
> I.e. is Intel taking a performance hit, or are their 'non-HPET' numbers cosmetically showing faster performance that isn't there?
When the system is forced to always use the HPET instead of the TSC, the performance hit is real and not just an artifact of inaccurate timers. When you're not overclocking, the TSC is usually reliable and TSC-based time measurements should differ from HPET-based time measurements only by the overhead of accessing the HPET. That overhead got much larger this year for Intel processors.
Ian's initial benchmarks didn't include overclocked processors, just a test configuration intended to be suitable for future comparison against overclocked processors.
e.g. FreeBSD assigns a higher quality score to TSC:
Timecounter "TSC-low" frequency 1500043470 Hz quality 1000
Timecounter "i8254" frequency 1193182 Hz quality 0
Timecounter "HPET" frequency 14318180 Hz quality 950
Timecounter "ACPI-fast" frequency 3579545 Hz quality 900 [ 0.062020] clocksource: Switched to clocksource hpet
[...]
[ 2.383237] clocksource: Switched to clocksource tsc
Here, it started with the HPET, calibrated the TSC, and switched to it. There's no need to disable the HPET for it to chose something else as the best timing source.