100% CPU usage - config.prc - client-sleep - c++

Return to C++ coding using Panda3D

100% CPU usage - config.prc - client-sleep - c++

Postby atari314 » Mon May 14, 2012 10:38 pm

Hey guys,

I'm stuck with a very weird problem that I just can't figure out.

When the Panda's render window loses focus (or get minimized) the CPU usage for that process jumps to 100% (while it's on focus, everything is perfect).

At the #panda3d channel, ThomasEgi suggested me to use the client-sleep (http://www.panda3d.org/manual/index.php/List_of_All_Config_Variables) on the Config.prc file. That was also pointed out by rdb here (http://www.panda3d.org/forums/viewtopic.php?t=10774) and discussed here (http://www.panda3d.org/forums/viewtopic.php?t=12744).

I assembled an absolute minimal test for it below. But it just don't work. When the window loses the focus the CPU usage jumps to 100%.

Code: Select all
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "load_prc_file.h"

PandaFramework myFramework;
WindowFramework *myWindow;

int main(int argc, char *argv[]) {

  load_prc_file_data("","client-sleep 0.01");

  myFramework.open_framework(argc, argv);
  myWindow = myFramework.open_window();
  myFramework.main_loop();
  myFramework.close_framework();

  return (0);
}


Can anyone spot anything there?
Anyone having the same issue?

Tyvm for any help or guidance.



EDIT: Solution: http://www.panda3d.org/forums/viewtopic.php?p=84916#84916
Last edited by atari314 on Tue May 15, 2012 8:01 pm, edited 1 time in total.
atari314
 
Posts: 47
Joined: Sun Mar 13, 2011 11:35 am

Postby atari314 » Mon May 14, 2012 11:03 pm

Found the answer:

"There is Python code in ShowBase.py to automatically reduce the CPU consumption when the Panda window is minimized, but this code doesn't exist on the C++ side so you'll have to add it yourself. Fortunately this is easy, and not platform-specific: you just need to listen for the window-event and check the WindowProperties for foreground and/or minimized properties to determine if your window has the focus or if it is minimized.

To reduce the CPU consumption, you only need to sleep a little bit in a task, like 0.1 seconds or so.

David" *

* http://www.panda3d.org/forums/viewtopic.php?t=9647
atari314
 
Posts: 47
Joined: Sun Mar 13, 2011 11:35 am

Postby atari314 » Tue May 15, 2012 7:59 pm

Complete solution:
Code: Select all
#include "pandaFramework.h"
#include "pandaSystem.h"

#include "genericAsyncTask.h"
#include "asyncTaskManager.h"

PandaFramework myFramework;
WindowFramework *myWindow;

PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr();

AsyncTask::DoneStatus windowStatus(GenericAsyncTask* task, void* data) {
  WindowProperties myProps = myWindow->get_graphics_window()->get_properties();
  if(myProps.get_minimized()==1) {
   Thread::sleep(0.01);
  }
  return AsyncTask::DS_cont;
};

int main(int argc, char *argv[]) {

  myFramework.open_framework(argc, argv);
  myWindow = myFramework.open_window();

  taskMgr -> add(new GenericAsyncTask("window status", &windowStatus, NULL));

  myFramework.main_loop();
  myFramework.close_framework();

  return (0);
}


Compiled with:
Code: Select all
g++ -c test.cpp -o test.o -fPIC -O2 -I/usr/include/python2.7/ -I/usr/include/panda3d/ && g++ test.o -o test -fPIC -L/usr/lib/panda3d/ -lp3framework -lpanda -lpandafx -lpandaexpress -lp3dtoolconfig -lp3dtool -lp3pystub -lp3direct
atari314
 
Posts: 47
Joined: Sun Mar 13, 2011 11:35 am

Postby vono » Wed Jul 11, 2012 2:42 pm

Cool, thank you for your investigation :)

Now, your code need to work when we multithread!
For example, when setting in config.prc:
threading-model Cull/Draw

:wink:
vono
 
Posts: 4
Joined: Wed Jul 11, 2012 4:17 am


Return to C++ coding using Panda3D

Who is online

Users browsing this forum: No registered users and 1 guest