nanosleep with small value
Hi,
seeing a strange thing happening here:
using nanosleep() with a smallish value gives me a very long sleeptime?
Is this because of a context switch being forced? Shouldn't the scheduler change affect that?
The test program:
===================================
#include <time.h>
#include <sched.h>
#include <stdio.h>
void delay_ns(unsigned long dly)
{
static struct timespec time;
int err;
{
time.tv_sec = 0;
time.tv_nsec = dly;
err = nanosleep(&time, NULL);
if (err) {
perror( "nanosleep failed" );
}
}
}main()
{
int i;
struct sched_param mysched;
int err;
if ( sched_getparam( 0, &mysched ) != 0 )
perror( "" );
else {
mysched.sched_priority = sched_get_priority_max(SCHED_FIFO);
err = sched_setscheduler(0, SCHED_FIFO, &mysched);
if( err != 0 ) {
fprintf (stderr,"sched_setscheduler returned: %d\n",
err );
perror( "" );
}
}
for (i=0; i < 1000; i++)
delay_ns(1000UL);
}==================================
The result running this is:
% time ./tst
real 0m8.000s user 0m0.000s sys 0m0.000s
I would have expected about 1000 * 1 us + overhead, but 8 seconds ????
Noticed this when trying to debug a PIC-programming software where the delay_ns() routine is used.
Increasing the nanosleep() argument to something more than HZ will give me the expected sleep times.
Best
Signature · 5 lines
Dag Nygren email: [email protected] Oy Espoon NewTech Ab phone: +358 9 8024910 Träsktorpet 3 fax: +358 9 8024916 02360 ESBO Mobile: +358 400 426312 FINLAND
Quoted 52 lines · Dag Nygren
Hi,
seeing a strange thing happening here:
usingnanosleep()with a smallish value gives me a very long sleeptime?Is this because of a context switch being forced?
Shouldn't the scheduler change affect that?The test program:
===================================
#include <time.h>
#include <sched.h>
#include <stdio.h>void
delay_ns(unsigned long dly){ static struct timespec time; int err; { time.tv_sec = 0; time.tv_nsec = dly; err = nanosleep(&time, NULL); if (err) { perror( "nanosleep failed" ); } } }
main(){ int i; struct sched_param mysched; int err; if ( sched_getparam( 0, &mysched ) != 0 ) perror( "" ); else { mysched.sched_priority = sched_get_priority_max(SCHED_FIFO); err = sched_setscheduler(0, SCHED_FIFO, &mysched); if( err != 0 ) { fprintf (stderr,"sched_setscheduler returned: %d\n", err ); perror( "" ); } } for (i=0; i < 1000; i++) delay_ns(1000UL); }
Quoted 7 lines
==================================
The result running this is:
% time ./tstreal 0m8.000s user 0m0.000s sys 0m0.000s
On an unprivilged account, I get this with
version 2.6.13.4
Script started on Thu 17 Nov 2005 11:44:47 AM EST
LINUX> time ./xxxsched_setscheduler returned: -1
Operation not permitted
real 0m2.000s
user 0m0.000s
sys 0m0.001s
LINUX> uname -r
2.6.13.4
LINUX> exit
Script done on Thu 17 Nov 2005 11:45:07 AM EST
Quoted 1 line
From the root account where the scheduler could be set:
Script started on Thu 17 Nov 2005 11:45:29 AM EST
[root@chaos root]# time /tmp/xxx
real 0m2.001s
user 0m0.000s
sys 0m0.001s
[root@chaos root]# exit
Script done on Thu 17 Nov 2005 11:45:53 AM EST
... essentially the same thing. And 2 seconds, not 8.
The HZ value for my kernel is 1000. It you are at 100 HZ, that might explain it.
Note that nanosleep() doesn't claim to be able to sleep less than the resolution of some kernel timer. It just takes parameters in seconds and nanoseconds.
Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.44 BogoMips). Warning : 98.36% of all statistics are fiction.
.
****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.
Thank you.
Quoted 1 line · linux-os (Dick Johnson) wrote/
On Thu, 17 Nov 2005, Dag Nygren wrote:
:
Quoted 32 lines
Quoted 4 lines
real 0m8.000s user 0m0.000s sys 0m0.000sOn an unprivilged account, I get this with
version 2.6.13.4Script started on Thu 17 Nov 2005 11:44:47 AM EST LINUX> time ./xxx
sched_setschedulerreturned: -1
Operation not permittedreal 0m2.000s
user 0m0.000s
sys 0m0.001s
LINUX> uname -r
2.6.13.4
LINUX> exit
Script done on Thu 17 Nov 2005 11:45:07 AM ESTQuoted 1 line
From the root account where the scheduler could be set:
Script started on Thu 17 Nov 2005 11:45:29 AM EST [root@chaos root]# time
/tmp/xxxreal 0m2.001s
user 0m0.000s
sys 0m0.001s
[root@chaos root]# exit
Quoted 6 lines
Script done on Thu 17 Nov 2005 11:45:53 AM EST
... essentially the same thing. And 2 seconds, not 8.
The HZ value for my kernel is 1000. It you are at 100 HZ, that might explain it.
No, it means he is at 250 HZ.
Quoted 5 lines
Note that
nanosleep()doesn't claim to be able to sleep less than the resolution of some kernel timer. It just takes parameters in seconds and nanoseconds.
In general small nanosleep() returns between 1/HZ and 2/HZ. In this code, the sleep is (indirectly) synchronised on a tick so it's always 2/HZ, the worse!
1000 small nanosleeps take therefore 1000 * 2/HZ s = 8s :-)
Eric
Quoted 56 lines · Dag Nygren
Hi,
seeing a strange thing happening here:
usingnanosleep()with a smallish value gives me a very long sleeptime?Is this because of a context switch being forced?
Shouldn't the scheduler change affect that?The test program:
===================================
#include <time.h>
#include <sched.h>
#include <stdio.h>void
delay_ns(unsigned long dly){ static struct timespec time; int err; { time.tv_sec = 0; time.tv_nsec = dly; err = nanosleep(&time, NULL); if (err) { perror( "nanosleep failed" ); } } }
main(){ int i; struct sched_param mysched; int err; if ( sched_getparam( 0, &mysched ) != 0 ) perror( "" ); else { mysched.sched_priority = sched_get_priority_max(SCHED_FIFO); err = sched_setscheduler(0, SCHED_FIFO, &mysched); if( err != 0 ) { fprintf (stderr,"sched_setscheduler returned: %d\n", err ); perror( "" ); } } for (i=0; i < 1000; i++) delay_ns(1000UL); }==================================
The result running this is:
% time ./tst
Quoted 6 lines
real 0m8.000s user 0m0.000s sys 0m0.000sI would have expected about 1000 * 1 us + overhead, but 8 seconds ????
Which kernel, what value of HZ? In either case, it's absurd to assume that the kernel is going to provide you 1 microsecond resolution in 2.6 mainline, as the best HZ value is 1000 (1 millisecond). And we don't busy-wait ever in nanosleep(). So the fastest your loop can run is 1000 * 1 ms = 1 second. That's assuming the only time-consuming thing is sleeping (minimal overhead). But, in sys_nanosleep(), we convert nanoseconds to jiffies and add 1 if you requested any sleep time. So,
HZ = 100
1000 * (10 + 1 ms) = 11 s
HZ = 250
1000 * (4 + 1 ms) = 5 sHZ = 1000
1000 * (1 + 1 ms) = 2 s (which is what Dick Johnson reported).
Note, that with HZ=250, there might be some extra rounding occurring timespec_to_jiffies() that I've forgotten.
So 8 s may not be terribly unreasonable. I don't know, though, what's add the 3 seconds if you're using 250.
Thanks,
Nish
Quoted 4 lines · Dag Nygren
Which kernel, what value of HZ?
Sorry, the kernel is 2.6.13 and HZ is 250.
Quoted 4 lines
In either case, it's absurd to assume
that the kernel is going to provide you 1 microsecond resolution in 2.6 mainline, as the best HZ value is 1000 (1 millisecond). And we don't busy-wait ever innanosleep().
Not?
The man page for nanosleep saya that times under 2 us are implemented by a busywait and this is why I expected it to work.
Quoted 10 lines
So the fastest your loop can run
is 1000 * 1 ms = 1 second. That's assuming the only time-consuming thing is sleeping (minimal overhead). But, insys_nanosleep(), we convert nanoseconds to jiffies and add 1 if you requested any sleep time. So,HZ = 100
1000 * (10 + 1 ms) = 11 s HZ = 250 1000 * (4 + 1 ms) = 5 s
Quoted 8 lines
HZ = 1000
1000 * (1 + 1 ms) = 2 s (which is what Dick Johnson reported).Note, that with HZ=250, there might be some extra rounding occurring
timespec_to_jiffies()that I've forgotten.So 8 s may not be terribly unreasonable. I don't know, though, what's add the 3 seconds if you're using 250.
OK, in that case the manpage should be changed. And an alternative has to be worked out by me ;-).
Thankyou
Dag