1999-08-19 00:06:53 +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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#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>
|
|
|
|
|
|
|
|
/*
|
2001-02-12 00:20:08 +00:00
|
|
|
* Global run queue.
|
1999-08-19 00:06:53 +00:00
|
|
|
*/
|
2001-02-12 00:20:08 +00:00
|
|
|
static struct runq runq;
|
|
|
|
SYSINIT(runq, SI_SUB_RUN_QUEUE, SI_ORDER_FIRST, runq_init, &runq)
|
1999-08-19 00:06:53 +00:00
|
|
|
|
|
|
|
/*
|
2001-02-12 00:20:08 +00:00
|
|
|
* Wrappers which implement old interface; act on global run queue.
|
1999-08-19 00:06:53 +00:00
|
|
|
*/
|
2001-02-12 00:20:08 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *
|
|
|
|
choosethread(void)
|
1999-08-19 00:06:53 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
return (runq_choose(&runq)->ke_thread);
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
1999-08-19 00:06:53 +00:00
|
|
|
|
2001-02-12 00:20:08 +00:00
|
|
|
int
|
|
|
|
procrunnable(void)
|
|
|
|
{
|
|
|
|
return runq_check(&runq);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
remrunqueue(struct thread *td)
|
2001-02-12 00:20:08 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
runq_remove(&runq, td->td_kse);
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
setrunqueue(struct thread *td)
|
1999-08-19 00:06:53 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
runq_add(&runq, td->td_kse);
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
1999-08-19 00:06:53 +00:00
|
|
|
|
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]) {
|
|
|
|
pri = (RQB_FFS(rqb->rqb_bits[i]) - 1) +
|
|
|
|
(i << RQB_L2BPW);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-03-11 18:57:30 +00:00
|
|
|
#ifdef INVARIANT_SUPPORT
|
|
|
|
/*
|
|
|
|
* Return true if the specified process is already in the run queue.
|
|
|
|
*/
|
|
|
|
static __inline int
|
2001-09-12 08:38:13 +00:00
|
|
|
runq_find(struct runq *rq, struct kse *ke)
|
2001-03-11 18:57:30 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct kse *ke2;
|
2001-03-11 18:57:30 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
for (i = 0; i < RQB_LEN; i++)
|
2001-09-12 08:38:13 +00:00
|
|
|
TAILQ_FOREACH(ke2, &rq->rq_queues[i], ke_procq)
|
|
|
|
if (ke2 == ke)
|
2001-03-11 18:57:30 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-08-19 00:06:53 +00:00
|
|
|
/*
|
2001-02-12 00:20:08 +00:00
|
|
|
* Add the process to the queue specified by its priority, and set the
|
|
|
|
* corresponding status bit.
|
1999-08-19 00:06:53 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
runq_add(struct runq *rq, struct kse *ke)
|
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
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
struct ksegrp *kg = ke->ke_ksegrp;
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
struct proc *p = ke->ke_proc;
|
|
|
|
#endif
|
|
|
|
if (ke->ke_flags & KEF_ONRUNQ)
|
|
|
|
return;
|
2000-09-07 01:33:02 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2001-02-12 00:20:08 +00:00
|
|
|
KASSERT(p->p_stat == SRUN, ("runq_add: proc %p (%s) not SRUN",
|
|
|
|
p, p->p_comm));
|
2001-09-12 08:38:13 +00:00
|
|
|
KASSERT(runq_find(rq, ke) == 0,
|
|
|
|
("runq_add: proc %p (%s) already in run queue", ke, p->p_comm));
|
|
|
|
pri = kg->kg_pri.pri_level / RQ_PPQ;
|
|
|
|
ke->ke_rqindex = pri;
|
2001-02-12 00:20:08 +00:00
|
|
|
runq_setbit(rq, pri);
|
|
|
|
rqh = &rq->rq_queues[pri];
|
|
|
|
CTR4(KTR_RUNQ, "runq_add: p=%p pri=%d %d rqh=%p",
|
2001-09-18 03:36:21 +00:00
|
|
|
ke->ke_proc, kg->kg_pri.pri_level, pri, rqh);
|
2001-09-12 08:38:13 +00:00
|
|
|
TAILQ_INSERT_TAIL(rqh, ke, ke_procq);
|
|
|
|
ke->ke_flags |= KEF_ONRUNQ;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-12 00:20:08 +00:00
|
|
|
* Find and remove the highest priority process from the run queue.
|
|
|
|
* If there are no runnable processes, the per-cpu idle process is
|
|
|
|
* returned. Will not return NULL under any circumstances.
|
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);
|
2001-02-12 00:20:08 +00:00
|
|
|
if ((pri = runq_findbit(rq)) != -1) {
|
|
|
|
rqh = &rq->rq_queues[pri];
|
2001-09-12 08:38:13 +00:00
|
|
|
ke = TAILQ_FIRST(rqh);
|
|
|
|
KASSERT(ke != NULL, ("runq_choose: no proc on busy queue"));
|
|
|
|
KASSERT(ke->ke_proc->p_stat == SRUN,
|
|
|
|
("runq_choose: process %d(%s) in state %d", ke->ke_proc->p_pid,
|
|
|
|
ke->ke_proc->p_comm, ke->ke_proc->p_stat));
|
|
|
|
CTR3(KTR_RUNQ, "runq_choose: pri=%d kse=%p rqh=%p", pri, ke, rqh);
|
|
|
|
TAILQ_REMOVE(rqh, ke, ke_procq);
|
2001-02-12 00:20:08 +00:00
|
|
|
if (TAILQ_EMPTY(rqh)) {
|
|
|
|
CTR0(KTR_RUNQ, "runq_choose: empty");
|
|
|
|
runq_clrbit(rq, pri);
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
ke->ke_flags &= ~KEF_ONRUNQ;
|
|
|
|
return (ke);
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
|
|
|
CTR1(KTR_RUNQ, "runq_choose: idleproc pri=%d", pri);
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
return (PCPU_GET(idlethread)->td_kse);
|
2001-02-12 00:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a run structure.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
runq_init(struct runq *rq)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2001-02-24 14:06:36 +00:00
|
|
|
bzero(rq, sizeof *rq);
|
2001-02-12 00:20:08 +00:00
|
|
|
for (i = 0; i < RQ_NQS; i++)
|
|
|
|
TAILQ_INIT(&rq->rq_queues[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the process from the queue specified by its priority, and clear the
|
|
|
|
* corresponding status bit if the queue becomes empty.
|
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
#ifdef KTR
|
|
|
|
struct ksegrp *kg = ke->ke_ksegrp;
|
|
|
|
#endif
|
2001-02-12 00:20:08 +00:00
|
|
|
struct rqhead *rqh;
|
|
|
|
int pri;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
if (!(ke->ke_flags & KEF_ONRUNQ))
|
|
|
|
return;
|
2001-02-12 00:20:08 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
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];
|
|
|
|
CTR4(KTR_RUNQ, "runq_remove: p=%p pri=%d %d rqh=%p",
|
2001-09-12 08:38:13 +00:00
|
|
|
ke, kg->kg_pri.pri_level, pri, rqh);
|
|
|
|
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
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
ke->ke_flags &= ~KEF_ONRUNQ;
|
1999-08-19 00:06:53 +00:00
|
|
|
}
|