[MPlayer-dev-eng] Major sleep() performance issue

Dieter Shirley dete at mac.com
Thu Feb 10 02:48:30 CET 2005


One of the common bits of lore about mplayer running under OS X is that 
it's much slower than VLC.  Well, my attempts to use VLC on the Mac 
have left me frustrated, while I'm very comfortable with mencoder and 
mplayer, so I set about tracking down where the problem lay.

The problem, somewhat ironically, is that the Darwin usleep() timer is 
really quite accurate, much more accurate than the 10ms that mplayer 
seems to assume.  Here's the code from mplayer.c:2440:

         float min=softsleep?0.021:0.005;
         current_module="sleep_timer";
         while(time_frame>min){
           if(time_frame<=0.020)
              usec_sleep(0); // sleeps 1 clock tick (10ms)!
           else
              usec_sleep(1000000*(time_frame-0.020));
           time_frame-=GetRelativeTime();

What ends up happening with this code is that usec_sleep(0) gets called 
*hundreds* of times for each frame of video.  This isn't a trivial call 
either, since it touches the kernel.  The upshot is that sleeping can 
take up to 30% of the CPU, more than decoding the video!

While I discovered this problem on the Mac, it's going to be an issue 
with any platform where the implementation of usec_sleep() has 
reasonable accuracy below 10ms.

I've "fixed" my local copy by changing both of the 0.020 fudge factors 
above to 0.005 (the same as min since softsleep is off), but this 
obviously doesn't constitute a general solution for platforms where 
sleep isn't so accurate.

Another possible solution would be to modify the Darwin implementation 
of usec_sleep to treat values <10000 as 10000, but it seems to me that 
this is a mistake; the greater accuracy of the sleep timer should be 
leveraged, not ignored.  (BTW - It seems that the Win32 port had a 
similar problem since it uses a guard like this.)

The best solution I can think of would be to have a global "minimum 
clock accuracy" that would be used instead of the hard-coded 10ms, 
which would be defined in the various timer-*.c files.

Cheers,
-dete



More information about the MPlayer-dev-eng mailing list