Skip to content

Port SpinWait(int iterations) to C# - #92611

Closed
EgorBo wants to merge 3 commits into
dotnet:mainfrom
EgorBo:manged-spinwait
Closed

EgorBo wants to merge 3 commits into
dotnet:mainfrom
EgorBo:manged-spinwait

Conversation

@EgorBo

@EgorBo EgorBo commented Sep 25, 2023 •

Copy link
Copy Markdown
Member

It seems to be simple unless I am missing something. Presumably, it should allow gc to interrupt at any point while previously we had to enter preemptive mode for too many iterations and could slightly stall gc in coop.

Also, I heard modern ARM cpus should use ISB instead of Yield

@ghost ghost added the area-VM-coreclr label Sep 25, 2023
@ghost ghost assigned EgorBo Sep 25, 2023
@EgorBo

This comment was marked as resolved.

@EgorBo

EgorBo commented Sep 25, 2023

Copy link
Copy Markdown
Member Author

Looks like Rust switched to ISB over YIELD: rust-lang/rust@c064b65

@EgorBo
EgorBo marked this pull request as ready for review September 26, 2023 00:17
@EgorBo

EgorBo commented Sep 26, 2023

Copy link
Copy Markdown
Member Author

@jkotas @kouvel should be ready for review unless I am missing something.

Should/can it be shared with NAOT (my understanding that NAOT doesn't calculate optimal number of yields for current cpu and has some hard-coded values instead)

@jkotas

jkotas commented Sep 26, 2023

Copy link
Copy Markdown
Member

What does it help with? If you are worried about the GC suspension starvation, it would be less code duplication to fix it by moving the check for coop threshold to managed code, like it is done in native AOT:

if (iterations > SpinWaitCoopThreshold)
{
LongSpinWait(iterations);
}
else
{
SpinWaitInternal(iterations);
}

@jkotas

jkotas commented Sep 26, 2023

Copy link
Copy Markdown
Member

Looks like Rust switched to ISB over YIELD: rust-lang/rust@c064b65

I do not think that the justification in this PR applies to our use. We do not care about long the yield takes - the calibration of the spin loops will take care of yield taking less time.

We do care about power saving and hyper-threading core scheduling effects that this PR has no data about.

@EgorBo

EgorBo commented Sep 26, 2023

Copy link
Copy Markdown
Member Author

Looks like Rust switched to ISB over YIELD: rust-lang/rust@c064b65

I do not think that the justification in this PR applies to our use. We do not care about long the yield takes - the calibration of the spin loops will take care of yield taking less time.

We do care about power saving and hyper-threading core scheduling effects that this PR has no data about.

I'll ask the ARM folks about it, I thought that Yield is a sort of nop while ISB is closer to x64's pause so should be better energy-wise.

What does it help with? If you are worried about the GC suspension starvation, it would be less code duplication to fix it by moving the check for coop threshold to managed code, like it is done in native AOT:

Ah, ok, will change to that

@jkotas

jkotas commented Sep 26, 2023

Copy link
Copy Markdown
Member

Yield is a sort of nop

The Arm manual suggests that the hw implementation may take advantage of the hint. So it can be nop on one Arm hw, but actual useful hint on a different Arm hw. It does not make it easier to measure.

ISB is closer to x64's pause so should be better energy-wise.

Wall clock time != energy consumption. It would be a good idea to check about this with Arm folks.

@EgorBo EgorBo closed this Sep 26, 2023
@jkotas

jkotas commented Sep 26, 2023

Copy link
Copy Markdown
Member

What does it help with? If you are worried about the GC suspension starvation, it would be less code duplication to fix it by moving the check for coop threshold to managed code, like it is done in native AOT:

Ah, ok, will change to that

Do you plan to work on this? If not, I am going to do it - it will help to reduce overhead for small number of spin counts.

@EgorBo

EgorBo commented Sep 26, 2023

Copy link
Copy Markdown
Member Author

What does it help with? If you are worried about the GC suspension starvation, it would be less code duplication to fix it by moving the check for coop threshold to managed code, like it is done in native AOT:

Ah, ok, will change to that

Do you plan to work on this? If not, I am going to do it - it will help to reduce overhead for small number of spin counts.

Ah, sure feel free, I am not sure I understood what you meant after looking at it

@ghost ghost locked as resolved and limited conversation to collaborators Oct 26, 2023
@kunalspathak

Copy link
Copy Markdown
Contributor

I'll ask the ARM folks about it, I thought that Yield is a sort of nop while ISB is closer to x64's pause so should be better energy-wise.

@a74nh @TamarChristinaArm - do you have more information on this?

@dotnet dotnet unlocked this conversation Dec 20, 2023
@a74nh

a74nh commented Dec 20, 2023

Copy link
Copy Markdown
Contributor

Yes, in current hardware, yield is mostly a nop.

Switching to an ISB will create a stall and therefore reduce power. Also, it should improve spin lock performance. Architecturally, it isn’t the best solution in the long term as it’s just relying on a side effect of the ISB forcing the cpu flush.

An alternative would be to create a data dependency between the loads (i.e. between the data returned from one load, and the address of the next) which would essentially create a pause which a full cpu flush. However, that’s not been tested in anger – maybe something for the new year.

Future hardware will have WFET and should be a better fit, but that’s not available yet.

@github-actions github-actions Bot locked and limited conversation to collaborators Jan 20, 2024
@dotnet dotnet unlocked this conversation Jan 26, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 26, 2024
@dotnet dotnet unlocked this conversation May 15, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants