Idle loop consumes (too) much CPU

Linux specific forum
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

Idle loop consumes (too) much CPU

Post by auser »

Following simulates 100 client-threads that are doing nothing exept to sleep for one ms each loop. I've two virtual machines running on the same host with same amount of assigned CPUs (one is WinXP 32bit one is Debian Wheezy 64bit). Same code on WinXP consumes most time 0% CPU while it consumes about 12%-13% on Linux (both time debugger is off). If I increase to 256 threads it already consumes about 34% on Linux while it's still most time 0% on windows (sometimes it's short time up to 2-5% ... which is still way less than 34% and grows down to 0% soon again). I read something regading some change in newer kernels so some nanosleep() or usleep() sleeps now way shorter than before (and it seems some apps was built with pretty tiny delays so the loops looped much more then previously). However the granularity of Delay() is only in Milliseconds anyway (so no nanoseconds or microseconds)? Any thoughts on this?

Code: Select all

Procedure idle(*void)
  Repeat
    Delay(1)
  ForEver
EndProcedure


For x = 1 To 100
  CreateThread(@idle(),0)
Next 


Repeat
  Delay(1)
ForEver
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Idle loop consumes (too) much CPU

Post by netmaestro »

What does it do to your cpu load if you increase from 1 ms to say, 3?
BERESHEIT
Fred
Administrator
Administrator
Posts: 18253
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Idle loop consumes (too) much CPU

Post by Fred »

you should use a semaphore to have a perfect wait
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

Re: Idle loop consumes (too) much CPU

Post by auser »

netmaestro wrote:What does it do to your cpu load if you increase from 1 ms to say, 3?
Depending on the testmachine it's between 4%-10% with 3msecs which is still too high.
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

Re: Idle loop consumes (too) much CPU

Post by auser »

Fred wrote:you should use a semaphore to have a perfect wait
Perfect wait sounds good. I've a few ideas how a semaphore could help wait but that would deny nonblocking loops (e.g. usage of "Trysemaphore" or nonblocking sockets) isn't it?
Post Reply