fiber event optimise

This commit is contained in:
zsx 2018-01-13 10:21:49 +08:00
parent ebf0361142
commit 8d5cf8ee23
7 changed files with 72 additions and 24 deletions

View File

@ -61,6 +61,7 @@ EVENT *event_create(int size)
ring_init(&ev->events); ring_init(&ev->events);
ev->timeout = -1; ev->timeout = -1;
ev->setsize = size; ev->setsize = size;
ev->fdcount = 0;
ev->maxfd = -1; ev->maxfd = -1;
#ifdef HAS_POLL #ifdef HAS_POLL
@ -99,10 +100,10 @@ static int check(EVENT *ev, FILE_EVENT *fe)
if (getsocktype(fe->fd) >= 0) { if (getsocktype(fe->fd) >= 0) {
return 0; return 0;
} }
return ev->check(ev, fe); return ev->checkfd(ev, fe);
} }
#else #else
static int check(EVENT *ev, FILE_EVENT *fe) static int checkfd(EVENT *ev, FILE_EVENT *fe)
{ {
struct stat s; struct stat s;
@ -122,11 +123,11 @@ static int check(EVENT *ev, FILE_EVENT *fe)
if (S_ISCHR(s.st_mode)) { if (S_ISCHR(s.st_mode)) {
return 0; return 0;
} }
if (isatty(fd)) { if (isatty(fe->fd)) {
return 0; return 0;
} }
return ev->check(ev, fe); return ev->checkfd(ev, fe);
} }
#endif #endif
@ -187,9 +188,13 @@ int event_add_read(EVENT *ev, FILE_EVENT *fe, event_proc *proc)
} }
if (!(fe->mask & EVENT_READ)) { if (!(fe->mask & EVENT_READ)) {
if (fe->type == TYPE_NONE && check(ev, fe) == -1) { if (fe->type == TYPE_NONE) {
fe->type = TYPE_NOSOCK; if (checkfd(ev, fe) == -1) {
return 0; fe->type = TYPE_NOSOCK;
return 0;
} else {
fe->type = TYPE_SOCK;
}
} }
if (fe->me.parent == &fe->me) { if (fe->me.parent == &fe->me) {
@ -222,9 +227,13 @@ int event_add_write(EVENT *ev, FILE_EVENT *fe, event_proc *proc)
} }
if (!(fe->mask & EVENT_WRITE)) { if (!(fe->mask & EVENT_WRITE)) {
if (fe->type == TYPE_NONE && check(ev, fe) == -1) { if (fe->type == TYPE_NONE) {
fe->type = TYPE_NOSOCK; if (checkfd(ev, fe) == -1) {
return 0; fe->type = TYPE_NOSOCK;
return 0;
} else {
fe->type = TYPE_SOCK;
}
} }
if (fe->me.parent == &fe->me) { if (fe->me.parent == &fe->me) {

View File

@ -124,6 +124,7 @@ struct EPOLL_EVENT {
struct EVENT { struct EVENT {
RING events; RING events;
int timeout; int timeout;
int fdcount;
socket_t setsize; socket_t setsize;
socket_t maxfd; socket_t maxfd;
@ -140,7 +141,7 @@ struct EVENT {
int (*event_wait)(EVENT *, int); int (*event_wait)(EVENT *, int);
event_oper *check; event_oper *checkfd;
event_oper *add_read; event_oper *add_read;
event_oper *add_write; event_oper *add_write;
event_oper *del_read; event_oper *del_read;

View File

@ -61,7 +61,7 @@ static void epoll_free(EVENT *ev)
static int epoll_add_read(EVENT_EPOLL *ep, FILE_EVENT *fe) static int epoll_add_read(EVENT_EPOLL *ep, FILE_EVENT *fe)
{ {
struct epoll_event ee; struct epoll_event ee;
int op; int op, n;
if ((fe->mask & EVENT_READ)) if ((fe->mask & EVENT_READ))
return 0; return 0;
@ -75,12 +75,15 @@ static int epoll_add_read(EVENT_EPOLL *ep, FILE_EVENT *fe)
if (fe->mask & EVENT_WRITE) { if (fe->mask & EVENT_WRITE) {
ee.events |= EPOLLOUT; ee.events |= EPOLLOUT;
op = EPOLL_CTL_MOD; op = EPOLL_CTL_MOD;
n = 0;
} else { } else {
op = EPOLL_CTL_ADD; op = EPOLL_CTL_ADD;
n = 1;
} }
if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) { if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) {
fe->mask |= EVENT_READ; fe->mask |= EVENT_READ;
ep->event.fdcount += n;
return 0; return 0;
} }
@ -94,7 +97,7 @@ static int epoll_add_read(EVENT_EPOLL *ep, FILE_EVENT *fe)
static int epoll_add_write(EVENT_EPOLL *ep, FILE_EVENT *fe) static int epoll_add_write(EVENT_EPOLL *ep, FILE_EVENT *fe)
{ {
struct epoll_event ee; struct epoll_event ee;
int op; int op, n;
ee.events = 0; ee.events = 0;
ee.data.u32 = 0; ee.data.u32 = 0;
@ -106,12 +109,15 @@ static int epoll_add_write(EVENT_EPOLL *ep, FILE_EVENT *fe)
if (fe->mask & EVENT_READ) { if (fe->mask & EVENT_READ) {
ee.events |= EPOLLIN; ee.events |= EPOLLIN;
op = EPOLL_CTL_MOD; op = EPOLL_CTL_MOD;
n = 0;
} else { } else {
op = EPOLL_CTL_ADD; op = EPOLL_CTL_ADD;
n = 1;
} }
if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) { if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) {
fe->mask |= EVENT_WRITE; fe->mask |= EVENT_WRITE;
ep->event.fdcount += n;
return 0; return 0;
} }
@ -125,7 +131,7 @@ static int epoll_add_write(EVENT_EPOLL *ep, FILE_EVENT *fe)
static int epoll_del_read(EVENT_EPOLL *ep, FILE_EVENT *fe) static int epoll_del_read(EVENT_EPOLL *ep, FILE_EVENT *fe)
{ {
struct epoll_event ee; struct epoll_event ee;
int op; int op, n = 0;
ee.events = 0; ee.events = 0;
ee.data.u64 = 0; ee.data.u64 = 0;
@ -135,12 +141,15 @@ static int epoll_del_read(EVENT_EPOLL *ep, FILE_EVENT *fe)
if (fe->mask & EVENT_WRITE) { if (fe->mask & EVENT_WRITE) {
ee.events = EPOLLOUT; ee.events = EPOLLOUT;
op = EPOLL_CTL_MOD; op = EPOLL_CTL_MOD;
n = 0;
} else { } else {
op = EPOLL_CTL_DEL; op = EPOLL_CTL_DEL;
n = -1;
} }
if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) { if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) {
fe->mask &= ~EVENT_READ; fe->mask &= ~EVENT_READ;
ep->event.fdcount += n;
return 0; return 0;
} }
@ -154,7 +163,7 @@ static int epoll_del_read(EVENT_EPOLL *ep, FILE_EVENT *fe)
static int epoll_del_write(EVENT_EPOLL *ep, FILE_EVENT *fe) static int epoll_del_write(EVENT_EPOLL *ep, FILE_EVENT *fe)
{ {
struct epoll_event ee; struct epoll_event ee;
int op; int op, n;
ee.events = 0; ee.events = 0;
ee.data.u64 = 0; ee.data.u64 = 0;
@ -164,12 +173,15 @@ static int epoll_del_write(EVENT_EPOLL *ep, FILE_EVENT *fe)
if (fe->mask & EVENT_READ) { if (fe->mask & EVENT_READ) {
ee.events = EPOLLIN; ee.events = EPOLLIN;
op = EPOLL_CTL_MOD; op = EPOLL_CTL_MOD;
n = 0;
} else { } else {
op = EPOLL_CTL_DEL; op = EPOLL_CTL_DEL;
n = -1;
} }
if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) { if (__sys_epoll_ctl(ep->epfd, op, fe->fd, &ee) == 0) {
fe->mask &= ~EVENT_WRITE; fe->mask &= ~EVENT_WRITE;
ep->event.fdcount += n;
return 0; return 0;
} }
@ -217,9 +229,17 @@ static int epoll_event_wait(EVENT *ev, int timeout)
return n; return n;
} }
static int epoll_check(EVENT *ev, FILE_EVENT *fe) static int epoll_checkfd(EVENT *ev UNUSED, FILE_EVENT *fe UNUSED)
{ {
return -1; if (ev->add_read(ev, fe) == -1) {
return -1;
}
if (ev->del_read(ev, fe) == -1) {
msg_error("%s(%d): del_read failed, fd=%d",
__FUNCTION__, __LINE__, fe->fd);
return -1;
}
return 0;
} }
static int epoll_handle(EVENT *ev) static int epoll_handle(EVENT *ev)
@ -236,7 +256,7 @@ static const char *epoll_name(void)
EVENT *event_epoll_create(int size) EVENT *event_epoll_create(int size)
{ {
EVENT_EPOLL *ep = (EVENT_EPOLL *) malloc(sizeof(EVENT_EPOLL)); EVENT_EPOLL *ep = (EVENT_EPOLL *) calloc(1, sizeof(EVENT_EPOLL));
if (__sys_epoll_create == NULL) { if (__sys_epoll_create == NULL) {
hook_init(); hook_init();
@ -254,7 +274,7 @@ EVENT *event_epoll_create(int size)
ep->event.free = epoll_free; ep->event.free = epoll_free;
ep->event.event_wait = epoll_event_wait; ep->event.event_wait = epoll_event_wait;
ep->event.check = (event_oper *) epoll_check; ep->event.checkfd = (event_oper *) epoll_checkfd;
ep->event.add_read = (event_oper *) epoll_add_read; ep->event.add_read = (event_oper *) epoll_add_read;
ep->event.add_write = (event_oper *) epoll_add_write; ep->event.add_write = (event_oper *) epoll_add_write;
ep->event.del_read = (event_oper *) epoll_del_read; ep->event.del_read = (event_oper *) epoll_del_read;

View File

@ -184,6 +184,11 @@ static int kqueue_wait(EVENT *ev, int timeout)
return n; return n;
} }
static int kqueue_checkfd(EVENT *ev UNUSED, FILE_EVENT *fe UNUSED)
{
return -1;
}
static int kqueue_handle(EVENT *ev) static int kqueue_handle(EVENT *ev)
{ {
EVENT_KQUEUE *ek = (EVENT_KQUEUE *) ev; EVENT_KQUEUE *ek = (EVENT_KQUEUE *) ev;
@ -198,7 +203,7 @@ static const char *kqueue_name(void)
EVENT *event_kqueue_create(int size) EVENT *event_kqueue_create(int size)
{ {
EVENT_KQUEUE *ek = (EVENT_KQUEUE *) malloc(sizeof(EVENT_KQUEUE)); EVENT_KQUEUE *ek = (EVENT_KQUEUE *) calloc(1, sizeof(EVENT_KQUEUE));
if (__sys_kqueue == NULL) { if (__sys_kqueue == NULL) {
hook_init(); hook_init();
@ -221,6 +226,7 @@ EVENT *event_kqueue_create(int size)
ek->event.free = kqueue_free; ek->event.free = kqueue_free;
ek->event.event_wait = kqueue_wait; ek->event.event_wait = kqueue_wait;
ek->event.checkfd = (event_oper *) kqueue_checkfd;
ek->event.add_read = (event_oper *) kqueue_add_read; ek->event.add_read = (event_oper *) kqueue_add_read;
ek->event.add_write = (event_oper *) kqueue_add_write; ek->event.add_write = (event_oper *) kqueue_add_write;
ek->event.del_read = (event_oper *) kqueue_del_read; ek->event.del_read = (event_oper *) kqueue_del_read;

View File

@ -177,6 +177,11 @@ static int poll_wait(EVENT *ev, int timeout)
return n; return n;
} }
static int poll_checkfd(EVENT *ev UNUSED, FILE_EVENT *fe UNUSED)
{
return -1;
}
static int poll_handle(EVENT *ev) static int poll_handle(EVENT *ev)
{ {
(void) ev; (void) ev;
@ -190,7 +195,7 @@ static const char *poll_name(void)
EVENT *event_poll_create(int size) EVENT *event_poll_create(int size)
{ {
EVENT_POLL *ep = (EVENT_POLL *) malloc(sizeof(EVENT_POLL)); EVENT_POLL *ep = (EVENT_POLL *) calloc(1, sizeof(EVENT_POLL));
if (__sys_poll == NULL) { if (__sys_poll == NULL) {
hook_init(); hook_init();
@ -208,6 +213,7 @@ EVENT *event_poll_create(int size)
ep->event.free = poll_free; ep->event.free = poll_free;
ep->event.event_wait = poll_wait; ep->event.event_wait = poll_wait;
ep->event.checkfd = (event_oper *) poll_checkfd;
ep->event.add_read = (event_oper *) poll_add_read; ep->event.add_read = (event_oper *) poll_add_read;
ep->event.add_write = (event_oper *) poll_add_write; ep->event.add_write = (event_oper *) poll_add_write;
ep->event.del_read = (event_oper *) poll_del_read; ep->event.del_read = (event_oper *) poll_del_read;

View File

@ -196,6 +196,11 @@ static int select_event_wait(EVENT *ev, int timeout)
return n; return n;
} }
static int select_checkfd(EVENT *ev UNUSED, FILE_EVENT *fe UNUSED)
{
return -1;
}
static int select_handle(EVENT *ev) static int select_handle(EVENT *ev)
{ {
(void) ev; (void) ev;
@ -209,7 +214,7 @@ static const char *select_name(void)
EVENT *event_select_create(int size) EVENT *event_select_create(int size)
{ {
EVENT_SELECT *es = (EVENT_SELECT *) malloc(sizeof(EVENT_SELECT)); EVENT_SELECT *es = (EVENT_SELECT *) calloc(1, sizeof(EVENT_SELECT));
if (__sys_select == NULL) { if (__sys_select == NULL) {
hook_init(); hook_init();
@ -231,6 +236,7 @@ EVENT *event_select_create(int size)
es->event.free = select_free; es->event.free = select_free;
es->event.event_wait = select_event_wait; es->event.event_wait = select_event_wait;
es->event.checkfd = (event_oper *) select_checkfd;
es->event.add_read = (event_oper *) select_add_read; es->event.add_read = (event_oper *) select_add_read;
es->event.add_write = (event_oper *) select_add_write; es->event.add_write = (event_oper *) select_add_write;
es->event.del_read = (event_oper *) select_del_read; es->event.del_read = (event_oper *) select_del_read;

View File

@ -191,7 +191,7 @@ static int wmsg_del_write(EVENT_WMSG *ev, FILE_EVENT *fe)
return 0; return 0;
} }
static int wmsg_check(EVENT_WMSG *ev, FILE_EVENT *fe) static int wmsg_checkfd(EVENT_WMSG *ev, FILE_EVENT *fe)
{ {
(void) ev; (void) ev;
return getsocktype(fe->fd) == -1 ? -1 : 0; return getsocktype(fe->fd) == -1 ? -1 : 0;
@ -431,7 +431,7 @@ EVENT *event_wmsg_create(int size)
ew->event.handle = wmsg_handle; ew->event.handle = wmsg_handle;
ew->event.free = wmsg_free; ew->event.free = wmsg_free;
ew->event.event_wait = wmsg_wait; ew->event.event_wait = wmsg_wait;
ew->event.check = (event_oper *) wmsg_check; ew->event.checkfd = (event_oper *) wmsg_checkfd;
ew->event.add_read = (event_oper *) wmsg_add_read; ew->event.add_read = (event_oper *) wmsg_add_read;
ew->event.add_write = (event_oper *) wmsg_add_write; ew->event.add_write = (event_oper *) wmsg_add_write;
ew->event.del_read = (event_oper *) wmsg_del_read; ew->event.del_read = (event_oper *) wmsg_del_read;