Bug 139324 - A simple timer implementation cause high CPU usage periodically
A simple timer implementation cause high CPU usage periodically
Product: Red Hat Enterprise Linux 3
Classification: Red Hat
Component: kernel (Show other bugs)
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Ingo Molnar
Brian Brock
Depends On:
  Show dependency treegraph
Reported: 2004-11-15 02:18 EST by Willy Gao
Modified: 2007-11-30 17:07 EST (History)
2 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2005-09-15 14:58:56 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
sample source(c) (903 bytes, text/plain)
2004-11-15 03:31 EST, Willy Gao
no flags Details

  None (edit)
Description Willy Gao 2004-11-15 02:18:34 EST
Description of problem:
We use a 10 msec non-stop timer in our program. The timer is 
implemented by pthread_cond_signal()/pthread_cond_wait()/select().
Nothing is done in the timer's callback, so the CPU usage is 
generally 0% according to top. But every 2 or 3 minutes, the CPU 
usage will rise to 10% and the state will remain 2-3 seconds. If five 
or more such programs are run at the same time, the idle time of one 
CPU can fall to 0% and the system performance will become busy for 
the mement.

following is the sample program:
#include <pthread.h>
#include <sys/time.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
unsigned int interval = 10000;
struct timeval tv;

void thread_func1(void *ptr)
    while (1) {
        pthread_cond_wait(&cond, &mutex);

void thread_func2(void *ptr)
    while (1) {
        tv.tv_sec = 0;
        tv.tv_usec = interval;
        select(0, 0, 0, 0, &tv);

int main()
    pthread_t thread1, thread2;
    pthread_create(&thread1, 0, (void *(*)(void *))&thread_func1, 0);
    pthread_create(&thread2, 0, (void *(*)(void *))&thread_func2, 0);
    pthread_join(thread1, 0);
    pthread_join(thread2, 0);

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. compile the sample program
2. monitor CPU status with top, set update interval to 1sec
3. start five sample programs
4. watch the CPU usage & idle time  

Actual results:
Every 2 or 3 minutes, the CPU usage of each process will rise to 10% 
percent and idle time of one CPU will fall to 0%.
(the state will remain 2-3 seconds.)

Expected results:
The CPU usage should be 0% always.

Additional info:
Our test box:
CPU: two Intel(R) Xeon(TM) CPU 3.06GHz with HyperThread
kernel: 2.4.21-15.0.3.ELsmp
(We cannot reproduct this problem if we use non-SMP kernel.)
gcc 3.2.3 20030502 (Red Hat Linux 3.2.3-20)

before this issue, we have encounted another issue related. When we 
use two non-stop timers(the same implemenation) at the same time, 
both of them will become dead after a certain time(from serveral 
seconds to one minute). The issue was identified to be a OS defect, 
particular to the NPTL implementation and is fixed in RHBA-2004:143 
for AS3.
Comment 1 Willy Gao 2004-11-15 03:31:38 EST
Created attachment 106698 [details]
sample source(c)
Comment 2 Jakub Jelinek 2004-11-15 12:13:22 EST
I believe this is just about kernel process time accounting.
Kernel samples at 100Hz, so if you are unlucky enough (and the interval you
are using in the test program is exactly 10msec) it can be seen as busy
during several samplings even if the process most of the remaining 10msec sleeps.
You can run oprofile or some other tool to see that the program really is not
taking too much CPU.
Comment 3 Willy Gao 2005-03-08 07:08:14 EST
We found it can be reproduced even by using a nanosleep or select in a forever 
loop, and even the interval is not 10ms. 

#include <sys/select.h>
int main()
    while (1) {
        struct timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 50000;
        select(0, 0, 0, 0, &tv);

Since the process is idle in most of the time, opofile also shows a reasonable 
number. But actually the system becomes busy when a lot of such processes 
started, for example the GUI operation. 

And one more strange thing is that even lots of such processes are started at 
different time, all processes's CPU usage rises at the same time.

Actually it seems something related with the kernel sampling time but we can 
not see the problem on any AS2 machines (and SuSE9).
Comment 4 Ingo Molnar 2005-09-15 14:58:56 EDT
all timers (even nanosleep) have a basic granularity of 10 msecs - so purely
timer-driven workloads might be under or over-sampled by the CPU utilization
measurement code. This is standard Linux behavior.

Note You need to log in before you can comment on or make changes to this bug.