golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
120.42k stars 17.29k forks source link

runtime/cgo: store M for C-created thread in pthread key #51679

Closed doujiang24 closed 1 year ago

doujiang24 commented 2 years ago

In a C thread, it's necessary to acquire an extra M by using needm while invoking a Go function from C. But, needm and dropm are heavy costs due to the signal-related syscalls. So, we change to not dropm while returning back to C, which means binding the extra M to the C thread until it exits, to avoid needm and dropm on each C to Go call. Instead, we only dropm while the C thread exits, so the extra M won't leak.

When invoking a Go function from C: Allocate a pthread variable using pthread_key_create, only once per shared object, and register a thread-exit-time destructor. And store the g0 of the current m into the thread-specified value of the pthread key, only once per C thread, so that the destructor will put the extra M back onto the extra M list while the C thread exits.

When returning back to C: Skip dropm in cgocallback, when the pthread variable has been created, so that the extra M will be reused the next time invoke a Go function from C.

This is purely a performance optimization. The old version, in which needm & dropm happen on each cgo call, is still correct too, and we have to keep the old version on systems with cgo but without pthreads, like Windows.

This optimization is significant, and the specific value depends on the OS system and CPU, but in general, it can be considered as 10x faster, for a simple Go function call from a C thread.

For the newly added BenchmarkCGoInCThread, some benchmark results:

  1. it's 28x faster, from 3395 ns/op to 121 ns/op, in darwin OS & Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  2. it's 6.5x faster, from 1495 ns/op to 230 ns/op, in Linux OS & Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz

Fixes #51676

gopherbot commented 2 years ago

This PR (HEAD: d680929323fe3293b48061d94bc2d5deac6e409c) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

This PR (HEAD: c6b3aec9ba52d49485edecceef2e9ffc5417f3b2) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 3:

(5 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: 8b314d4e1c30338bc748978145e59661faa8b71b) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 4:

(6 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 4:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 5:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 5:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: e683556a0d706799c694a692fa236c5fb8287bc0) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 6:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 6:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 6:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: 198e59fbcfccdc66bf4621bd47b873358ba79eae) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 7:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: d2688085d5ef351b2b702ad666f06951743c74cb) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 8:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 8:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 8: Run-TryBot+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 8:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 8:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 8:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 8: TryBot-Result-1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: 410da47dd7ad388c4497dc4f31d08408a9901bc6) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 9:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 9: Run-TryBot+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 9:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 9:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 9: TryBot-Result-1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: 163e1eb871efafa7037f86c9c02f2cf87400de6f) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 10:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from thepudds:

Patch Set 10:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 10: Run-TryBot+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 10:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 10:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 10: TryBot-Result-1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: f3e4fc48bee77d59ef3ea921073a332758710daa) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 11:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 11: Run-TryBot+1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 11:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 11:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 11: TryBot-Result-1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

This PR (HEAD: 081a39482058140fe19621e3817e1307e0e9149a) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 2 years ago

Message from DeJiang Zhu:

Patch Set 12:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Ian Lance Taylor:

Patch Set 13: Run-TryBot+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 13:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 2 years ago

Message from Gopher Robot:

Patch Set 13: TryBot-Result+1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 1 year ago

Message from DeJiang Zhu:

Patch Set 13:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 1 year ago

This PR (HEAD: addc9fffe62b4048350f43e28c45449c4a04cb2e) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/392854 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot commented 1 year ago

Message from xie cui:

Patch Set 14: Run-TryBot+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!

gopherbot commented 1 year ago

Message from Gopher Robot:

Patch Set 14:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/392854. After addressing review feedback, remember to publish your drafts!