usleep not respected on rhel5

Solution Unverified - Updated -

Issue

Simple test:

#include <stdio.h>
#include <time.h>
#include <unistd.h>

#define NS_PER_SECOND 1000000000LL
#define TIMESPEC_TO_NS( aTime ) ( ( NS_PER_SECOND * ( ( long long int ) aTime.tv_sec ) ) + aTime.tv_nsec )

int main()
{

 struct timespec lLastTime;
        struct timespec lCurrentTime;
        clock_gettime( CLOCK_REALTIME, &lLastTime );

   int i;
   int nr=1000;
   for (i = 0; i < nr; i++)
    {

        usleep(500);
    }


        clock_gettime( CLOCK_REALTIME, &lCurrentTime );
        long long int lDifference = ( TIMESPEC_TO_NS( lCurrentTime ) - TIMESPEC_TO_NS( lLastTime ) );
        printf( "Waited (usecs): %lld\n", lDifference  / 1000 / nr  );

    return 0;
}

compiled as

# gcc -o usleep2 usleep2.c -lrt

the results are quite different in rhel5 and rhel6

on rhel5 (clock source jiffie)

linxdev20,/home/grucher/malloc # ./usleep2
Waited (usecs): 999
linxdev20,/home/grucher/malloc # ./usleep2
Waited (usecs): 1000
linxdev20,/home/grucher/malloc # ./usleep2
Waited (usecs): 1988
linxdev20,/home/grucher/malloc # ./usleep2
Waited (usecs): 2000
linxdev20,/home/grucher/malloc # ./usleep2
Waited (usecs): 1497

on rhel6 (nohz=0 and tsc)

ny380g8l,/root # ./usleep2
Waited (usecs): 550
ny380g8l,/root # ./usleep2
Waited (usecs): 550
ny380g8l,/root # ./usleep2
Waited (usecs): 550
ny380g8l,/root # ./usleep2
Waited (usecs): 550
ny380g8l,/root # ./usleep2
Waited (usecs): 550

We think the difference can be related to the tick time.

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.