CPU usage

View: New views
12 Messages — Rating Filter:   Alert me  

CPU usage

by Edward Byard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hi

My SDL app hogs the CPU unless in a state where I'm waiting for user input. Since I'm using SDL_PollEvent, I
never miss an event, but CPU is at 100% the whole time.

If I put a 100ms delay every pass of the loop, CPU drops to ~25% which is fine.

So....how do I get CPU usage down to the ~25% figure without using a delay in the loop?

This isn't a Windows app, just a console app.

Any help muchly appreciated!

Ed


Support the World Aids Awareness campaign this month with Yahoo! for Good
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Alvin Beach-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Friday 08 February 2008 13:43:02 Edward Byard wrote:

> Hi
>
> My SDL app hogs the CPU unless in a state where I'm waiting for user input.
> Since I'm using SDL_PollEvent, I never miss an event, but CPU is at 100%
> the whole time.
>
> If I put a 100ms delay every pass of the loop, CPU drops to ~25% which is
> fine.
>
> So....how do I get CPU usage down to the ~25% figure without using a delay
> in the loop?
>
> This isn't a Windows app, just a console app.
>
> Any help muchly appreciated!
>
> Ed

It's hard to say without knowing how you are using SDL_PollEvent. If I had to
guess, I say you are doing a spin-loop on it. In case you missed it,
SDL_PollEvent returns right away regardless if events are in the queue. If
you are constantly calling SDL_PollEvent, then I'd fully expect it to behave
as you said.

Have a look at SDL_WaitEvent() and see if it fits into your program. If you
aren't sure, can you post an example of how you read/poll the events?

Alvin
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Albert Zeyer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Am 08.02.2008 um 19:43 schrieb Edward Byard:

> Hi
>
> My SDL app hogs the CPU unless in a state where I'm waiting for user  
> input. Since I'm using SDL_PollEvent, I
> never miss an event, but CPU is at 100% the whole time.
>
> If I put a 100ms delay every pass of the loop, CPU drops to ~25%  
> which is fine.
>
> So....how do I get CPU usage down to the ~25% figure without using a  
> delay in the loop?
>
> This isn't a Windows app, just a console app.
>
> Any help muchly appreciated!
>
> Ed

Hi,

This is an issue we were also working on at our project (OpenLieroX, a  
game) recently.

Like Alvin said, use SDL_WaitEvent() where you can. You have to  
separate a bit code which has to be executed frequently and code that  
only has to be executed if an event occurs (for example the user has  
pressed a key or your application got a quit signal or a socket  
received some data).

Though, if your app is not event-based this needs some work to do. In  
other cases, a delay is the correct way to let your app sleep a bit.  
It means that you give some time away for other processes which means  
that you don't use 100% CPU time.

X% CPU time just means that your application always do something and  
never sleeps.

(In some implementations, SDL_WaitEvent() simply is a loop where it  
checks SDL_PollEvent() and if no event has occurred, it just sleeps a  
bit and tries again.)

We made it like this: All menu code uses SDL_WaitEvent() and only  
while the game itself is running, we use SDL_PollEvent() with a  
SDL_Delay(). You can set a MaxFPS value and SDL_Delay() sleeps exactly  
so long that the FPS is not higher than the MaxFPS value.

Greetings,
Albert


_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Parent Message unknown Re: CPU usage

by Edward Byard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
I think I've sorted it.

I used to have a while (SDL_PollEvent) which is now an if (SDL_PollEvent) which handles the input,
and then put a 50ms delay outside this which brings the CPU down to 30%.

A bit more code re-organising and using a delay in some major animation work has got CPU usage down to
30% and made the system more stable and, it seems, the animation is much smoother.

Thanks for your advice!

Ed

----- Original Message ----
From: Alvin <alvinbeach@...>
To: sdl@...
Sent: Friday, 8 February, 2008 5:55:24 PM
Subject: Re: [SDL] CPU usage

On Friday 08 February 2008 13:43:02 Edward Byard wrote:

