View Single Post
Old 08-15-2002, 01:50 AM   #79
BitBasher
Registered
 
Join Date: Nov 2001

Location: Canada
Posts: 5
More on going idle

It's been a few years, but I had real trouble getting an app to actually "go idle".

WRT the aquarium, assuming you're running on a reasonable fast CPU, each frame rendered is going to take SOME CPU time - and rendering can be fairly CPU intensive. Assuming that the aquarium is set to no FPS limit, then I'd expect it to take 100% CPU no matter what - as the aquarium is most likely attempting to render AS FAST AS POSSIBLE, so it effective should become CPU-bound. Only other tasks with equal or higher priority should be able to steal CPU ticks from the Aquarium if it's running FPS unlimited.

But, if the frame rate is 60Hz limited, (and assuming your CPU take significantly less than 1/60th of a second to use the CPU to render a single frame), then it becomes fairly interesting that the aquarium is still pegged at 100%.

This is where I had trouble with a old app I wrote which was sucking too much CPU time when it had very little to do. I can vaguely remember that if the app simply exited when it was completed (and returned control back to Windows), if there was nothing else going on in the system, Windows would effectively turn right around and return all the CPU control back to my app. In other words, Windows had nothing else to do, so he kept giving me back all the free CPU time, even though I didn't need it.

I think the problem was solved by telling Windows to Yield my process. I don't remember all the deatails (fog brain), but effectively I had to explicitly tell Windows that I no longer wanted anymore of my current timeslice. This trick caused Windows to not immediately return CPU control to me, but then Windows would give the remaining portion of my timeslice over to the idle task. After the next timeslice, Windows would give me control again, I'd do my little teeny bit of work, then I'd relinquish the remainder of this next timeslice, etc.

This resulted in my app only consuming about 25% of the CPU instead of 100% with the same effective results. Now, I wish I could remember how I did that.

I have another idle app I run called PRIME95, which calculates REALLY HONKING BIG prime numbers. It is VERY well behaved at only using as much CPU idle time as you give it. I think the source code is available for it, so it might be worth investigating.

Off the top of my head, my suspicion is that if the Aquarium uses PeekMessage() or fairly fast Timer, it's probably fooling Windows into giving it too much CPU time even though it doesn't need it. I'm not sure if this could be done, but an idle thread calling WaitForObject() might produce better CPU utilization.

I'll see if I can dig up any of my really OLD notes on CPU usage handling and yielding timeslices. This might require some site excavation though!

BB.
BitBasher is offline   Reply With Quote