CPU Usage

I’ve been using SDL for 2 weeks, so I’m somekind of a newbie with SDL.
everything went fine until I noticed that my app uses about 80~90% (too
much!) of the CPU, although I’m using a framerate limiter to limit the
fps to 30.

well, this is my problem, I want to lower the CPU usage of my app, it
varies according to what’s drawn on the screen, but I couldn’t manage to
get it below 40% (which I consider high)

I’ve checked various apps/games, some using SDL, other using
DirectDraw(coz I thought the problem is with SDL’s implementation), and
I found some DD and SDL apps which use little < 25% which’s perfect.
so I knew it can be achieved.

one of the apps I checked was the testsprite sample that comes with SDL,
I’ve checked the source code, and I found out that using
SDL_UpdateRects() lowers the CPU usage much, but I can’t use it
everywhere in my app, sometimes I need to redraw the whole screen.

I found that by only clearing the screen to black, and then updating, I
get a relatively high CPU usage(60-70%)!
here’s the drawing part of the game loop.
{
SDL_FillRect(screen, NULL, black);
SDL_Update(screen, 0, 0, 0, 0);
SDL_framelimiterDelay(&fpsManager); // the framerate limiter
}

so could someone help me identify the solution?
please don’t tell me to use OpenGL, I already know OpenGL, and I know
it’ll solve the problem, but it’s not suitable for my app because of
some requirements.

Info:
Desktop configuration: 102476832
OS: Windows XP
RAM: 512 MB
VGA Card: RIVA TNT2 32 MB.

SDL Video Mode: Windowed 64048024(I’ve tried also 32 bpp), I use
SWSURFACE coz I have a fair amount of alpha blending.
SDL_VideoInfo:
Is it possible to create hardware surfaces : 1
Is there a window manager available : 1
Are hardware to hardware blits accelerated : 1
Are hardware to hardware colorkey blits accelerated : 1
Are hardware to hardware alpha blits accelerated : 0
Are software to hardware blits accelerated : 1
Are software to hardware colorkey blits accelerated : 1
Are software to hardware alpha blits accelerated : 0
Are color fills accelerated : 1
Total amount of video memory in Kilobytes : 28688
Driver Name: directx

sorry for the long post and thanks for your effort.

I’ve been using SDL for 2 weeks, so I’m somekind of a newbie with SDL.
everything went fine until I noticed that my app uses about 80~90% (too
much!) of the CPU, although I’m using a framerate limiter to limit the
fps to 30.

well, this is my problem, I want to lower the CPU usage of my app, it
varies according to what’s drawn on the screen, but I couldn’t manage to
get it below 40% (which I consider high)

I’ve checked various apps/games, some using SDL, other using
DirectDraw(coz I thought the problem is with SDL’s implementation), and
I found some DD and SDL apps which use little < 25% which’s perfect.
so I knew it can be achieved.
[ … ]

Why does it even matter how much CPU you’re using? Is it making your
computer slower? Is the game running unsatisfactorily slow? If not,
then your CPU% is fine.
Possibly, the framerate limiter does a busy-wait loop instead of
sleeping – this is fine, but that would account for the much higher
CPU%.

Unless it’s connected to some other unsatisfactory occurrence, you can
safely stop worrying.

– JoshOn 7/28/05, Diaa Sami wrote:

one of the apps I checked was the testsprite sample that comes with SDL,
I’ve checked the source code, and I found out that using
SDL_UpdateRects() lowers the CPU usage much, but I can’t use it
everywhere in my app, sometimes I need to redraw the whole screen.

I found that by only clearing the screen to black, and then updating, I
get a relatively high CPU usage(60-70%)!
here’s the drawing part of the game loop.
{
SDL_FillRect(screen, NULL, black);
SDL_Update(screen, 0, 0, 0, 0);
SDL_framelimiterDelay(&fpsManager); // the framerate limiter
}

so could someone help me identify the solution?
please don’t tell me to use OpenGL, I already know OpenGL, and I know
it’ll solve the problem, but it’s not suitable for my app because of
some requirements.

Info:
Desktop configuration: 102476832
OS: Windows XP
RAM: 512 MB
VGA Card: RIVA TNT2 32 MB.

SDL Video Mode: Windowed 64048024(I’ve tried also 32 bpp), I use
SWSURFACE coz I have a fair amount of alpha blending.
SDL_VideoInfo:
Is it possible to create hardware surfaces : 1
Is there a window manager available : 1
Are hardware to hardware blits accelerated : 1
Are hardware to hardware colorkey blits accelerated : 1
Are hardware to hardware alpha blits accelerated : 0
Are software to hardware blits accelerated : 1
Are software to hardware colorkey blits accelerated : 1
Are software to hardware alpha blits accelerated : 0
Are color fills accelerated : 1
Total amount of video memory in Kilobytes : 28688
Driver Name: directx

sorry for the long post and thanks for your effort.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hello,

I’ve been lurking on this list about 3 weeks now because of this
exact issue: High CPU usage on SDL apps that aren’t doing anything
that should cause high CPU usage.

Why does it even matter how much CPU you’re using? Is it making your
computer slower? Is the game running unsatisfactorily slow? If not,
then your CPU% is fine.

SDL’s really awesome, but it’s event architecture causes it to
use a LOT of CPU. Responses like "Why does it even matter"
are very discouraging. It matters because SDL doesn’t have to
use a lot of resources, and because a single game running on your
PC is not the only thing SDL is good for. Try running 3 or 4
SDL apps at the same time. Suddenly none of them perform well.

Possibly, the framerate limiter does a busy-wait loop instead of
sleeping – this is fine, but that would account for the much higher
CPU%.

This is exactly the case for event loops. But it’s not “fine”.
These tight loops turn a really fast CPU into a really slow toaster
for no reason.

If you’re in a multi-process or multi-user environment, it’s not
ok to burn 40% of your CPU waiting for input from the user, or
throttling redraws… It also prevents laptops from calling idle
loops which slow their clocks down.

It is a real problem. Anybody got any thoughts on a real solution?

Jonathan

Hello,

I’ve been lurking on this list about 3 weeks now because of this
exact issue: High CPU usage on SDL apps that aren’t doing anything
that should cause high CPU usage.

Why does it even matter how much CPU you’re using? Is it making your
computer slower? Is the game running unsatisfactorily slow? If not,
then your CPU% is fine.

SDL’s really awesome, but it’s event architecture causes it to
use a LOT of CPU. Responses like "Why does it even matter"
are very discouraging. It matters because SDL doesn’t have to
use a lot of resources, and because a single game running on your
PC is not the only thing SDL is good for. Try running 3 or 4
SDL apps at the same time. Suddenly none of them perform well.

Possibly, the framerate limiter does a busy-wait loop instead of
sleeping – this is fine, but that would account for the much higher
CPU%.

This is exactly the case for event loops. But it’s not “fine”.
These tight loops turn a really fast CPU into a really slow toaster
for no reason.

If you’re in a multi-process or multi-user environment, it’s not
ok to burn 40% of your CPU waiting for input from the user, or
throttling redraws… It also prevents laptops from calling idle
loops which slow their clocks down.

It is a real problem. Anybody got any thoughts on a real solution?

I understand what you mean. The original poster seemed to be worrying
about CPU% rather than speed. I was pointing out that if it’s fast
enough, you don’t need to worry about CPU%. For single games (which
is what it sounded like the OP was writing), it usually is.

However, I know that’s not true in all cases, and I don’t know of a
solution for the times when it isn’t. Anyone else?

– JoshOn 7/28/05, Jonathan Stark wrote:

Jonathan


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi,

I’ve been using SDL for 2 weeks, so I’m somekind of a newbie with SDL.
everything went fine until I noticed that my app uses about 80~90% (too
much!) of the CPU, although I’m using a framerate limiter to limit the
fps to 30.

well, this is my problem, I want to lower the CPU usage of my app, it
varies according to what’s drawn on the screen, but I couldn’t manage to
get it below 40% (which I consider high)

I’ve checked various apps/games, some using SDL, other using
DirectDraw(coz I thought the problem is with SDL’s implementation), and
I found some DD and SDL apps which use little < 25% which’s perfect.
so I knew it can be achieved.
[ … ]

Why does it even matter how much CPU you’re using? Is it making your
computer slower? Is the game running unsatisfactorily slow? If not,
then your CPU% is fine.
Possibly, the framerate limiter does a busy-wait loop instead of
sleeping – this is fine, but that would account for the much higher
CPU%.

Unless it’s connected to some other unsatisfactory occurrence, you can
safely stop worrying.

I have to disagree, because of laptop users. I remember a mahjongg game
I had been playing on my laptop, and it would peg the CPU at 100% in some
busy-wait loop when absolutely nothing was happening (no animations,
movement, etc.), which would make the laptop’s CPU fan kick up, and
presumably eat more battery. If it is a busy-wait loop, see if you can
replace it with something that doesn’t peg the CPU (SDL_Delay() or
something similar.)

Now, I suppose something important to clarify is to make sure that the
high CPU usage is “real”, and not just an error or mis-reading by the
profile tool you’re using. Modifying the program in response to that
would in fact be silly :slight_smile:

-JonOn Thu, 28 Jul 2005, Joshua Oreman wrote:

On 7/28/05, Diaa Sami wrote:

– Josh

one of the apps I checked was the testsprite sample that comes with SDL,
I’ve checked the source code, and I found out that using
SDL_UpdateRects() lowers the CPU usage much, but I can’t use it
everywhere in my app, sometimes I need to redraw the whole screen.

I found that by only clearing the screen to black, and then updating, I
get a relatively high CPU usage(60-70%)!
here’s the drawing part of the game loop.
{
SDL_FillRect(screen, NULL, black);
SDL_Update(screen, 0, 0, 0, 0);
SDL_framelimiterDelay(&fpsManager); // the framerate limiter
}

so could someone help me identify the solution?
please don’t tell me to use OpenGL, I already know OpenGL, and I know
it’ll solve the problem, but it’s not suitable for my app because of
some requirements.

Info:
Desktop configuration: 102476832
OS: Windows XP
RAM: 512 MB
VGA Card: RIVA TNT2 32 MB.

SDL Video Mode: Windowed 64048024(I’ve tried also 32 bpp), I use
SWSURFACE coz I have a fair amount of alpha blending.
SDL_VideoInfo:
Is it possible to create hardware surfaces : 1
Is there a window manager available : 1
Are hardware to hardware blits accelerated : 1
Are hardware to hardware colorkey blits accelerated : 1
Are hardware to hardware alpha blits accelerated : 0
Are software to hardware blits accelerated : 1
Are software to hardware colorkey blits accelerated : 1
Are software to hardware alpha blits accelerated : 0
Are color fills accelerated : 1
Total amount of video memory in Kilobytes : 28688
Driver Name: directx

sorry for the long post and thanks for your effort.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I’ve been using SDL for 2 weeks, so I’m somekind of a newbie with SDL.
everything went fine until I noticed that my app uses about 80~90% (too
much!) of the CPU, although I’m using a framerate limiter to limit the
fps to 30.

I found that by only clearing the screen to black, and then updating, I
get a relatively high CPU usage(60-70%)!
here’s the drawing part of the game loop.
{
SDL_FillRect(screen, NULL, black);
SDL_Update(screen, 0, 0, 0, 0);
SDL_framelimiterDelay(&fpsManager); // the framerate limiter
}

I’m working on a game now and delt with about the same problem. My
solution first limits the frame length to 16ms using SDL_GetTicks(),
SDL_Delay(), and some math. I don’t know if your framerate limiter is
implemented in a similar manner.

I also made a fairly generalized rendering system on top of SDL that
figures out what rectangular regions actually need to be redrawn and then
just draws those. The game I’m making usually doesn’t have to draw a whole
frame every frame, so it works very well for me. I hope it’ll allow the
game to run on older hardware, but I haven’t tested on an older machine
yet.

I’m also using 640x480 resolution without hardware acceleration. When my
game does render the whole frame every frame with a background graphic
(loaded from a JPEG file), it uses about 25% of the CPU’s time. I’ve got
an Nvidia GeForce FX 5200, so it might be quicker than your setup even
without much help from the GPU. Also have a 2GHz Sempron.On Thu, 28 Jul 2005, Diaa Sami wrote:


Jeff Jackowski
http://ro.com/~jeffj/

2005/7/28, Diaa Sami :

I’ve been using SDL for 2 weeks, so I’m somekind of a newbie with SDL.
everything went fine until I noticed that my app uses about 80~90% (too
much!) of the CPU, although I’m using a framerate limiter to limit the
fps to 30.
SDL_FillRect(screen, NULL, black);
SDL_Update(screen, 0, 0, 0, 0);
SDL_framelimiterDelay(&fpsManager);

Just add a SDL_Delay() in your game loop… this will free the CPU for a little.
(but a frame-rate limiter should already do this)–
SkunkGuru.

well

first, I’m using the framerate limiter that comes with SDL_gfx library,
and it uses SDL_Delay(), so it’s not a busy wait loop, so this is not
the cause of high CPU usage.

second, I care about it because on my PC, it takes considerably longer
for any app to start while the game is running and eating more than 50%
of the CPU usage(also the point that Jon Pimble pointed out is important)

third, I use Windows XP’s task manager along with sysinternals Process
Explorer to monitor the CPU Usage, and I guess these tool are good enough.

there’s something I wanted to mention, while experimenting, I tried
disabling the framerate limiter, of course the CPU usage increased, but
the framerate didn’t increase much, this shows why the CPU usage is high
even though the framelimiter is on.

let me explain more:
here are the result of my experimentation.

first, turn on the FPS limiter, the CPU usage is high, something like
70%-80% and the FPS is constant ~25 fps.
second, turn off the FPS limiter, the CPU usage is higher > 90% and the
FPS is ~30-35 fps.

so the FPS limiter saved the time for drawing 5-10 frames, which is ~15%
of the CPU usage, so the problem is that the FPS(with the limiter off)
is low in the first place, so using the limiter doesn’t save much.

note: I forgot to mention in my previous post that my CPU is Athlon XP
2600+.

Given those numbers, I would profile your code to see where the
bottleneck is. It does not appear to be a drawing issue per say. If
the slowdown is in your code, optimize it to the best of your ability,
if it’s in SDL, try out glSDL, it could speed things up for you.

Just my 2 cents.

Richard

Diaa Sami wrote:> well

first, I’m using the framerate limiter that comes with SDL_gfx
library, and it uses SDL_Delay(), so it’s not a busy wait loop, so
this is not the cause of high CPU usage.

second, I care about it because on my PC, it takes considerably longer
for any app to start while the game is running and eating more than
50% of the CPU usage(also the point that Jon Pimble pointed out is
important)

third, I use Windows XP’s task manager along with sysinternals Process
Explorer to monitor the CPU Usage, and I guess these tool are good
enough.

there’s something I wanted to mention, while experimenting, I tried
disabling the framerate limiter, of course the CPU usage increased,
but the framerate didn’t increase much, this shows why the CPU usage
is high even though the framelimiter is on.

let me explain more:
here are the result of my experimentation.

first, turn on the FPS limiter, the CPU usage is high, something like
70%-80% and the FPS is constant ~25 fps.
second, turn off the FPS limiter, the CPU usage is higher > 90% and
the FPS is ~30-35 fps.

so the FPS limiter saved the time for drawing 5-10 frames, which is
~15% of the CPU usage, so the problem is that the FPS(with the limiter
off) is low in the first place, so using the limiter doesn’t save much.

note: I forgot to mention in my previous post that my CPU is Athlon XP
2600+.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I’m no expert, but I would assume that means you’re just doing a lot
of processor intensive drawing, redrawing the whole scene each time
may well be part of it. What sort of things are going on each frame
in your game?

–ScottOn 29 Jul, 2005, at 12:56 PM, Diaa Sami wrote:

well

first, I’m using the framerate limiter that comes with SDL_gfx
library, and it uses SDL_Delay(), so it’s not a busy wait loop, so
this is not the cause of high CPU usage.

second, I care about it because on my PC, it takes considerably
longer for any app to start while the game is running and eating
more than 50% of the CPU usage(also the point that Jon Pimble
pointed out is important)

third, I use Windows XP’s task manager along with sysinternals
Process Explorer to monitor the CPU Usage, and I guess these tool
are good enough.

there’s something I wanted to mention, while experimenting, I tried
disabling the framerate limiter, of course the CPU usage increased,
but the framerate didn’t increase much, this shows why the CPU
usage is high even though the framelimiter is on.

let me explain more:
here are the result of my experimentation.

first, turn on the FPS limiter, the CPU usage is high, something
like 70%-80% and the FPS is constant ~25 fps.
second, turn off the FPS limiter, the CPU usage is higher > 90% and
the FPS is ~30-35 fps.

so the FPS limiter saved the time for drawing 5-10 frames, which is
~15% of the CPU usage, so the problem is that the FPS(with the
limiter off) is low in the first place, so using the limiter
doesn’t save much.

note: I forgot to mention in my previous post that my CPU is Athlon
XP 2600+.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I’m drawing transparent images over 70% of the screen.
but the FPS is low even without drawing(and without the limiter, of course)

when I draw nothing to the screen and turn off the fps limiter, I get
around 95 fps.
the code:

// SDL setup
screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE);

// drawing loop
SDL_FillRect(screen, NULL, black);
SDL_UpdateRect(screen, 0, 0, 0, 0);

if this is normal, then I’ll have to use SDL_UpdateRects to speed up the
drawing, otherwise please tell me what’s wrong with my SDL configuration.

what are the number is you change to SDL_SWSURFACE?

/OlofOn 7/30/05, Diaa Sami wrote:

I’m drawing transparent images over 70% of the screen.
but the FPS is low even without drawing(and without the limiter, of course)

when I draw nothing to the screen and turn off the fps limiter, I get
around 95 fps.
the code:

    // SDL setup
    screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE);

    // drawing loop
    SDL_FillRect(screen, NULL, black);
    SDL_UpdateRect(screen, 0, 0, 0, 0);

if this is normal, then I’ll have to use SDL_UpdateRects to speed up the
drawing, otherwise please tell me what’s wrong with my SDL configuration.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Diaa Sami wrote:

I’m drawing transparent images over 70% of the screen.
but the FPS is low even without drawing(and without the limiter, of
course)

when I draw nothing to the screen and turn off the fps limiter, I get
around 95 fps.
the code:

// SDL setup
screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE);

// drawing loop
SDL_FillRect(screen, NULL, black);
SDL_UpdateRect(screen, 0, 0, 0, 0);

if this is normal, then I’ll have to use SDL_UpdateRects to speed up
the drawing, otherwise please tell me what’s wrong with my SDL
configuration.

SDL_UpdateRect(screen, 0, 0, 0, 0); means updateing the whole screen,
you know. if the screen is not doublebuffered and/or not a hwsurface,
this means copying the whole surface each frame. comment it and see
what it does to your framrate.

clemens

Diaa Sami wrote:

I’m drawing transparent images over 70% of the screen.
but the FPS is low even without drawing(and without the limiter, of
course)

when I draw nothing to the screen and turn off the fps limiter, I get
around 95 fps.
the code:

  // SDL setup
  screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE);

  // drawing loop
  SDL_FillRect(screen, NULL, black);
  SDL_UpdateRect(screen, 0, 0, 0, 0);

if this is normal, then I’ll have to use SDL_UpdateRects to speed up
the drawing, otherwise please tell me what’s wrong with my SDL
configuration.

