November 20, 2013

Qt 4.8.5 timers and high CPU usage on Linux

Posted at November 20, 2013 03:52 PM in Computers .

I am using the QBasicTimer class in my application for a number of QObject timers. It's supposed to be the most efficient timer class Qt offers in terms of overhead (as per the documentation), but I noticed fairly high CPU usage (3%) on a 30ms timer even when the timer function was doing nothing. This seemed odd on a RHEL 6.4 server with multiple, very fast cores.

After some research, I discovered this is a known problem due to a timer granularity mismatch between glib (used for the event loop on Linux) and the Qt timer API. For some reason, the current code decides to use some busy-wait-type method to try and precisely handle the granularity matching, resulting in a short CPU spike right before the timer expires.

The bug was identified years ago. There's a simple one line fix -- the patch can be found here. The fix improved my performance measurably -- the CPU usage went down to 0.3%, a 10x improvement!

Apparently the patch has finally been incorporated into one of the Qt 5.x releases, but I hope it makes it into a future Qt 4.x patch soon!

Comments

Posting of new comments has been disabled for this post.