2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2001-02-12 00:20:08 +00:00
|
|
|
* Copyright (c) 2001 Jake Burkholder <jake@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
1999-08-19 00:06:53 +00:00
|
|
|
*
|
|
|
|
* 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 AND CONTRIBUTORS ``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.
|
|
|
|
*/
|
|
|
|
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
/***
|
|
|
|
Here is the logic..
|
|
|
|
|
|
|
|
If there are N processors, then there are at most N KSEs (kernel
|
|
|
|
schedulable entities) working to process threads that belong to a
|
2003-12-12 21:25:56 +00:00
|
|
|
KSEGROUP (kg). If there are X of these KSEs actually running at the
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
moment in question, then there are at most M (N-X) of these KSEs on
|
|
|
|
the run queue, as running KSEs are not on the queue.
|
|
|
|
|
|
|
|
Runnable threads are queued off the KSEGROUP in priority order.
|
|
|
|
If there are M or more threads runnable, the top M threads
|
|
|
|
(by priority) are 'preassigned' to the M KSEs not running. The KSEs take
|
|
|
|
their priority from those threads and are put on the run queue.
|
|
|
|
|
|
|
|
The last thread that had a priority high enough to have a KSE associated
|
|
|
|
with it, AND IS ON THE RUN QUEUE is pointed to by
|
|
|
|
kg->kg_last_assigned. If no threads queued off the KSEGROUP have KSEs
|
|
|
|
assigned as all the available KSEs are activly running, or because there
|
|
|
|
are no threads queued, that pointer is NULL.
|
|
|
|
|
|
|
|
When a KSE is removed from the run queue to become runnable, we know
|
|
|
|
it was associated with the highest priority thread in the queue (at the head
|
|
|
|
of the queue). If it is also the last assigned we know M was 1 and must
|
|
|
|
now be 0. Since the thread is no longer queued that pointer must be
|
|
|
|
removed from it. Since we know there were no more KSEs available,
|
|
|
|
(M was 1 and is now 0) and since we are not FREEING our KSE
|
|
|
|
but using it, we know there are STILL no more KSEs available, we can prove
|
|
|
|
that the next thread in the ksegrp list will not have a KSE to assign to
|
|
|
|
it, so we can show that the pointer must be made 'invalid' (NULL).
|
|
|
|
|
|
|
|
The pointer exists so that when a new thread is made runnable, it can
|
|
|
|
have its priority compared with the last assigned thread to see if
|
|
|
|
it should 'steal' its KSE or not.. i.e. is it 'earlier'
|
|
|
|
on the list than that thread or later.. If it's earlier, then the KSE is
|
|
|
|
removed from the last assigned (which is now not assigned a KSE)
|
|
|
|
and reassigned to the new thread, which is placed earlier in the list.
|
|
|
|
The pointer is then backed up to the previous thread (which may or may not
|
|
|
|
be the new thread).
|
|
|
|
|
2005-12-28 17:13:31 +00:00
|
|
|
When a thread sleeps or is removed, the KSE becomes available and if there
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
are queued threads that are not assigned KSEs, the highest priority one of
|
|
|
|
them is assigned the KSE, which is then placed back on the run queue at
|
|
|
|
the approipriate place, and the kg->kg_last_assigned pointer is adjusted down
|
|
|
|
to point to it.
|
|
|
|
|
|
|
|
The following diagram shows 2 KSEs and 3 threads from a single process.
|
|
|
|
|
|
|
|
RUNQ: --->KSE---KSE--... (KSEs queued at priorities from threads)
|
2005-12-28 17:13:31 +00:00
|
|
|
\ \____
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
\ \
|
|
|
|
KSEGROUP---thread--thread--thread (queued in priority order)
|
2005-12-28 17:13:31 +00:00
|
|
|
\ /
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
\_______________/
|
|
|
|
(last_assigned)
|
|
|
|
|
|
|
|
The result of this scheme is that the M available KSEs are always
|
|
|
|
queued at the priorities they have inherrited from the M highest priority
|
2005-12-28 17:13:31 +00:00
|
|
|
threads for that KSEGROUP. If this situation changes, the KSEs are
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
reassigned to keep this true.
|
2003-06-11 00:56:59 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
|
2004-09-01 06:42:02 +00:00
|
|
|
#include "opt_sched.h"
|
2004-07-02 20:21:44 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
#ifndef KERN_SWITCH_INCLUDE
|
1999-08-19 00:06:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2004-07-10 21:36:01 +00:00
|
|
|
#include <sys/kdb.h>
|
1999-08-19 00:06:53 +00:00
|
|
|
#include <sys/kernel.h>
|
2000-09-07 01:33:02 +00:00
|
|
|
#include <sys/ktr.h>
|
2001-03-28 09:17:56 +00:00
|
|
|
#include <sys/lock.h>
|
2000-10-20 07:58:15 +00:00
|
|
|
#include <sys/mutex.h>
|
1999-08-19 00:06:53 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/queue.h>
|
2002-10-12 05:32:24 +00:00
|
|
|
#include <sys/sched.h>
|
2004-09-05 02:09:54 +00:00
|
|
|
#else /* KERN_SWITCH_INCLUDE */
|
2003-11-17 08:58:16 +00:00
|
|
|
#if defined(SMP) && (defined(__i386__) || defined(__amd64__))
|
2003-04-02 23:53:30 +00:00
|
|
|
#include <sys/smp.h>
|
|
|
|
#endif
|
2004-09-01 06:42:02 +00:00
|
|
|
#if defined(SMP) && defined(SCHED_4BSD)
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2005-12-18 18:10:57 +00:00
|
|
|
/* Uncomment this to enable logging of critical_enter/exit. */
|
|
|
|
#if 0
|
|
|
|
#define KTR_CRITICAL KTR_SCHED
|
|
|
|
#else
|
|
|
|
#define KTR_CRITICAL 0
|
|
|
|
#endif
|
|
|
|
|
2004-09-02 18:59:15 +00:00
|
|
|
#ifdef FULL_PREEMPTION
|
|
|
|
#ifndef PREEMPTION
|
|
|
|
#error "The FULL_PREEMPTION option requires the PREEMPTION option"
|
|
|
|
#endif
|
|
|
|
#endif
|
1999-08-19 00:06:53 +00:00
|
|
|
|
2002-05-25 01:12:23 +00:00
|
|
|
CTASSERT((RQB_BPW * RQB_LEN) == RQ_NQS);
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
#define td_kse td_sched
|
|
|
|
|
2005-03-20 17:05:12 +00:00
|
|
|
/*
|
|
|
|
* kern.sched.preemption allows user space to determine if preemption support
|
|
|
|
* is compiled in or not. It is not currently a boot or runtime flag that
|
|
|
|
* can be changed.
|
|
|
|
*/
|
|
|
|
#ifdef PREEMPTION
|
|
|
|
static int kern_sched_preemption = 1;
|
|
|
|
#else
|
|
|
|
static int kern_sched_preemption = 0;
|
|
|
|
#endif
|
|
|
|
SYSCTL_INT(_kern_sched, OID_AUTO, preemption, CTLFLAG_RD,
|
|
|
|
&kern_sched_preemption, 0, "Kernel preemption enabled");
|
|
|
|
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
/************************************************************************
|
|
|
|
* Functions that manipulate runnability from a thread perspective. *
|
|
|
|
************************************************************************/
|
1999-08-19 00:06:53 +00:00
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* Select the KSE that will be run next. From that find the thread, and
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
* remove it from the KSEGRP's run queue. If there is thread clustering,
|
|
|
|
* this will be what does it.
|
1999-08-19 00:06:53 +00:00
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *
|
|
|
|
choosethread(void)
|
1999-08-19 00:06:53 +00:00
|
|
|
{
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
struct kse *ke;
|
|
|
|
struct thread *td;
|
|
|
|
struct ksegrp *kg;
|
|
|
|
|
2003-11-17 08:58:16 +00:00
|
|
|
#if defined(SMP) && (defined(__i386__) || defined(__amd64__))
|
2003-04-02 23:53:30 +00:00
|
|
|
if (smp_active == 0 && PCPU_GET(cpuid) != 0) {
|
|
|
|
/* Shutting down, run idlethread on AP's */
|
|
|
|
td = PCPU_GET(idlethread);
|
|
|
|
ke = td->td_kse;
|
|
|
|
CTR1(KTR_RUNQ, "choosethread: td=%p (idle)", td);
|
|
|
|
ke->ke_flags |= KEF_DIDRUN;
|
|
|
|
TD_SET_RUNNING(td);
|
|
|
|
return (td);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-07-17 02:23:44 +00:00
|
|
|
retry:
|
2003-04-02 23:53:30 +00:00
|
|
|
ke = sched_choose();
|
|
|
|
if (ke) {
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
td = ke->ke_thread;
|
|
|
|
KASSERT((td->td_kse == ke), ("kse/thread mismatch"));
|
|
|
|
kg = ke->ke_ksegrp;
|
2004-09-05 02:09:54 +00:00
|
|
|
if (td->td_proc->p_flag & P_HADTHREADS) {
|
2002-09-23 05:27:30 +00:00
|
|
|
if (kg->kg_last_assigned == td) {
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
kg->kg_last_assigned = TAILQ_PREV(td,
|
|
|
|
threadqueue, td_runq);
|
2002-09-23 05:27:30 +00:00
|
|
|
}
|
2003-03-09 04:27:46 +00:00
|
|
|
TAILQ_REMOVE(&kg->kg_runq, td, td_runq);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
}
|
|
|
|
CTR2(KTR_RUNQ, "choosethread: td=%p pri=%d",
|
|
|
|
td, td->td_priority);
|
|
|
|
} else {
|
2002-07-12 20:16:46 +00:00
|
|
|
/* Simulate runq_choose() having returned the idle thread */
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
td = PCPU_GET(idlethread);
|
2002-08-30 00:25:49 +00:00
|
|
|
ke = td->td_kse;
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
CTR1(KTR_RUNQ, "choosethread: td=%p (idle)", td);
|
|
|
|
}
|
2002-08-30 00:25:49 +00:00
|
|
|
ke->ke_flags |= KEF_DIDRUN;
|
2002-12-28 01:23:07 +00:00
|
|
|
|
|
|
|
/*
|
2003-05-21 18:53:25 +00:00
|
|
|
* If we are in panic, only allow system threads,
|
|
|
|
* plus the one we are running in, to be run.
|
2002-12-28 01:23:07 +00:00
|
|
|
*/
|
2002-07-17 02:23:44 +00:00
|
|
|
if (panicstr && ((td->td_proc->p_flag & P_SYSTEM) == 0 &&
|
2003-05-21 18:53:25 +00:00
|
|
|
(td->td_flags & TDF_INPANIC) == 0)) {
|
|
|
|
/* note that it is no longer on the run queue */
|
|
|
|
TD_SET_CAN_RUN(td);
|
2002-07-17 02:23:44 +00:00
|
|
|
goto retry;
|
2003-05-21 18:53:25 +00:00
|
|
|
}
|
2002-12-28 01:23:07 +00:00
|
|
|
|
2002-09-11 08:13:56 +00:00
|
|
|
TD_SET_RUNNING(td);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
return (td);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-09-05 02:09:54 +00:00
|
|
|
* Given a surplus system slot, try assign a new runnable thread to it.
|
|
|
|
* Called from:
|
|
|
|
* sched_thread_exit() (local)
|
|
|
|
* sched_switch() (local)
|
|
|
|
* sched_thread_exit() (local)
|
2004-09-16 07:12:59 +00:00
|
|
|
* remrunqueue() (local) (not at the moment)
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
static void
|
|
|
|
slot_fill(struct ksegrp *kg)
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
|
2002-09-23 05:27:30 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2004-09-05 02:09:54 +00:00
|
|
|
while (kg->kg_avail_opennings > 0) {
|
|
|
|
/*
|
|
|
|
* Find the first unassigned thread
|
|
|
|
*/
|
|
|
|
if ((td = kg->kg_last_assigned) != NULL)
|
|
|
|
td = TAILQ_NEXT(td, td_runq);
|
|
|
|
else
|
|
|
|
td = TAILQ_FIRST(&kg->kg_runq);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
/*
|
|
|
|
* If we found one, send it to the system scheduler.
|
|
|
|
*/
|
|
|
|
if (td) {
|
|
|
|
kg->kg_last_assigned = td;
|
2004-10-12 16:30:20 +00:00
|
|
|
sched_add(td, SRQ_YIELDING);
|
2004-09-05 02:09:54 +00:00
|
|
|
CTR2(KTR_RUNQ, "slot_fill: td%p -> kg%p", td, kg);
|
|
|
|
} else {
|
|
|
|
/* no threads to use up the slots. quit now */
|
|
|
|
break;
|
|
|
|
}
|
2002-10-09 02:33:36 +00:00
|
|
|
}
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
1999-08-19 00:06:53 +00:00
|
|
|
|
2004-09-15 03:51:51 +00:00
|
|
|
#ifdef SCHED_4BSD
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
/*
|
|
|
|
* Remove a thread from its KSEGRP's run queue.
|
|
|
|
* This in turn may remove it from a KSE if it was already assigned
|
|
|
|
* to one, possibly causing a new thread to be assigned to the KSE
|
2003-02-17 05:14:26 +00:00
|
|
|
* and the KSE getting a new priority.
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
*/
|
2002-10-14 20:34:31 +00:00
|
|
|
static void
|
2001-09-12 08:38:13 +00:00
|
|
|
remrunqueue(struct thread *td)
|
2001-02-12 00:20:08 +00:00
|
|
|
{
|
2002-10-09 02:33:36 +00:00
|
|
|
struct thread *td2, *td3;
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
struct ksegrp *kg;
|
|
|
|
struct kse *ke;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2003-02-17 05:14:26 +00:00
|
|
|
KASSERT((TD_ON_RUNQ(td)), ("remrunqueue: Bad state on run queue"));
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
kg = td->td_ksegrp;
|
|
|
|
ke = td->td_kse;
|
|
|
|
CTR1(KTR_RUNQ, "remrunqueue: td%p", td);
|
2002-09-11 08:13:56 +00:00
|
|
|
TD_SET_CAN_RUN(td);
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
* If it is not a threaded process, take the shortcut.
|
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
|
2004-09-10 21:04:38 +00:00
|
|
|
/* remve from sys run queue and free up a slot */
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_rem(td);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-10-09 02:33:36 +00:00
|
|
|
td3 = TAILQ_PREV(td, threadqueue, td_runq);
|
|
|
|
TAILQ_REMOVE(&kg->kg_runq, td, td_runq);
|
2004-09-05 02:09:54 +00:00
|
|
|
if (ke->ke_state == KES_ONRUNQ) {
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
/*
|
2004-09-10 21:04:38 +00:00
|
|
|
* This thread has been assigned to the system run queue.
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
* We need to dissociate it and try assign the
|
|
|
|
* KSE to the next available thread. Then, we should
|
|
|
|
* see if we need to move the KSE in the run queues.
|
|
|
|
*/
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_rem(td);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
td2 = kg->kg_last_assigned;
|
2003-02-17 05:14:26 +00:00
|
|
|
KASSERT((td2 != NULL), ("last assigned has wrong value"));
|
2005-12-28 17:13:31 +00:00
|
|
|
if (td2 == td)
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
kg->kg_last_assigned = td3;
|
2004-09-10 21:04:38 +00:00
|
|
|
/* slot_fill(kg); */ /* will replace it with another */
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
}
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|
2004-09-15 03:51:51 +00:00
|
|
|
#endif
|
2002-10-14 20:34:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Change the priority of a thread that is on the run queue.
|
|
|
|
*/
|
|
|
|
void
|
2005-12-28 17:13:31 +00:00
|
|
|
adjustrunqueue( struct thread *td, int newpri)
|
2002-10-14 20:34:31 +00:00
|
|
|
{
|
|
|
|
struct ksegrp *kg;
|
|
|
|
struct kse *ke;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2003-02-17 05:14:26 +00:00
|
|
|
KASSERT((TD_ON_RUNQ(td)), ("adjustrunqueue: Bad state on run queue"));
|
|
|
|
|
2003-02-01 12:17:09 +00:00
|
|
|
ke = td->td_kse;
|
|
|
|
CTR1(KTR_RUNQ, "adjustrunqueue: td%p", td);
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
* If it is not a threaded process, take the shortcut.
|
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
|
2002-10-14 20:34:31 +00:00
|
|
|
/* We only care about the kse in the run queue. */
|
2002-10-14 20:43:02 +00:00
|
|
|
td->td_priority = newpri;
|
Add scheduler CORE, the work I have done half a year ago, recent,
I picked it up again. The scheduler is forked from ULE, but the
algorithm to detect an interactive process is almost completely
different with ULE, it comes from Linux paper "Understanding the
Linux 2.6.8.1 CPU Scheduler", although I still use same word
"score" as a priority boost in ULE scheduler.
Briefly, the scheduler has following characteristic:
1. Timesharing process's nice value is seriously respected,
timeslice and interaction detecting algorithm are based
on nice value.
2. per-cpu scheduling queue and load balancing.
3. O(1) scheduling.
4. Some cpu affinity code in wakeup path.
5. Support POSIX SCHED_FIFO and SCHED_RR.
Unlike scheduler 4BSD and ULE which using fuzzy RQ_PPQ, the scheduler
uses 256 priority queues. Unlike ULE which using pull and push, the
scheduelr uses pull method, the main reason is to let relative idle
cpu do the work, but current the whole scheduler is protected by the
big sched_lock, so the benefit is not visible, it really can be worse
than nothing because all other cpu are locked out when we are doing
balancing work, which the 4BSD scheduelr does not have this problem.
The scheduler does not support hyperthreading very well, in fact,
the scheduler does not make the difference between physical CPU and
logical CPU, this should be improved in feature. The scheduler has
priority inversion problem on MP machine, it is not good for
realtime scheduling, it can cause realtime process starving.
As a result, it seems the MySQL super-smack runs better on my
Pentium-D machine when using libthr, despite on UP or SMP kernel.
2006-06-13 13:12:56 +00:00
|
|
|
#ifndef SCHED_CORE
|
|
|
|
if (ke->ke_rqindex != (newpri / RQ_PPQ))
|
|
|
|
#else
|
|
|
|
if (ke->ke_rqindex != newpri)
|
|
|
|
#endif
|
|
|
|
{
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_rem(td);
|
2004-09-01 02:11:28 +00:00
|
|
|
sched_add(td, SRQ_BORING);
|
2002-10-14 20:34:31 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
|
|
|
|
/* It is a threaded process */
|
2002-10-14 20:34:31 +00:00
|
|
|
kg = td->td_ksegrp;
|
2005-08-03 01:23:45 +00:00
|
|
|
if (ke->ke_state == KES_ONRUNQ
|
|
|
|
#ifdef SCHED_ULE
|
|
|
|
|| ((ke->ke_flags & KEF_ASSIGNED) != 0 &&
|
|
|
|
(ke->ke_flags & KEF_REMOVED) == 0)
|
|
|
|
#endif
|
|
|
|
) {
|
2002-10-14 20:34:31 +00:00
|
|
|
if (kg->kg_last_assigned == td) {
|
|
|
|
kg->kg_last_assigned =
|
|
|
|
TAILQ_PREV(td, threadqueue, td_runq);
|
|
|
|
}
|
2003-10-16 08:39:15 +00:00
|
|
|
sched_rem(td);
|
2002-10-14 20:34:31 +00:00
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&kg->kg_runq, td, td_runq);
|
2004-09-16 07:12:59 +00:00
|
|
|
TD_SET_CAN_RUN(td);
|
2002-10-14 20:34:31 +00:00
|
|
|
td->td_priority = newpri;
|
2004-09-01 02:11:28 +00:00
|
|
|
setrunqueue(td, SRQ_BORING);
|
2002-10-14 20:34:31 +00:00
|
|
|
}
|
2004-10-12 16:30:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called when a thread is about to be put on a
|
2005-12-28 17:13:31 +00:00
|
|
|
* ksegrp run queue because it has been made runnable or its
|
|
|
|
* priority has been adjusted and the ksegrp does not have a
|
2004-10-12 16:30:20 +00:00
|
|
|
* free kse slot. It determines if a thread from the same ksegrp
|
|
|
|
* should be preempted. If so, it tries to switch threads
|
|
|
|
* if the thread is on the same cpu or notifies another cpu that
|
2005-12-28 17:13:31 +00:00
|
|
|
* it should switch threads.
|
2004-10-12 16:30:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
maybe_preempt_in_ksegrp(struct thread *td)
|
2004-10-13 22:07:04 +00:00
|
|
|
#if !defined(SMP)
|
|
|
|
{
|
|
|
|
struct thread *running_thread;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
running_thread = curthread;
|
|
|
|
|
|
|
|
if (running_thread->td_ksegrp != td->td_ksegrp)
|
|
|
|
return;
|
|
|
|
|
2005-05-19 01:08:30 +00:00
|
|
|
if (td->td_priority >= running_thread->td_priority)
|
2004-10-13 22:07:04 +00:00
|
|
|
return;
|
|
|
|
#ifdef PREEMPTION
|
2005-06-09 18:26:31 +00:00
|
|
|
#ifndef FULL_PREEMPTION
|
|
|
|
if (td->td_priority > PRI_MAX_ITHD) {
|
|
|
|
running_thread->td_flags |= TDF_NEEDRESCHED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* FULL_PREEMPTION */
|
|
|
|
|
2005-12-28 17:13:31 +00:00
|
|
|
if (running_thread->td_critnest > 1)
|
2005-04-08 03:37:53 +00:00
|
|
|
running_thread->td_owepreempt = 1;
|
2005-12-28 17:13:31 +00:00
|
|
|
else
|
2004-10-13 22:07:04 +00:00
|
|
|
mi_switch(SW_INVOL, NULL);
|
2005-12-28 17:13:31 +00:00
|
|
|
|
2005-06-09 18:26:31 +00:00
|
|
|
#else /* PREEMPTION */
|
2004-10-13 22:07:04 +00:00
|
|
|
running_thread->td_flags |= TDF_NEEDRESCHED;
|
2005-06-09 18:26:31 +00:00
|
|
|
#endif /* PREEMPTION */
|
2004-10-13 22:07:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* SMP */
|
2004-10-12 16:30:20 +00:00
|
|
|
{
|
2004-10-12 20:57:37 +00:00
|
|
|
struct thread *running_thread;
|
2004-10-12 16:30:20 +00:00
|
|
|
int worst_pri;
|
|
|
|
struct ksegrp *kg;
|
|
|
|
cpumask_t cpumask,dontuse;
|
|
|
|
struct pcpu *pc;
|
|
|
|
struct pcpu *best_pcpu;
|
|
|
|
struct thread *cputhread;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
|
|
|
|
running_thread = curthread;
|
|
|
|
|
|
|
|
#if !defined(KSEG_PEEMPT_BEST_CPU)
|
|
|
|
if (running_thread->td_ksegrp != td->td_ksegrp) {
|
|
|
|
#endif
|
|
|
|
kg = td->td_ksegrp;
|
|
|
|
|
|
|
|
/* if someone is ahead of this thread, wait our turn */
|
2005-12-28 17:13:31 +00:00
|
|
|
if (td != TAILQ_FIRST(&kg->kg_runq))
|
2004-10-12 16:30:20 +00:00
|
|
|
return;
|
2005-12-28 17:13:31 +00:00
|
|
|
|
2004-10-12 16:30:20 +00:00
|
|
|
worst_pri = td->td_priority;
|
|
|
|
best_pcpu = NULL;
|
|
|
|
dontuse = stopped_cpus | idle_cpus_mask;
|
2005-12-28 17:13:31 +00:00
|
|
|
|
|
|
|
/*
|
2004-10-12 16:30:20 +00:00
|
|
|
* Find a cpu with the worst priority that runs at thread from
|
|
|
|
* the same ksegrp - if multiple exist give first the last run
|
2005-12-28 17:13:31 +00:00
|
|
|
* cpu and then the current cpu priority
|
2004-10-12 16:30:20 +00:00
|
|
|
*/
|
2005-12-28 17:13:31 +00:00
|
|
|
|
2004-10-12 16:30:20 +00:00
|
|
|
SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
|
|
|
|
cpumask = pc->pc_cpumask;
|
|
|
|
cputhread = pc->pc_curthread;
|
|
|
|
|
2005-12-28 17:13:31 +00:00
|
|
|
if ((cpumask & dontuse) ||
|
2004-10-12 16:30:20 +00:00
|
|
|
cputhread->td_ksegrp != kg)
|
2005-12-28 17:13:31 +00:00
|
|
|
continue;
|
2004-10-12 16:30:20 +00:00
|
|
|
|
|
|
|
if (cputhread->td_priority > worst_pri) {
|
|
|
|
worst_pri = cputhread->td_priority;
|
2005-12-28 17:13:31 +00:00
|
|
|
best_pcpu = pc;
|
2004-10-12 16:30:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-12-28 17:13:31 +00:00
|
|
|
|
2004-10-12 16:30:20 +00:00
|
|
|
if (cputhread->td_priority == worst_pri &&
|
2005-12-28 17:13:31 +00:00
|
|
|
best_pcpu != NULL &&
|
2004-10-12 16:30:20 +00:00
|
|
|
(td->td_lastcpu == pc->pc_cpuid ||
|
|
|
|
(PCPU_GET(cpumask) == cpumask &&
|
2005-12-28 17:13:31 +00:00
|
|
|
td->td_lastcpu != best_pcpu->pc_cpuid)))
|
2004-10-12 16:30:20 +00:00
|
|
|
best_pcpu = pc;
|
2005-12-28 17:13:31 +00:00
|
|
|
}
|
|
|
|
|
2004-10-12 16:30:20 +00:00
|
|
|
/* Check if we need to preempt someone */
|
2005-12-28 17:13:31 +00:00
|
|
|
if (best_pcpu == NULL)
|
2004-10-12 16:30:20 +00:00
|
|
|
return;
|
|
|
|
|
2005-06-09 18:26:31 +00:00
|
|
|
#if defined(IPI_PREEMPTION) && defined(PREEMPTION)
|
|
|
|
#if !defined(FULL_PREEMPTION)
|
2005-06-09 19:43:08 +00:00
|
|
|
if (td->td_priority <= PRI_MAX_ITHD)
|
2005-06-09 18:26:31 +00:00
|
|
|
#endif /* ! FULL_PREEMPTION */
|
|
|
|
{
|
|
|
|
ipi_selected(best_pcpu->pc_cpumask, IPI_PREEMPT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* defined(IPI_PREEMPTION) && defined(PREEMPTION) */
|
|
|
|
|
2004-10-12 16:30:20 +00:00
|
|
|
if (PCPU_GET(cpuid) != best_pcpu->pc_cpuid) {
|
|
|
|
best_pcpu->pc_curthread->td_flags |= TDF_NEEDRESCHED;
|
|
|
|
ipi_selected(best_pcpu->pc_cpumask, IPI_AST);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if !defined(KSEG_PEEMPT_BEST_CPU)
|
2005-12-28 17:13:31 +00:00
|
|
|
}
|
2004-10-12 16:30:20 +00:00
|
|
|
#endif
|
|
|
|
|
2005-05-19 01:08:30 +00:00
|
|
|
if (td->td_priority >= running_thread->td_priority)
|
2004-10-12 16:30:20 +00:00
|
|
|
return;
|
|
|
|
#ifdef PREEMPTION
|
2005-06-09 18:26:31 +00:00
|
|
|
|
|
|
|
#if !defined(FULL_PREEMPTION)
|
2005-06-09 19:43:08 +00:00
|
|
|
if (td->td_priority > PRI_MAX_ITHD) {
|
2005-06-09 18:26:31 +00:00
|
|
|
running_thread->td_flags |= TDF_NEEDRESCHED;
|
|
|
|
}
|
|
|
|
#endif /* ! FULL_PREEMPTION */
|
2005-12-28 17:13:31 +00:00
|
|
|
|
|
|
|
if (running_thread->td_critnest > 1)
|
2005-04-08 03:37:53 +00:00
|
|
|
running_thread->td_owepreempt = 1;
|
2005-12-28 17:13:31 +00:00
|
|
|
else
|
2004-10-12 16:30:20 +00:00
|
|
|
mi_switch(SW_INVOL, NULL);
|
2005-12-28 17:13:31 +00:00
|
|
|
|
2005-06-09 18:26:31 +00:00
|
|
|
#else /* PREEMPTION */
|
2004-10-12 16:30:20 +00:00
|
|
|
running_thread->td_flags |= TDF_NEEDRESCHED;
|
2005-06-09 18:26:31 +00:00
|
|
|
#endif /* PREEMPTION */
|
2004-10-12 16:30:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-10-13 22:07:04 +00:00
|
|
|
#endif /* !SMP */
|
|
|
|
|
2004-10-12 16:30:20 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
int limitcount;
|
1999-08-19 00:06:53 +00:00
|
|
|
void
|
2004-09-01 02:11:28 +00:00
|
|
|
setrunqueue(struct thread *td, int flags)
|
1999-08-19 00:06:53 +00:00
|
|
|
{
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
struct ksegrp *kg;
|
|
|
|
struct thread *td2;
|
|
|
|
struct thread *tda;
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
CTR3(KTR_RUNQ, "setrunqueue: td:%p kg:%p pid:%d",
|
|
|
|
td, td->td_ksegrp, td->td_proc->p_pid);
|
2004-12-26 00:14:21 +00:00
|
|
|
CTR5(KTR_SCHED, "setrunqueue: %p(%s) prio %d by %p(%s)",
|
|
|
|
td, td->td_proc->p_comm, td->td_priority, curthread,
|
|
|
|
curthread->td_proc->p_comm);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2004-09-13 23:02:52 +00:00
|
|
|
KASSERT((td->td_inhibitors == 0),
|
|
|
|
("setrunqueue: trying to run inhibitted thread"));
|
2002-09-11 08:13:56 +00:00
|
|
|
KASSERT((TD_CAN_RUN(td) || TD_IS_RUNNING(td)),
|
|
|
|
("setrunqueue: bad thread state"));
|
|
|
|
TD_SET_RUNQ(td);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
kg = td->td_ksegrp;
|
2004-09-05 02:09:54 +00:00
|
|
|
if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
|
2002-10-09 02:33:36 +00:00
|
|
|
/*
|
|
|
|
* Common path optimisation: Only one of everything
|
|
|
|
* and the KSE is always already attached.
|
|
|
|
* Totally ignore the ksegrp run queue.
|
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
if (kg->kg_avail_opennings != 1) {
|
2004-09-07 06:38:22 +00:00
|
|
|
if (limitcount < 1) {
|
2004-09-05 02:09:54 +00:00
|
|
|
limitcount++;
|
2004-09-07 06:38:22 +00:00
|
|
|
printf("pid %d: corrected slot count (%d->1)\n",
|
2004-09-05 02:09:54 +00:00
|
|
|
td->td_proc->p_pid, kg->kg_avail_opennings);
|
|
|
|
|
|
|
|
}
|
|
|
|
kg->kg_avail_opennings = 1;
|
|
|
|
}
|
2004-09-01 02:11:28 +00:00
|
|
|
sched_add(td, flags);
|
2002-10-09 02:33:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-12-28 17:13:31 +00:00
|
|
|
/*
|
|
|
|
* If the concurrency has reduced, and we would go in the
|
|
|
|
* assigned section, then keep removing entries from the
|
|
|
|
* system run queue, until we are not in that section
|
2004-09-16 07:12:59 +00:00
|
|
|
* or there is room for us to be put in that section.
|
|
|
|
* What we MUST avoid is the case where there are threads of less
|
|
|
|
* priority than the new one scheduled, but it can not
|
|
|
|
* be scheduled itself. That would lead to a non contiguous set
|
|
|
|
* of scheduled threads, and everything would break.
|
2005-12-28 17:13:31 +00:00
|
|
|
*/
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
tda = kg->kg_last_assigned;
|
2004-09-16 07:12:59 +00:00
|
|
|
while ((kg->kg_avail_opennings <= 0) &&
|
|
|
|
(tda && (tda->td_priority > td->td_priority))) {
|
2004-09-05 02:09:54 +00:00
|
|
|
/*
|
|
|
|
* None free, but there is one we can commandeer.
|
2002-07-14 03:43:33 +00:00
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
CTR2(KTR_RUNQ,
|
|
|
|
"setrunqueue: kg:%p: take slot from td: %p", kg, tda);
|
|
|
|
sched_rem(tda);
|
|
|
|
tda = kg->kg_last_assigned =
|
|
|
|
TAILQ_PREV(tda, threadqueue, td_runq);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the thread to the ksegrp's run queue at
|
|
|
|
* the appropriate place.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(td2, &kg->kg_runq, td_runq) {
|
|
|
|
if (td2->td_priority > td->td_priority) {
|
|
|
|
TAILQ_INSERT_BEFORE(td2, td, td_runq);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (td2 == NULL) {
|
|
|
|
/* We ran off the end of the TAILQ or it was empty. */
|
|
|
|
TAILQ_INSERT_TAIL(&kg->kg_runq, td, td_runq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-09-05 02:09:54 +00:00
|
|
|
* If we have a slot to use, then put the thread on the system
|
|
|
|
* run queue and if needed, readjust the last_assigned pointer.
|
2004-09-16 07:12:59 +00:00
|
|
|
* it may be that we need to schedule something anyhow
|
|
|
|
* even if the availabel slots are -ve so that
|
|
|
|
* all the items < last_assigned are scheduled.
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
if (kg->kg_avail_opennings > 0) {
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
if (tda == NULL) {
|
|
|
|
/*
|
|
|
|
* No pre-existing last assigned so whoever is first
|
2004-09-16 07:12:59 +00:00
|
|
|
* gets the slot.. (maybe us)
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
*/
|
|
|
|
td2 = TAILQ_FIRST(&kg->kg_runq);
|
|
|
|
kg->kg_last_assigned = td2;
|
|
|
|
} else if (tda->td_priority > td->td_priority) {
|
2004-09-05 02:09:54 +00:00
|
|
|
td2 = td;
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
} else {
|
2005-12-28 17:13:31 +00:00
|
|
|
/*
|
|
|
|
* We are past last_assigned, so
|
2004-09-16 07:12:59 +00:00
|
|
|
* give the next slot to whatever is next,
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
* which may or may not be us.
|
|
|
|
*/
|
|
|
|
td2 = TAILQ_NEXT(tda, td_runq);
|
|
|
|
kg->kg_last_assigned = td2;
|
|
|
|
}
|
2004-09-05 02:09:54 +00:00
|
|
|
sched_add(td2, flags);
|
2004-08-09 18:21:12 +00:00
|
|
|
} else {
|
|
|
|
CTR3(KTR_RUNQ, "setrunqueue: held: td%p kg%p pid%d",
|
|
|
|
td, td->td_ksegrp, td->td_proc->p_pid);
|
2004-10-12 16:30:20 +00:00
|
|
|
if ((flags & SRQ_YIELDING) == 0)
|
|
|
|
maybe_preempt_in_ksegrp(td);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
}
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
1999-08-19 00:06:53 +00:00
|
|
|
|
2004-07-02 20:21:44 +00:00
|
|
|
/*
|
|
|
|
* Kernel thread preemption implementation. Critical sections mark
|
|
|
|
* regions of code in which preemptions are not allowed.
|
|
|
|
*/
|
2001-12-18 00:27:18 +00:00
|
|
|
void
|
|
|
|
critical_enter(void)
|
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
|
|
|
|
td = curthread;
|
|
|
|
td->td_critnest++;
|
2005-12-18 18:10:57 +00:00
|
|
|
CTR4(KTR_CRITICAL, "critical_enter by thread %p (%ld, %s) to %d", td,
|
2004-11-07 23:11:32 +00:00
|
|
|
(long)td->td_proc->p_pid, td->td_proc->p_comm, td->td_critnest);
|
2001-12-18 00:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
critical_exit(void)
|
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
|
|
|
|
td = curthread;
|
2004-02-02 08:13:27 +00:00
|
|
|
KASSERT(td->td_critnest != 0,
|
|
|
|
("critical_exit: td_critnest == 0"));
|
2005-05-23 23:01:53 +00:00
|
|
|
#ifdef PREEMPTION
|
2001-12-18 00:27:18 +00:00
|
|
|
if (td->td_critnest == 1) {
|
2005-04-08 03:37:53 +00:00
|
|
|
td->td_critnest = 0;
|
2004-07-16 21:04:55 +00:00
|
|
|
mtx_assert(&sched_lock, MA_NOTOWNED);
|
2005-04-08 03:37:53 +00:00
|
|
|
if (td->td_owepreempt) {
|
|
|
|
td->td_critnest = 1;
|
2004-07-02 20:21:44 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2005-04-08 03:37:53 +00:00
|
|
|
td->td_critnest--;
|
2004-07-02 20:21:44 +00:00
|
|
|
mi_switch(SW_INVOL, NULL);
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
}
|
2005-12-28 17:13:31 +00:00
|
|
|
} else
|
2004-07-02 20:21:44 +00:00
|
|
|
#endif
|
2001-12-18 00:27:18 +00:00
|
|
|
td->td_critnest--;
|
2005-12-28 17:13:31 +00:00
|
|
|
|
2005-12-18 18:10:57 +00:00
|
|
|
CTR4(KTR_CRITICAL, "critical_exit by thread %p (%ld, %s) to %d", td,
|
2004-11-07 23:11:32 +00:00
|
|
|
(long)td->td_proc->p_pid, td->td_proc->p_comm, td->td_critnest);
|
2001-12-18 00:27:18 +00:00
|
|
|
}
|
|
|
|
|
2004-07-02 20:21:44 +00:00
|
|
|
/*
|
|
|
|
* This function is called when a thread is about to be put on run queue
|
|
|
|
* because it has been made runnable or its priority has been adjusted. It
|
|
|
|
* determines if the new thread should be immediately preempted to. If so,
|
|
|
|
* it switches to it and eventually returns true. If not, it returns false
|
|
|
|
* so that the caller may place the thread on an appropriate run queue.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
maybe_preempt(struct thread *td)
|
|
|
|
{
|
2004-07-03 00:57:43 +00:00
|
|
|
#ifdef PREEMPTION
|
2004-07-02 20:21:44 +00:00
|
|
|
struct thread *ctd;
|
|
|
|
int cpri, pri;
|
2004-07-03 00:57:43 +00:00
|
|
|
#endif
|
2004-07-02 20:21:44 +00:00
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
#ifdef PREEMPTION
|
|
|
|
/*
|
|
|
|
* The new thread should not preempt the current thread if any of the
|
|
|
|
* following conditions are true:
|
|
|
|
*
|
2005-03-17 15:18:01 +00:00
|
|
|
* - The kernel is in the throes of crashing (panicstr).
|
2004-07-16 21:04:55 +00:00
|
|
|
* - The current thread has a higher (numerically lower) or
|
|
|
|
* equivalent priority. Note that this prevents curthread from
|
|
|
|
* trying to preempt to itself.
|
2004-07-02 20:21:44 +00:00
|
|
|
* - It is too early in the boot for context switches (cold is set).
|
|
|
|
* - The current thread has an inhibitor set or is in the process of
|
|
|
|
* exiting. In this case, the current thread is about to switch
|
|
|
|
* out anyways, so there's no point in preempting. If we did,
|
|
|
|
* the current thread would not be properly resumed as well, so
|
|
|
|
* just avoid that whole landmine.
|
|
|
|
* - If the new thread's priority is not a realtime priority and
|
|
|
|
* the current thread's priority is not an idle priority and
|
|
|
|
* FULL_PREEMPTION is disabled.
|
|
|
|
*
|
|
|
|
* If all of these conditions are false, but the current thread is in
|
|
|
|
* a nested critical section, then we have to defer the preemption
|
|
|
|
* until we exit the critical section. Otherwise, switch immediately
|
|
|
|
* to the new thread.
|
|
|
|
*/
|
|
|
|
ctd = curthread;
|
2004-09-06 07:23:14 +00:00
|
|
|
KASSERT ((ctd->td_kse != NULL && ctd->td_kse->ke_thread == ctd),
|
|
|
|
("thread has no (or wrong) sched-private part."));
|
2004-09-13 23:02:52 +00:00
|
|
|
KASSERT((td->td_inhibitors == 0),
|
|
|
|
("maybe_preempt: trying to run inhibitted thread"));
|
2004-07-02 20:21:44 +00:00
|
|
|
pri = td->td_priority;
|
|
|
|
cpri = ctd->td_priority;
|
2005-03-17 15:18:01 +00:00
|
|
|
if (panicstr != NULL || pri >= cpri || cold /* || dumping */ ||
|
|
|
|
TD_IS_INHIBITED(ctd) || td->td_kse->ke_state != KES_THREAD)
|
2004-07-02 20:21:44 +00:00
|
|
|
return (0);
|
|
|
|
#ifndef FULL_PREEMPTION
|
2005-06-10 03:00:29 +00:00
|
|
|
if (pri > PRI_MAX_ITHD && cpri < PRI_MIN_IDLE)
|
2004-07-02 20:21:44 +00:00
|
|
|
return (0);
|
|
|
|
#endif
|
2005-06-09 19:43:08 +00:00
|
|
|
|
2004-07-02 20:21:44 +00:00
|
|
|
if (ctd->td_critnest > 1) {
|
|
|
|
CTR1(KTR_PROC, "maybe_preempt: in critical section %d",
|
|
|
|
ctd->td_critnest);
|
2005-04-08 03:37:53 +00:00
|
|
|
ctd->td_owepreempt = 1;
|
2004-07-02 20:21:44 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-10-05 22:03:10 +00:00
|
|
|
* Thread is runnable but not yet put on system run queue.
|
2004-07-02 20:21:44 +00:00
|
|
|
*/
|
|
|
|
MPASS(TD_ON_RUNQ(td));
|
2004-09-13 23:06:39 +00:00
|
|
|
MPASS(td->td_sched->ke_state != KES_ONRUNQ);
|
|
|
|
if (td->td_proc->p_flag & P_HADTHREADS) {
|
|
|
|
/*
|
|
|
|
* If this is a threaded process we actually ARE on the
|
|
|
|
* ksegrp run queue so take it off that first.
|
2004-09-16 05:37:40 +00:00
|
|
|
* Also undo any damage done to the last_assigned pointer.
|
|
|
|
* XXX Fix setrunqueue so this isn't needed
|
2004-09-13 23:06:39 +00:00
|
|
|
*/
|
2004-09-16 05:37:40 +00:00
|
|
|
struct ksegrp *kg;
|
|
|
|
|
|
|
|
kg = td->td_ksegrp;
|
|
|
|
if (kg->kg_last_assigned == td)
|
|
|
|
kg->kg_last_assigned =
|
2004-09-16 07:12:59 +00:00
|
|
|
TAILQ_PREV(td, threadqueue, td_runq);
|
2004-09-16 05:37:40 +00:00
|
|
|
TAILQ_REMOVE(&kg->kg_runq, td, td_runq);
|
2004-09-13 23:06:39 +00:00
|
|
|
}
|
2005-12-28 17:13:31 +00:00
|
|
|
|
2004-07-02 20:21:44 +00:00
|
|
|
TD_SET_RUNNING(td);
|
|
|
|
CTR3(KTR_PROC, "preempting to thread %p (pid %d, %s)\n", td,
|
|
|
|
td->td_proc->p_pid, td->td_proc->p_comm);
|
2004-10-05 22:03:10 +00:00
|
|
|
mi_switch(SW_INVOL|SW_PREEMPT, td);
|
2004-07-02 20:21:44 +00:00
|
|
|
return (1);
|
|
|
|
#else
|
|
|
|
return (0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-08-06 15:49:44 +00:00
|
|
|
#if 0
|
2004-07-02 20:21:44 +00:00
|
|
|
#ifndef PREEMPTION
|
|
|
|
/* XXX: There should be a non-static version of this. */
|
|
|
|
static void
|
|
|
|
printf_caddr_t(void *data)
|
|
|
|
{
|
|
|
|
printf("%s", (char *)data);
|
|
|
|
}
|
|
|
|
static char preempt_warning[] =
|
|
|
|
"WARNING: Kernel preemption is disabled, expect reduced performance.\n";
|
|
|
|
SYSINIT(preempt_warning, SI_SUB_COPYRIGHT, SI_ORDER_ANY, printf_caddr_t,
|
|
|
|
preempt_warning)
|
|
|
|
#endif
|
2004-08-06 15:49:44 +00:00
|
|
|
#endif
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* SYSTEM RUN QUEUE manipulations and tests *
|
|
|
|
************************************************************************/
|
|
|
|
/*
|
|
|
|
* Initialize a run structure.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
runq_init(struct runq *rq)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bzero(rq, sizeof *rq);
|
|
|
|
for (i = 0; i < RQ_NQS; i++)
|
|
|
|
TAILQ_INIT(&rq->rq_queues[i]);
|
|
|
|
}
|
|
|
|
|
2001-02-12 00:20:08 +00:00
|
|
|
/*
|
|
|
|
* Clear the status bit of the queue corresponding to priority level pri,
|
|
|
|
* indicating that it is empty.
|
|
|
|
*/
|
|
|
|
static __inline void
|
|
|
|
runq_clrbit(struct runq *rq, int pri)
|
|
|
|
{
|
|
|
|
struct rqbits *rqb;
|
2000-09-07 01:33:02 +00:00
|
|
|
|
2001-02-12 00:20:08 +00:00
|
|
|
rqb = &rq->rq_status;
|
|
|
|
CTR4(KTR_RUNQ, "runq_clrbit: bits=%#x %#x bit=%#x word=%d",
|
|
|
|
rqb->rqb_bits[RQB_WORD(pri)],
|
|
|
|
rqb->rqb_bits[RQB_WORD(pri)] & ~RQB_BIT(pri),
|
|
|
|
RQB_BIT(pri), RQB_WORD(pri));
|
|
|
|
rqb->rqb_bits[RQB_WORD(pri)] &= ~RQB_BIT(pri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the index of the first non-empty run queue. This is done by
|
|
|
|
* scanning the status bits, a set bit indicates a non-empty queue.
|
|
|
|
*/
|
|
|
|
static __inline int
|
|
|
|
runq_findbit(struct runq *rq)
|
|
|
|
{
|
|
|
|
struct rqbits *rqb;
|
|
|
|
int pri;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
rqb = &rq->rq_status;
|
|
|
|
for (i = 0; i < RQB_LEN; i++)
|
|
|
|
if (rqb->rqb_bits[i]) {
|
2002-06-20 06:21:20 +00:00
|
|
|
pri = RQB_FFS(rqb->rqb_bits[i]) + (i << RQB_L2BPW);
|
2001-02-12 00:20:08 +00:00
|
|
|
CTR3(KTR_RUNQ, "runq_findbit: bits=%#x i=%d pri=%d",
|
|
|
|
rqb->rqb_bits[i], i, pri);
|
|
|
|
return (pri);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the status bit of the queue corresponding to priority level pri,
|
|
|
|
* indicating that it is non-empty.
|
|
|
|
*/
|
|
|
|
static __inline void
|
|
|
|
runq_setbit(struct runq *rq, int pri)
|
|
|
|
{
|
|
|
|
struct rqbits *rqb;
|
|
|
|
|
|
|
|
rqb = &rq->rq_status;
|
|
|
|
CTR4(KTR_RUNQ, "runq_setbit: bits=%#x %#x bit=%#x word=%d",
|
|
|
|
rqb->rqb_bits[RQB_WORD(pri)],
|
|
|
|
rqb->rqb_bits[RQB_WORD(pri)] | RQB_BIT(pri),
|
|
|
|
RQB_BIT(pri), RQB_WORD(pri));
|
|
|
|
rqb->rqb_bits[RQB_WORD(pri)] |= RQB_BIT(pri);
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
* Add the KSE to the queue specified by its priority, and set the
|
2001-02-12 00:20:08 +00:00
|
|
|
* corresponding status bit.
|
1999-08-19 00:06:53 +00:00
|
|
|
*/
|
|
|
|
void
|
2004-10-05 22:03:10 +00:00
|
|
|
runq_add(struct runq *rq, struct kse *ke, int flags)
|
1999-08-19 00:06:53 +00:00
|
|
|
{
|
2001-02-12 00:20:08 +00:00
|
|
|
struct rqhead *rqh;
|
|
|
|
int pri;
|
1999-08-19 00:06:53 +00:00
|
|
|
|
2002-02-11 20:37:54 +00:00
|
|
|
pri = ke->ke_thread->td_priority / RQ_PPQ;
|
2001-09-12 08:38:13 +00:00
|
|
|
ke->ke_rqindex = pri;
|
2001-02-12 00:20:08 +00:00
|
|
|
runq_setbit(rq, pri);
|
|
|
|
rqh = &rq->rq_queues[pri];
|
2004-08-09 18:21:12 +00:00
|
|
|
CTR5(KTR_RUNQ, "runq_add: td=%p ke=%p pri=%d %d rqh=%p",
|
|
|
|
ke->ke_thread, ke, ke->ke_thread->td_priority, pri, rqh);
|
2004-10-05 22:03:10 +00:00
|
|
|
if (flags & SRQ_PREEMPTED) {
|
|
|
|
TAILQ_INSERT_HEAD(rqh, ke, ke_procq);
|
|
|
|
} else {
|
|
|
|
TAILQ_INSERT_TAIL(rqh, ke, ke_procq);
|
|
|
|
}
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-12 00:20:08 +00:00
|
|
|
* Return true if there are runnable processes of any priority on the run
|
|
|
|
* queue, false otherwise. Has no side effects, does not modify the run
|
|
|
|
* queue structure.
|
1999-08-19 00:06:53 +00:00
|
|
|
*/
|
2001-02-12 00:20:08 +00:00
|
|
|
int
|
|
|
|
runq_check(struct runq *rq)
|
1999-08-19 00:06:53 +00:00
|
|
|
{
|
2001-02-12 00:20:08 +00:00
|
|
|
struct rqbits *rqb;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
rqb = &rq->rq_status;
|
|
|
|
for (i = 0; i < RQB_LEN; i++)
|
|
|
|
if (rqb->rqb_bits[i]) {
|
|
|
|
CTR2(KTR_RUNQ, "runq_check: bits=%#x i=%d",
|
|
|
|
rqb->rqb_bits[i], i);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
CTR0(KTR_RUNQ, "runq_check: empty");
|
|
|
|
|
|
|
|
return (0);
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|
|
|
|
|
2004-09-01 06:42:02 +00:00
|
|
|
#if defined(SMP) && defined(SCHED_4BSD)
|
|
|
|
int runq_fuzz = 1;
|
|
|
|
SYSCTL_INT(_kern_sched, OID_AUTO, runq_fuzz, CTLFLAG_RW, &runq_fuzz, 0, "");
|
|
|
|
#endif
|
|
|
|
|
1999-08-19 00:06:53 +00:00
|
|
|
/*
|
2002-10-12 05:32:24 +00:00
|
|
|
* Find the highest priority process on the run queue.
|
1999-08-19 00:06:53 +00:00
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
struct kse *
|
2001-02-12 00:20:08 +00:00
|
|
|
runq_choose(struct runq *rq)
|
1999-08-19 00:06:53 +00:00
|
|
|
{
|
2001-02-12 00:20:08 +00:00
|
|
|
struct rqhead *rqh;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct kse *ke;
|
2001-02-12 00:20:08 +00:00
|
|
|
int pri;
|
1999-08-19 00:06:53 +00:00
|
|
|
|
2000-09-07 01:33:02 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
while ((pri = runq_findbit(rq)) != -1) {
|
2001-02-12 00:20:08 +00:00
|
|
|
rqh = &rq->rq_queues[pri];
|
2004-09-01 06:42:02 +00:00
|
|
|
#if defined(SMP) && defined(SCHED_4BSD)
|
|
|
|
/* fuzz == 1 is normal.. 0 or less are ignored */
|
|
|
|
if (runq_fuzz > 1) {
|
|
|
|
/*
|
|
|
|
* In the first couple of entries, check if
|
|
|
|
* there is one for our CPU as a preference.
|
|
|
|
*/
|
|
|
|
int count = runq_fuzz;
|
|
|
|
int cpu = PCPU_GET(cpuid);
|
|
|
|
struct kse *ke2;
|
|
|
|
ke2 = ke = TAILQ_FIRST(rqh);
|
|
|
|
|
|
|
|
while (count-- && ke2) {
|
|
|
|
if (ke->ke_thread->td_lastcpu == cpu) {
|
|
|
|
ke = ke2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ke2 = TAILQ_NEXT(ke2, ke_procq);
|
|
|
|
}
|
2005-12-28 17:13:31 +00:00
|
|
|
} else
|
2004-09-01 06:42:02 +00:00
|
|
|
#endif
|
|
|
|
ke = TAILQ_FIRST(rqh);
|
2001-09-12 08:38:13 +00:00
|
|
|
KASSERT(ke != NULL, ("runq_choose: no proc on busy queue"));
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
CTR3(KTR_RUNQ,
|
|
|
|
"runq_choose: pri=%d kse=%p rqh=%p", pri, ke, rqh);
|
2001-09-12 08:38:13 +00:00
|
|
|
return (ke);
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
|
|
|
CTR1(KTR_RUNQ, "runq_choose: idleproc pri=%d", pri);
|
|
|
|
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
return (NULL);
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
* Remove the KSE from the queue specified by its priority, and clear the
|
2001-02-12 00:20:08 +00:00
|
|
|
* corresponding status bit if the queue becomes empty.
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
* Caller must set ke->ke_state afterwards.
|
2001-02-12 00:20:08 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
runq_remove(struct runq *rq, struct kse *ke)
|
2001-02-12 00:20:08 +00:00
|
|
|
{
|
|
|
|
struct rqhead *rqh;
|
|
|
|
int pri;
|
|
|
|
|
2002-07-30 06:54:05 +00:00
|
|
|
KASSERT(ke->ke_proc->p_sflag & PS_INMEM,
|
|
|
|
("runq_remove: process swapped out"));
|
2001-09-12 08:38:13 +00:00
|
|
|
pri = ke->ke_rqindex;
|
2001-02-12 00:20:08 +00:00
|
|
|
rqh = &rq->rq_queues[pri];
|
2004-08-09 18:21:12 +00:00
|
|
|
CTR5(KTR_RUNQ, "runq_remove: td=%p, ke=%p pri=%d %d rqh=%p",
|
|
|
|
ke->ke_thread, ke, ke->ke_thread->td_priority, pri, rqh);
|
2001-09-12 08:38:13 +00:00
|
|
|
KASSERT(ke != NULL, ("runq_remove: no proc on busy queue"));
|
|
|
|
TAILQ_REMOVE(rqh, ke, ke_procq);
|
2001-02-12 00:20:08 +00:00
|
|
|
if (TAILQ_EMPTY(rqh)) {
|
|
|
|
CTR0(KTR_RUNQ, "runq_remove: empty");
|
|
|
|
runq_clrbit(rq, pri);
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
}
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
/****** functions that are temporarily here ***********/
|
|
|
|
#include <vm/uma.h>
|
|
|
|
extern struct mtx kse_zombie_lock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate scheduler specific per-process resources.
|
|
|
|
* The thread and ksegrp have already been linked in.
|
|
|
|
* In this case just set the default concurrency value.
|
|
|
|
*
|
|
|
|
* Called from:
|
|
|
|
* proc_init() (UMA init method)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sched_newproc(struct proc *p, struct ksegrp *kg, struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* This can go in sched_fork */
|
|
|
|
sched_init_concurrency(kg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* thread is being either created or recycled.
|
|
|
|
* Fix up the per-scheduler resources associated with it.
|
|
|
|
* Called from:
|
|
|
|
* sched_fork_thread()
|
|
|
|
* thread_dtor() (*may go away)
|
|
|
|
* thread_init() (*may go away)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sched_newthread(struct thread *td)
|
|
|
|
{
|
|
|
|
struct td_sched *ke;
|
|
|
|
|
|
|
|
ke = (struct td_sched *) (td + 1);
|
|
|
|
bzero(ke, sizeof(*ke));
|
|
|
|
td->td_sched = ke;
|
|
|
|
ke->ke_thread = td;
|
|
|
|
ke->ke_state = KES_THREAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up an initial concurrency of 1
|
|
|
|
* and set the given thread (if given) to be using that
|
|
|
|
* concurrency slot.
|
|
|
|
* May be used "offline"..before the ksegrp is attached to the world
|
|
|
|
* and thus wouldn't need schedlock in that case.
|
|
|
|
* Called from:
|
|
|
|
* thr_create()
|
|
|
|
* proc_init() (UMA) via sched_newproc()
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sched_init_concurrency(struct ksegrp *kg)
|
|
|
|
{
|
|
|
|
|
2004-10-05 21:10:44 +00:00
|
|
|
CTR1(KTR_RUNQ,"kg %p init slots and concurrency to 1", kg);
|
2004-09-05 02:09:54 +00:00
|
|
|
kg->kg_concurrency = 1;
|
|
|
|
kg->kg_avail_opennings = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change the concurrency of an existing ksegrp to N
|
|
|
|
* Called from:
|
|
|
|
* kse_create()
|
|
|
|
* kse_exit()
|
|
|
|
* thread_exit()
|
|
|
|
* thread_single()
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sched_set_concurrency(struct ksegrp *kg, int concurrency)
|
|
|
|
{
|
|
|
|
|
2004-10-05 21:10:44 +00:00
|
|
|
CTR4(KTR_RUNQ,"kg %p set concurrency to %d, slots %d -> %d",
|
|
|
|
kg,
|
|
|
|
concurrency,
|
|
|
|
kg->kg_avail_opennings,
|
|
|
|
kg->kg_avail_opennings + (concurrency - kg->kg_concurrency));
|
2004-09-05 02:09:54 +00:00
|
|
|
kg->kg_avail_opennings += (concurrency - kg->kg_concurrency);
|
|
|
|
kg->kg_concurrency = concurrency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from thread_exit() for all exiting thread
|
|
|
|
*
|
|
|
|
* Not to be confused with sched_exit_thread()
|
|
|
|
* that is only called from thread_exit() for threads exiting
|
|
|
|
* without the rest of the process exiting because it is also called from
|
|
|
|
* sched_exit() and we wouldn't want to call it twice.
|
|
|
|
* XXX This can probably be fixed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sched_thread_exit(struct thread *td)
|
|
|
|
{
|
|
|
|
|
2004-10-05 21:10:44 +00:00
|
|
|
SLOT_RELEASE(td->td_ksegrp);
|
2004-09-05 02:09:54 +00:00
|
|
|
slot_fill(td->td_ksegrp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* KERN_SWITCH_INCLUDE */
|