Skip to content

Spin wait tuning #8744

Closed
Closed

Description

@helloguo

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

  1. helloguo commented on Aug 15, 2017

    @helloguo
    Author
  2. jkotas commented on Aug 15, 2017

    @jkotas
    Member
  3. vancem commented on Aug 15, 2017

    @vancem
  4. redknightlois commented on Aug 16, 2017

    @redknightlois

    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.

    image

    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.

  5. vancem commented on Aug 16, 2017

    @vancem

    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...

  6. benaadams commented on Aug 16, 2017

    @benaadams
    Member

    /cc @stephentoub FYI

  7. redknightlois commented on Aug 16, 2017

    @redknightlois

    @vancem just point us to the private build for it and we can test it.

  8. helloguo commented on Aug 16, 2017

    @helloguo
    Author

    @redknightlois what's your hardware?

  9. redknightlois commented on Aug 16, 2017

    @redknightlois

    Skylake & Kaby Lake, probably by tomorrow we will have the same test on pre-Skylake.

  10. helloguo commented on Aug 18, 2017

    @helloguo
    Author

    @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 PAUSE inst needs to be tuned accordingly. And some test cases would be helpful to narrow down the problem.

  11. redknightlois commented on Aug 18, 2017

    @redknightlois

    @helloguo haven't go to the office yet. Will be there in a few hours.

  12. redknightlois commented on Aug 19, 2017

    @redknightlois

    @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:

    • i7-6700K = 3.5K request/sec
    • i7-7700 = 3.5K request/sec
    • i5-3470 = 12K request/sec

    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.

  13. helloguo commented on Aug 21, 2017

    @helloguo
    Author

    @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.

  14. kouvel commented on Aug 21, 2017

    @kouvel
    Contributor

    @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.

  15. 30 remaining items

  16. redknightlois commented on Sep 27, 2017

    @redknightlois

    Any ETA on the service release now that the 2.0.1 is scheduled to be (if not already) out in the open?

  17. kouvel commented on Sep 28, 2017

    @kouvel
    Contributor

    I'm not sure about the cadence of .NET Core servicing releases, @karelz, is there a rough timeframe for 2.0.2 now?

  18. karelz commented on Sep 28, 2017

    @karelz
    Member

    @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)

  19. kouvel commented on Sep 28, 2017

    @kouvel
    Contributor

    I think @redknightlois is looking for 2.0.2, as the fix was available when 2.0.1 was almost finalized

  20. changed the title [-]Spin count tuning[/-] [+]Spin wait tuning[/+] on Sep 28, 2017
  21. redknightlois commented on Sep 29, 2017

    @redknightlois

    @karelz @kouvel Yes, looking for the 2.0.2 release :)

  22. removed their assignment
    on Oct 31, 2017
  23. redknightlois commented on May 16, 2018

    @redknightlois

    Shouldnt this be closed?

  24. kouvel commented on May 16, 2018

    @kouvel
    Contributor

    Yes some spinning cases remain but they are covered by other issues

  25. transferred this issue fromdotnet/coreclron Jan 31, 2020
  26. added this to the Future milestone on Jan 31, 2020
  27. danmoseley commented on Nov 17, 2020

    @danmoseley
    Contributor

    @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.

  28. StephenBonikowsky commented on Nov 17, 2020

    @StephenBonikowsky
    Contributor

    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.

  29. ghost locked as resolved and limited conversation to collaborators on Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions