Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » My CPU usage is absurd

This thread is locked; no one can reply to it. rss feed Print
 1   2 
My CPU usage is absurd
Gabriel Campos
Member #12,034
June 2010
avatar

I dont know how to solve this. My CPU goes to above 50% in only drawing a sprite and a background. :o:o:o
I looked and find the responsable for that...

in the line

void State_inGame::render()
{
    level.render(0); // Guilts :(
    level.render(1);

    for(std::vector<Entity *>::iterator it = v_entity.begin(); it!= v_entity.end(); it++)
    {
        Entity *e = *it;
        e->render();
    }
}

level.render(0);
level.render(1);
These are the responsable for that. If i comment these lines, no problems. So, why this two lines are making that? I am using mappy_a5.

This is the function level.render

void Level::render(int layer)
{
    MapChangeLayer(layer);
    MapDrawBG(0, 0, 0, 0, 1600, 608); EDIT: This is the responsable! Why? ???
}

Thanx

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thomas Fjellstrom
Member #476
June 2000
avatar

What version of Allegro? I suspect those functions are from Mappy.

Edgar's advice is for Allegro 5, and don't really apply to Allegro 4

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thomas Fjellstrom
Member #476
June 2000
avatar

He said he was using mappy_a5

You know what? Ignore that Moose guy. He's stupid.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Gabriel Campos
Member #12,034
June 2010
avatar

Still not working. =/

I have a character sprite (little sprite) and the tiles drawed by the mappy(allegro 5). Only this, so if even the bitmaps(png files by the way) loaded in the memory could not do so much to my CPU. The CPU gets a 60% usage.
I comment this line

MapDrawBg(0, 0, 0, 0, 1600, 608);

and voia la! No bg, only the sprite, BUT the CPU gets 1 or 2% usage. So, this is the problem. Could be a bug or my mistake loading the map? I saved in fmp format. Any ideias? :P

Thomas Fjellstrom
Member #476
June 2000
avatar

Make sure you stuff as many tiles and sprites into the same big texture as is possible.

Switching between textures has a lot of overhead.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Arthur Kalliokoski
Second in Command
February 2005
avatar

If you have an older card, and your texture isn't a power of two, it could be using a software path to render it.

They all watch too much MSNBC... they get ideas.

Audric
Member #907
January 2001

I'm surprised that you don't need to pass a target bitmap to MapDrawBG. So where does it draw? On the global 'screen' bitmap ? If this is the case, you get the very expensive copy of RAM to VRAM.

Thomas Fjellstrom
Member #476
June 2000
avatar

Audric said:

I'm surprised that you don't need to pass a target bitmap to MapDrawBG. So where does it draw? On the global 'screen' bitmap ? If this is the case, you get the very expensive copy of RAM to VRAM.

He's using A5, there's a per-thread "current target", so it draws all his loaded images to the current target.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Gabriel Campos
Member #12,034
June 2010
avatar

There is a better map tile editor for allegro 5? What do you guys uses?

Trent Gamblin
Member #261
April 2000
avatar

My guess is you're using D3D and drawing to a bitmap without the ALLEGRO_NO_PRESERVE_TEXTURE flag set. Or just plain using memory bitmaps.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

It entirely depends on what mapDrawBG is doing behind the scenes, and whether it is using memory bitmaps or not, and whether the tiles are on the same atlas, and so on....

So, again, could you let me know where you got mappyA5 from?
Edit- nevermind, I found Neil's thread.
https://www.allegro.cc/forums/thread/606962

Trent Gamblin
Member #261
April 2000
avatar

Even if he's not using atlases it's not going to use full cores unless it's an ancient system. It's a memory bitmap or no ALLEGRO_NO_PRESERVE_TEXTURE flag or else (unlikely) the Mappy code is really terrible.

piccolo
Member #3,163
January 2003
avatar

2013... What is your cpu spec?

wow
-------------------------------
i am who you are not am i

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You could try profiling your code after building it with debugging and profiling symbols. With MinGW it is -pg. Look it up for your compiler, and then run it through a profiler. You build it with profiling symbols, then run it, then analyze the output result file with a profiler like gprof and a.out.

mingw32-g++ -Wall -g -pg -o mygame-profiler.exe src1.cpp src2.cpp mappy.c -lallegro-5.1.7-monolith-md
mygame-profiler.exe
gprof a.out

And, I briefly looked at mappy_A5 and it does not use an atlas, it uses an array of ALLEGRO_BITMAP** abmTiles;, and so it does not use held bitmap drawing either. I think that was before we had the general knowledge of that around here. The dll mappy.exe wanted was allegro 5.0.2, and I dont' have that installed so the demo doesn't run for me. :P I might try recompiling the demo later.

mappy does however respect the new bitmap flags, so if its not set to ALLEGRO_VIDEO_BITMAP if won't be a video bitmap, but video is the default, so you would have had to have changed it. :?

type568
Member #8,381
March 2007
avatar

Nobody has mentioned usage of some sleep function. Perhaps you're looping it somewhere without a rest?

Chris Katko
Member #1,881
January 2002
avatar

Yeah, I was scrolling the whole thread waiting for someone to mention it.

50%? Sounds like half of a dual-core CPU.

Add a rest(0) to your main loop and see what happens. You can be doing nothing at all and you're still going to get maximum single threaded cpu utilization unless you add a rest.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Kris Asick
Member #1,424
July 2001

To anyone who's suggested that using rest(0) or Sleep(0) will reduce CPU usage, it will not!

At least, not on Windows.

The way Sleep() works on Windows (and subsequently Allegro's rest() function) is that, if you pass it a 0, it will only give up the present timeslice to other threads of equal priority if possible. If not, it burns it itself. :P

Pass anything higher than 0 and you could lose up to 50 ms of CPU time which could seriously curtail your framerate, so don't do that. ;)

Since I've never used Mappy however I have no idea what it's doing behind the scenes or what other problems could be happening. As a thought though, try forcing Allegro into OpenGL mode if you're not already, and if you are, try not forcing it into OpenGL mode. See if that makes a difference.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Elias
Member #358
May 2000

Pass anything higher than 0 and you could lose up to 50 ms of CPU time which could seriously curtail your framerate, so don't do that.

I'd say that was only true a very long time ago. All the precompiled Linux kernels I've encountered in the last 10 years or so would use a 1000Hz scheduler clock, so rest(1) would sleep 1ms. Under Windows that's the default even longer (starting with XP I'd say). And I'd be very surprised if OSX will sleep more than 1ms either.

So yes, someone using Windows 98 or a self-compiled Linux might get a 10ms wait (I don't ever used a system with a 20Hz timer so 50 ms seem very unlikely) - but the chance of encountering it will be very much zero :P

Also, for Allegro 5 that should not be relevant as al_wait_for_event already will make your program not use CPU except when needed.

--
"Either help out or stop whining" - Evert

Kris Asick
Member #1,424
July 2001

Elias said:

I'd say that was only true a very long time ago. All the precompiled Linux kernels I've encountered in the last 10 years or so would use a 1000Hz scheduler clock, so rest(1) would sleep 1ms. Under Windows that's the default even longer (starting with XP I'd say). And I'd be very surprised if OSX will sleep more than 1ms either.
So yes, someone using Windows 98 or a self-compiled Linux might get a 10ms wait (I don't ever used a system with a 20Hz timer so 50 ms seem very unlikely) - but the chance of encountering it will be very much zero

Don't be so sure of that. On my last Windows 98 system, I was getting a granularity of about 10 ms with Sleep(). On my last Windows XP system, with much newer and better hardware, I was getting a granularity closer to 50 ms with Sleep(). I have no idea what hardware/software/driver/etc. stuff affects this but because you can't depend on the scheduler granularity to be super-low, even with a powerful system, you could end up losing a lot more time than you would want under Windows.

Besides, even if the granularity was as low as 10 ms, that still tops out at 100 FPS when there's monitors out there that can do 120. :P

I dunno about Linux but I'll take your word for it that it's a fixed 1 ms granularity. ;)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Thomas Fjellstrom
Member #476
June 2000
avatar

When a scheduler timer is running (on modern kernels the regular periodic timer tick can and will be turned off when the system is idle) it is usually set to 1000hz on desktops/laptops. On servers you should probably set it to 100 or 250.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Gabriel Campos
Member #12,034
June 2010
avatar

I uses a notebook pentium dual core 2.4ghz, 3Mb memory...is not a good computer, but for sprites is a pretty machine. I stopped using mappy and start to use tiled that gives me a txt files. Make my class that stores that values in a vector and draw on the map. Still burns my cpu. :o

Maybe its because of for loop?
i have two for loops: one for the background map, and one to the level map. boths for make calculations. Maybe is this?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

 1   2 


Go to: