Repository navigation
Description
Spin-Wait can have a significant cost on power and performance if not done properly. The PAUSE instruction on Skylake changed from previous generations such as Haswell and Broadwell, from reference "Intel® 64 and IA-32 Architectures Optimization Reference Manual" section 8.4.7, "The latency of PAUSE instruction in prior generation microarchitecture is about 10 cycles, whereas on Skylake microarchitecture it has been extended to as many as 140 cycles." "As the PAUSE latency has been increased significantly, workloads that are sensitive to PAUSE latency will suffer some performance loss." We are seeing issues with spinning on Skylake Server with TechEmpower Plaintext with excessive amount of time being spent in spin code. It’s beneficial to tune the spin count or spin cycles and we have a proposal below for fix:
For example, the original spin code form j_join::join is shown below.
int spin_count = 4096 * g_num_processors;
for (int j = 0; j < spin_count; j++)
{
if (color != join_struct.lock_color)
{
break;
}
YieldProcessor(); // indicate to the processor that we are spinning
}
Assume YieldProcessor() took 10 cycles when the above code was first written and tuned. We could use spin cycles instead of spin count like this:
int spin_count = 4096 * g_num_processors;
// Assume YieldProcessor() took 10 cycles
// when the above code was first written and tuned
ptrdiff_t endTime = get_cycle_count() + spin_count * 10;
while (get_cycle_count() < endTime)
{
if (color != join_struct.lock_color)
{
break;
}
YieldProcessor(); // indicate to the processor that we are spinning
}
Activity
helloguo commentedon Aug 15, 2017
/cc @jkotas
jkotas commentedon Aug 15, 2017
cc @vancem
vancem commentedon Aug 15, 2017
cc @kouvel
benaadams commentedon Aug 16, 2017
Might have a major effect on Thread.SpinWait(iterations)
For example in ManualResetEventSlim looping on
ihttps://github.com/dotnet/coreclr/blob/5944aa55a541b144349a5c0ba0aecccfde9b5646/src/mscorlib/src/System/Threading/ManualResetEventSlim.cs#L572-L587
which calls
https://github.com/dotnet/coreclr/blob/5944aa55a541b144349a5c0ba0aecccfde9b5646/src/vm/comsynchronizable.cpp#L1628-L1657
redknightlois commentedon Aug 16, 2017
I have been tracking an unusual MAJOR regression (as in capital letters major) from 60K req/sec (running the loading and the target process at the same machine) to 3K req/sec that may be related to this. We have only seen this after upgrading the solution to CoreCLR 2.0. Under profiler the only suspect for the odd behavior is the ManualResetEventSlim. We see an inordinate amount of CPU time being assigned when that should have been very low.
It is fair to say that we know our current bottleneck pre 2.0 was thread state handoff, so we expected to be the same on 2.0. But, this behavior took me off guard. If you think it could be related, just let me know and we can connect with Skype to help you build an environment for investigation.
vancem commentedon Aug 16, 2017
I have been in conversation with @kouvel on spinning, and I would like us to move to make a small modification to spin loops such that they remember the spin count that worked in the past (or more importantly the fact that it did NOT work in the past), and use that to determine how much spinning to do this time (or to bail immediately). This change was motivated to simply reduce spin overhead, but it should also help with this issue.
This does not completely fix the problem, because although you will bail quickly when you bail, the spinning you do do is 14X longer. It seems to me that what we should do is make it so that SpinWait(count) normalizes it argument (that is a count always represents a certain number of machine instruction units (cycles), so on machines where pause is large). This approach may have problems, but it seems like something that should be on the table...
benaadams commentedon Aug 16, 2017
/cc @stephentoub FYI
redknightlois commentedon Aug 16, 2017
@vancem just point us to the private build for it and we can test it.
helloguo commentedon Aug 16, 2017
@redknightlois what's your hardware?
redknightlois commentedon Aug 16, 2017
Skylake & Kaby Lake, probably by tomorrow we will have the same test on pre-Skylake.
helloguo commentedon Aug 18, 2017
@redknightlois Do you see the same issue on pre-Skylake platforms? If yes, it may suggest something else. If you only see the issue on Skylake, it may suggest the spin loop with long
PAUSEinst needs to be tuned accordingly. And some test cases would be helpful to narrow down the problem.redknightlois commentedon Aug 18, 2017
@helloguo haven't go to the office yet. Will be there in a few hours.
redknightlois commentedon Aug 19, 2017
@helloguo @vancem I can confirm with absolute certainty (I believe this is the very first time in my life I can say that) that what we are looking at on our side is this issue at work. We run the same thing on Skylake i7-6700K, Kaby Lake i7-7700 and Ivy Bridge i5-3470.
Results:
Moreover, we can also confirm with high confidence that CoreCLR 1.1 also suffers from this on a lower degree. We were able to workaround the Wait CPU consumption manually yielding the thread, waiting until we hit the 5th round to call the primitive Wait. That diminish the CPU cost of wait from 30% to 8% in the exact same scenario and allow us to jump from 30K to 43K request/sec on i7-7700.
Hope we can fix this soon, because we cannot ship a Release Candidate with this issue there.
helloguo commentedon Aug 21, 2017
@redknightlois Thanks for your detailed numbers. Can you share how to set up and run your workload? So that I could run it against my prototype.
kouvel commentedon Aug 21, 2017
@redknightlois, thanks for the information. One other difference between the above procs could be that the i7s have hyperthreading and the i5 doesn't, and the pause count is multiplied by number of procs, so the i7s could be issuing twice as many pauses. Do you have hyperthreading enabled on those?
Probably the multiplying should be removed anyway.
Also @redknightlois, what spin counts are you using for the problematic ManualResetEventSlims? I don't think it matters but just to test similarly on my side.
30 remaining items
redknightlois commentedon Sep 27, 2017
Any ETA on the service release now that the 2.0.1 is scheduled to be (if not already) out in the open?
kouvel commentedon Sep 28, 2017
I'm not sure about the cadence of .NET Core servicing releases, @karelz, is there a rough timeframe for 2.0.2 now?
karelz commentedon Sep 28, 2017
@leecow do we have rough ETA for 2.0.1 release?
Is it something we could put on roadmap? (with caveat that ETAs can change)
kouvel commentedon Sep 28, 2017
I think @redknightlois is looking for 2.0.2, as the fix was available when 2.0.1 was almost finalized
[-]Spin count tuning[/-][+]Spin wait tuning[/+]redknightlois commentedon Sep 29, 2017
@karelz @kouvel Yes, looking for the 2.0.2 release :)
redknightlois commentedon May 16, 2018
Shouldnt this be closed?
kouvel commentedon May 16, 2018
Yes some spinning cases remain but they are covered by other issues
danmoseley commentedon Nov 17, 2020
@StephenBonikowsky this issue is closed but it went on the partner board. If there's a partner asking for work here, can you please help them create a new issue.
StephenBonikowsky commentedon Nov 17, 2020
I followed-up with the customer, what they were reporting was this issue but in their .NET Framework service.
I've removed the project link.