From d01bc571f7e55c7376f34e86be4e5660887bd30c Mon Sep 17 00:00:00 2001 From: Tao Qingyun Date: Tue, 22 Jun 2021 00:24:05 +0000 Subject: [PATCH] runtime: make ncgocall a global counter ncgocall was stored per M, runtime.NumCgoCall lost the counter when a M die. Fixes #46789 Change-Id: I85831fbb2713f4c30d1800d07e1f47aa0031970e GitHub-Last-Rev: cbc15fa870de776d3fbf3b62fc9a5e01792e6a26 GitHub-Pull-Request: golang/go#46842 Reviewed-on: https://go-review.googlesource.com/c/go/+/329729 Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor Trust: Alexander Rakoczy --- src/runtime/cgocall.go | 2 ++ src/runtime/debug.go | 2 +- src/runtime/proc.go | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/runtime/cgocall.go b/src/runtime/cgocall.go index 0e287d0b8ed..8ffb48a888e 100644 --- a/src/runtime/cgocall.go +++ b/src/runtime/cgocall.go @@ -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 for dead m + // Call from Go to C. // // This must be nosplit because it's used for syscalls on some diff --git a/src/runtime/debug.go b/src/runtime/debug.go index f411b226766..82deefa200c 100644 --- a/src/runtime/debug.go +++ b/src/runtime/debug.go @@ -45,7 +45,7 @@ func NumCPU() int { // NumCgoCall returns the number of cgo calls made by the current process. func NumCgoCall() int64 { - var n int64 + var n = int64(atomic.Load64(&ncgocall)) for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink { n += int64(mp.ncgocall) } diff --git a/src/runtime/proc.go b/src/runtime/proc.go index 8f1a4439456..4c92588a66e 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -1522,6 +1522,8 @@ found: } unlock(&sched.lock) + atomic.Xadd64(&ncgocall, int64(m.ncgocall)) + // Release the P. handoffp(releasep()) // After this point we must not have write barriers.