From 72adf922cc1b5601152b0e7ea8d9edf3884ccf71 Mon Sep 17 00:00:00 2001 From: visa Date: Wed, 2 Jun 2021 13:56:28 +0000 Subject: [PATCH] Enable pool cache on knote pool Use the pool cache to reduce the overhead of memory management in function kqueue_register(). When EV_ADD is given, kqueue_register() pre-allocates a knote to avoid potential sleeping in the middle of the critical section that spans from knote lookup to insertion. However, the pre-allocation is useless if the lookup finds a matching knote. The cost of knote allocation will become significant with kqueue-based poll(2) and select(2) because the frequency of allocation will increase. Most of the cost appears to come from the locking inside the pool. The pool cache amortizes it by using CPU-local caches of free knotes as buffers. OK dlg@ mpi@ --- sys/kern/init_main.c | 8 +++++--- sys/kern/kern_event.c | 9 +++++++-- sys/sys/event.h | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 7902f2ea0b6..89d6b9dc577 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init_main.c,v 1.306 2021/02/08 10:51:01 mpi Exp $ */ +/* $OpenBSD: init_main.c,v 1.307 2021/06/02 13:56:28 visa Exp $ */ /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */ /* @@ -71,6 +71,7 @@ #include #endif #include +#include #include #include #include @@ -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); @@ -432,7 +432,9 @@ main(void *framep) prof_init(); #endif - mbcpuinit(); /* enable per cpu mbuf data */ + /* Enable per-CPU data. */ + mbcpuinit(); + kqueue_init_percpu(); uvm_init_percpu(); /* init exec and emul */ diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index 7f821a67ed7..9884b55d5ad 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_event.c,v 1.163 2021/04/22 15:30:12 visa Exp $ */ +/* $OpenBSD: kern_event.c,v 1.164 2021/06/02 13:56:28 visa Exp $ */ /*- * Copyright (c) 1999,2000,2001 Jonathan Lemon @@ -70,7 +70,6 @@ struct kqueue *kqueue_alloc(struct filedesc *); void kqueue_terminate(struct proc *p, struct kqueue *); -void kqueue_init(void); void KQREF(struct kqueue *); void KQRELE(struct kqueue *); @@ -231,6 +230,12 @@ kqueue_init(void) PR_WAITOK, "knotepl", NULL); } +void +kqueue_init_percpu(void) +{ + pool_cache_init(&knote_pool); +} + int filt_fileattach(struct knote *kn) { diff --git a/sys/sys/event.h b/sys/sys/event.h index 105f7e5376f..27e9d974efc 100644 --- a/sys/sys/event.h +++ b/sys/sys/event.h @@ -1,4 +1,4 @@ -/* $OpenBSD: event.h,v 1.54 2021/02/24 14:59:52 visa Exp $ */ +/* $OpenBSD: event.h,v 1.55 2021/06/02 13:56:28 visa Exp $ */ /*- * Copyright (c) 1999,2000,2001 Jonathan Lemon @@ -292,6 +292,8 @@ extern void knote_fdclose(struct proc *p, int fd); extern void knote_processexit(struct proc *); extern void knote_modify(const struct kevent *, struct knote *); extern void knote_submit(struct knote *, struct kevent *); +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 kqueue_scan(struct kqueue_scan_state *, int, struct kevent *, -- 2.20.1