Use poll() instead of select()

Jaime Torres kde-optimize@mail.kde.org
Thu, 30 Jan 2003 11:15:43 +0100


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C2C848.8C2E4BC0
Content-Type: text/plain;
	charset="iso-8859-1"

Hello,

  I have done a small test with the very recent kde 3.1 on a
linux-i686-2.4.20.

  I have readed in http://www.kegel.com/dkftpbench/Poller_bench.html
  that poll() is faster than select() on Linux,*BSD and I thinked that also
in comercial unixes, but I know that poll() is simpler to use than select().

  I have only changed select by poll in the dcop server, resulting in a dcop
library of 258 bytes less and less memory normaly implies less swap and
better performance.

  I have count the number of selects and there are more than 33 only in
kdelibs.

Regards.

The diffs are:

39d38
< #include <poll.h>
1604,1616c1603,1609
< 	    
< 	    // * // Convert to poll
<     struct pollfd fds;
<     fds.fd=socket();
<     fds.events=POLLIN;
< //	    fd_set fds;
< //	    struct timeval tv;
< //	    FD_ZERO( &fds );
< //	    FD_SET( socket(), &fds );
< //	    tv.tv_sec = msecs / 1000;
< //	    tv.tv_usec = (msecs % 1000) * 1000;
< 	if (poll (&fds,1,msecs)<=0) {
< //	    if ( select( socket() + 1, &fds, 0, 0, &tv ) <= 0 ) {
---
> 	    fd_set fds;
> 	    struct timeval tv;
> 	    FD_ZERO( &fds );
> 	    FD_SET( socket(), &fds );
> 	    tv.tv_sec = msecs / 1000;
> 	    tv.tv_usec = (msecs % 1000) * 1000;
> 	    if ( select( socket() + 1, &fds, 0, 0, &tv ) <= 0 ) {
1652,1665c1645,1651
<     
<     // * // Convert it to poll
<     //fd_set fds;
<     struct pollfd fds;
<     fds.fd=fd;
<     fds.events=POLLIN;
<     
<   //  timeval timeout;
<    // timeout.tv_sec = 0;
<    // timeout.tv_usec = 0;
<    // FD_ZERO(&fds);
<    // FD_SET(fd, &fds);
<    int result=poll(&fds,1,0);
< //    int result = select(fd+1, &fds, 0, 0, &timeout);
---
>     fd_set fds;
>     timeval timeout;
>     timeout.tv_sec = 0;
>     timeout.tv_usec = 0;
>     FD_ZERO(&fds);
>     FD_SET(fd, &fds);
>     int result = select(fd+1, &fds, 0, 0, &timeout);


------_=_NextPart_001_01C2C848.8C2E4BC0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2653.12">
<TITLE>Use poll() instead of select()</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>Hello,</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; I have done a small test with the very recent =
kde 3.1 on a linux-i686-2.4.20.</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; I have readed in <A =
HREF=3D"http://www.kegel.com/dkftpbench/Poller_bench.html" =
TARGET=3D"_blank">http://www.kegel.com/dkftpbench/Poller_bench.html</A><=
/FONT>
<BR><FONT SIZE=3D2>&nbsp; that poll() is faster than select() on =
Linux,*BSD and I thinked that also in comercial unixes, but I know that =
poll() is simpler to use than select().</FONT></P>

<P><FONT SIZE=3D2>&nbsp; I have only changed select by poll in the dcop =
server, resulting in a dcop library of 258 bytes less and less memory =
normaly implies less swap and better performance.</FONT></P>

<P><FONT SIZE=3D2>&nbsp; I have count the number of selects and there =
are more than 33 only in kdelibs.</FONT>
</P>

<P><FONT SIZE=3D2>Regards.</FONT>
</P>

<P><FONT SIZE=3D2>The diffs are:</FONT>
</P>

<P><FONT SIZE=3D2>39d38</FONT>
<BR><FONT SIZE=3D2>&lt; #include &lt;poll.h&gt;</FONT>
<BR><FONT SIZE=3D2>1604,1616c1603,1609</FONT>
<BR><FONT SIZE=3D2>&lt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2>&lt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; // * // Convert to poll</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; struct pollfd =
fds;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; =
fds.fd=3Dsocket();</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; =
fds.events=3DPOLLIN;</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fd_set =
fds;</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; struct =
timeval tv;</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
FD_ZERO( &amp;fds );</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FD_SET( =
socket(), &amp;fds );</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
tv.tv_sec =3D msecs / 1000;</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
tv.tv_usec =3D (msecs % 1000) * 1000;</FONT>
<BR><FONT SIZE=3D2>&lt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (poll =
(&amp;fds,1,msecs)&lt;=3D0) {</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if ( =
select( socket() + 1, &amp;fds, 0, 0, &amp;tv ) &lt;=3D 0 ) {</FONT>
<BR><FONT SIZE=3D2>---</FONT>
<BR><FONT SIZE=3D2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; fd_set fds;</FONT>
<BR><FONT SIZE=3D2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; struct timeval tv;</FONT>
<BR><FONT SIZE=3D2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; FD_ZERO( &amp;fds );</FONT>
<BR><FONT SIZE=3D2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; FD_SET( socket(), &amp;fds );</FONT>
<BR><FONT SIZE=3D2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; tv.tv_sec =3D msecs / 1000;</FONT>
<BR><FONT SIZE=3D2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; tv.tv_usec =3D (msecs % 1000) * 1000;</FONT>
<BR><FONT SIZE=3D2>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; if ( select( socket() + 1, &amp;fds, 0, 0, &amp;tv ) =
&lt;=3D 0 ) {</FONT>
<BR><FONT SIZE=3D2>1652,1665c1645,1651</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; // * // Convert it to =
poll</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; //fd_set fds;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; struct pollfd =
fds;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; fds.fd=3Dfd;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; =
fds.events=3DPOLLIN;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp; //&nbsp; timeval timeout;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp; // timeout.tv_sec =3D =
0;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp; // timeout.tv_usec =3D =
0;</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp; // FD_ZERO(&amp;fds);</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp; // FD_SET(fd, =
&amp;fds);</FONT>
<BR><FONT SIZE=3D2>&lt;&nbsp;&nbsp;&nbsp; int =
result=3Dpoll(&amp;fds,1,0);</FONT>
<BR><FONT SIZE=3D2>&lt; //&nbsp;&nbsp;&nbsp; int result =3D =
select(fd+1, &amp;fds, 0, 0, &amp;timeout);</FONT>
<BR><FONT SIZE=3D2>---</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; fd_set fds;</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; timeval timeout;</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; timeout.tv_sec =3D =
0;</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; timeout.tv_usec =3D =
0;</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; =
FD_ZERO(&amp;fds);</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; FD_SET(fd, =
&amp;fds);</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; int result =3D =
select(fd+1, &amp;fds, 0, 0, &amp;timeout);</FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C2C848.8C2E4BC0--