Precise latency (usleep, nanosleep, select)

Source: Internet
Author: User

From: http://hi.baidu.com/hesper/blog/item/059ec73df71fe9cf9e3d6214.html

Precise latency (usleep, nanosleep, select)

/*
Make: gcc-O test_sleep test_sleep.c
*/
/* # Include "comm_main.h "*/
# Include <stdio. h>;
# Include <stdlib. h>;
# Include <time. h>;
# Include <sys/time. h>;
# Include <errno. h>;
# Include <string. h>;
# Include <unistd. h>;
# Include <sys/types. h>;

# Define print_useage {/
Fprintf (stderr, "/n usage: % s USEC", argv [0]);/
Fprintf (stderr, "/n ");/
}

Int
Main (INT argc, char ** argv)
{
Unsigned int ntimetestsec = 0;/* sec */
Unsigned int ntimetest = 0;/* USEC */
Struct timeval tvbegin;
Struct timeval tvnow;
Int ret = 0;
Unsigned int ndelay = 0;/* USEC */
Fd_set rfds;
Struct timeval TV;
Int FD = 1;
Int I = 0;
Struct timespec req;
Unsigned int delay [20] =
{500000,100 000, 50000,100 00, 1000,900,500,100, 10, 1, 0 };
Int nreduce = 0;/* Error */

# If 0
If (argc <2)
{
Print_useage;
Exit (1 );
}
Ndelay = atoi (argv [1]);
# Endif

Fprintf (stderr, "% 18 S % 12 S % 12 S % 12 S/N", "function", "Time (USEC )",
"Realtime ",
"Reduce ");
Fprintf (stderr,

"-------------------------------------------------------------------/N ");

For (I = 0; I <20; I ++)
{
If (delay [I] <= 0)
Break;
Ndelay = delay [I];

/* Test usleep */
Gettimeofday (& tvbegin, null );
Ret = usleep (ndelay );
If (-1 = RET)
{
Fprintf (stderr, "usleep error. errno = % d [% s]/n", errno,
Strerror (errno ));
}
Gettimeofday (& tvnow, null );
Ntimetest =
(Tvnow. TV _sec-tvbegin. TV _sec) * 1000000 + tvnow. TV _usec-
Tvbegin. TV _usec;
Nreduce = ntimetest-ndelay;
Fprintf (stderr, "/T usleep % 8u % 8u % 8d/N", ndelay,
Ntimetest, nreduce );

/* Test nanosleep */
Gettimeofday (& tvbegin, null );
Req. TV _sec = ndelay/1000000;
Req. TV _nsec = (ndelay % 1000000) * 1000;
Ret = nanosleep (& req, null );
If (-1 = RET)
{
Fprintf (stderr, "/T nanosleep % 8u not support/N ",
Ndelay );
}
Else
{
Gettimeofday (& tvnow, null );
Ntimetest =
(Tvnow. TV _sec-tvbegin. TV _sec) * 1000000 + tvnow. TV _usec-
Tvbegin. TV _usec;
Nreduce = ntimetest-ndelay;
Fprintf (stderr, "/T nanosleep % 8u % 8u % 8d/N ",
Ndelay,
Ntimetest, nreduce );
}

/* Test select */
Gettimeofday (& tvbegin, null );
Fd_zero (& rfds );
Fd_set (FD, & rfds );
TV. TV _sec = 0;
TV. TV _usec = ndelay;
Ret = select (0, null, & TV );
If (-1 = RET)
{
Fprintf (stderr, "select error. errno = % d [% s]/n", errno,
Strerror (errno ));
}
Gettimeofday (& tvnow, null );
Ntimetest =
(Tvnow. TV _sec-tvbegin. TV _sec) * 1000000 + tvnow. TV _usec-
Tvbegin. TV _usec;
Nreduce = ntimetest-ndelay;
Fprintf (stderr, "/T select % 8u % 8u % 8d/N", ndelay,
Ntimetest,
Nreduce );

}

Return 0;
}

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------

