|
This thread is locked; no one can reply to it. |
1
2
|
my game uses 90% CPU |
koless
Member #4,996
September 2004
|
my game uses 90% CPU how to prevent it?
|
Jonatan Hedborg
Member #4,886
July 2004
|
First of all, use the [CODE][/CODE] tags (only in non-caps)
|
Chris Katko
Member #1,881
January 2002
|
Long answer: -----sig: |
Ceagon Xylas
Member #5,495
February 2005
|
Call it at the end of your game loop [after logic and drawing sequences.] while(!game_over) { game_logic.update(); game_logic.draw_buffer(); rest(0); }
|
Elverion
Member #6,239
September 2005
|
Something like...
should work pretty well. Using the yield_timeslice() function will mean your program will yield it's processor usage if some other program needs it more, otherwise it will still use 100%. This means that your program might look like it's using 100% of the processor, but in reality most of it is going to waste unless it's actually needed. The rest(1) part will actually make the program appear to use less CPU. -- |
CGamesPlay
Member #2,559
July 2002
|
Quote: Using the yield_timeslice() function will mean your program will yield it's processor usage if some other program needs it more, otherwise it will still use 100%. Recently yield_timeslice was changed to rest(1), so it will cause 0% CPU usage. -- Ryan Patterson - <http://cgamesplay.com/> |
Elverion
Member #6,239
September 2005
|
Hmm, didn't know that. When did this happen? Is this in the stable 4.2.0? -- |
A J
Member #3,025
December 2002
|
Quote: Recently yield_timeslice was changed to rest(1), so it will cause 0% CPU usage. rest(1) is not yielding, its sleeping. When did someone decide to redefine the meanings of words. ___________________________ |
Kitty Cat
Member #2,815
October 2002
|
rest(0) does the same thing as yield_timeslice, which does not save the CPU (it'll come right back to your program when nothing else wants it). Using rest(1) is what you want, but you only want to call it when you're not falling behind. while(playing) { while(counter <= 0) rest(1); while(counter > 0) { do_logic(); --counter; } draw(); }
-- |
CGamesPlay
Member #2,559
July 2002
|
I thought we had a big discussion and it was decided that: yes, yield_timeslice is by semantics rest(0), but everyone actually wants rest(1), so it was changed... -- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Leverton
Supreme Loser
January 1999
|
/* sys_directx_yield_timeslice: * Yields remaining timeslice portion to the system. */ static void sys_directx_yield_timeslice(void) { Sleep(0); } I believe at one point during the WIPs, it was changed to Sleep(1). Yielding is not the same as resting for 1 msec and it's a good thing that it is still Sleep(0). However, if you look back at news, you'll see "Deprecated yield_timeslice in favour of rest(0)." I don't remember why; maybe it had to do with differences in cross platform behaviour. But whatever, I think yield_timeslice makes a lot more sense then manually calling rest(0). If it was truly deprecated just because people wanted 0% CPU usage, that's stupid. Yield is yield. It's not rest. |
Milan Mimica
Member #3,877
September 2003
|
Quote: Recently yield_timeslice was changed to rest(1), so it will cause 0% CPU usage.
Did not!
-- |
Tomek Kozera
Member #6,507
October 2005
|
I don`t know pretty much about writing games, so whould someone explain to me what would rest(0) do? It would rest 0 miliseconds so... I guess, that since is rests even for 0 time, then the program would give back the processor to other processes. So it`s like forcing the game to give the prcessor time to other applications without acutally slowing down the game. Is that correct? An in what context is 'yield' used here? I get a bit confused, it seems that I don`t know all the meanings of this word. |
Milan Mimica
Member #3,877
September 2003
|
On Unixes, rest(0) is implemented as sched_yield. It will give back the processor to other processes, but it will also "slow down" your game because it will resume after more than 0 ms. It depends on the mood of other processes. However, yielding will be forced by OS every now and then. On linux 2.6.x it depends of kernel config CONFIG_HZ.
-- |
Thomas Harte
Member #33
April 2000
|
Quote: This isn't exactly a problem, because the more CPU usage, the faster your game runs, and the more you can do in a second. This is a problem. If you don't put the CPU to sleep when it could be sleeping then you require that the processor be run constantly at it's highest frequency. All laptops and many well designed desktops cut power to the CPU when it is sleeping a lot, e.g. by reducing the clock speed. Once the power is cut heat output decreases and often fans can be slowed down or switched off, reducing noise output. If your program uses 100% CPU just because it can then it will probably not stay on those machines for long. Here is how I achieve non-100% CPU usage with Allegro (in this example, limiting to 70 fps):
EDIT: code fixed. [My site] [Tetrominoes] |
Birdeeoh
Member #6,862
February 2006
|
I don`t know pretty much about writing games, so whould someone explain to me what would rest(0) do? It would rest 0 miliseconds so... I guess, that since is rests even for 0 time, then the program would give back the processor to other processes. So it`s like forcing the game to give the prcessor time to other applications without acutally slowing down the game. Is that correct? An in what context is 'yield' used here? I get a bit confused, it seems that I don`t know all the meanings of this word. Tomek - rest() in allegro, which is also sleep() on unices and Sleep() on Windows, tells the operating system to put the process to sleep for the specified number of milliseconds. The concept of "sleep" is threading/operating system speak that means, in plain English, "Hey, operating system - I'm done for now. Let other programs have the CPU, and don't bother me again for X milliseconds." The concept of "yield" is threading/operating system speak that means, in plain English, "Hey, operating system - I can stop what I'm doing right now so you can let other programs do stuff they need to do. BUT, if noone else has any work to do, then please let me know so I can keep going" That's what rest(0) or the windows Sleep(0) do. They tell the o/s to give the CPU to others, but if noone else wants/needs it, let your process have it back. That's why rest(0) won't accomplish what you're going for. The CPU will still stay at 100% because your program is still working fulltime - it's just "playing nice" with everyone else. If you actually rest(1), you are telling the operating system to not even let you have the CPU until at least 1ms has passed. Therefore, the CPU usage WILL actually drop. With today's CPU speeds, 1 millisecond is an eternity. If every millisecond you wake up, do a few hundred thousand instructions, then go back to sleep, you're barely using the CPU. I hope that helps you understand the terminology better! On a related note, as someone pointed out already, some desktops and many/most laptops the system crank up the fans when the CPU is at full usage for a long time. Other power-management side-effects can also occur. This is why going full throttle is bad form unless you actually have to. I have a P4 3.0ghz in a laptop that is very quiet when at rest, but when something is using 100% cpu the thing SCREAMS. I play hard core 3d games on my laptop and expect them to be using full resources all the time - I wear headphones. But other games that are poorly designed don't last long on my computer. I once ran across a KICK ASS solitaire suite that I loved, but it used 100% CPU. I'm sorry, there's no way you actually need 3 billion instruction cycles a second for me to play free cell. [url http://developer.berlios.de/projects/openlayer/]OpenLayer[/url is an OpenGL accelerated 2D library for fast and easy graphics development under Allegro |
Tobias Dammers
Member #2,604
August 2002
|
The best way would be using thread wake-up events, based on a timer. In short, you set up a timer to wake your main thread at regular intervals (with each timer click). After you are done drawing a frame and updating logic, make the main thread sleep until signalled again. It will use 0% cpu until the timer ticks again, but will continue if it already has. If you don't want to multi-thread, use the rest() approach. --- |
Milan Mimica
Member #3,877
September 2003
|
What's the difference between your approach and this?
-- |
Kitty Cat
Member #2,815
October 2002
|
Because that doesn't rest at all . But seriously.. while(timer <= 0) rest(1); causes the thread to rest anywhere between 1ms and 10ms per call, which isn't very accurate, and it cuases the program to rest-check-rest-check-etc (which takes some, if negligible, CPU time) until its ready, instead of just staying down and letting the OS wake the thread up on time. -- |
Milan Mimica
Member #3,877
September 2003
|
How do you implement this timer thread?
-- |
Tobias Dammers
Member #2,604
August 2002
|
1. Write a thread function for the timer thread, which sleeps for the desired amount of time, then sets an event. For more details: --- |
Kitty Cat
Member #2,815
October 2002
|
Hello, Windows-only only land. Nice to see you. -- |
Tobias Dammers
Member #2,604
August 2002
|
Yay, hello there. Haven't done any multithread programming on other platform, so I guess someone else should do the appropriate googling. --- |
GullRaDriel
Member #3,861
September 2003
|
Pthread is cross platfrom. "Code is like shit - it only smells if it is not yours" |
gnolam
Member #2,030
March 2002
|
But it is also a pain in the ass to build and use on non-*n?x platforms, or at least so I've heard. -- |
|
1
2
|