Critical Sections Considered Harmful

I’ve been spending today dealing with some performance tuning. After benchmarking with the lovely JProfiler I found three bottlenecks in my code that need to be tuned.

One really has me scratching my head. It’s certainly a critical section and there’s no way more than one thread should access the code block at the same time.

It isn’t network bound but CPU bound.

Enter modern hardware and SMP schedulers. In the bad olde days it used to be that using an SMP machine was a treat. Now it’s the norm. SMP motherboards, hyperthreaded processors, and dual cores. Oh my!

Now lets say you have two CPUs. Both are at 100% CPU.. CPU A goes to acquire a lock to compute the above critical section. CPU B then falls to 0% CPU and starts to run another thread which then calls the same critical section as CPU A. Now CPU B is deadlocked waiting for CPU A to complete but since A is at 100% it will take a while to return.

Ouch. Now CPU A has caused a denial of service on CPU B.

I can’t think of a way an scheduler could handle this it doesn’t know that CPU A is going to need to acquire a lock without first executing the code.

One solution could be to migrate threads with locks onto CPU B but of course that’s not instantaneous.

It seems to make sense to migrate tasks executing with open locks to a less burdened processor.

%d bloggers like this: