Code Monkey home page Code Monkey logo

golang-mutex-tracer's Introduction

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

golang-mutex-tracer's People

Contributors

lukemarsden avatar robinus2 avatar simonwo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.