2008-11-19 12:03:04     usleep issue

Document created by Aaronwu Employee on Aug 9, 2013
Version 1Show Document
  • View in full screen mode

2008-11-19 12:03:04     usleep issue

Linux Newbie (INDIA)

Message: 65553   

 

HI,

 

We are using 2007R1 distribution for BF533. I have kept the tick period as 1ms.

 

In the main, I just put a small piece of code, which prints value for every 10 seconds. This is done using usleep of 1000.

 

The value is not printing exactly at 10secs interval, instead it is taking 20secs.

 

Below is the piece of code. Can anyone suggest is there any  way to get millisecond accuracy in user space using built in functions?

 

while(1)

        {

                printf("calculate timer starts\n");

 

                gTenSec = 10000;

                gCount= 0;

                while(gTenSec)

                {

 

                        gCount++;

                        usleep(1000);

                        gTenSec = gTenSec - 1;

                }

                printf("After 10 secs Count %d\n", gCount);

 

                printf("calculate timer ends\n\n");

        }

 

 

Thanks in Advance

QuoteReplyEditDelete

 

 

2008-11-19 21:39:55     Re: usleep issue

Yi Li (CHINA)

Message: 65566   

 

This test case is invalid. There are multiple processes running.

 

-Yi

QuoteReplyEditDelete

 

 

2008-11-20 02:08:37     Re: usleep issue

Linux Newbie (INDIA)

Message: 65572   

 

PID  Uid        VSZ Stat Command

    1 root        682 S   /sbin/init

    2 root            SWN [ksoftirqd/0]

    3 root            SW  [watchdog/0]

    4 root            SW< [events/0]

    5 root            SW< [khelper]

    6 root            SW< [kthread]

   18 root            SW< [kblockd/0]

   30 root            SW  [pdflush]

   31 root            SW  [pdflush]

   32 root            SW< [kswapd0]

   33 root            SW< [aio/0]

   44 root            SW  [mtdblockd]

   68 root        550 S   inetd

   78 root        822 S   /bin/sh

 

The above were the process Id's apart from our test application.

 

It is also observed that if I give usleep(900) in the above code, instead of usleep(1000), then we can get prints exactly for every 10secs.

QuoteReplyEditDelete

 

 

2008-11-20 03:00:12     Re: usleep issue

Yi Li (CHINA)

Message: 65576   

 

In your test, you cannot assume usleep(1000) * 10000 will be finished in exactly 10 seconds. What do you want to test?

 

-Yi

QuoteReplyEditDelete

 

 

2008-11-20 07:01:49     Re: usleep issue

Linux Newbie (INDIA)

Message: 65589   

 

you cannot assume usleep(1000) * 10000 will be finished in exactly 10 seconds

 

Can you please explain why our assumption is wrong?

QuoteReplyEditDelete

 

 

2008-11-20 08:53:46     Re: usleep issue

Ian Jeffray (UNITED KINGDOM)

Message: 65605   

 

It does not take 0 milliseconds for your loop processing code to run, so add that time, times 10000 loops

 

It does not take 0 milliseconds for Linux to schedule in your process, so add that time, times 10000 loops

 

Moreover,  check out the man page for usleep, especially the bit in braces:

 

DESCRIPTION

       The  usleep()  function  suspends  execution  of  the calling process for (at least) usec microseconds.  The sleep may be

       lengthened slightly by any system activity or by the time spent processing the call  or  by  the  granularity  of  system

       timers.

 

It sleeps for _at least_ that many microseconds.  Add a percentage extra, times your 10000 loops and you'll see why your overall elapsed time is greater than the sum of individual sleep requests.

 

 

QuoteReplyEditDelete

 

 

2008-11-21 02:03:44     Re: usleep issue

Linux Newbie (INDIA)

Message: 65648   

 

Hi,

 

I do agree that we cannot expect the accuracy for 1ms(as our tick also 1ms). But in the loop, total deviation comes to 20secs.(i.e.double then expected).

 

Also as mentioned there is not much background process nor ISR's(only BF timer ticks).

 

Hence in this scenario, having 20secs time delay is very high.

QuoteReplyEditDelete

 

 

2008-11-21 04:15:12     Re: usleep issue

Yi Li (CHINA)

Message: 65650   

 

I think a proper way to test whether usleep() work as expect would looks like:

 

gettimeofday(t0);

 

usleep(1000000);

 

gettimeofday(t1);

 

Then calculate (t1 - t0).

 

In your test, if there is1000 us (1ms) latency (comes from scheduler, system call, etc) for each usleep(1000), you will finally get 1000 * 10000 us total latency if loop for 10000 times. So the way of measurement is not correct.

 

What is more, Linux is not hard-realtime, we cannot expect each usleep(1) to sleep exactly 1 us.  (You may get better average latency value if setting the process as SCHED_FIFO, or use the ADEOS patch: http://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:adeos. But the max interrupt/schedule latency is still tens of microsecond) .

 

And for 2008R1 release (I am not sure in the case 2007R1), if enabling CONFIG_GENERIC_TIME, by default the kernel will use timer tick (the timer interrupt) as clock source, so the timer accurancy will be measured in MS (10^-3 sec).

 

-Yi

QuoteReplyEditDelete

 

 

2008-12-16 14:43:47     Re: usleep issue

Alan Aguilar (PERU)

Message: 66767   

 

HI,

 

We are using 2007R1 distribution for BF533. I have kept the tick period as 1ms.

 

In the main, I just put a small piece of code, which prints value for every 10 seconds. This is done using usleep of 1000.

 

The value is not printing exactly at 10secs interval, instead it is taking 20secs.

 

Below is the piece of code. Can anyone suggest is there any  way to get millisecond accuracy in user space using built in functions?

 

while(1)

        {

                printf("calculate timer starts\n");

 

                gTenSec = 10000;

                gCount= 0;

                while(gTenSec)

                {

 

                        gCount++;

                        usleep(1000);

                        gTenSec = gTenSec - 1;

                }

                printf("After 10 secs Count %d\n", gCount);

 

                printf("calculate timer ends\n\n");

        }

 

 

Thanks in Advance

---

123.txt

Attachments

Outcomes