g_timeout_add implementation not very accurate
Submitted by Phillip Susi
Link to original bug (#668054)
Description
Created attachment 205407 test program
The glib timer implementation recomputes the next timeout by adding the interval to the current monotonic time each time a timeout fires. This causes any delay in servicing the timer to accumulate over time to sometimes rather large errors. Correcting the next timeout to be computed as previous += interval rather than current + interval corrects this error.
I wrote a simple test program to measure the error. I measured the error on a dual core 3 GHz cpu system under a few conditions for 15 seconds both before and after this patch and got the following results:
Unloaded: 44ms absolute error, 0.296% relative With stress -c 2 running in the background: 59.77ms ( 0.398% relative ) With a linux kernel make -j 3 in the background: 380.3ms ( 2.535% relative ) With stress -c 2 and niceing the test: 3170.8ms ( 21.138% )
After this patch:
Unloaded: 1.0ms ( 0.0067% ) Stress: 0.385ms ( 0.0026% ) Kernel make: 294ms ( 1.963% ) Stress + nice: 5.04ms ( 0.0315% )
Attachment 205407, "test program":
timers.c
Version: 2.30.x