Page 1 of 1

Idle loop consumes (too) much CPU

Posted: Thu Dec 19, 2013 4:06 pm
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

Re: Idle loop consumes (too) much CPU

Posted: Thu Dec 19, 2013 5:17 pm
by netmaestro
What does it do to your cpu load if you increase from 1 ms to say, 3?

Re: Idle loop consumes (too) much CPU

Posted: Thu Dec 19, 2013 5:45 pm
by Fred
you should use a semaphore to have a perfect wait

Re: Idle loop consumes (too) much CPU

Posted: Tue Jan 07, 2014 12:07 pm
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.

Re: Idle loop consumes (too) much CPU

Posted: Tue Jan 07, 2014 12:15 pm
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?