You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
yieldprocessornormalized appears to be incorrect in some scenarios #100242
The general premise is that while the logic attempts to be robust and appears to get the results correct on Windows, for at least a subset of machines tested, the same general logic does not return a consistent result for the same hardware on Linux. Additionally, there are some potential scenarios where the logic will be incorrect when accounting for big.LITTLE (or power/efficiency) micro-architectures and where results can become skewed due to external factors such as processor downclock (energy savings), processing boosting, or even context switches caused by the Operating System.
The simplest example is to test the results on a well documented system, such as an AMD Zen4 chip where pause is documented to take exactly 64 cycles. On Windows, the computed time is typically around 14ns (which is spot on for a 4.5GHz base clock processor). However, if you were to actually log the min/max times and the processor ID as seen over all calls, you'd see something similar to the following:
This variance is caused by processor frequency changes as well as by OS context switches, but also shows that the core being tested may not be consistent which can negatively impact other cores.
On Linux, however, the same code is returning drastically different measurements despite the knowledge that pause will take exactly 64 cycles:
Similar discrepancies are seen on Arm64 for devices like the Volterra (Windows DevKit) or a Raspberry PI.
However, for Arm64 in particular it is worth noting that we currently use yield. The architecture manuals actually specify that yield may be a nop on many systems and in practice this can be visibly seen in the measurements:
If you were to replace the yield with a true nop, you'd actually see identical measurements.
Many other runtimes have correspondingly switched to emitting isb instead due to the fact that yield is behaving as a nop on a very large amount of real hardware. This has a latency more around 9-10ns, which is much more similar to the x86/x64 pause instruction.
Additionally, Armv8 has an optional feature (which is now required in Armv8.7+) called Wait for Event and Wait for Event with Timeout. This new feature is intentionally designed to be used with spin locks and comes with explicit documented samples on how to use it correctly. As such, it may be desirable to adjust Arm64 to use wfe if available and otherwise to use isb if the underlying yield reports timings similar to nop.
This should result in better energy efficiency, fewer instructions executed, etc.
x64 similarly has newer optional functionality in the form of tpause (timed pause) which is preferred over pause on newer hardware and which allows explicitly opting for better perf or better energy efficiency. -- It also has umwait and umonitor which allow similar efficient waiting for an address to be read/written and which can be used for efficient semaphores or other optimizations.
It's just modifying YieldProcessorNormalization::PerformMeasurement() to print out the data to the console. But the same general premise can be observed from the FireEtwYieldProcessorMeasurement event or by setting a breakpoint.
Where the processor ID can be queried via GetCurrentProcessorNumberEx() (Win32) or sched_getcpu (Linux).
It's just modifying YieldProcessorNormalization::PerformMeasurement() to print out the data to the console. But the same general premise can be observed from the FireEtwYieldProcessorMeasurement event or by setting a breakpoint.
Where the processor ID can be queried via GetCurrentProcessorNumberEx() (Win32) or sched_getcpu (Linux).
Some variance is to be expected in normal situations, but are you saying that this variance occurs even with nothing else happening on the machine?
I'm consistently seeing Linux report numbers that are lower than Windows (and therefore also lower than the actual hardware documented time) and the actual variance to be quite substantial in some cases, really depending on the previous power state of the CPU.
The Intel Software Optimization manual then documents that a core license change (which can occur for heavy AVX2/AVX512 usage as an example) can take up to 500 microseconds and then will spend at least 2ms before it changes back to the higher clock speed. It also documents that many operating systems may use a time constant on the order of 10-100ms to detect processor workload demand and thus change the explicitly requested frequency.
These documented timings show that there can be a pretty substantial window under which pause latency can be mismeasured for a given core. It also doesn't factor in the implications around heterogenous architectures (where pause may have different latencies across different cores).
On Windows, we do get eventual consistency since the normalization is semi-regularly run for a thread as needed, but it's still not amazing and can get skewed if context switches happen and the core a thread is on gets changed.
Many other runtimes have correspondingly switched to emitting isb instead due to the fact that yield is behaving as a nop on a very large amount of real hardware. This has a latency more around 9-10ns, which is much more similar to the x86/x64 pause instruction.
Activity
tannergooding commentedon Mar 25, 2024
The simplest example is to test the results on a well documented system, such as an AMD Zen4 chip where
pauseis documented to take exactly 64 cycles. On Windows, the computed time is typically around 14ns (which is spot on for a 4.5GHz base clock processor). However, if you were to actually log the min/max times and the processor ID as seen over all calls, you'd see something similar to the following:This variance is caused by processor frequency changes as well as by OS context switches, but also shows that the core being tested may not be consistent which can negatively impact other cores.
On Linux, however, the same code is returning drastically different measurements despite the knowledge that
pausewill take exactly 64 cycles:dotnet-policy-service commentedon Mar 25, 2024
Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.
tannergooding commentedon Mar 25, 2024
Similar discrepancies are seen on Arm64 for devices like the
Volterra(Windows DevKit) or a Raspberry PI.However, for Arm64 in particular it is worth noting that we currently use
yield. The architecture manuals actually specify thatyieldmay be anopon many systems and in practice this can be visibly seen in the measurements:If you were to replace the
yieldwith a truenop, you'd actually see identical measurements.Many other runtimes have correspondingly switched to emitting
isbinstead due to the fact thatyieldis behaving as anopon a very large amount of real hardware. This has a latency more around 9-10ns, which is much more similar to the x86/x64pauseinstruction.Additionally, Armv8 has an optional feature (which is now required in Armv8.7+) called
Wait for EventandWait for Event with Timeout. This new feature is intentionally designed to be used withspin locksand comes with explicit documented samples on how to use it correctly. As such, it may be desirable to adjust Arm64 to usewfeif available and otherwise to useisbif the underlyingyieldreports timings similar tonop.This should result in better energy efficiency, fewer instructions executed, etc.
tannergooding commentedon Mar 25, 2024
x64 similarly has newer optional functionality in the form of
tpause(timed pause) which is preferred overpauseon newer hardware and which allows explicitly opting for better perf or better energy efficiency. -- It also hasumwaitandumonitorwhich allow similar efficient waiting for an address to be read/written and which can be used for efficient semaphores or other optimizations.kouvel commentedon Mar 25, 2024
@tannergooding could you please share the test case that yields the measurements in #100242 (comment)?
tannergooding commentedon Mar 25, 2024
It's just modifying
YieldProcessorNormalization::PerformMeasurement()to print out the data to the console. But the same general premise can be observed from theFireEtwYieldProcessorMeasurementevent or by setting a breakpoint.Where the processor ID can be queried via
GetCurrentProcessorNumberEx()(Win32) orsched_getcpu(Linux).kouvel commentedon Mar 25, 2024
Some variance is to be expected in normal situations, but are you saying that this variance occurs even with nothing else happening on the machine?
tannergooding commentedon Mar 25, 2024
Yes.
I'm consistently seeing Linux report numbers that are lower than Windows (and therefore also lower than the actual hardware documented time) and the actual variance to be quite substantial in some cases, really depending on the previous power state of the CPU.
The Intel Software Optimization manual then documents that a core license change (which can occur for heavy AVX2/AVX512 usage as an example) can take up to 500 microseconds and then will spend at least 2ms before it changes back to the higher clock speed. It also documents that many operating systems may use a time constant on the order of 10-100ms to detect processor workload demand and thus change the explicitly requested frequency.
These documented timings show that there can be a pretty substantial window under which
pauselatency can be mismeasured for a given core. It also doesn't factor in the implications around heterogenous architectures (wherepausemay have different latencies across different cores).On Windows, we do get eventual consistency since the normalization is semi-regularly run for a thread as needed, but it's still not amazing and can get skewed if context switches happen and the core a thread is on gets changed.
tannergooding commentedon Mar 25, 2024
I believe @EgorBo saw similar numbers as I did when he was looking at some Linux related perf traces for Arm64.
EgorBo commentedon Mar 25, 2024
It was discussed here: #92611 (comment)