Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys - Track LWPs in a per-process radixtree. It uses no ext...



details:   https://anonhg.NetBSD.org/src/rev/83748c726ae1
branches:  trunk
changeset: 744319:83748c726ae1
user:      ad <ad%NetBSD.org@localhost>
date:      Wed Jan 29 15:47:51 2020 +0000

description:
- Track LWPs in a per-process radixtree.  It uses no extra memory in the
  single threaded case.  Replace scans of p->p_lwps with lookups in the
  tree.  Find free LIDs for new LWPs in the tree.  Replace the hashed sleep
  queues for park/unpark with lookups in the tree under cover of a RW lock.

- lwp_wait(): if waiting on a specific LWP, find the LWP via tree lookup and
  return EINVAL if it's detached, not ESRCH.

- Group the locks in struct proc at the end of the struct in their own cache
  line.

- Add some comments.

diffstat:

 sys/compat/common/kern_50.c              |   10 +-
 sys/compat/common/kern_time_60.c         |   10 +-
 sys/compat/netbsd32/netbsd32_compat_50.c |   12 +-
 sys/compat/netbsd32/netbsd32_compat_60.c |   12 +-
 sys/compat/netbsd32/netbsd32_lwp.c       |   10 +-
 sys/kern/kern_exec.c                     |   35 +++-
 sys/kern/kern_exit.c                     |   11 +-
 sys/kern/kern_fork.c                     |    6 +-
 sys/kern/kern_lwp.c                      |  216 +++++++++++++++++-------------
 sys/kern/kern_proc.c                     |   12 +-
 sys/kern/sys_lwp.c                       |  202 +++++++---------------------
 sys/sys/lwp.h                            |    7 +-
 sys/sys/proc.h                           |   20 ++-
 13 files changed, 264 insertions(+), 299 deletions(-)

diffs (truncated from 1149 to 300 lines):

