Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » High CPU usage when flipping backbuffers on OpenGL display

This thread is locked; no one can reply to it. rss feed Print
High CPU usage when flipping backbuffers on OpenGL display
titonbrujah
Member #11,156
July 2009
avatar

Hey guys... I noticed some weird CPU usage while developing a project using an opengl window in allegro 5.0.5

Basically, the program was using ~15% cpu time (a core on some 60% usage since both machines I tested were quad-cores)

I removed all calls to the rest of my program and compiled this source code:

#SelectExpand
1#include <iostream> 2#include <allegro5/allegro.h> 3#include <memory> 4//#include "Game.h" 5#include <allegro5/allegro_primitives.h> 6#include <allegro5/allegro_font.h> 7#include <allegro5/allegro_ttf.h> 8#include <allegro5/allegro_image.h> 9 10const int FPS = 60; 11 12bool initializeAllegro(ALLEGRO_DISPLAY*& display, ALLEGRO_EVENT_QUEUE*& queue, ALLEGRO_TIMER*& timer) { 13 if (!al_init()) return false; 14 if(!al_init_image_addon()) { 15 std::cout << "Error initializing image addon!\n"; 16 return false; 17 } 18 if (!al_install_keyboard()){ 19 std::cout << "Error installing keyboard!\n"; 20 return false; 21 } 22 if (!al_install_mouse()){ 23 std::cout << "Error installing mouse!\n"; 24 return false; 25 } 26 al_set_new_display_flags(ALLEGRO_OPENGL); 27 //display = al_create_display(Game::WIDTH, Game::HEIGHT); 28 display = al_create_display(800, 800); 29 if (!display){ 30 std::cout << "Error creating display!\n"; 31 return false; 32 } 33 al_init_font_addon(); 34 if (!al_init_ttf_addon()){ 35 std::cout << "Error initializing TTF!\n"; 36 return false; 37 } 38 if (!al_init_primitives_addon()){ 39 std::cout << "Error initializing primitives!\n"; 40 return false; 41 } 42 queue = al_create_event_queue(); 43 if (!queue){ 44 std::cout << "Error creating queue!\n"; 45 return false; 46 } 47 al_register_event_source(queue, al_get_display_event_source(display)); 48 al_register_event_source(queue, al_get_keyboard_event_source()); 49// if (al_hide_mouse_cursor(display) && al_grab_mouse(display)) { 50// al_register_event_source(queue, al_get_mouse_event_source()); 51// } else al_show_mouse_cursor(display); 52 timer = al_create_timer(1.0/FPS); 53 if (!timer) { 54 std::cout << "Error creating timer!\n"; 55 return false; 56 } 57 al_start_timer(timer); 58 al_register_event_source(queue, al_get_timer_event_source(timer)); 59 { 60 ALLEGRO_PATH* p = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 61 al_set_path_filename(p, ""); 62 al_append_path_component(p, "data"); 63 al_make_path_canonical(p); 64// std::cout << al_path_cstr(p, '/'); 65 al_change_directory(al_path_cstr(p, '/')); 66 al_destroy_path(p); 67 } 68 return true; 69} 70 71int main(){ 72 ALLEGRO_DISPLAY* display = nullptr; 73 ALLEGRO_EVENT_QUEUE* queue = nullptr; 74 ALLEGRO_TIMER* timer = nullptr; 75 if (!initializeAllegro(display, queue, timer)){ 76 std::cout << "Error during allegro initialization!\n"; 77 al_rest(10); 78 return 1; 79 } 80// auto game = std::make_shared<Game>(); 81 bool cont = true; 82 bool redraw = true; 83 while (cont 84 //&& !game->isOver() 85 ){ 86 ALLEGRO_EVENT ev; 87 al_wait_for_event(queue, &ev); 88 const ALLEGRO_EVENT_TYPE type = ev.type; 89 switch(type){ 90 case ALLEGRO_EVENT_DISPLAY_CLOSE: 91 cont = false; 92 break; 93 case ALLEGRO_EVENT_TIMER: 94 //game->step(); 95 redraw = true; 96 break; 97 case ALLEGRO_EVENT_MOUSE_AXES: 98// al_set_mouse_xy(display, Game::WIDTH/2.0f, Game::HEIGHT/2.0f); 99 default: 100 //game->onEvent(ev); 101 break; 102 } 103 if (redraw && al_event_queue_is_empty(queue)){ 104 redraw = false; 105 //game->draw(); 106 al_flip_display(); 107 } 108 } 109 110 al_destroy_event_queue(queue); 111 al_destroy_timer(timer); 112 al_uninstall_keyboard(); 113 al_uninstall_mouse(); 114 al_destroy_display(display); 115 return 0; 116}

windows binary: http://www.allegro.cc/files/attachment/605652
allegro dll for convenience: http://www.allegro.cc/files/attachment/605653

Still uses ~15% cpu on both machines I tested (both quad-cores, one with a GeForce 9800 GTX and one with a 430). Tried updating the graphics drivers on the 430 and running the program for about 20 minutes, but nothing changed.

Either commenting out al_flip_display(); (line 106) or al_set_new_display_flags(ALLEGRO_OPENGL); (line 26) made cpu usage plummet down to 0%

<My Allegro projects>
[SH09: Xtreme Programming: The Literal Game (Bonus Level)] [SH11: Hungry Hare] [GGJ12: From Beyond]

jmasterx
Member #11,410
October 2009

I have always had this problem on my laptop for some reason. Have you tried toggling vsync on / off? That's what solved it on my laptop. Well, I still do not get nearly as low a cpu usage as with D3D, but it helped.

titonbrujah
Member #11,156
July 2009
avatar

jmasterx said:

I have always had this problem on my laptop for some reason. Have you tried toggling vsync on / off? That's what solved it on my laptop. Well, I still do not get nearly as low a cpu usage as with D3D, but it helped.

Indeed, adding al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_REQUIRE) (force vsync off) right before creating the display made cpu usage oscillate between 0% and 1% :D

Still, I think this is something worth looking into... It's probably just some code not yielding the cpu while waiting for the vsync, but the game was running a bit choppy before this and it's much better now

<My Allegro projects>
[SH09: Xtreme Programming: The Literal Game (Bonus Level)] [SH11: Hungry Hare] [GGJ12: From Beyond]

Thomas Fjellstrom
Member #476
June 2000
avatar

Allegro itself doesn't wait for vsync. It lets the video driver (or hardware) do it. So if there's a problem, its not something we can really fix.

--
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

Max Savenkov
Member #4,613
May 2004
avatar

I had that problem a while ago with GeForce 560 Ti on Windows 7. Had to disable VSync by default and implement frame-limit. No tearing (that I can see), and performance is great.

Go to: