Using high efficiency nanolayer?

I noticed that a small test program that calls nanosleep shows a huge difference in CPU usage when running on Linux machines with a kernel newer than 2.6.22.

#include <time.h> int main (void) { struct timespec sleepTime; struct timespec returnTime; sleepTime.tv_sec = 0; sleepTime.tv_nsec = 1000; while (1) { nanosleep(&sleepTime, &returnTime); } return 0; } 

(Yes, I understand that this program does nothing)

If I compile this and run it on an openSUSE 10.3 machine (2.6.22.19-0.2 by default), the program will not even appear in the list of processes generated "from above", indicating to me that it uses very little processor time. If I ran it on an openSUSE 11.1 machine (2.6.27.23-0.1-default), the top one shows that the program takes 40% of the processor time. Running on Fedora 9 (2.6.25-14.fc9.i686) and Fedora 10 also showed the same high CPU utilization at the top.

Was there a change in the kernel that affects this?

+5
source share
2 answers

This is due to the introduction of NO_HZ in the main line scheduler.

Previously, your sleep of 1000 ns usually slept for a solid tick - 1,000,000 ns. Now that the car is otherwise inactive, it is actually just a dream for what you requested. Thus, it runs the while () and syscall loops about 1000 times more often - hence much more CPU usage. If you increase tv_nsec, you should see a reduction in CPU usage.

+18
source

I do not have a final answer ... but the first thing I would like to see is the configuration parameters with which the kernel was compiled:

 cat /boot/config-`uname -r` 

Parameters that, in my opinion, may be relevant are CONFIG_HZ , CONFIG_HPET_TIMER and CONFIG_HIGH_RES_TIMERS . Perhaps they differ between your kernels ... this can help you narrow it down.

It used to be on 2.4 kernels that would wait, waiting less than 2 ms, if they are executed in accordance with the real-time scheduler policies ( SCHED_FIFO or SCHED_RR , see nanosleep man page ), but since all the kernels are 2.6, this is not seems like a factor.

+2
source

All Articles