muxtracer

package module
v0.0.0-...-f74bc02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

README

golang-mutex-tracer

Helps you debug slow lock/unlocks for golang sync Mutex/RWMutex

Example usage

Import with an alias for minimal impact.

Enable per lock:

import sync "github.com/RobinUS2/golang-mutex-tracer"

l := sync.Mutex{}
l.EnableTracer()
l.Lock()
l.Unlock()

Enable with customer settings per lock:

import sync "github.com/RobinUS2/golang-mutex-tracer"

l := sync.Mutex{}
l.EnableTracerWithOpts(sync.Opts{
    Threshold: 10 * time.Millisecond,
})
l.Lock()
l.Unlock()

Enable with customer name for lock:

import sync "github.com/RobinUS2/golang-mutex-tracer"

l := sync.Mutex{}
l.EnableTracerWithOpts(sync.Opts{
    Threshold: 10 * time.Millisecond,
    Id: "myLock",
})
l.Lock()
l.Unlock()

Enable for all locks (that use the import):

import sync "github.com/RobinUS2/golang-mutex-tracer"

l := sync.Mutex{}
sync.SetGlobalOpts(sync.Opts{
    Threshold: 100 * time.Millisecond,
    Enabled:   true,
})
l.Lock()
l.Unlock()

Example output

2019/02/20 13:32:04 testLock violation CRITICAL section took 23.477ms 23477000 (threshold 10ms)

Benchmark

Yes, there is performance impact. This is in the order of 1000 nanoseconds which is 0.001 milliseconds. However, the purpose of this project is to debug long blocked locks / contention, it should not be used continuously during production.

goos: darwin
goarch: amd64
BenchmarkRWNativeLock-8                          	 5000000	       381 ns/op
BenchmarkRWTracerLockDisabled-8                  	 5000000	       377 ns/op
BenchmarkRWTracerLockEnabled-8                   	 5000000	       385 ns/op
BenchmarkRWNativeLockWithConcurrency-8           	  200000	      6536 ns/op
BenchmarkRWTracerLockDisabledWithConcurrency-8   	  200000	      6504 ns/op
BenchmarkRWTracerLockEnabledWithConcurrency-8    	  200000	      6435 ns/op
BenchmarkNativeLock-8                            	100000000	        15.0 ns/op
BenchmarkTracerLockDisabled-8                    	 5000000	       366 ns/op
BenchmarkTracerLockEnabled-8                     	 5000000	       373 ns/op
BenchmarkNativeLockWithConcurrency-8             	 1000000	      1019 ns/op
BenchmarkTracerLockDisabledWithConcurrency-8     	  200000	      6431 ns/op
BenchmarkTracerLockEnabledWithConcurrency-8      	  200000	      6396 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResetDefaults

func ResetDefaults()

func SetGlobalOpts

func SetGlobalOpts(o Opts)

Types

type Actual

type Actual uint64

type Id

type Id []byte

type Mutex

type Mutex struct {
	// contains filtered or unexported fields
}

func (*Mutex) DisableTracer

func (m *Mutex) DisableTracer()

func (*Mutex) EnableTracer

func (m *Mutex) EnableTracer()

func (*Mutex) EnableTracerWithOpts

func (m *Mutex) EnableTracerWithOpts(o Opts)

func (*Mutex) Lock

func (m *Mutex) Lock()

func (*Mutex) Unlock

func (m *Mutex) Unlock()

type Now

type Now uint64

type Opts

type Opts struct {
	Threshold time.Duration
	Enabled   bool
	Id        string // use with
}

type RWMutex

type RWMutex struct {
	// contains filtered or unexported fields
}

func (*RWMutex) DisableTracer

func (m *RWMutex) DisableTracer()

func (*RWMutex) EnableTracer

func (m *RWMutex) EnableTracer()

func (*RWMutex) EnableTracerWithOpts

func (m *RWMutex) EnableTracerWithOpts(o Opts)

func (*RWMutex) Lock

func (m *RWMutex) Lock()

func (*RWMutex) RLock

func (m *RWMutex) RLock()

func (*RWMutex) RUnlock

func (m *RWMutex) RUnlock()

func (*RWMutex) Unlock

func (m *RWMutex) Unlock()

type Threshold

type Threshold uint64

type TraceLocker

type TraceLocker interface {
	EnableTracer()
	DisableTracer()
	EnableTracerWithOpts(o Opts)
}

type ViolationType

type ViolationType int
const (
	ViolationDefault  ViolationType = iota // 0, unused
	ViolationLock                          // 1, time to lock (awaiting)
	ViolationCritical                      // 2, time spent in the lock
)

func (ViolationType) String

func (v ViolationType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL