Re: Multitasking in Mac OS Gets 100% CPU
- Posted by mattlewis (admin) Nov 07, 2012
- 1432 views
Interestingly, checking gnome-system-monitor with sleep(1) the "Waiting Channel" column displays hrtimer_nanosleep, but with sleep(0) it simply shows "0". Is the sleep() function not calling "nanosleep" when we pass 0 as a parameter?
-Greg
Looks like we have a bug in Wait() in be_task.c (which is used to implement the sleep() routine).
We have code in there to busy-wait if we are attempting to sleep in fractional sections, and the fractions are too small - but we also have a check so if we're sleeping for less than 1 second we just busy-wait it out.
I think this was done back when we supported DOS so we could sleep for fractional seconds from DOS apps...
I just profiled this (on Linux) and I don't see Wait() being called at all (though I agree that we should probably move all of the waiting time in Wait() into the nanosleep call). If you add a print statement after the call to get_key(), you'll see that a lot of time is spent going around in that loop.
Most of the time is spent in nodelaych() (and really, in a call to tcsetattr), which is how we get keyboard input in non-windows systems. Windows works differently. So perhaps Win7 has a more efficient way of handling console input than XP. Perhaps our implementation of nodelaych() could be improved. Do we really need to call tcsetattr() each time we call get_key()?
Matt