April 30, 2022 By IBM Instana Team 2 min read

Mutexes are often a source of contention that can result in performance issues or deadlocks. It’s no different in Go, and locating the root cause is often challenging. In obvious deadlock situations where all goroutines are waiting, the runtime may be able to detect or predict mutex-related issues and panic. Generally, the problems will manifest themselves at the application logic level.

Let’s look at this simple example:

lock := &sync.Mutex{}
// goroutine1
go func() {
lock.Lock()

// here we make other goroutine1 wait
time.Sleep(500 * time.Millisecond)

fmt.Printf(“%v: goroutine1 releasing…\n”, time.Now().UnixNano())
lock.Unlock()
}()

// goroutine2
go func() {
fmt.Printf(“%v: goroutine2 acquiring…\n”, time.Now().UnixNano())
lock.Lock()
fmt.Printf(“%v: goroutine2 done\n”, time.Now().UnixNano())
}()

time.Sleep(1 * time.Second)

The lock is obtained in the first goroutine, and the second goroutine has to wait for it.

Problems like this will most likely not be detected in the development phase, when there is no concurrent use of an application, and they will result in a performance issue only in the production environment. As a side note, it’s always a good idea to have automated performance regression testing in place, which will simulate concurrent live traffic.

Utilizing pprof

Go has a built-in block profiling and tracing toolset for such situations: pprof. Basically, an application has to expose the profilers on an HTTP port by importing the net/http/pprof package. Afterward, different profiles can be requested by running go tool pprof http://localhost:6060/debug/pprof/block.

Although pprof’s block profiler or tracer can be extremely helpful in identifying contention issues, there are a few obstacles in using pprof against production environment:

  • The profiler’s HTTP handler, which accepts profiling requests, needs to attach itself to the application’s HTTP server or have one running, which means extra security measures should be taken to protect the listening port.
  • Locating and accessing the application node’s host to run the go tool pprof against may be tricky in container environments like Kubernetes.
  • If the application has a deadlock or cannot respond to pprof requests, no profiling or tracing is possible. Profiles recorded before the problem was detected would be very helpful in cases like this.

For production and development environments, the IBM Instana platform provides automatic blocking call profiling. It reports regularly and profiles to the dashboard, which are accessible in the Hot spots/Time section.

Getting started with the IBM Instana platform

The IBM Instana platform is a powerful tool that can help developers quickly and efficiently detect lock contention issues in Go applications. With its automated profiling capabilities, the platform can easily identify the sections of code where multiple goroutines are competing for the same lock, allowing developers to pinpoint and resolve contention issues.

Using the platform, developers can help ensure that their applications perform optimally and deliver the best possible user experience. So, if you’re developing Go applications and looking for an effective way to detect lock contention, consider using the IBM Instana platform to streamline your debugging process and improve your application’s overall performance.

If you’re not already an IBM Instana user, you can sign up for a free two-week trial.

Get started with IBM Instana
Was this article helpful?
YesNo

More from IBM Instana

Probable Root Cause: Accelerating incident remediation with causal AI 

5 min read - It has been proven time and time again that a business application’s outages are very costly. The estimated cost of an average downtime can run USD 50,000 to 500,000 per hour, and more as businesses are actively moving to digitization. The complexity of applications is growing as well, so Site Reliability Engineers (SREs) require hours—and sometimes days—to identify and resolve problems.   To alleviate this problem, we have introduced the new feature Probable Root Cause as part of Intelligent Incident…

Observe GenAI with IBM Instana Observability

6 min read - The emergence of generative artificial intelligence (GenAI), powered by large language models (LLMs) has accelerated the widespread adoption of artificial intelligence. GenAI is proving to be very effective in tackling a variety of complex use cases with AI systems operating at levels that are comparable to humans. Organisations are quickly realizing the value of AI and its transformative potential for business, adding trillions of dollars to the economy. Given this emerging landscape, IBM Instana Observability is on a mission to…

Average 219% ROI: The Total Economic Impact™ of IBM Instana Observability

2 min read - What can your organization achieve with a modern observability solution? Data from a new Forrester Consulting study showed that a composite organization that used the IBM Instana™ Observability platform achieved a 219% ROI over three years. Likewise, it saw a 90% reduction in troubleshooting time by providing high fidelity data to the right people at the right time. About the study IBM commissioned Forrester to conduct the Total Economic Impact™ (TEI) study by interviewing four clients about the value of their…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters