Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling of violation logging #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ type Mutex struct {
lock sync.Mutex

// internal trace fields
threshold atomic.Uint64 // 0 when disabled, else threshold in nanoseconds
beginAwaitLock atomic.Uint64 // start time in unix nanoseconds from start waiting for lock
beginAwaitUnlock atomic.Uint64 // start time in unix nanoseconds from start waiting for unlock
lockObtained atomic.Uint64 // once we've entered the lock in unix nanoseconds
id []byte // if set this will be printed as string
threshold atomic.Uint64 // 0 when disabled, else threshold in nanoseconds
beginAwaitLock atomic.Uint64 // start time in unix nanoseconds from start waiting for lock
beginAwaitUnlock atomic.Uint64 // start time in unix nanoseconds from start waiting for unlock
lockObtained atomic.Uint64 // once we've entered the lock in unix nanoseconds
id []byte // if set this will be printed as string
DisableViolationLog bool
}

func (m *Mutex) Lock() {
Expand Down Expand Up @@ -66,7 +67,7 @@ func (m *Mutex) traceEndAwaitLock(threshold Threshold) {
// check for no overflow
took = ts - start
}
if took >= uint64(threshold) {
if took >= uint64(threshold) && !m.DisableViolationLog {
logViolation(Id(m.id), threshold, Actual(took), Now(ts), ViolationLock)
}
}
Expand All @@ -86,7 +87,7 @@ func (m *Mutex) traceEndAwaitUnlock(threshold Threshold) {
took = ts - lockObtained
}

if took >= uint64(threshold) && lockObtained > 0 {
if took >= uint64(threshold) && lockObtained > 0 && !m.DisableViolationLog {
// lockObtained = 0 when the tracer is enabled half way
logViolation(Id(m.id), threshold, Actual(took), Now(ts), ViolationCritical)
}
Expand Down
15 changes: 8 additions & 7 deletions lock_rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ type RWMutex struct {
lock sync.RWMutex

// internal trace fields
threshold atomic.Uint64 // 0 when disabled, else threshold in nanoseconds
beginAwaitLock atomic.Uint64 // start time in unix nanoseconds from start waiting for lock
beginAwaitUnlock atomic.Uint64 // start time in unix nanoseconds from start waiting for unlock
lockObtained atomic.Uint64 // once we've entered the lock in unix nanoseconds
id []byte // if set this will be printed as string
threshold atomic.Uint64 // 0 when disabled, else threshold in nanoseconds
beginAwaitLock atomic.Uint64 // start time in unix nanoseconds from start waiting for lock
beginAwaitUnlock atomic.Uint64 // start time in unix nanoseconds from start waiting for unlock
lockObtained atomic.Uint64 // once we've entered the lock in unix nanoseconds
id []byte // if set this will be printed as string
DisableViolationLog bool
}

func (m *RWMutex) Lock() {
Expand Down Expand Up @@ -94,7 +95,7 @@ func (m *RWMutex) traceEndAwaitLock(threshold Threshold) {
// check for no overflow
took = ts - start
}
if took >= uint64(threshold) {
if took >= uint64(threshold) && !m.DisableViolationLog {
logViolation(Id(m.id), threshold, Actual(took), Now(ts), ViolationLock)
}
}
Expand All @@ -114,7 +115,7 @@ func (m *RWMutex) traceEndAwaitUnlock(threshold Threshold) {
took = ts - lockObtained
}

if took >= uint64(threshold) && lockObtained > 0 {
if took >= uint64(threshold) && lockObtained > 0 && !m.DisableViolationLog {
// lockObtained = 0 when the tracer is enabled half way
logViolation(Id(m.id), threshold, Actual(took), Now(ts), ViolationCritical)
}
Expand Down
11 changes: 7 additions & 4 deletions opts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package muxtracer

import "time"
import (
"time"
)

type Opts struct {
Threshold time.Duration
Enabled bool
Id string // use with
Threshold time.Duration
Enabled bool
Id string // use with
DisableViolationLog bool
}
3 changes: 3 additions & 0 deletions tracers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func logViolation(id Id, threshold Threshold, actual Actual, now Now, violationT
if id != nil {
idStr = string(id) + " "
}

log.Printf("%sviolation %s section took %s %d (threshold %s)", idStr, violationType.String(), time.Duration(actual).String(), actual, time.Duration(threshold).String())
}

Expand All @@ -32,6 +33,7 @@ func (m *Mutex) EnableTracerWithOpts(o Opts) {
m.id = []byte(o.Id)
}
m.threshold.Store(uint64(o.Threshold.Nanoseconds()))
m.DisableViolationLog = o.DisableViolationLog
}

func (m *Mutex) DisableTracer() {
Expand All @@ -47,6 +49,7 @@ func (m *RWMutex) EnableTracerWithOpts(o Opts) {
m.id = []byte(o.Id)
}
m.threshold.Store(uint64(o.Threshold.Nanoseconds()))
m.DisableViolationLog = o.DisableViolationLog
}

func (m *RWMutex) DisableTracer() {
Expand Down