Skip to content

Commit

Permalink
runtime: make ncgocall a global counter
Browse files Browse the repository at this point in the history
ncgocall was stored per M, runtime.NumCgoCall lost the counter when a M die.

Fixes #46789

Change-Id: I85831fbb2713f4c30d1800d07e1f47aa0031970e
GitHub-Last-Rev: cbc15fa
GitHub-Pull-Request: #46842
Reviewed-on: https://go-review.googlesource.com/c/go/+/329729
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Alexander Rakoczy <alex@golang.org>
  • Loading branch information
qingyunha authored and toothrot committed Jun 25, 2021
1 parent 37f9a8f commit d01bc57
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/runtime/cgocall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d01bc57

Please sign in to comment.