Test
Ibm aix 3.4 single CPU
Sleep can be used in multiple threads. It only blocks this thread and does not affect other threads in the process.
Nanosleep is not supported
Supports usleep and select
The following uses gettimeofday to test and analyze the actual precision of usleep and select.
Function time (USEC) realtime reduce
-------------------------------------------------------------------
Usleep 500000 500026 26
Nanosleep 500000 not support
Select 500000 500026 26
Usleep 100000 100021 21
Nanosleep 100000 not support
Select 100000 100025 25
Usleep 50000 50021 21
Nanosleep 50000 not support
Select 50000 50107 107
Usleep 10000 10099 99
Nanosleep 10000 not support
Select 10000 10025 25
Usleep 1000 1021 21
Nanosleep 1000 not support
Select 1000 1024 24
Usleep 900 920 20
Nanosleep 900 not support
Select 900 1024 124
Usleep 500 523 23
Nanosleep 500 not support
Select 500 1024 524
Usleep 100 119 19
Nanosleep 100 not support
Select 100 1023 923
Usleep 10 31 21
Nanosleep 10 not support
Select 10 1024 1014
Usleep 1 19 18
Nanosleep 1 not support
Select 1 1 1026 1025


It can be concluded that under AIX 3.4:
Select can only be accurate to milliseconds
Usleep can be accurate to microseconds
Within 1 ms or more, the accuracy of the two is basically the same

Same as above, tested with Linux 2.4.20-8smp dual-CPU
Function time (USEC) realtime reduce
-------------------------------------------------------------------
Usleep 500000 506453 6453
Nanosleep 500000 509930 9930
Select 500000 499990-10
Usleep 100000 110023 10023
Nanosleep 100000 109955 9955
Select 100000 99992-8
Usleep 50000 59971 9971
Nanosleep 50000 59990 9990
Select 50000 50025 25
Usleep 10000 19991 9991
Nanosleep 10000 19988 9988
Select 10000 9956-44
Usleep 1000 19990 18990
Nanosleep 1000 19989 18989
Select 1000 10024 9024
Usleep 900 20009 19109
Nanosleep 900 19972 19072
Select 900 9943 9043
Usleep 500 19975 19475
Nanosleep 500 19971 19471
Select 500 10012 9512
Usleep 100 19975 19875
Nanosleep 100 19976 19876
Select 100 9943 9843
Usleep 10 19988 19978
Nanosleep 10 19961 19951
Select 10 10011 10001
Usleep 1 19978 19977
Nanosleep 1 19985 19984
Select 1 1 9932 9931
Test with 2.4.21-4. elsmp #1 SMP 4 CPU
Function time (USEC) realtime reduce
-------------------------------------------------------------------
Usleep 500000 501267 1267
Nanosleep 500000 509964 9964
Select 500000 499981-19
Usleep 100000 109944 9944
Nanosleep 100000 109925 9925
Select 100000 99963-37
Usleep 50000 59904 9904
Nanosleep 50000 59973 9973
Select 50000 49956-44
Usleep 10000 19988 9988
Nanosleep 10000 20008 10008
Select 10000 10020 20
Usleep 1000 19988 18988
Nanosleep 1000 19980 18980
Select 1000 9943 8943
Usleep 900 19975 19075
Nanosleep 900 19986 19086
Select 900 9905 9005
Usleep 500 19989 19489
Nanosleep 500 19910 19410
Select 500 10000 9500
Usleep 100 19355 19255
Nanosleep 100 19902 19802
Select 100 9988 9888
Usleep 10 19977 19967
Nanosleep 10 19988 19978
Select 10 9943 9933
Usleep 1 20007 20006
Nanosleep 1 19947 19946
Select 1 1 9980 9979

The following conclusions can be drawn:
1. Supports usleep, nanosleep, and select
2. Select is accurate to 10 ms. Accurate in over 10 milliseconds
3. usleep and nanosleep are not accurate.
Similarly, the following conclusions can be drawn through other testing procedures:
Sleep can be used in multiple threads. It only blocks this thread and does not affect other threads in the process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.