> Hi
>
> My SDL app hogs the CPU unless in a state where I'm waiting for user input.
> Since I'm using SDL_PollEvent, I never miss an event, but CPU is at 100%
> the whole time.
>
> If I put a 100ms delay every pass of the loop, CPU drops to ~25% which is
> fine.
>
> So....how do I get CPU usage down to the ~25% figure without using a delay
> in the loop?
>
> This isn't a Windows app, just a console app.
>
> Any help muchly appreciated!
>
> Ed

It's hard to say without knowing how you are using SDL_PollEvent. If I had to
guess, I say you are doing a spin-loop on it. In case you missed it,
SDL_PollEvent returns right away regardless if events are in the queue. If
you are constantly calling SDL_PollEvent, then I'd fully expect it to behave
as you said.

Have a look at SDL_WaitEvent() and see if it fits into your program. If you
aren't sure, can you post an example of how you read/poll the events?

Alvin
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



Sent from Yahoo! - a smarter inbox.
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Torsten Giebl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello !


> A bit more code re-organising and using a delay in some major  
> animation work has got CPU usage down to
> 30% and made the system more stable and, it seems, the animation is  
> much smoother.


The problem is that the more CPU cycles your app eats, the
less CPU cycles are there for your app/game/programm.

Maybe you don't need to use SDL_Delay with a delay that
high, have you tested it with SDL_Delay(1) or SDL_Delay (10) ?


But the best thing indeed is to look at the actual frames,
if you would possibly get 80 FPS and need only 60 FPS
then give the 20FPS free with SDL_Delay.


CU

_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Torsten Giebl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello !


>> A bit more code re-organising and using a delay in some major
>> animation work has got CPU usage down to
>> 30% and made the system more stable and, it seems, the animation is
>> much smoother.
>
>
> The problem is that the more CPU cycles your app eats, the
> less CPU cycles are there for your app/game/programm.


That was wrong i meant the more CPU cycles your app eats, the
less CPU cycles are there for X11, Win32, Quartz to update the screen.


CU

_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Solra Bizna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Feb 8, 2008 10:55 AM, Alvin <alvinbeach@...> wrote:
> Have a look at SDL_WaitEvent() and see if it fits into your program. If you
> aren't sure, can you post an example of how you read/poll the events?
IMO, there's a pretty major gap in functionality between WaitEvent and
PollEvent. Carbon's WaitNextEvent has a timeout that you can set
appropriately, what we need is something like that for SDL. (Aside
from Edward's use case, events may not be the only thing the app wants
to block on.)
-:sigma.SB
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Alex Barry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey,
From my experience as a game programmer, if you are designing a game that's fullscreen, you shouldn't ever worry about the CPU usage, considering there won't be any other programs in use while it's running.  If you really feel you need a delay, use SDL_Delay( 1 ) which will give up the smallest timeslice available, while still giving your program the most attention.  Again, I can never stress enough that if you are making a fullscreen game, you should never need a delay

Alex

On Feb 8, 2008 5:22 PM, Solra Bizna <sbizna@...> wrote:
On Feb 8, 2008 10:55 AM, Alvin <alvinbeach@...> wrote:
> Have a look at SDL_WaitEvent() and see if it fits into your program. If you
> aren't sure, can you post an example of how you read/poll the events?
IMO, there's a pretty major gap in functionality between WaitEvent and
PollEvent. Carbon's WaitNextEvent has a timeout that you can set
appropriately, what we need is something like that for SDL. (Aside
from Edward's use case, events may not be the only thing the app wants
to block on.)
-:sigma.SB
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Lilith Calbridge :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can't say I agree with that.  Many are the times when I've left lengthy usenet downloads going in the background while I brought up a game.  Sure 'nuff the downloads dropped to a very noticeable crawl.

Lil

>>> On 2/8/2008 at 5:50 PM, "Alex Barry" <alex.barry@...> wrote:
> Hey,
>>From my experience as a game programmer, if you are designing a game that's
> fullscreen, you shouldn't ever worry about the CPU usage, considering there
> won't be any other programs in use while it's running.  If you really feel
> you need a delay, use SDL_Delay( 1 ) which will give up the smallest
> timeslice available, while still giving your program the most attention.
> Again, I can never stress enough that if you are making a fullscreen game,
> you should never *need* a delay
>
> Alex
>
> On Feb 8, 2008 5:22 PM, Solra Bizna <sbizna@...> wrote:
>
>> On Feb 8, 2008 10:55 AM, Alvin <alvinbeach@...> wrote:
>> > Have a look at SDL_WaitEvent() and see if it fits into your program. If
>> you
>> > aren't sure, can you post an example of how you read/poll the events?
>> IMO, there's a pretty major gap in functionality between WaitEvent and
>> PollEvent. Carbon's WaitNextEvent has a timeout that you can set
>> appropriately, what we need is something like that for SDL. (Aside
>> from Edward's use case, events may not be the only thing the app wants
>> to block on.)
>> -:sigma.SB
>> _______________________________________________
>> SDL mailing list
>> SDL@...
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org 
>>

