]> Cypherpunks.ru repositories - gostls13.git/commit
runtime/cgo: store M for C-created thread in pthread key
authordoujiang24 <doujiang24@gmail.com>
Fri, 31 Mar 2023 19:45:39 +0000 (15:45 -0400)
committerCherry Mui <cherryyz@google.com>
Mon, 3 Apr 2023 18:34:11 +0000 (18:34 +0000)
commitccad8a9f9c7de7f51478954a5ee667cd7b61fbc1
treeee9eecf9e15c608b4c04450af499d6e061498e5d
parent33d8cdeedc4e69cd2d4eae10f024af826a73ce47
runtime/cgo: store M for C-created thread in pthread key

This reapplies CL 392854, with the followup fixes in CL 479255,
CL 479915, and CL 481057 incorporated.

CL 392854, by doujiang24 <doujiang24@gmail.com>, speed up C to Go
calls by binding the M to the C thread. See below for its
description.
CL 479255 is a followup fix for a small bug in ARM assembly code.
CL 479915 is another followup fix to address C to Go calls after
the C code uses some stack, but that CL is also buggy.
CL 481057, by Michael Knyszek, is a followup fix for a memory leak
bug of CL 479915.

[Original CL 392854 description]

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

[CL 479915 description]

Currently, when C calls into Go the first time, we grab an M
using needm, which sets m.g0's stack bounds using the SP. We don't
know how big the stack is, so we simply assume 32K. Previously,
when the Go function returns to C, we drop the M, and the next
time C calls into Go, we put a new stack bound on the g0 based on
the current SP. After CL 392854, we don't drop the M, and the next
time C calls into Go, we reuse the same g0, without recomputing
the stack bounds. If the C code uses quite a bit of stack space
before calling into Go, the SP may be well below the 32K stack
bound we assumed, so the runtime thinks the g0 stack overflows.

This CL makes needm get a more accurate stack bound from
pthread. (In some platforms this may still be a guess as we don't
know exactly where we are in the C stack), but it is probably
better than simply assuming 32K.

Fixes #51676.
Fixes #59294.

Change-Id: I9bf1400106d5c08ce621d2ed1df3a2d9e3f55494
Reviewed-on: https://go-review.googlesource.com/c/go/+/481061
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
Reviewed-by: DeJiang Zhu (doujiang) <doujiang24@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
43 files changed:
misc/cgo/test/cgo_test.go
misc/cgo/test/cthread_unix.c
misc/cgo/test/cthread_windows.c
misc/cgo/test/testx.go
misc/cgo/testcarchive/carchive_test.go
misc/cgo/testcarchive/testdata/libgo9/a.go [new file with mode: 0644]
misc/cgo/testcarchive/testdata/main9.c [new file with mode: 0644]
src/runtime/asm_386.s
src/runtime/asm_amd64.s
src/runtime/asm_arm.s
src/runtime/asm_arm64.s
src/runtime/asm_loong64.s
src/runtime/asm_mips64x.s
src/runtime/asm_mipsx.s
src/runtime/asm_ppc64x.s
src/runtime/asm_riscv64.s
src/runtime/asm_s390x.s
src/runtime/cgo.go
src/runtime/cgo/asm_386.s
src/runtime/cgo/asm_amd64.s
src/runtime/cgo/asm_arm.s
src/runtime/cgo/asm_arm64.s
src/runtime/cgo/asm_loong64.s
src/runtime/cgo/asm_mips64x.s
src/runtime/cgo/asm_mipsx.s
src/runtime/cgo/asm_ppc64x.s
src/runtime/cgo/asm_riscv64.s
src/runtime/cgo/asm_s390x.s
src/runtime/cgo/asm_wasm.s
src/runtime/cgo/callbacks.go
src/runtime/cgo/gcc_libinit.c
src/runtime/cgo/gcc_libinit_windows.c
src/runtime/cgo/gcc_stack_darwin.c [new file with mode: 0644]
src/runtime/cgo/gcc_stack_unix.c [new file with mode: 0644]
src/runtime/cgo/gcc_stack_windows.c [new file with mode: 0644]
src/runtime/cgo/libcgo.h
src/runtime/cgocall.go
src/runtime/crash_cgo_test.go
src/runtime/proc.go
src/runtime/runtime2.go
src/runtime/signal_unix.go
src/runtime/stubs.go
src/runtime/testdata/testprogcgo/bindm.go [new file with mode: 0644]