From 9087b5d95a3c1b32b4bfbf4a9f467416c0af8ab8 Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Sat, 23 Sep 2023 15:25:17 -0700 Subject: [PATCH] disable tracking lock stack traces by default (#481) --- utils/lock_tracker.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/utils/lock_tracker.go b/utils/lock_tracker.go index f4ea6d74..6d8d3181 100644 --- a/utils/lock_tracker.go +++ b/utils/lock_tracker.go @@ -26,6 +26,7 @@ import ( var lockTrackerEnabled = false var enableLockTrackerOnce sync.Once var lowResTime uint32 = uint32(time.Now().Unix()) +var enableLockTrackerStackTrace uint32 // EnableLockTracker enable lock tracking background worker. This should be // called during init @@ -36,6 +37,14 @@ func EnableLockTracker() { }) } +func ToggleLockTrackerStackTraces(enable bool) { + var v uint32 + if enable { + v = 1 + } + atomic.StoreUint32(&enableLockTrackerStackTrace, v) +} + func updateLowResTime() { ticker := time.NewTicker(time.Second) for t := range ticker.C { @@ -151,13 +160,15 @@ func (t *lockTracker) trackLock() { if atomic.AddInt32(&t.held, 1) == 1 { atomic.StoreUint32(&t.ts, atomic.LoadUint32(&lowResTime)) - for { - n := runtime.Stack(t.stack[:cap(t.stack)], false) - if n < cap(t.stack) { - t.stack = t.stack[:n] - break + if atomic.LoadUint32(&enableLockTrackerStackTrace) == 1 { + for { + n := runtime.Stack(t.stack[:cap(t.stack)], false) + if n < cap(t.stack) { + t.stack = t.stack[:n] + break + } + t.stack = make([]byte, len(t.stack)*2) } - t.stack = make([]byte, len(t.stack)*2) } } }