From: Jamie Lokier <jamie@shareable.org>
To: Ben Mansell <ben@zeus.com>
Cc: Davide Libenzi <davidel@xmailserver.org>,
Steven Dake <sdake@mvista.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: Is POLLHUP an input-only or bidirectional condition? (was: epoll reporting events when it hasn't been asked to)
Date: Sun, 4 Apr 2004 21:24:37 +0100 [thread overview]
Message-ID: <20040404202437.GA16266@mail.shareable.org> (raw)
In-Reply-To: <Pine.LNX.4.58.0404041912460.5216@stones.cam.zeus.com>
Ben Mansell wrote:
> Since you have to generate the pollfd array for each time you call
> poll(), there is no real extra cost in taking a fd out temporarily
Wrong. You don't have to generate the pollfd array each time. That's
why there are separate events and revents fields. Quite often no
changes are required between each call to poll(), and only small
changes the rest of the time.
(Of course there is no escaping the O(n) overhead that the _kernel_
has when it scans the array, but it's avoidable in userspace).
> With epoll, adding a fd into the epoll set is a separate operation from
> the epoll_wait(), so if you really don't want to listen for any events
> on one FD, you'll have to do a EPOLL_DEL, and then later on do a
> EPOLL_ADD again if you want to bring it back in. Which is a bit nasty
> and inefficient.
No. If you don't want to listen for any events, and you predict those
events haven't occurred already (POLLHUP, POLLERR, usually POLLIN),
don't do any epoll_ctl() operations at all. Just call epoll_wait().
When you receive an event that you didn't want to listen for, set the
corresponding flag in your userspace structure, and call EPOLL_CTL_MOD
or EPOLL_CTL_DEL, depending on whether there are any other events you
still want to listen for.
See, your proposed method is slower than mine. I avoid *all*
epoll_ctl() calls in the common path.
Only in the uncommon path might I process an unwanted POLLHUP or
POLLERR event, and in those cases either I may as well close the fd
now (POLLHUP, after read to determine if it's EOF or an error), or the
EPOLL_CTL_DEL if I want to ignore that fd for a while (POLLERR) is
negligable because that's a rare event.
> As Richard Kettlewell's excellent poll test shows, relying on anything
> but the basics of poll() is impossible if you are trying to write code
> for several different OSs (or just different versions of the same OS!)
> Whatever poll() returns, all you can do is force a read() or a write()
> to try and find out what events really happened.
Indeed. Sometimes I wonder why there is anything other than POLLIN
and POLLOUT, given that the only reasonable response to the other
flags is to call read() to find out what happened. (Then again, maybe
read() isn't enough to get error conditions (as flagged by POLLERR) on
some broken OSs, and only MSG_ERRQUEUE will report them? I don't know).
> This is not something you'd want to do if the application, by
> unsetting POLLIN & POLLOUT, has shown that it doesn't want to read()
> or write().
Indeed. That's why if you do receive POLLHUP or POLLERR and you're
not interested in handling them right now, then _after_ receiving the
events call EPOLL_CTL_DEL, not before. That lazy method usually
avoids the system call.
-- Jamie
next prev parent reply other threads:[~2004-04-04 20:24 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-01 18:25 epoll reporting events when it hasn't been asked to Ben Mansell
2004-04-01 19:28 ` Davide Libenzi
2004-04-01 23:29 ` Steven Dake
2004-04-02 9:04 ` Ben Mansell
2004-04-02 15:22 ` Davide Libenzi
2004-04-02 18:40 ` Is POLLHUP an input-only or bidirectional condition? (was: epoll reporting events when it hasn't been asked to) Jamie Lokier
2004-04-03 12:19 ` Is POLLHUP an input-only or bidirectional condition? Richard Kettlewell
2004-04-03 21:44 ` Is POLLHUP an input-only or bidirectional condition? (was: epoll reporting events when it hasn't been asked to) Davide Libenzi
2004-04-03 22:35 ` Jamie Lokier
2004-04-04 1:28 ` Davide Libenzi
2004-04-04 2:08 ` Jamie Lokier
2004-04-04 2:49 ` Davide Libenzi
2004-04-04 18:51 ` Ben Mansell
2004-04-04 19:41 ` Davide Libenzi
2004-04-04 20:24 ` Jamie Lokier [this message]
2004-04-14 17:59 ` epoll reporting events when it hasn't been asked to Dirk Morris
2004-04-14 19:39 ` Jamie Lokier
2004-04-14 20:21 ` Dirk Morris
2004-04-14 21:48 ` Jamie Lokier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040404202437.GA16266@mail.shareable.org \
--to=jamie@shareable.org \
--cc=ben@zeus.com \
--cc=davidel@xmailserver.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sdake@mvista.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox