man-pages/man7/epoll.7

390 lines
10 KiB
Groff
Raw Normal View History

2004-11-03 13:51:07 +00:00
.\"
.\" epoll by Davide Libenzi ( efficient event notification retrieval )
.\" Copyright (C) 2003 Davide Libenzi
.\"
.\" This program is free software; you can redistribute it and/or modify
.\" it under the terms of the GNU General Public License as published by
.\" the Free Software Foundation; either version 2 of the License, or
.\" (at your option) any later version.
.\"
.\" This program is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
.\"
.\" Davide Libenzi <davidel@xmailserver.org>
.\"
2007-06-14 21:11:22 +00:00
.TH EPOLL 7 2007-06-22 "Linux" "Linux Programmer's Manual"
2004-11-03 13:51:07 +00:00
.SH NAME
epoll \- I/O event notification facility
.SH SYNOPSIS
.B #include <sys/epoll.h>
.SH DESCRIPTION
.B epoll
is a variant of
2004-11-03 13:51:07 +00:00
.BR poll (2)
2007-06-14 21:11:22 +00:00
that can be used either as an edge-triggered or a level-triggered
interface and scales well to large numbers of watched file descriptors.
Three system calls are provided to
2004-11-03 13:51:07 +00:00
set up and control an
.B epoll
set:
2004-11-03 13:51:07 +00:00
.BR epoll_create (2),
.BR epoll_ctl (2),
.BR epoll_wait (2).
An
.B epoll
set is connected to a file descriptor created by
.BR epoll_create (2).
Interest for certain file descriptors is then registered via
2004-11-03 13:51:07 +00:00
.BR epoll_ctl (2).
Finally, the actual wait is started by
2004-11-03 13:51:07 +00:00
.BR epoll_wait (2).
2007-06-14 21:11:22 +00:00
.SS Level-Triggered and Edge-Triggered
2004-11-03 13:51:07 +00:00
The
.B epoll
2007-06-14 21:11:22 +00:00
event distribution interface is able to behave both as edge-triggered
(ET) and level-triggered (LT).
2007-06-16 05:58:39 +00:00
The difference between the two mechanisms
can be described as follows.
Suppose that
2004-11-03 13:51:07 +00:00
this scenario happens :
2007-06-16 05:58:39 +00:00
.IP 1. 3
2007-06-14 21:11:22 +00:00
The file descriptor that represents the read side of a pipe
.RI ( rfd )
is added inside the
2004-11-03 13:51:07 +00:00
.B epoll
device.
2007-06-16 05:58:39 +00:00
.IP 2.
A pipe writer writes 2 kB of data on the write side of the pipe.
.IP 3.
2004-11-03 13:51:07 +00:00
A call to
.BR epoll_wait (2)
is done that will return
2007-06-14 21:11:22 +00:00
.I rfd
as a ready file descriptor.
2007-06-16 05:58:39 +00:00
.IP 4.
The pipe reader reads 1 kB of data from
2007-06-14 21:11:22 +00:00
.IR rfd .
2007-06-16 05:58:39 +00:00
.IP 5.
2004-11-03 13:51:07 +00:00
A call to
.BR epoll_wait (2)
is done.
.PP
If the
2007-06-14 21:11:22 +00:00
.I rfd
2004-11-03 13:51:07 +00:00
file descriptor has been added to the
.B epoll
interface using the
.B EPOLLET
flag, the call to
.BR epoll_wait (2)
2007-06-16 05:58:39 +00:00
done in step
2004-11-03 13:51:07 +00:00
.B 5
2007-06-14 21:11:22 +00:00
will probably hang despite the available data still present in the file
input buffer;
meanwhile the remote peer might be expecting a response based on the
data it already sent.
2007-06-16 05:58:39 +00:00
The reason for this is that edge-triggered mode only
delivers events when changes occur on the monitored file descriptor.
2004-11-03 13:51:07 +00:00
So, in step
.B 5
the caller might end up waiting for some data that is already present inside
the input buffer.
In the above example, an event on
2007-06-14 21:11:22 +00:00
.I rfd
2004-11-03 13:51:07 +00:00
will be generated because of the write done in
2005-10-28 17:48:03 +00:00
.BR 2
and the event is consumed in
2004-11-03 13:51:07 +00:00
.BR 3 .
Since the read operation done in
.B 4
does not consume the whole buffer data, the call to
.BR epoll_wait (2)
done in step
.B 5
2007-06-14 21:11:22 +00:00
might block indefinitely.
An application that employs the
2004-11-03 13:51:07 +00:00
.B EPOLLET
2007-06-14 21:11:22 +00:00
flag (edge-triggered)
2004-11-03 13:51:07 +00:00
should use non-blocking file descriptors to avoid having a blocking
2007-06-14 21:11:22 +00:00
read or write starve a task that is handling multiple file descriptors.
2004-11-03 13:51:07 +00:00
The suggested way to use
.B epoll
2007-06-14 21:11:22 +00:00
as an edge-triggered
2005-10-28 17:48:03 +00:00
.RB ( EPOLLET )
2007-06-14 21:11:22 +00:00
interface is as follows:
2004-11-03 13:51:07 +00:00
.RS
.TP
2004-11-03 13:51:07 +00:00
.B i
with non-blocking file descriptors
.TP
2004-11-03 13:51:07 +00:00
.B ii
2007-06-16 05:58:39 +00:00
by waiting for an event only after
2004-11-03 13:51:07 +00:00
.BR read (2)
or
2004-11-03 13:51:07 +00:00
.BR write (2)
return EAGAIN
.RE
.PP
2007-06-14 21:11:22 +00:00
By contrast, when used as a level-triggered interface,
2004-11-03 13:51:07 +00:00
.B epoll
2007-06-14 21:11:22 +00:00
is simplay a faster
2004-11-03 13:51:07 +00:00
.BR poll (2),
and can be used wherever the latter is used since it shares the
same semantics.
2007-06-14 21:11:22 +00:00
Since even with the edge-triggered
2004-11-03 13:51:07 +00:00
.B epoll
2007-06-14 21:11:22 +00:00
multiple events can be generated upon receipt of multiple chunks of data,
2004-11-03 13:51:07 +00:00
the caller has the option to specify the
.B EPOLLONESHOT
flag, to tell
.B epoll
2006-05-29 01:20:08 +00:00
to disable the associated file descriptor after the receipt of an event with
2004-11-03 13:51:07 +00:00
.BR epoll_wait (2).
When the
.B EPOLLONESHOT
flag is specified,
2007-06-14 21:11:22 +00:00
it is the caller's responsibility to rearm the file descriptor using
2004-11-03 13:51:07 +00:00
.BR epoll_ctl (2)
with
.BR EPOLL_CTL_MOD .
.SS Example for Suggested Usage
2004-11-03 13:51:07 +00:00
While the usage of
.B epoll
2007-06-14 21:11:22 +00:00
when employed as a level-triggered interface does have the same
semantics as
2004-11-03 13:51:07 +00:00
.BR poll (2),
2007-06-14 21:11:22 +00:00
the edge-triggered usage requires more clarification to avoid stalls
in the application event loop.
In this example, listener is a
2004-11-03 13:51:07 +00:00
non-blocking socket on which
.BR listen (2)
has been called.
The function do_use_fd() uses the new ready
2004-11-03 13:51:07 +00:00
file descriptor until EAGAIN is returned by either
.BR read (2)
or
.BR write (2).
2007-06-14 21:11:22 +00:00
An event-driven state machine application should, after having received
2004-11-03 13:51:07 +00:00
EAGAIN, record its current state so that at the next call to do_use_fd()
it will continue to
.BR read (2)
or
.BR write (2)
from where it stopped before.
2004-11-03 13:51:07 +00:00
.nf
struct epoll_event ev, *events;
2007-06-20 20:52:14 +00:00
for (;;) {
2005-07-06 12:57:38 +00:00
nfds = epoll_wait(kdpfd, events, maxevents, \-1);
2004-11-03 13:51:07 +00:00
2007-04-05 12:36:57 +00:00
for (n = 0; n < nfds; ++n) {
if (events[n].data.fd == listener) {
2004-11-03 13:51:07 +00:00
client = accept(listener, (struct sockaddr *) &local,
&addrlen);
2007-06-20 20:52:14 +00:00
if (client < 0){
2004-11-03 13:51:07 +00:00
perror("accept");
continue;
}
setnonblocking(client);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = client;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, client, &ev) < 0) {
2005-08-16 08:16:01 +00:00
fprintf(stderr, "epoll set insertion error: fd=%d\\n",
2004-11-03 13:51:07 +00:00
client);
2005-07-06 12:57:38 +00:00
return \-1;
2004-11-03 13:51:07 +00:00
}
2007-04-05 12:36:57 +00:00
} else {
2004-11-03 13:51:07 +00:00
do_use_fd(events[n].data.fd);
2007-04-05 12:36:57 +00:00
}
2004-11-03 13:51:07 +00:00
}
}
.fi
2007-06-14 21:11:22 +00:00
When used as an edge-triggered interface, for performance reasons, it is
possible to add the file descriptor inside the epoll interface
.RB ( EPOLL_CTL_ADD )
2007-06-16 05:58:39 +00:00
once by specifying
2007-06-14 21:11:22 +00:00
.RB ( EPOLLIN | EPOLLOUT ).
This allows you to avoid
2004-11-03 13:51:07 +00:00
continuously switching between
.B EPOLLIN
and
.B EPOLLOUT
calling
.BR epoll_ctl (2)
with
.BR EPOLL_CTL_MOD .
.SS Questions and Answers
.TP
.B Q1
2007-06-16 05:58:39 +00:00
What happens if you add the same file descriptor to an epoll set twice?
2004-11-03 13:51:07 +00:00
.TP
.B A1
You will probably get EEXIST.
However, it is possible that two
2007-06-14 21:11:22 +00:00
threads may add the same file descriptor twice.
This is a harmless condition.
2004-11-03 13:51:07 +00:00
.TP
.B Q2
2004-11-03 13:51:07 +00:00
Can two
.B epoll
2007-06-14 21:11:22 +00:00
sets wait for the same file descriptor?
If so, are events reported to both
2004-11-03 13:51:07 +00:00
.B epoll
2007-06-14 21:11:22 +00:00
file descriptors?
2004-11-03 13:51:07 +00:00
.TP
.B A2
2007-06-14 21:11:22 +00:00
Yes, and events would be reported to both.
However, it is not recommended.
2004-11-03 13:51:07 +00:00
.TP
.B Q3
Is the
.B epoll
2007-06-14 21:11:22 +00:00
file descriptor itself poll/epoll/selectable?
2004-11-03 13:51:07 +00:00
.TP
.B A3
Yes.
.TP
.B Q4
2004-11-03 13:51:07 +00:00
What happens if the
.B epoll
2007-06-14 21:11:22 +00:00
file descriptor is put into its own file descriptor set?
2004-11-03 13:51:07 +00:00
.TP
.B A4
It will fail.
However, you can add an
2004-11-03 13:51:07 +00:00
.B epoll
2007-06-14 21:11:22 +00:00
file descriptor inside another epoll file descriptor set.
2004-11-03 13:51:07 +00:00
.TP
.B Q5
Can I send the
.B epoll
2007-06-14 21:11:22 +00:00
file descriptor over a unix-socket to another process?
2004-11-03 13:51:07 +00:00
.TP
.B A5
No.
.TP
.B Q6
2007-06-14 21:11:22 +00:00
Will closing a file descriptor cause it to be removed from all
2004-11-03 13:51:07 +00:00
.B epoll
sets automatically?
.TP
.B A6
Yes.
.TP
.B Q7
2007-06-14 21:11:22 +00:00
If more than one event occurs between
2004-11-03 13:51:07 +00:00
.BR epoll_wait (2)
calls, are they combined or reported separately?
.TP
.B A7
They will be combined.
.TP
.B Q8
2007-06-14 21:11:22 +00:00
Does an operation on a file descriptor affect the
already collected but not yet reported events?
2004-11-03 13:51:07 +00:00
.TP
.B A8
2007-06-14 21:11:22 +00:00
You can do two operations on an existing file descriptor.
Remove would be meaningless for
this case.
Modify will re-read available I/O.
2004-11-03 13:51:07 +00:00
.TP
.B Q9
2007-06-14 21:11:22 +00:00
Do I need to continuously read/write a file descriptor
until EAGAIN when using the
2004-11-03 13:51:07 +00:00
.B EPOLLET
2007-06-14 21:11:22 +00:00
flag (edge-triggered behavior) ?
2004-11-03 13:51:07 +00:00
.TP
.B A9
No you don't.
Receiving an event from
2004-11-03 13:51:07 +00:00
.BR epoll_wait (2)
should suggest to you that such file descriptor is ready
for the requested I/O operation.
You have simply to consider it ready until you will receive the
next EAGAIN.
When and how you will use such file descriptor is entirely up
to you.
Also, the condition that the read/write I/O space is exhausted can
2007-06-14 21:11:22 +00:00
be detected by checking the amount of data read from / written to the target
file descriptor.
For example, if you call
2004-11-03 13:51:07 +00:00
.BR read (2)
by asking to read a certain amount of data and
.BR read (2)
2007-06-14 21:11:22 +00:00
returns a lower number of bytes,
you can be sure of having exhausted the read
I/O space for such file descriptor.
2007-06-14 21:11:22 +00:00
The same is true when writing using the
.BR write (2).
.SS Possible Pitfalls and Ways to Avoid Them
2004-11-03 13:51:07 +00:00
.TP
2007-06-14 21:11:22 +00:00
.B o Starvation (edge-triggered)
2004-11-03 13:51:07 +00:00
.PP
If there is a large amount of I/O space,
it is possible that by trying to drain
it the other files will not get processed causing starvation.
2007-06-14 21:11:22 +00:00
(This problem is not specific to
.BR epoll .)
2004-11-03 13:51:07 +00:00
.PP
The solution is to maintain a ready list
and mark the file descriptor as ready
2004-11-03 13:51:07 +00:00
in its associated data structure, thereby allowing the application to
remember which files need to be processed but still round robin amongst
all the ready files.
This also supports ignoring subsequent events you
2007-06-14 21:11:22 +00:00
receive for file descriptors that are already ready.
2004-11-03 13:51:07 +00:00
.TP
.B o If using an event cache...
2004-11-03 13:51:07 +00:00
.PP
2007-06-14 21:11:22 +00:00
If you use an event cache or store all the file descriptors returned from
2004-11-03 13:51:07 +00:00
.BR epoll_wait (2),
then make sure to provide a way to mark
2007-06-14 21:11:22 +00:00
its closure dynamically (i.e., caused by
a previous event's processing).
Suppose you receive 100 events from
2004-11-03 13:51:07 +00:00
.BR epoll_wait (2),
and in event #47 a condition causes event #13 to be closed.
If you remove the structure and
2007-05-11 23:29:44 +00:00
.BR close (2)
2007-06-14 21:11:22 +00:00
the file descriptor for event #13, then your
event cache might still say there are events waiting for that
file descriptor causing confusion.
.PP
2004-11-03 13:51:07 +00:00
One solution for this is to call, during the processing of event 47,
.BR epoll_ctl ( EPOLL_CTL_DEL )
2007-06-14 21:11:22 +00:00
to delete file descriptor 13 and
2007-05-11 23:29:44 +00:00
.BR close (2),
2005-10-19 16:30:05 +00:00
then mark its associated
data structure as removed and link it to a cleanup list.
If you find another
2007-06-14 21:11:22 +00:00
event for file descriptor 13 in your batch processing,
you will discover the file descriptor had been
2004-11-03 13:51:07 +00:00
previously removed and there will be no confusion.
.SH VERSIONS
.BR epoll (7)
is a new API introduced in Linux kernel 2.5.44.
Its interface should be finalized in Linux kernel 2.5.66.
2004-11-03 13:51:07 +00:00
.SH CONFORMING TO
The epoll API is Linux specific.
Some other systems provide similar
mechanisms, for example, FreeBSD has
.IR kqueue ,
and Solaris has
.IR /dev/poll .
2004-11-03 13:51:07 +00:00
.SH "SEE ALSO"
.BR epoll_create (2),
.BR epoll_ctl (2),
.BR epoll_wait (2)