_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by chaz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I agree.  Sure it will take more time to implement correctly, but sharing
system resources including (especially?) cpu time is the right thing to do,
even for full-screen apps.  Consider if somebody wants to play your game on
a laptop via battery power.  Obviously they wouldn't be able to play very
long if your game just sits in a loop without yielding.  Maybe people don't
usually play games on battery, but maybe the reason is because most games
are not implemented correctly and do burn down the battery needlessly.  Just
make your app do the right thing.  Maybe most users won't notice how nicely
your app shares the system, but I think you're bound to make a few people
happier.

Chaz

----- Original Message -----
From: "Lilith Calbridge" <lilith@...>

> Can't say I agree with that.  Many are the times when I've left lengthy
> usenet downloads going in the background while I brought up a game.  Sure
> 'nuff the downloads dropped to a very noticeable crawl.
>
> Lil
>
>>>> On 2/8/2008 at 5:50 PM, "Alex Barry" <alex.barry@...> wrote:
>> Hey,
>>>From my experience as a game programmer, if you are designing a game
>>>that's
>> fullscreen, you shouldn't ever worry about the CPU usage, considering
>> there
>> won't be any other programs in use while it's running.  If you really
>> feel
>> you need a delay, use SDL_Delay( 1 ) which will give up the smallest
>> timeslice available, while still giving your program the most attention.
>> Again, I can never stress enough that if you are making a fullscreen
>> game,
>> you should never *need* a delay
>>
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Solra Bizna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Feb 8, 2008 4:50 PM, Alex Barry <alex.barry@...> wrote:
> From my experience as a game programmer, if you are designing a game that's
> fullscreen, you shouldn't ever worry about the CPU usage, considering there
> won't be any other programs in use while it's running.
> On Feb 8, 2008 5:22 PM, Solra Bizna <sbizna@...> wrote:
> > IMO, there's a pretty major gap in functionality between WaitEvent and
> > PollEvent. Carbon's WaitNextEvent has a timeout that you can set
> > appropriately, what we need is something like that for SDL.
Yes, but SDL is also useful for writing other applications, including
applications that require some animation or need to respond to other
asynchronous events but don't want to grab the CPU by its horns.
(Before someone says "use threads," not all SDL ports support threads.)
-:sigma.SB
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: CPU usage

by Alex Barry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Perhaps I should have added to why you shouldn't use a delay.  Most games hog CPU power, but there is a handy thing called v-sync, which adds a tiny delay to make sure the screen is essentially ready.  This still keeps CPU usage high (which is good for your game), but doesn't hog it

hope that helps

On Feb 8, 2008 8:11 PM, Solra Bizna <sbizna@...> wrote:
On Feb 8, 2008 4:50 PM, Alex Barry <alex.barry@...> wrote:
> From my experience as a game programmer, if you are designing a game that's
> fullscreen, you shouldn't ever worry about the CPU usage, considering there
> won't be any other programs in use while it's running.
> On Feb 8, 2008 5:22 PM, Solra Bizna <sbizna@...> wrote:
> > IMO, there's a pretty major gap in functionality between WaitEvent and
> > PollEvent. Carbon's WaitNextEvent has a timeout that you can set
> > appropriately, what we need is something like that for SDL.
Yes, but SDL is also useful for writing other applications, including
applications that require some animation or need to respond to other
asynchronous events but don't want to grab the CPU by its horns.
(Before someone says "use threads," not all SDL ports support threads.)
-:sigma.SB
_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


_______________________________________________
SDL mailing list
SDL@...
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
LightInTheBox - Buy quality products at wholesale price