On Tue, Aug 18, 2020 at 11:04:47AM +0200, Martin Pieuchot wrote: > Diff below changes the order of operations in kqueue_register() to get > rid of an unnecessary pool_get(). When an event is already present on > the list try to acquire it first. Note that knote_acquire() may sleep > in which case the list might have changed so the lookup has to always > begin from the start. > > This will help with lazy removal of knote in poll/select. In this > scenario EV_ADD is generally always done with an knote already on the > list.
Some of the overhead could be absorbed by using a pool cache, as shown in the diff below. However, I am not suggesting that the cache should be taken into use right now. The frequency of knote pool usage is relatively low currently; there are other pools that would benefit more from caching. A related question is what implications the increased use of the pool cache feature would have under memory pressure. Index: kern/init_main.c =================================================================== RCS file: src/sys/kern/init_main.c,v retrieving revision 1.300 diff -u -p -r1.300 init_main.c --- kern/init_main.c 16 Jun 2020 05:09:29 -0000 1.300 +++ kern/init_main.c 18 Aug 2020 15:09:38 -0000 @@ -71,6 +71,7 @@ #include <sys/msg.h> #endif #include <sys/domain.h> +#include <sys/event.h> #include <sys/msgbuf.h> #include <sys/mbuf.h> #include <sys/pipe.h> @@ -148,7 +149,6 @@ void crypto_init(void); void db_ctf_init(void); void prof_init(void); void init_exec(void); -void kqueue_init(void); void futex_init(void); void taskq_init(void); void timeout_proc_init(void); @@ -431,7 +431,9 @@ main(void *framep) prof_init(); #endif - mbcpuinit(); /* enable per cpu mbuf data */ + /* Enable per cpu data. */ + mbcpuinit(); + kqueue_init_percpu(); /* init exec and emul */ init_exec(); Index: kern/kern_event.c =================================================================== RCS file: src/sys/kern/kern_event.c,v retrieving revision 1.142 diff -u -p -r1.142 kern_event.c --- kern/kern_event.c 12 Aug 2020 13:49:24 -0000 1.142 +++ kern/kern_event.c 18 Aug 2020 15:09:38 -0000 @@ -205,6 +205,12 @@ kqueue_init(void) PR_WAITOK, "knotepl", NULL); } +void +kqueue_init_percpu(void) +{ + pool_cache_init(&knote_pool); +} + int filt_fileattach(struct knote *kn) { Index: sys/event.h =================================================================== RCS file: src/sys/sys/event.h,v retrieving revision 1.44 diff -u -p -r1.44 event.h --- sys/event.h 22 Jun 2020 13:14:32 -0000 1.44 +++ sys/event.h 18 Aug 2020 15:09:38 -0000 @@ -210,6 +210,8 @@ extern void knote_activate(struct knote extern void knote_remove(struct proc *p, struct knlist *list); extern void knote_fdclose(struct proc *p, int fd); extern void knote_processexit(struct proc *); +extern void kqueue_init(void); +extern void kqueue_init_percpu(void); extern int kqueue_register(struct kqueue *kq, struct kevent *kev, struct proc *p); extern int filt_seltrue(struct knote *kn, long hint);