linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* select(0,NULL,NULL,NULL,&t1) used for delay
@ 2005-10-06 13:49 Madhu K.S.
  2005-10-06 14:25 ` Jesper Juhl
  2005-10-06 14:27 ` Alex Riesen
  0 siblings, 2 replies; 11+ messages in thread
From: Madhu K.S. @ 2005-10-06 13:49 UTC (permalink / raw)
  To: linux-kernel

Hi all,


In many application we use select() system call for delay.

example:
select(0,NULL,NULL,NULL,&t1);


select() for delay is very inefficient. I modified sys_select() code for
efficiency .Here are the changes to fs/select.c.

Please suggest on these changes. 

I know nanosleep() can be used instead of select(), but please suggest
on my changes.


file : fs/select.c
function : sys_select()




                          timeout += sec * (unsigned long) HZ;
                }
        }
-
+
+
        ret = -EINVAL;
        if (n < 0)
                goto out_nofds;
-
+       if ( (n == 0) && (inp == NULL) && (outp == NULL) &&
		(exp==	NULL)){
+                printf("\n I am inside new select condition timeout
			%d\n",timeout);
+                set_current_state(TASK_INTERRUPTIBLE);
+                ret = 0;
+                timeout = schedule_timeout(timeout);
+                if (signal_pending(current))
+                        ret = -ERESTARTNOHAND;
+                if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
+                        time_t sec = 0, usec = 0;
+                        if (timeout) {
+                                sec = timeout / HZ;
+                                usec = timeout % HZ;
+                                usec *= (1000000/HZ);
+                        }
+                        put_user(sec, &tvp->tv_sec);
+                        put_user(usec, &tvp->tv_usec);
+                }
+                current->state = TASK_RUNNING;
+                goto out_nofds;
+        }
+
        /* max_fdset can increase, so grab it once to avoid race */
        max_fdset = current->files->max_fdset;
        if (n > max_fdset)



Thank you very much.
Thanks for your assistances.

Madhu K.S.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 13:49 select(0,NULL,NULL,NULL,&t1) used for delay Madhu K.S.
@ 2005-10-06 14:25 ` Jesper Juhl
  2005-10-06 14:28   ` Jesper Juhl
  2005-10-06 19:05   ` Howard Chu
  2005-10-06 14:27 ` Alex Riesen
  1 sibling, 2 replies; 11+ messages in thread
From: Jesper Juhl @ 2005-10-06 14:25 UTC (permalink / raw)
  To: madhu.subbaiah; +Cc: linux-kernel

On 10/6/05, Madhu K.S. <madhu.subbaiah@wipro.com> wrote:
> Hi all,
>
>
> In many application we use select() system call for delay.
>
> example:
> select(0,NULL,NULL,NULL,&t1);
>
>
> select() for delay is very inefficient. I modified sys_select() code for
> efficiency .Here are the changes to fs/select.c.
>
> Please suggest on these changes.
>

A few tiny comments below.


> I know nanosleep() can be used instead of select(), but please suggest
> on my changes.
>
>
> file : fs/select.c
> function : sys_select()
>
Submitting an actual applyable patch is preferred. Makes it possible
to easily apply your changes to test the changes and then the file and
function is also part of the patch so you won't have to spell that out
explicitly.
Use   diff -up