SDL_UpdateRect(screen, 0, 0, 0, 0); means updateing the whole screen,
you know. if the screen is not doublebuffered and/or not a hwsurface,
this means copying the whole surface each frame. comment it and see

is that entirely true?

a doublebuffered and/or hwsurface will only “flip pointers” with a
call to SDL_Flip(), no? will it also flip pointers with
SDL_UpdateRect(screen, 0, 0, 0, 0)?On 7/30/05, Clemens Kirchgatterer <clemens at 1541.org> wrote:

what it does to your framrate.

clemens


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Olof Bjarnason <olof.bjarnason at gmail.com> wrote:

a doublebuffered and/or hwsurface will only “flip pointers” with a
call to SDL_Flip(), no? will it also flip pointers with
SDL_UpdateRect(screen, 0, 0, 0, 0)?

AFAIK, SDL_UpdateRect(screen, 0, 0, 0, 0) and SDL_Flip(screen) are
interchangable. They each do the right_thing depending on the surface
type of the screen.

clemens

Olof Bjarnason <@Olof_Bjarnason> wrote:

a doublebuffered and/or hwsurface will only “flip pointers” with a
call to SDL_Flip(), no? will it also flip pointers with
SDL_UpdateRect(screen, 0, 0, 0, 0)?

AFAIK, SDL_UpdateRect(screen, 0, 0, 0, 0) and SDL_Flip(screen) are
interchangable. They each do the right_thing depending on the surface
type of the screen.

If that is the case, someone might wanna update this doc wiki page:

http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fUpdateRect

/OlofOn 7/31/05, Clemens Kirchgatterer <clemens at 1541.org> wrote:

clemens

Hello,

I’ve been lurking on this list about 3 weeks now because of this
exact issue: High CPU usage on SDL apps that aren’t doing anything
that should cause high CPU usage.

Why does it even matter how much CPU you’re using? Is it making your
computer slower? Is the game running unsatisfactorily slow? If not,
then your CPU% is fine.

SDL’s really awesome, but it’s event architecture causes it to
use a LOT of CPU. Responses like "Why does it even matter"
are very discouraging. It matters because SDL doesn’t have to
use a lot of resources, and because a single game running on your
PC is not the only thing SDL is good for. Try running 3 or 4
SDL apps at the same time. Suddenly none of them perform well.

Possibly, the framerate limiter does a busy-wait loop instead of
sleeping – this is fine, but that would account for the much higher
CPU%.

This is exactly the case for event loops. But it’s not “fine”.
These tight loops turn a really fast CPU into a really slow toaster
for no reason.

That is simply not true. Event loops do not add overhead, and they are
required to interface with operating systems, such as Windows, where
interactive applications are required to read and process an event loop.

It is true that many people seem to have trouble understanding how to
write efficient programs in an event loop environment. I have seen
literally hundreds of examples of people updating the screen on each
event. That is especially true, for some reason, with mouse events.

But, seriously, event loops do not add overhead. They are not polling
loops.

	Bob PendletonOn Thu, 2005-07-28 at 17:49 -0700, Jonathan Stark wrote:

If you’re in a multi-process or multi-user environment, it’s not
ok to burn 40% of your CPU waiting for input from the user, or
throttling redraws… It also prevents laptops from calling idle
loops which slow their clocks down.

It is a real problem. Anybody got any thoughts on a real solution?

Jonathan


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


±-------------------------------------+

Hello,

I’ve been lurking on this list about 3 weeks now because of this
exact issue: High CPU usage on SDL apps that aren’t doing anything
that should cause high CPU usage.

BTW: using a single buffered hardware surface is a bad idea. When you
call update rects you are likely to see the CPU copy the hardware buffer
to the visible buffer by passing it through main memory. That really
kills your performance. Not to mention that most of the operations you
can do to a 2D hardware buffer are not hardware accelerated and so they
must be performed in software across the graphics bus. Another serious
performance killer.

Use a software buffer or use OpenGL.

	Bob PendletonOn Thu, 2005-07-28 at 17:49 -0700, Jonathan Stark wrote:


±-------------------------------------+