runtime: make ncgocall a global counter

ncgocall was stored per M, runtime.NumCgoCall lost the counter when a M die.

Fixes #46789
This commit is contained in:
Tao Qingyun 2021-06-21 09:26:43 +08:00
parent 460900a7b5
commit 929a787d85
3 changed files with 5 additions and 8 deletions

View File

@ -110,6 +110,8 @@ func syscall_cgocaller(fn unsafe.Pointer, args ...uintptr) uintptr {
return as.retval
}
var ncgocall uint64 // number of cgo calls in total
// Call from Go to C.
//
// This must be nosplit because it's used for syscalls on some
@ -130,8 +132,8 @@ func cgocall(fn, arg unsafe.Pointer) int32 {
racereleasemerge(unsafe.Pointer(&racecgosync))
}
atomic.Xadd64(&ncgocall, 1)
mp := getg().m
mp.ncgocall++
mp.ncgo++
// Reset traceback.

View File

@ -6,7 +6,7 @@ package runtime
import (
"runtime/internal/atomic"
"unsafe"
_ "unsafe"
)
// GOMAXPROCS sets the maximum number of CPUs that can be executing
@ -45,11 +45,7 @@ func NumCPU() int {
// NumCgoCall returns the number of cgo calls made by the current process.
func NumCgoCall() int64 {
var n int64
for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
n += int64(mp.ncgocall)
}
return n
return int64(atomic.Load64(&ncgocall))
}
// NumGoroutine returns the number of goroutines that currently exist.

View File

@ -541,7 +541,6 @@ type m struct {
fastrand [2]uint32
needextram bool
traceback uint8
ncgocall uint64 // number of cgo calls in total
ncgo int32 // number of cgo calls currently in progress
cgoCallersUse uint32 // if non-zero, cgoCallers in use temporarily
cgoCallers *cgoCallers // cgo traceback if crashing in cgo call