Re: Unexpected cascaded epoll behavior - my mistake or kernel bug

From: Davide Libenzi
Date: Mon Jan 26 2009 - 15:36:19 EST


On Mon, 26 Jan 2009, Davide Libenzi wrote:

> On Mon, 26 Jan 2009, Pavel Pisa wrote:
>
> > Hello Davide,
> >
> > thanks for fast reply and effort.
> >
> > I have tested your patch with patched and unpatched 2.6.28.2 kernel.
> > The outputs are attached. The patched kernel passes all your tests.
> >
> > But I have bad news. My library code does not fall into busy loop
> > after your patching but my FIFO tests do not work even in
> > single level epoll scenario. The strace output is attached as well.
> > I try to do more testing tomorrow. I have returned from weekend trip
> > at the evening today and I have not much time for deeper analysis.
> >
> > But it looks like write level sensitive events are not triggering
> > for epoll at all. The pipe is not fill by any character and specified
> > timeout is triggered with next message as fail results.
> > @ pipe #X evptrig wr timeout
> > @ pipe #X evptrig rd timeout
> >
> > If you want to test the code on your box, download library
> >
> > http://cmp.felk.cvut.cz/~pisa/ulan/ul_evpoll-090123.tar.gz
> >
> > The simple "make" in the top directory should work.
>
> It'd be really great if you could spare me having to dig into few
> thousands lines of userspace library code, and you could isolate a little
> bit better what is the expected result, and the error result.

Never mind, I looked myself and I'm able to replicate it with the simple
attached test program. Looking into it ...



- Davide

/*
* epoll_pipe_pingpong by Davide Libenzi (Another epoll and pipes test)
* Copyright (C) 2009 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@xxxxxxxxxxxxxxx>
*
*/

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <poll.h>
#include <sys/epoll.h>
#include <sys/wait.h>

#define EPWAIT_TIMEO (1 * 1000)

static int xepoll_create(int n) {
int epfd;

if ((epfd = epoll_create(n)) == -1) {
perror("epoll_create");
exit(2);
}

return epfd;
}

static void xepoll_ctl(int epfd, int cmd, int fd, struct epoll_event *evt) {
if (epoll_ctl(epfd, cmd, fd, evt) < 0) {
perror("epoll_ctl");
exit(3);
}
}

static void xpipe(int *fds) {
if (pipe(fds)) {
perror("pipe");
exit(4);
}
}

static int run_test(void) {
int n, epfd, loops;
int pfds[2];
char rdb[2];
struct epoll_event evt, evts[8];

epfd = xepoll_create(1);

xpipe(pfds);

fcntl(pfds[0], F_SETFL, fcntl(pfds[0], F_GETFL, 0) | O_NONBLOCK);

memset(&evt, 0, sizeof(evt));

evt.data.fd = pfds[0];
evt.events = EPOLLIN;
xepoll_ctl(epfd, EPOLL_CTL_ADD, pfds[0], &evt);

evt.data.fd = pfds[1];
evt.events = EPOLLOUT;
xepoll_ctl(epfd, EPOLL_CTL_ADD, pfds[1], &evt);

for (loops = 100; loops > 0; loops--) {
n = epoll_wait(epfd, evts, 8, EPWAIT_TIMEO);
if (n <= 0) {
fprintf(stderr, "Aiee!! Why no events?!\n");
break;
}
for (; n > 0; n--) {
struct epoll_event *pevt = &evts[n - 1];

if (pevt->data.fd == pfds[1] &&
pevt->events & EPOLLOUT) {
fprintf(stdout, "Write side, strong side!\n");
write(pfds[1], "w", 1);
} else if (pevt->data.fd == pfds[0] &&
pevt->events & EPOLLIN) {
fprintf(stdout, "Read side, strong side!\n");
while (read(pfds[0], rdb, sizeof(rdb)) > 0);
}
}
}

return 0;
}

int main(int ac, char **av) {
return run_test();
}