All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jamie Lokier <jamie@shareable.org>
To: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
Cc: linux-fsdevel@vger.kernel.org
Subject: Re: Bug: epoll_wait timeout is shorter than requested
Date: Mon, 17 Jan 2005 11:48:21 +0000	[thread overview]
Message-ID: <20050117114821.GB20152@mail.shareable.org> (raw)
In-Reply-To: <87651wl32d.fsf@qrnik.zagroda>

Marcin 'Qrczak' Kowalczyk wrote:
> A program to exhibit the bug:

The epoll argument rounds like select(), not like poll().
It was done deliberately.

The epoll_wait() behaviour is deliberate, so that it is possible to
wait repeatedly for short time intervals of 1 timer tick.

> It should wait one second (at least). Example output on Linux-2.6.10:
> start: 1105958820.439410
> stop:  1105958821.438636
> 
> With poll used instead of epoll the timeout is OK:
> start: 1105958827.975209
> stop:  1105958828.975944
> 
> Is this list a good place to report this?

For example, on a system with a 100 Hz timer tick, if you want to write
a program which actually times out on each tick, you can write:

    select (nfs, rfds, wfds, efds, { 0, 10000 });
or
    epoll_wait (epfd, events, maxevents, 10);

That will pause until the next tick, allowing programs which need to
do some work smoothly at 100 Hz.

(If you simply want to track the kernel's tick whatever it is, you can
use {0,1} or 1 as the timeouts respectively).

With poll(), because of the way the timeout argument is rounded up by
a tick in the kernel, that's impossible:

    poll (fds, nfds, -1);   <- Waits for a long time.
    poll (fds, nfds, 0);    <- Does not wait at all.
    poll (fds, nfds, 1);    <- Waits for *second* tick after current one.

This means you can only service application events at 50 Hz on a
system where the kernel tick is 100 Hz, if using poll().

This limitation makes poll() unsuitable on Linux for programs which
need to service events at or close to the kernel's tick rate.

This isn't just a problem for programs doing low jitter work.  Many
programs call select/poll/epoll, and then call gettimeofday() after to
decide whether the next "timer" application event is ready to be
serviced, or whether to call select/poll/epoll again.  With the poll()
behaviour, if a previous poll() finished _just_ before the timer event
is ready, the application will call poll() again with timeout 1, and
then it will wait 10-20ms (on a 100 Hz kernel) instead of the far more
desirable 0-10ms.

For historical reasons, perhaps even accidentally, Linux' select()
call rounds the timeout differently and is suitable for this.  We
decided to not change poll() in case it breaks something, but to make
epoll copy the select() rounding because it's more useful.

Note that all programs which depend on select/poll/epoll waiting for
at least the specified time _should_ check the time afterwards anyway,
for example by calling gettimeofday() and waiting again if the desired
time isn't reached yet.

This is because all three calls will return earlier than expected
under some circumstances, i.e. EINTR.

-- Jamie

  reply	other threads:[~2005-01-17 11:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-17 11:15 Bug: epoll_wait timeout is shorter than requested Marcin 'Qrczak' Kowalczyk
2005-01-17 11:48 ` Jamie Lokier [this message]
2005-01-17 13:41   ` Marcin 'Qrczak' Kowalczyk
2005-01-17 14:33     ` Jamie Lokier
2005-01-17 14:43       ` Jamie Lokier
2005-01-17 16:18       ` Marcin 'Qrczak' Kowalczyk
2005-01-17 16:48         ` Jamie Lokier
2005-01-18 23:27           ` Marcin 'Qrczak' Kowalczyk

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=20050117114821.GB20152@mail.shareable.org \
    --to=jamie@shareable.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=qrczak@knm.org.pl \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.