Is there a way to measure lock contention?

I was surprised I could not find such a template in Instruments / xctrace; maybe it's in something else and I couldn't find it?

(I am trying to figure out why my throughput got slow. Is it because a mutex is too heavy? Or is there a lot of contention over the lock? How long do the locks tend to be held? Etc.)

Hi kithrup,

Unfortunately, there is currently no way to measure lock usage on the system. This data is usually very high throughput and tracing it becomes fairly expensive.

What you could do instead is to try and capture System Trace and look into the Thread States. If your issue is caused by multiple threads going on / off the CPU while waiting for a shared resource, it should be visualized when inspecting the Thread tracks in the trace.

In there, you will see intervals indicating thread running or being blocked. After selecting a thread, narrative view will be visible that will allow you to check sequence of events and interactions between threads (going blocked, unblocked), which will have a backtrace associated with it. If you see two threads that keep switching between each other, this would be a good starter to investigate. Backtrace would usually point into a place where lock is acquired and system call is being made.

If you record a trace and can attach a screenshot or full trace here, we could take a further look and give you some more concrete pointers.

Kacper

Is there a way to measure lock contention?
 
 
Q