analyzing lock contention in multi-threaded app?

Bengt Richter bokr at
Tue Jun 17 21:02:47 CEST 2003

On Tue, 17 Jun 2003 11:40:21 -0500, Skip Montanaro <skip at> wrote:
>I can see I was too vague in my description.  I'm not really concerned with
>deadlock since no thread holds more than one lock at a time.  I'm more
>interested in knowing if a thread had to wait to get a lock or take an
>object off the queue.
What about using time.clock() to measure the wait you are concerned with?
You could classify the wait times and count occurrences in the categories
something like (untested!)
   counts = [0]*nslots
   dt = time.clock() # hopefully thread-safe
   # acquire lock or whatever
   dt = time.clock-dt
   counts[min(int(dt*scale),nslots-1)] +=1 # for some scale that maps the common

Then you could print a histogram that should tell you something about the
distribution of waiting time. You'll have to fiddle with scale and nslots
to represent your reality with appropriate detail.

You could make a class and use a global instance to collect the data in self.counts
for whatever initialized nslots and scale. And maybe have print and clear methods etc.
If you access the latter from an interactive thread, I guess you might need a lock
for the counts data...

Bengt Richter

More information about the Python-list mailing list