>
>
>
>                           timeout += sec * (unsigned long) HZ;
>                 }
>         }
> -
> +
> +
>         ret = -EINVAL;
>         if (n < 0)
>                 goto out_nofds;
> -
> +       if ( (n == 0) && (inp == NULL) && (outp == NULL) &&
>                 (exp==  NULL)){
No space for the beginning parenthesis and space before the opening
bracket is preferred:
       if ((n == 0) && (inp == NULL) && (outp == NULL) &&
               (exp==  NULL)) {


> +                printf("\n I am inside new select condition timeout
>                         %d\n",timeout);
Having a printk() here certainly won't help performance.

> +                set_current_state(TASK_INTERRUPTIBLE);
> +                ret = 0;
> +                timeout = schedule_timeout(timeout);
> +                if (signal_pending(current))
> +                        ret = -ERESTARTNOHAND;
Wouldn't it make sense to jump out at this point if there's a signal pending?
                if (signal_pending(current)) {
                        ret = -ERESTARTNOHAND;
                        goto out;
                }
Or am I missing something?

> +                if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
> +                        time_t sec = 0, usec = 0;
> +                        if (timeout) {
> +                                sec = timeout / HZ;
> +                                usec = timeout % HZ;
> +                                usec *= (1000000/HZ);
Small style thing:   usec *= (1000000 / HZ);


> +                        }
> +                        put_user(sec, &tvp->tv_sec);
> +                        put_user(usec, &tvp->tv_usec);
> +                }
> +                current->state = TASK_RUNNING;
> +                goto out_nofds;
> +        }
> +
>         /* max_fdset can increase, so grab it once to avoid race */
>         max_fdset = current->files->max_fdset;
>         if (n > max_fdset)
>
[snip]

--
Jesper Juhl <jesper.juhl@gmail.com>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 13:49 select(0,NULL,NULL,NULL,&t1) used for delay Madhu K.S.
  2005-10-06 14:25 ` Jesper Juhl
@ 2005-10-06 14:27 ` Alex Riesen
  2005-10-06 15:26   ` Christopher Friesen
  2005-10-10 18:06   ` Bill Davidsen
  1 sibling, 2 replies; 11+ messages in thread
From: Alex Riesen @ 2005-10-06 14:27 UTC (permalink / raw)
  To: madhu.subbaiah; +Cc: linux-kernel

On 10/6/05, Madhu K.S. <madhu.subbaiah@wipro.com> wrote:
> Hi all,
>
> In many application we use select() system call for delay.
>
> example:
> select(0,NULL,NULL,NULL,&t1);
>
> select() for delay is very inefficient. I modified sys_select() code for

Why don't you just use nanosleep(2) (or usleep)?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 14:25 ` Jesper Juhl
@ 2005-10-06 14:28   ` Jesper Juhl
  2005-10-06 19:05   ` Howard Chu
  1 sibling, 0 replies; 11+ messages in thread
From: Jesper Juhl @ 2005-10-06 14:28 UTC (permalink / raw)
  To: madhu.subbaiah; +Cc: linux-kernel

On 10/6/05, Jesper Juhl <jesper.juhl@gmail.com> wrote:
> On 10/6/05, Madhu K.S. <madhu.subbaiah@wipro.com> wrote:
[snip]
> > +                                usec *= (1000000/HZ);
> Small style thing:   usec *= (1000000 / HZ);
>
Ohh and the parenthesis are not needed.
                                 usec *= 1000000 / HZ;

[snip]
--
Jesper Juhl <jesper.juhl@gmail.com>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 14:27 ` Alex Riesen
@ 2005-10-06 15:26   ` Christopher Friesen
  2005-10-06 15:42     ` Bernd Petrovitsch
  2005-10-06 17:30     ` Bob Copeland
  2005-10-10 18:06   ` Bill Davidsen
  1 sibling, 2 replies; 11+ messages in thread
From: Christopher Friesen @ 2005-10-06 15:26 UTC (permalink / raw)
  To: Alex Riesen; +Cc: linux-kernel

Alex Riesen wrote:

> Why don't you just use nanosleep(2) (or usleep)?

I can think of one main reason...existing code.  Also, nanosleep() 
rounds up excessively in many kernel versions, so that a request to 
sleep for less than 1 tick ends up sleeping for 2 ticks.

The select() man page explicitly mentions this usage;

"Some code calls select with all three sets empty, n zero, and a 
non-null timeout as a fairly portable way to sleep with subsecond 
precision."

Chris


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 15:26   ` Christopher Friesen
@ 2005-10-06 15:42     ` Bernd Petrovitsch
  2005-10-06 15:57       ` Christopher Friesen
  2005-10-06 17:30     ` Bob Copeland
  1 sibling, 1 reply; 11+ messages in thread
From: Bernd Petrovitsch @ 2005-10-06 15:42 UTC (permalink / raw)
  To: Christopher Friesen; +Cc: Alex Riesen, linux-kernel

On Thu, 2005-10-06 at 09:26 -0600, Christopher Friesen wrote:
> Alex Riesen wrote:
> 
> > Why don't you just use nanosleep(2) (or usleep)?
> 
> I can think of one main reason...existing code.  Also, nanosleep() 

And it's cooler to hack the kernel than to create and use a
portable_sleep() function and use it.

> rounds up excessively in many kernel versions, so that a request to 
> sleep for less than 1 tick ends up sleeping for 2 ticks.
                                                  ^^^^^^^

> The select() man page explicitly mentions this usage;
> 
> "Some code calls select with all three sets empty, n zero, and a 
> non-null timeout as a fairly portable way to sleep with subsecond 
                                                          ^^^^^^^^^
> precision."
  ^^^^^^^^^

You do realize that "subsecond precision" is probably meant as
improvement to sleep(3) and surely not to nanosleep(2)?

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 15:42     ` Bernd Petrovitsch
@ 2005-10-06 15:57       ` Christopher Friesen
  0 siblings, 0 replies; 11+ messages in thread
From: Christopher Friesen @ 2005-10-06 15:57 UTC (permalink / raw)
  To: Bernd Petrovitsch; +Cc: Alex Riesen, linux-kernel

Bernd Petrovitsch wrote:
> On Thu, 2005-10-06 at 09:26 -0600, Christopher Friesen wrote:

> And it's cooler to hack the kernel than to create and use a
> portable_sleep() function and use it.

If there is a substantial codebase using select() for sleeping, then it 
makes sense to improve the efficiency of the kernel.  Fix it in one 
place, make all the apps run better.

>>The select() man page explicitly mentions this usage;
>>
>>"Some code calls select with all three sets empty, n zero, and a 
>>non-null timeout as a fairly portable way to sleep with subsecond 
>                                                           ^^^^^^^^^
>>precision."
>   ^^^^^^^^^
> 
> You do realize that "subsecond precision" is probably meant as
> improvement to sleep(3) and surely not to nanosleep(2)?

select() allows for the selection of sleep time with microsecond 
precision.  The mainline kernel can't sleep for that small an interval 
anyway, so there's not really any difference in sleep precision between 
the two.

As I mentioned earlier, select() actually sleeps more accurately than 
nanosleep() on many kernels.  I haven't tested the most recent to see if 
this is still true though.

Chris

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 15:26   ` Christopher Friesen
  2005-10-06 15:42     ` Bernd Petrovitsch
@ 2005-10-06 17:30     ` Bob Copeland
  1 sibling, 0 replies; 11+ messages in thread
From: Bob Copeland @ 2005-10-06 17:30 UTC (permalink / raw)
  To: Christopher Friesen; +Cc: Alex Riesen, linux-kernel

> The select() man page explicitly mentions this usage;
>
> "Some code calls select with all three sets empty, n zero, and a
> non-null timeout as a fairly portable way to sleep with subsecond
> precision."

Perl's documentation also notes it as one of the many ways to do a
subsecond sleep:

>You can effect a sleep of 250 milliseconds this way:
>
>                  select(undef, undef, undef, 0.25);

TMTOWTDI.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 14:25 ` Jesper Juhl
  2005-10-06 14:28   ` Jesper Juhl
@ 2005-10-06 19:05   ` Howard Chu
  2005-10-06 19:41     ` Christopher Friesen
  1 sibling, 1 reply; 11+ messages in thread
From: Howard Chu @ 2005-10-06 19:05 UTC (permalink / raw)
  To: Jesper Juhl; +Cc: madhu.subbaiah, linux-kernel

Jesper Juhl wrote:
> Or am I missing something?
>   
Insert obligatory joke about optimizing delay loops... ?

-- 
  -- Howard Chu
  Chief Architect, Symas Corp.  http://www.symas.com
  Director, Highland Sun        http://highlandsun.com/hyc
  OpenLDAP Core Team            http://www.openldap.org/project/


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 19:05   ` Howard Chu
@ 2005-10-06 19:41     ` Christopher Friesen
  0 siblings, 0 replies; 11+ messages in thread
From: Christopher Friesen @ 2005-10-06 19:41 UTC (permalink / raw)
  To: Howard Chu; +Cc: Jesper Juhl, madhu.subbaiah, linux-kernel

Howard Chu wrote:

> Insert obligatory joke about optimizing delay loops... ?

It's not a delay if there's a signal pending.

Chris

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: select(0,NULL,NULL,NULL,&t1) used for delay
  2005-10-06 14:27 ` Alex Riesen
  2005-10-06 15:26   ` Christopher Friesen
@ 2005-10-10 18:06   ` Bill Davidsen
  1 sibling, 0 replies; 11+ messages in thread
From: Bill Davidsen @ 2005-10-10 18:06 UTC (permalink / raw)
  To: Alex Riesen; +Cc: linux-kernel

Alex Riesen wrote:
> On 10/6/05, Madhu K.S. <madhu.subbaiah@wipro.com> wrote:
> 
>>Hi all,
>>
>>In many application we use select() system call for delay.
>>
>>example:
>>select(0,NULL,NULL,NULL,&t1);
>>
>>select() for delay is very inefficient. I modified sys_select() code for
> 
> 
> Why don't you just use nanosleep(2) (or usleep)?

I think the answers here are (a) because there's an existing code base, 
and (b) as long as the functionality is required it might as well be 
provided optimally. If tou're going to do something at all, do it right.

-- 
    -bill davidsen (davidsen@tmr.com)
"The secret to procrastination is to put things off until the
  last possible moment - but no longer"  -me

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-10-10 18:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-06 13:49 select(0,NULL,NULL,NULL,&t1) used for delay Madhu K.S.
2005-10-06 14:25 ` Jesper Juhl
2005-10-06 14:28   ` Jesper Juhl
2005-10-06 19:05   ` Howard Chu
2005-10-06 19:41     ` Christopher Friesen
2005-10-06 14:27 ` Alex Riesen
2005-10-06 15:26   ` Christopher Friesen
2005-10-06 15:42     ` Bernd Petrovitsch
2005-10-06 15:57       ` Christopher Friesen
2005-10-06 17:30     ` Bob Copeland
2005-10-10 18:06   ` Bill Davidsen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).