diff -r 2474239e330b -r 83748c726ae1 sys/compat/common/kern_50.c
--- a/sys/compat/common/kern_50.c       Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/compat/common/kern_50.c       Wed Jan 29 15:47:51 2020 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: kern_50.c,v 1.2 2019/01/27 02:08:39 pgoyette Exp $     */
+/*     $NetBSD: kern_50.c,v 1.3 2020/01/29 15:47:51 ad Exp $   */
 
 /*-
- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
+ * Copyright (c) 2008, 2009, 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_50.c,v 1.2 2019/01/27 02:08:39 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_50.c,v 1.3 2020/01/29 15:47:51 ad Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_netbsd.h"
@@ -80,12 +80,12 @@
        }
 
        if (SCARG(uap, unpark) != 0) {
-               error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
+               error = lwp_unpark(&SCARG(uap, unpark), 1);
                if (error != 0)
                        return error;
        }
 
-       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp, SCARG(uap, hint));
+       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp);
 }
 
 static int
diff -r 2474239e330b -r 83748c726ae1 sys/compat/common/kern_time_60.c
--- a/sys/compat/common/kern_time_60.c  Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/compat/common/kern_time_60.c  Wed Jan 29 15:47:51 2020 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: kern_time_60.c,v 1.2 2019/01/27 02:08:39 pgoyette Exp $        */
+/*     $NetBSD: kern_time_60.c,v 1.3 2020/01/29 15:47:51 ad Exp $      */
 
 /*-
- * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * Copyright (c) 2013, 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_time_60.c,v 1.2 2019/01/27 02:08:39 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_time_60.c,v 1.3 2020/01/29 15:47:51 ad Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_netbsd.h"
@@ -75,12 +75,12 @@
        }
 
        if (SCARG(uap, unpark) != 0) {
-               error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
+               error = lwp_unpark(&SCARG(uap, unpark), 1);
                if (error != 0)
                        return error;
        }
 
-       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp, SCARG(uap, hint));
+       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp);
 }
 
 int
diff -r 2474239e330b -r 83748c726ae1 sys/compat/netbsd32/netbsd32_compat_50.c
--- a/sys/compat/netbsd32/netbsd32_compat_50.c  Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/compat/netbsd32/netbsd32_compat_50.c  Wed Jan 29 15:47:51 2020 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: netbsd32_compat_50.c,v 1.44 2020/01/01 14:52:38 maxv Exp $     */
+/*     $NetBSD: netbsd32_compat_50.c,v 1.45 2020/01/29 15:47:51 ad Exp $       */
 
 /*-
- * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_50.c,v 1.44 2020/01/01 14:52:38 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_50.c,v 1.45 2020/01/29 15:47:51 ad Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_netbsd.h"
@@ -564,14 +564,12 @@
        }
 
        if (SCARG(uap, unpark) != 0) {
-               error = lwp_unpark(SCARG(uap, unpark),
-                   SCARG_P32(uap, unparkhint));
+               error = lwp_unpark(&SCARG(uap, unpark), 1);
                if (error != 0)
                        return error;
        }
 
-       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp,
-           SCARG_P32(uap, hint));
+       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp);
 }
 
 static int
diff -r 2474239e330b -r 83748c726ae1 sys/compat/netbsd32/netbsd32_compat_60.c
--- a/sys/compat/netbsd32/netbsd32_compat_60.c  Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/compat/netbsd32/netbsd32_compat_60.c  Wed Jan 29 15:47:51 2020 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: netbsd32_compat_60.c,v 1.5 2019/12/15 16:48:26 tsutsui Exp $   */
+/*     $NetBSD: netbsd32_compat_60.c,v 1.6 2020/01/29 15:47:52 ad Exp $        */
 
 /*-
- * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_60.c,v 1.5 2019/12/15 16:48:26 tsutsui Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_60.c,v 1.6 2020/01/29 15:47:52 ad Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -70,14 +70,12 @@
        }
 
        if (SCARG(uap, unpark) != 0) {
-               error = lwp_unpark(SCARG(uap, unpark),
-                   SCARG_P32(uap, unparkhint));
+               error = lwp_unpark(&SCARG(uap, unpark), 1);
                if (error != 0)
                        return error;
        }
 
-       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp,
-           SCARG_P32(uap, hint));
+       return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp);
 }
 
 static struct syscall_package compat_netbsd32_60_syscalls[] = {
diff -r 2474239e330b -r 83748c726ae1 sys/compat/netbsd32/netbsd32_lwp.c
--- a/sys/compat/netbsd32/netbsd32_lwp.c        Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/compat/netbsd32/netbsd32_lwp.c        Wed Jan 29 15:47:51 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: netbsd32_lwp.c,v 1.21 2020/01/26 19:08:09 ad Exp $     */
+/*     $NetBSD: netbsd32_lwp.c,v 1.22 2020/01/29 15:47:52 ad Exp $     */
 
 /*
  *  Copyright (c) 2005, 2006, 2007, 2020 The NetBSD Foundation.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_lwp.c,v 1.21 2020/01/26 19:08:09 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_lwp.c,v 1.22 2020/01/29 15:47:52 ad Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -183,14 +183,12 @@
        }
 
        if (SCARG(uap, unpark) != 0) {
-               error = lwp_unpark(SCARG(uap, unpark),
-                   SCARG_P32(uap, unparkhint));
+               error = lwp_unpark(&SCARG(uap, unpark), 1);
                if (error != 0)
                        return error;
        }
 
-       return lwp_park(SCARG(uap, clock_id), SCARG(uap, flags), tsp,
-           SCARG_P32(uap, hint));
+       return lwp_park(SCARG(uap, clock_id), SCARG(uap, flags), tsp);
 }
 
 int
diff -r 2474239e330b -r 83748c726ae1 sys/kern/kern_exec.c
--- a/sys/kern/kern_exec.c      Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/kern/kern_exec.c      Wed Jan 29 15:47:51 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exec.c,v 1.489 2020/01/23 10:05:44 ad Exp $       */
+/*     $NetBSD: kern_exec.c,v 1.490 2020/01/29 15:47:52 ad Exp $       */
 
 /*-
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.489 2020/01/23 10:05:44 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.490 2020/01/29 15:47:52 ad Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -1141,13 +1141,31 @@
                (*p->p_emul->e_proc_exit)(p);
 
        /*
-        * This is now LWP 1.
+        * This is now LWP 1.  Re-number the LWP if needed.  Don't bother
+        * with p_treelock here as this is the only live LWP in the proc
+        * right now.
         */
-       /* XXX elsewhere */
-       mutex_enter(p->p_lock);
-       p->p_nlwpid = 1;
-       l->l_lid = 1;
-       mutex_exit(p->p_lock);
+       while (__predict_false(l->l_lid != 1)) {
+               lwp_t *l2 __diagused;
+               int error;
+
+               mutex_enter(p->p_lock);
+               error = radix_tree_insert_node(&p->p_lwptree, 1 - 1, l);
+               if (error == 0) {
+                       l2 = radix_tree_remove_node(&p->p_lwptree,
+                           (uint64_t)(l->l_lid - 1));
+                       KASSERT(l2 == l);
+                       p->p_nlwpid = 2;
+                       l->l_lid = 1;
+               }
+               mutex_exit(p->p_lock);
+
+               if (error == 0)
+                       break;
+
+               KASSERT(error == ENOMEM);
+               radix_tree_await_memory();
+       }
 
        /*
         * Call exec hook. Emulation code may NOT store reference to anything
@@ -2526,6 +2544,7 @@
        mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
        mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
        rw_init(&p2->p_reflock);
+       rw_init(&p2->p_treelock);
        cv_init(&p2->p_waitcv, "wait");
        cv_init(&p2->p_lwpcv, "lwpwait");
 
diff -r 2474239e330b -r 83748c726ae1 sys/kern/kern_exit.c
--- a/sys/kern/kern_exit.c      Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/kern/kern_exit.c      Wed Jan 29 15:47:51 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exit.c,v 1.281 2020/01/27 21:09:33 ad Exp $       */
+/*     $NetBSD: kern_exit.c,v 1.282 2020/01/29 15:47:52 ad Exp $       */
 
 /*-
  * Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.281 2020/01/27 21:09:33 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.282 2020/01/29 15:47:52 ad Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_dtrace.h"
@@ -202,6 +202,7 @@
        ksiginfo_t      ksi;
        ksiginfoq_t     kq;
        int             wakeinit;
+       struct lwp      *l2 __diagused;
 
        p = l->l_proc;
 
@@ -565,6 +566,11 @@
        p->p_nrlwps--;
        p->p_nzlwps++;
        p->p_ndlwps = 0;
+       /* Don't bother with p_treelock as no other LWPs remain. */
+       l2 = radix_tree_remove_node(&p->p_lwptree, (uint64_t)(l->l_lid - 1));
+       KASSERT(l2 == l);
+       KASSERT(radix_tree_empty_tree_p(&p->p_lwptree));
+       radix_tree_fini_tree(&p->p_lwptree);
        mutex_exit(p->p_lock);
 
        /*
@@ -1256,6 +1262,7 @@
        cv_destroy(&p->p_waitcv);
        cv_destroy(&p->p_lwpcv);
        rw_destroy(&p->p_reflock);
+       rw_destroy(&p->p_treelock);
 
        proc_free_mem(p);
 }
diff -r 2474239e330b -r 83748c726ae1 sys/kern/kern_fork.c
--- a/sys/kern/kern_fork.c      Wed Jan 29 15:31:14 2020 +0000
+++ b/sys/kern/kern_fork.c      Wed Jan 29 15:47:51 2020 +0000



Home | Main Index | Thread Index | Old Index