From cbc15fa870de776d3fbf3b62fc9a5e01792e6a26 Mon Sep 17 00:00:00 2001 From: Tao Qingyun Date: Mon, 21 Jun 2021 09:26:43 +0800 Subject: [PATCH] runtime: make ncgocall a global counter ncgocall was stored per M, runtime.NumCgoCall lost the counter when a M die. Fixes #46789 --- src/runtime/debug.go | 2 +- src/runtime/proc.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/runtime/debug.go b/src/runtime/debug.go index fd6cbd484e..82deefa200 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(ncgocall) + 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 c4dc8717b0..4c92588a66 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -1507,7 +1507,6 @@ func mexit(osStack bool) { } throw("m not found in allm") found: - ncgocall += m.ncgocall if !osStack { // Delay reaping m until it's done with the stack. // @@ -1523,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.