100% CPU usage

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Murphy
Posts: 290
Joined: Mon Dec 13, 2004 12:06 am
Location: United States
Contact:

Re: 100% CPU usage

Post by Murphy »

Midnight wrote:So the question is, how can i reduce the cpu usage?
Why do you want to reduce CPU usage? Take a look at Quake 3. It uses 100% CPU when it's not really doing anything. Most games do. This is a result of how their main loop works.

A really common "solution" to this "problem" is to put one or a few Sleep(0)s in your code. This gives up the remainder of your timeslice if there are other things scheduled to run. This gives other threads/processes the chance to run, resulting in smoother multi-tasking. If there are no other threads waiting to run, Sleep(0) doesn't have much effect. Either way, the CPU usage will stay at 100% -- your app will still eat as much as it can.

So what's the downside of having 100% CPU usage? Are you telling me that your processor isn't designed to run at 100%? ;)


The correct way to deal with this is to write something more complicated than the typical main loop (i.e. while (1) { doWaitingEvents(); renderFrame(); }). For example, have one thread run the render loop actually taking desired frames per second into consideration, while another thread waits on UI input. This method has downsides, though. For example, it often doesn't look as smooth under any circumstances and certainly not on machines that are only marginally powerful enough.
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

He wants it to not use 100% because its a GUI editing application not a game...
Murphy
Posts: 290
Joined: Mon Dec 13, 2004 12:06 am
Location: United States
Contact:

Post by Murphy »

Dances wrote:He wants it to not use 100% because its a GUI editing application not a game...
So?


But in that case -- where raw performance and rock-solid framerate aren't super-super important -- rewriting the main loop is a really viable solution.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

the reason is because I'm developing an editor not a full blown game for this example. I see no reason to use up the extra power and kick my pc into over driver just to create some simple GUI.

I am a multi tasker and I push my pc pretty hard with all the games and things I play.

there is no reason to have it use 100% except to wear out my fan bearings and listen to the oh so pleasent hum of my pc in overdrive over GUI :?

it's also desirable to use as little code as possible while still maintaining stability and proper function.. sleep seems to suit this project perfectly... why wouldn't I use it?
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

what value are you passing to sleep?
Image
MBA
Posts: 4
Joined: Tue Jul 26, 2005 9:46 am

Post by MBA »

To Murphy:
I won't argue why you would put a sleep in your mainloop, but in some cases it can be a necessity and in some cases its just bullocks to do it.
But splitting up the operations can be quite nice. For example I find it unacceptable to scan for user input with the same rate i refresh the screen.
I am aware of the ability to reschedule a thread with sleep(0) and yielding the cpu to other tasks - I often use the pthread yield() instead.
After your post i tested the scheduler latency on a standard linux box with the cpu timestamp counter and found it to be consistent with the common knowledge that it takes about 10ms for a taskswitch. But I got curious and did the same thing with a windowsbox: heres the test code, to those not familiar with the tsc - its a (pentium compatible) internal cpu register which increments every clockcycle fed to your processor.

Code: Select all

#include <iostream>
#include <windows.h>


int main()
{
    unsigned long long int t0, t1; //Need two 64 bit variables for the tsc
    double msec_time;
    __asm volatile("rdtsc" : "=A" (t0));
    Sleep(0);
    __asm volatile("rdtsc" : "=A" (t1));
    msec_time = (double)(t1-t0)/(1.8*1000000000.0); //1.8 ghz processor
    printf("actual time slept [msc] %f \n",msec_time);    
}
This test made me wonder if there are any taskswitch involved at all? The measurement went as low as a few usec's. Do you know the actual timing of the windows scheduler?

To Fred: an acceptible delay/sleep with regard to user input would be between 10ms to a maximum of 100ms - larger and the user would begin to notice a lag - and you probably wouldn't benefit from a lower value. If you are delaying your refresh of the screen you should ask some of the hardcore graphics guys here on the site i'm just a humble embedded system designer... with a cpu-cycle-usage psychosis and a no-need-to-waste those-little-bastards-it-will-soon-enough-be-all-hogged-by-those-crazy- application-programmers mental disorder :-)
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

GUYS YOU ARE funking CRAZY.

sleep(10);

Did the trick 2 days ago!!!

What the gently caress is that strange comment at the bottom of your post MBA??

Thats the wierdest thing I've ever seen.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Spintz wrote:what value are you passing to sleep?
WTF??
DrAnonymous
Posts: 34
Joined: Thu Aug 04, 2005 9:37 pm

Post by DrAnonymous »

Mid - Why are you swearing and acting all nutty? People are trying to give you more info about how Sleep() affects a system. Sleeping and yielding threads are not difficult concepts, but understanding the larger picture of when to do things is another matter.

I'd like to have a way of telling the irrlicht video device driver to lock my framerate at 30/60 or what ever number I choose. I'm going to be using it on a system that already wants 100% of the CPU. I only need a max of 60fps and can give up the processor for the rest of the timel.

My 2 cents-
Dr. A>
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Acting all nutty?

I didn't realise text could convey my actions.

And here all this time I thought I was only showing thought.

Help is great I love help now tell me how the comments outlined were in anyway even intended to be helpful and not just out of there ASS.

swearing well thats just a form of expression get used to it.
Guest

Post by Guest »

Really interesting discussion :wink: . I think another way to reduce irrlicht's framerate (and CPU usage) is to turn on vertical sync. It will slow down irrlicht to your monitor's refresh rate (60-80 fps). It can be turned on in function createDevice(...).
Post Reply