2010-10-29 19:32:23 +04:00
|
|
|
/* $NetBSD: threads.c,v 1.13 2010/10/29 15:32:24 pooka Exp $ */
|
2009-11-04 22:17:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007-2009 Antti Kantee. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Development of this software was supported by
|
|
|
|
* The Finnish Cultural Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-10-29 19:32:23 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.13 2010/10/29 15:32:24 pooka Exp $");
|
2009-11-04 22:17:53 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2010-06-01 03:09:29 +04:00
|
|
|
#include <sys/atomic.h>
|
2009-11-04 22:17:53 +03:00
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/kthread.h>
|
2010-06-03 23:36:21 +04:00
|
|
|
#include <sys/malloc.h>
|
2009-11-04 22:17:53 +03:00
|
|
|
#include <sys/systm.h>
|
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
|
|
|
#include <rump/rumpuser.h>
|
|
|
|
|
|
|
|
#include "rump_private.h"
|
|
|
|
|
|
|
|
struct kthdesc {
|
|
|
|
void (*f)(void *);
|
|
|
|
void *arg;
|
|
|
|
struct lwp *mylwp;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
|
|
|
threadbouncer(void *arg)
|
|
|
|
{
|
|
|
|
struct kthdesc *k = arg;
|
2009-11-09 22:00:52 +03:00
|
|
|
struct lwp *l = k->mylwp;
|
2009-11-04 22:17:53 +03:00
|
|
|
void (*f)(void *);
|
|
|
|
void *thrarg;
|
|
|
|
|
|
|
|
f = k->f;
|
|
|
|
thrarg = k->arg;
|
2009-11-09 22:00:52 +03:00
|
|
|
|
2009-11-09 22:02:49 +03:00
|
|
|
/* schedule ourselves */
|
2009-11-09 22:00:52 +03:00
|
|
|
rumpuser_set_curlwp(l);
|
2009-11-04 22:17:53 +03:00
|
|
|
rump_schedule();
|
|
|
|
|
2010-06-03 23:36:21 +04:00
|
|
|
/* free dance struct */
|
|
|
|
free(k, M_TEMP);
|
|
|
|
|
2009-11-04 22:17:53 +03:00
|
|
|
if ((curlwp->l_pflag & LP_MPSAFE) == 0)
|
|
|
|
KERNEL_LOCK(1, NULL);
|
|
|
|
|
|
|
|
f(thrarg);
|
|
|
|
|
|
|
|
panic("unreachable, should kthread_exit()");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kthread_create(pri_t pri, int flags, struct cpu_info *ci,
|
|
|
|
void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char thrstore[MAXCOMLEN];
|
|
|
|
const char *thrname = NULL;
|
|
|
|
va_list ap;
|
|
|
|
struct kthdesc *k;
|
|
|
|
struct lwp *l;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
thrstore[0] = '\0';
|
|
|
|
if (fmt) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(thrstore, sizeof(thrstore), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
thrname = thrstore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't want a module unload thread.
|
|
|
|
* (XXX: yes, this is a kludge too, and the kernel should
|
|
|
|
* have a more flexible method for configuring which threads
|
|
|
|
* we want).
|
|
|
|
*/
|
|
|
|
if (strcmp(thrstore, "modunload") == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rump_threads) {
|
|
|
|
/* fake them */
|
|
|
|
if (strcmp(thrstore, "vrele") == 0) {
|
|
|
|
printf("rump warning: threads not enabled, not starting"
|
|
|
|
" vrele thread\n");
|
|
|
|
return 0;
|
|
|
|
} else if (strcmp(thrstore, "cachegc") == 0) {
|
|
|
|
printf("rump warning: threads not enabled, not starting"
|
|
|
|
" namecache g/c thread\n");
|
|
|
|
return 0;
|
|
|
|
} else if (strcmp(thrstore, "nfssilly") == 0) {
|
|
|
|
printf("rump warning: threads not enabled, not enabling"
|
|
|
|
" nfs silly rename\n");
|
|
|
|
return 0;
|
|
|
|
} else if (strcmp(thrstore, "unpgc") == 0) {
|
|
|
|
printf("rump warning: threads not enabled, not enabling"
|
|
|
|
" UNP garbage collection\n");
|
|
|
|
return 0;
|
2010-01-27 23:16:16 +03:00
|
|
|
} else if (strncmp(thrstore, "pmf", sizeof("pmf")-1) == 0) {
|
|
|
|
printf("rump warning: threads not enabled, not enabling"
|
|
|
|
" pmf thread\n");
|
|
|
|
return 0;
|
2009-12-03 15:16:36 +03:00
|
|
|
} else if (strncmp(thrstore, "xcall", sizeof("xcall")-1) == 0) {
|
|
|
|
printf("rump warning: threads not enabled, CPU xcall"
|
|
|
|
" not functional\n");
|
|
|
|
return 0;
|
2009-11-04 22:17:53 +03:00
|
|
|
} else
|
|
|
|
panic("threads not available, setenv RUMP_THREADS 1");
|
|
|
|
}
|
|
|
|
KASSERT(fmt != NULL);
|
|
|
|
|
2010-06-03 23:36:21 +04:00
|
|
|
k = malloc(sizeof(*k), M_TEMP, M_WAITOK);
|
2009-11-04 22:17:53 +03:00
|
|
|
k->f = func;
|
|
|
|
k->arg = arg;
|
2010-10-29 19:32:23 +04:00
|
|
|
k->mylwp = l = rump__lwproc_alloclwp(&proc0);
|
Improve the CPU scheduler for a host MP system with multithreaded
access. The old scheduler had a global freelist which caused a
cache crisis with multiple host threads trying to schedule a virtual
CPU simultaneously.
The rump scheduler is different from a normal thread scheduler, so
it has different requirements. First, we schedule a CPU for a
thread (which we get from the host scheduler) instead of scheduling
a thread onto a CPU. Second, scheduling points are at every
entry/exit to/from the rump kernel, including (but not limited to)
syscall entry points and hypercalls. This means scheduling happens
a lot more frequently than in a normal kernel.
For every lwp, cache the previously used CPU. When scheduling,
attempt to reuse the same CPU. If we get it, we can use it directly
without any memory barriers or expensive locks. If the CPU is
taken, migrate. Use a lock/wait only in the slowpath. Be very
wary of walking the entire CPU array because that does not lead to
a happy cacher.
The migration algorithm could probably benefit from improved
heuristics and tuning. Even as such, with the new scheduler an
application which has two threads making rlimit syscalls in a tight
loop experiences almost 400% speedup. The exact speedup is difficult
to pinpoint, though, since the old scheduler caused very jittery
results due to cache contention. Also, the rump version is now
70% faster than the counterpart which calls the host kernel.
2010-05-28 20:44:14 +04:00
|
|
|
l->l_flag |= LW_SYSTEM;
|
2009-11-04 22:17:53 +03:00
|
|
|
if (flags & KTHREAD_MPSAFE)
|
|
|
|
l->l_pflag |= LP_MPSAFE;
|
2009-11-09 22:00:52 +03:00
|
|
|
if (flags & KTHREAD_INTR)
|
|
|
|
l->l_pflag |= LP_INTR;
|
2009-12-01 12:50:51 +03:00
|
|
|
if (ci) {
|
|
|
|
l->l_pflag |= LP_BOUND;
|
Improve the CPU scheduler for a host MP system with multithreaded
access. The old scheduler had a global freelist which caused a
cache crisis with multiple host threads trying to schedule a virtual
CPU simultaneously.
The rump scheduler is different from a normal thread scheduler, so
it has different requirements. First, we schedule a CPU for a
thread (which we get from the host scheduler) instead of scheduling
a thread onto a CPU. Second, scheduling points are at every
entry/exit to/from the rump kernel, including (but not limited to)
syscall entry points and hypercalls. This means scheduling happens
a lot more frequently than in a normal kernel.
For every lwp, cache the previously used CPU. When scheduling,
attempt to reuse the same CPU. If we get it, we can use it directly
without any memory barriers or expensive locks. If the CPU is
taken, migrate. Use a lock/wait only in the slowpath. Be very
wary of walking the entire CPU array because that does not lead to
a happy cacher.
The migration algorithm could probably benefit from improved
heuristics and tuning. Even as such, with the new scheduler an
application which has two threads making rlimit syscalls in a tight
loop experiences almost 400% speedup. The exact speedup is difficult
to pinpoint, though, since the old scheduler caused very jittery
results due to cache contention. Also, the rump version is now
70% faster than the counterpart which calls the host kernel.
2010-05-28 20:44:14 +04:00
|
|
|
l->l_target_cpu = ci;
|
2009-12-01 12:50:51 +03:00
|
|
|
}
|
2010-02-09 19:53:13 +03:00
|
|
|
if (thrname) {
|
|
|
|
l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
|
|
|
|
strlcpy(l->l_name, thrname, MAXCOMLEN);
|
|
|
|
}
|
|
|
|
|
2010-06-01 03:09:29 +04:00
|
|
|
rv = rumpuser_thread_create(threadbouncer, k, thrname,
|
|
|
|
(flags & KTHREAD_JOINABLE) == KTHREAD_JOINABLE, &l->l_ctxlink);
|
2009-11-04 22:17:53 +03:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2010-06-01 03:09:29 +04:00
|
|
|
if (newlp) {
|
2009-11-04 22:17:53 +03:00
|
|
|
*newlp = l;
|
2010-06-01 03:09:29 +04:00
|
|
|
} else {
|
|
|
|
KASSERT((flags & KTHREAD_JOINABLE) == 0);
|
|
|
|
}
|
|
|
|
|
2009-11-04 22:17:53 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kthread_exit(int ecode)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((curlwp->l_pflag & LP_MPSAFE) == 0)
|
2009-12-05 15:54:11 +03:00
|
|
|
KERNEL_UNLOCK_LAST(NULL);
|
2010-09-01 23:37:58 +04:00
|
|
|
rump_lwproc_releaselwp();
|
2010-06-01 03:09:29 +04:00
|
|
|
/* unschedule includes membar */
|
2009-11-04 22:17:53 +03:00
|
|
|
rump_unschedule();
|
|
|
|
rumpuser_thread_exit();
|
|
|
|
}
|
2010-06-01 03:09:29 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
kthread_join(struct lwp *l)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
KASSERT(l->l_ctxlink != NULL);
|
|
|
|
rv = rumpuser_thread_join(l->l_ctxlink);
|
|
|
|
membar_consumer();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|