From 63ceff95fa7cc93dd848b503dedcef53b918cdc3 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Wed, 31 Aug 2022 21:34:23 +0000 Subject: runtime/metrics: add /sync/mutex/wait/total:seconds metric This change adds a metric to the runtime/metrics package which tracks total mutex wait time for sync.Mutex and sync.RWMutex. The purpose of this metric is to be able to quickly get an idea of the total mutex wait time. The implementation of this metric piggybacks off of the existing G runnable tracking infrastructure, as well as the wait reason set on a G when it goes into _Gwaiting. Fixes #49881. Change-Id: I4691abf64ac3574bec69b4d7d4428b1573130517 Reviewed-on: https://go-review.googlesource.com/c/go/+/427618 Reviewed-by: Michael Pratt Auto-Submit: Michael Knyszek Run-TryBot: Michael Knyszek TryBot-Result: Gopher Robot --- src/runtime/runtime2.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/runtime/runtime2.go') diff --git a/src/runtime/runtime2.go b/src/runtime/runtime2.go index 1d36126a03..284f9d395d 100644 --- a/src/runtime/runtime2.go +++ b/src/runtime/runtime2.go @@ -468,7 +468,7 @@ type g struct { sysblocktraced bool // StartTrace has emitted EvGoInSyscall about this goroutine tracking bool // whether we're tracking this G for sched latency statistics trackingSeq uint8 // used to decide whether to track this G - runnableStamp int64 // timestamp of when the G last became runnable, only used when tracking + trackingStamp int64 // timestamp of when the G last started being tracked runnableTime int64 // the amount of time spent runnable, cleared when running, only used when tracking sysexitticks int64 // cputicks when syscall has returned (for tracing) traceseq uint64 // trace event sequencer @@ -843,6 +843,10 @@ type schedt struct { // // Reset on each GC cycle. idleTime atomic.Int64 + + // totalMutexWaitTime is the sum of time goroutines have spent in _Gwaiting + // with a waitreason of the form waitReasonSync{RW,}Mutex{R,}Lock. + totalMutexWaitTime atomic.Int64 } // Values for the flags field of a sigTabT. @@ -1109,6 +1113,12 @@ func (w waitReason) String() string { return waitReasonStrings[w] } +func (w waitReason) isMutexWait() bool { + return w == waitReasonSyncMutexLock || + w == waitReasonSyncRWMutexRLock || + w == waitReasonSyncRWMutexLock +} + var ( allm *m gomaxprocs int32 -- cgit v1.3