help: 100% CPU usage..

Started by
25 comments, last by GameDev.net 17 years, 10 months ago
Quote:Original post by Show
I don't get it.
If what you say is true, then how come playing Windows 3d pinball doesn't use 100% cpu?
With pinball opened, it only says '6%' under window task manager, and the rest is system idle process.
For world of warcraft, it runs at around 85% cpu...compare to my game that only draws a tile map runs at 100%.
Ummmm...

If the CPU runs @ 100% all the time, wouldnt the temp be too high?
I always though CPUs just allocate whatever is needed for an app, and save the rest of resource..


The reason why pinball doesn't use that much of the CPU, is the program is telling the computer that it doesn't need that many resources. It only updates once every x milliseconds.

On the other hand, even if you think you have only one triangle, it is possible to use 100% of the CPU. If you don't cap what you're displaying, or use an intelligent algorithm, the computer basically looks at the situation and says

Hey, I have one triangle and was told to run this continuously.

So that's what it does. It runs that one triangle at 3500 frames per second using every spare CPU clock cycle, because that's what you told it to do.

I know I mentioned that you should be using close to 100% of the CPU clock cycle, because I made a few assumptions about what you're trying to do. Other people mentioned its not always a smart idea. And we're both right to a certain degree. You need to use as much of the computer as you feel is necessary.

If you get 1000+ fps, maybe you can lay off the CPU some, and save the battery life. On the other hand, if you're running at 2 fps at 10% CPU intake, maybe you want to increase it.
Advertisement
I personally write my programs to use up to 100% CPU usage, but not moer than necessary to maintain a certain framerate (30 for non-real time / twitch games and usually whatever the refreash of the monitor is for twitch games -like first person shooters)

Of course for cutting edge games with realistic physics and intelligent AI you WILL use 100% CPU all the time, cause your going to be doing the most you can all the time. I was simply trying to say that being a game is not enough in itself to justify that attitude. You really need to be an action real-time game before you adopt the attitude that the system is yours to abuse at will.

Of course each method of using less than 100% CPU has tradoffs, so there is no one-size-fits-all solution to this problem.
I have a question. I was under the impression that the CPU constantly executes instructions whenever it's on. I thought that it being idle just meant that it is in a busy-wait loop.

But if that's true, then a program running at 100% would not affect the battery or fan; it would just take time from other processes.

So does the CPU actually stop executing when it's idle?
It doesn't really stop, but modern processors can be made to slow down (see SpeedStep).

Also, executing idle instructions is potentially easier on the processor, because signals (except the clock) aren't changing as often. CMOS logic draws much more power to change a signal than it does to keep it steady (even if 'high').
Its very similar to the difference in your video card between running just a windows desktop, running a 3d program, and running a 3d program that uses all the technology (shaders, etc). The areas of the chip that are not in use are usually using almost no power and generating almost no heat.

A chip these days is a lot like a house where, at any given time not every light or outlet is in use - but there is at least a certain minimum amount of power being consumed at all times. And more advance technologies let chips completely turn off whole areas of the chip while not in use (like closing off rooms and vents in a guest room to conserve heating and cooling bill).
since your obviously still learning ,you shouldn't try to ground all the cpu time for your app.
when your app is in foreground it has more priority to execute ,so what ever background
process there is ,they have to take less time ,and this might as well cause your pc to crash.
simply after rendering every frame throw in a sleep(0) ,just to give everything else a chance to
execute. when your game becomes fairly complex , and your in need for more resources ,
you should avoid running it in windowed mode . this way you get to use much more cpu time ,
and everything else requirments fall to minimum ( nothing else is being drawn).
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Quote:Original post by CTar
Quote:Original post by Sr_Guapo
Quote:Original post by pulpfist
I guess your right.
How about a yield call in Windows API? Is there such a thing?


It is called "Sleep()". Just use that function and pass the number of milliseconds. I have no idea if it is windows only or anything (I'll look it up in a second).


It's Windows only and you shouldn't just pass it a number of MS. Sleep(0) is about the same as a yield. You shouldn't pass anything greater unless you wait for a very long time. Sleep(1) often results in more than 10ms waits, which is completely unacceptable.

EDIT: Also you shouldn't use yield unless you can see you are rendering way too much (>150 FPS, at least). imagine slowing your game down 3% when the user already have low-end hardware.


using sleep and/or yield is generally only useful for multithreaded games, (if you want to use a separate thread to load content on the fly to avoid loadscreens for example, the load thread should then yield often or possibly even sleep a bit so that it doesn't hurt the performance too much), the same is true for almost any low priority task performed by a separate thread. the main loop shouldn't have to yield. (if you got another high priority thread aswell it would either run on its own core most of the time or the OS process scheduler would sort things out for you).

you can use v-sync and fixed size timesteps to avoid rendering or running too fast. no need to sleep at all, just let the game waste some cycles instead, (its far more reliable),

you could possibly add a short sleep as an option for multi-user systems(unix/linux) or when running in windowed mode. if you run in fullscreen on a single-user system (windows) your users wouldn't want any other program to take cpu time from the game.

if its a small game (such as minesweeper or freecell that comes with windows) sleeping gets alot more important since it becomes quite likely that the user is running other programs at the same time and only uses the game to pass the time, such as playing a game of minesweeper while rendering a movie in blender or 3dsmax (you wouldn't want the game to take 50% of your cpu and double your rendering time then).

so basically you should only sleep if:
1) you do the sleeping in a low priority thread. (to give more time to the games main thread)
2) the game is running in a window
3) the game is running on a multi-user system
4) the user specifically asks for it

This topic is closed to new replies.

Advertisement