1994-05-24 10:09:53 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1982, 1986, 1990, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*
|
1996-03-11 05:48:57 +00:00
|
|
|
* @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
2001-08-21 20:09:05 +00:00
|
|
|
#include "opt_ddb.h"
|
1996-01-03 21:42:35 +00:00
|
|
|
#include "opt_ktrace.h"
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/condvar.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/kernel.h>
|
2000-09-07 01:33:02 +00:00
|
|
|
#include <sys/ktr.h>
|
2000-12-06 00:33:58 +00:00
|
|
|
#include <sys/lock.h>
|
2000-10-20 07:52:10 +00:00
|
|
|
#include <sys/mutex.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/proc.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/signalvar.h>
|
2001-04-27 19:28:25 +00:00
|
|
|
#include <sys/smp.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/sx.h>
|
1997-08-08 22:48:57 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-12-02 05:41:30 +00:00
|
|
|
#include <sys/sysproto.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/vmmeter.h>
|
2001-08-21 20:09:05 +00:00
|
|
|
#ifdef DDB
|
|
|
|
#include <ddb/ddb.h>
|
|
|
|
#endif
|
1994-05-24 10:09:53 +00:00
|
|
|
#ifdef KTRACE
|
1998-03-28 10:33:27 +00:00
|
|
|
#include <sys/uio.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
2002-03-19 21:25:46 +00:00
|
|
|
static void sched_setup(void *dummy);
|
1999-03-03 18:15:29 +00:00
|
|
|
SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL)
|
1995-08-28 09:19:25 +00:00
|
|
|
|
1999-02-22 16:57:48 +00:00
|
|
|
int hogticks;
|
1999-03-03 18:15:29 +00:00
|
|
|
int lbolt;
|
|
|
|
int sched_quantum; /* Roundrobin scheduling quantum in ticks. */
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-10-20 16:07:17 +00:00
|
|
|
static struct callout loadav_callout;
|
2000-11-27 22:52:31 +00:00
|
|
|
static struct callout schedcpu_callout;
|
|
|
|
static struct callout roundrobin_callout;
|
|
|
|
|
2001-10-20 13:10:43 +00:00
|
|
|
struct loadavg averunnable =
|
|
|
|
{ {0, 0, 0}, FSCALE }; /* load average, of runnable procs */
|
|
|
|
/*
|
|
|
|
* Constants for averages over 1, 5, and 15 minutes
|
|
|
|
* when sampling at 5 second intervals.
|
|
|
|
*/
|
|
|
|
static fixpt_t cexp[3] = {
|
|
|
|
0.9200444146293232 * FSCALE, /* exp(-1/12) */
|
|
|
|
0.9834714538216174 * FSCALE, /* exp(-1/60) */
|
|
|
|
0.9944598480048967 * FSCALE, /* exp(-1/180) */
|
|
|
|
};
|
|
|
|
|
2002-03-19 21:25:46 +00:00
|
|
|
static void endtsleep(void *);
|
|
|
|
static void loadav(void *arg);
|
|
|
|
static void roundrobin(void *arg);
|
|
|
|
static void schedcpu(void *arg);
|
1995-03-16 18:17:34 +00:00
|
|
|
|
1997-08-08 22:48:57 +00:00
|
|
|
static int
|
2000-07-04 11:25:35 +00:00
|
|
|
sysctl_kern_quantum(SYSCTL_HANDLER_ARGS)
|
1997-08-08 22:48:57 +00:00
|
|
|
{
|
1999-03-03 18:15:29 +00:00
|
|
|
int error, new_val;
|
1997-08-08 22:48:57 +00:00
|
|
|
|
1999-03-03 18:15:29 +00:00
|
|
|
new_val = sched_quantum * tick;
|
1997-08-08 22:48:57 +00:00
|
|
|
error = sysctl_handle_int(oidp, &new_val, 0, req);
|
1999-03-03 18:15:29 +00:00
|
|
|
if (error != 0 || req->newptr == NULL)
|
|
|
|
return (error);
|
|
|
|
if (new_val < tick)
|
|
|
|
return (EINVAL);
|
|
|
|
sched_quantum = new_val / tick;
|
|
|
|
hogticks = 2 * sched_quantum;
|
|
|
|
return (0);
|
1997-08-08 22:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_PROC(_kern, OID_AUTO, quantum, CTLTYPE_INT|CTLFLAG_RW,
|
2001-12-16 16:07:20 +00:00
|
|
|
0, sizeof sched_quantum, sysctl_kern_quantum, "I",
|
|
|
|
"Roundrobin scheduling quantum in microseconds");
|
1997-08-08 22:48:57 +00:00
|
|
|
|
2000-03-02 16:20:07 +00:00
|
|
|
/*
|
|
|
|
* Arrange to reschedule if necessary, taking the priorities and
|
|
|
|
* schedulers into account.
|
1998-03-04 10:25:55 +00:00
|
|
|
*/
|
2001-01-16 01:00:43 +00:00
|
|
|
void
|
2002-02-11 20:37:54 +00:00
|
|
|
maybe_resched(struct thread *td)
|
1998-03-04 10:25:55 +00:00
|
|
|
{
|
|
|
|
|
2001-02-22 13:47:01 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-02-11 20:37:54 +00:00
|
|
|
if (td->td_priority < curthread->td_priority)
|
2001-09-12 08:38:13 +00:00
|
|
|
curthread->td_kse->ke_flags |= KEF_NEEDRESCHED;
|
1998-03-04 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
1999-03-03 18:15:29 +00:00
|
|
|
int
|
|
|
|
roundrobin_interval(void)
|
1998-03-04 10:25:55 +00:00
|
|
|
{
|
1999-03-03 18:15:29 +00:00
|
|
|
return (sched_quantum);
|
1998-03-04 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Force switch among equal priority processes every 100ms.
|
2001-07-03 05:33:09 +00:00
|
|
|
* We don't actually need to force a context switch of the current process.
|
|
|
|
* The act of firing the event triggers a context switch to softclock() and
|
|
|
|
* then switching back out again which is equivalent to a preemption, thus
|
|
|
|
* no further work is needed on the local CPU.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1997-11-25 07:07:48 +00:00
|
|
|
static void
|
1994-05-24 10:09:53 +00:00
|
|
|
roundrobin(arg)
|
|
|
|
void *arg;
|
|
|
|
{
|
2001-02-10 19:07:32 +00:00
|
|
|
|
|
|
|
#ifdef SMP
|
2001-07-03 05:33:09 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
1998-05-17 22:12:14 +00:00
|
|
|
forward_roundrobin();
|
2001-04-27 19:28:25 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2001-07-03 05:33:09 +00:00
|
|
|
#endif
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2000-11-27 22:52:31 +00:00
|
|
|
callout_reset(&roundrobin_callout, sched_quantum, roundrobin, NULL);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constants for digital decay and forget:
|
|
|
|
* 90% of (p_estcpu) usage in 5 * loadav time
|
|
|
|
* 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
|
|
|
|
* Note that, as ps(1) mentions, this can let percentages
|
|
|
|
* total over 100% (I've seen 137.9% for 3 processes).
|
|
|
|
*
|
1999-11-27 15:27:11 +00:00
|
|
|
* Note that schedclock() updates p_estcpu and p_cpticks asynchronously.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
|
|
|
* We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
|
|
|
|
* That is, the system wants to compute a value of decay such
|
|
|
|
* that the following for loop:
|
|
|
|
* for (i = 0; i < (5 * loadavg); i++)
|
|
|
|
* p_estcpu *= decay;
|
|
|
|
* will compute
|
|
|
|
* p_estcpu *= 0.1;
|
|
|
|
* for all values of loadavg:
|
|
|
|
*
|
|
|
|
* Mathematically this loop can be expressed by saying:
|
|
|
|
* decay ** (5 * loadavg) ~= .1
|
|
|
|
*
|
|
|
|
* The system computes decay as:
|
|
|
|
* decay = (2 * loadavg) / (2 * loadavg + 1)
|
|
|
|
*
|
|
|
|
* We wish to prove that the system's computation of decay
|
|
|
|
* will always fulfill the equation:
|
|
|
|
* decay ** (5 * loadavg) ~= .1
|
|
|
|
*
|
|
|
|
* If we compute b as:
|
|
|
|
* b = 2 * loadavg
|
|
|
|
* then
|
|
|
|
* decay = b / (b + 1)
|
|
|
|
*
|
|
|
|
* We now need to prove two things:
|
|
|
|
* 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
|
|
|
|
* 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
|
1995-05-30 08:16:23 +00:00
|
|
|
*
|
1994-05-24 10:09:53 +00:00
|
|
|
* Facts:
|
|
|
|
* For x close to zero, exp(x) =~ 1 + x, since
|
|
|
|
* exp(x) = 0! + x**1/1! + x**2/2! + ... .
|
|
|
|
* therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
|
|
|
|
* For x close to zero, ln(1+x) =~ x, since
|
|
|
|
* ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
|
|
|
|
* therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
|
|
|
|
* ln(.1) =~ -2.30
|
|
|
|
*
|
|
|
|
* Proof of (1):
|
|
|
|
* Solve (factor)**(power) =~ .1 given power (5*loadav):
|
|
|
|
* solving for factor,
|
|
|
|
* ln(factor) =~ (-2.30/5*loadav), or
|
|
|
|
* factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
|
|
|
|
* exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
|
|
|
|
*
|
|
|
|
* Proof of (2):
|
|
|
|
* Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
|
|
|
|
* solving for power,
|
|
|
|
* power*ln(b/(b+1)) =~ -2.30, or
|
|
|
|
* power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
|
|
|
|
*
|
|
|
|
* Actual power values for the implemented algorithm are as follows:
|
|
|
|
* loadav: 1 2 3 4
|
|
|
|
* power: 5.68 10.32 14.94 19.55
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* calculations for digital decay to forget 90% of usage in 5*loadav sec */
|
|
|
|
#define loadfactor(loadav) (2 * (loadav))
|
|
|
|
#define decay_cpu(loadfac, cpu) (((loadfac) * (cpu)) / ((loadfac) + FSCALE))
|
|
|
|
|
|
|
|
/* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
|
1997-11-22 08:35:46 +00:00
|
|
|
static fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
|
1998-06-30 21:25:58 +00:00
|
|
|
SYSCTL_INT(_kern, OID_AUTO, ccpu, CTLFLAG_RD, &ccpu, 0, "");
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1998-10-25 17:44:59 +00:00
|
|
|
/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
|
|
|
|
static int fscale __unused = FSCALE;
|
1998-07-11 13:06:41 +00:00
|
|
|
SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
|
|
|
|
* faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
|
|
|
|
* and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
|
|
|
|
*
|
|
|
|
* To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
|
|
|
|
* 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
|
|
|
|
*
|
1998-02-25 06:04:46 +00:00
|
|
|
* If you don't want to bother with the faster/more-accurate formula, you
|
1994-05-24 10:09:53 +00:00
|
|
|
* can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
|
|
|
|
* (more general) method of calculating the %age of CPU used by a process.
|
|
|
|
*/
|
|
|
|
#define CCPU_SHIFT 11
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recompute process priorities, every hz ticks.
|
2000-12-01 04:55:52 +00:00
|
|
|
* MP-safe, called without the Giant mutex.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1997-11-25 07:07:48 +00:00
|
|
|
static void
|
1994-05-24 10:09:53 +00:00
|
|
|
schedcpu(arg)
|
|
|
|
void *arg;
|
|
|
|
{
|
|
|
|
register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
|
2002-02-11 20:37:54 +00:00
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
|
|
|
struct kse *ke;
|
|
|
|
struct ksegrp *kg;
|
|
|
|
int realstathz;
|
2001-09-12 08:38:13 +00:00
|
|
|
int awake;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1998-11-26 16:49:55 +00:00
|
|
|
realstathz = stathz ? stathz : hz;
|
2001-03-28 11:52:56 +00:00
|
|
|
sx_slock(&allproc_lock);
|
2001-09-12 08:38:13 +00:00
|
|
|
FOREACH_PROC_IN_SYSTEM(p) {
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
p->p_swtime++;
|
2001-09-12 08:38:13 +00:00
|
|
|
FOREACH_KSEGRP_IN_PROC(p, kg) {
|
|
|
|
awake = 0;
|
|
|
|
FOREACH_KSE_IN_GROUP(kg, ke) {
|
|
|
|
/*
|
|
|
|
* Increment time in/out of memory and sleep
|
|
|
|
* time (if sleeping). We ignore overflow;
|
|
|
|
* with 16-bit int's (remember them?)
|
|
|
|
* overflow takes 45 days.
|
|
|
|
*/
|
|
|
|
/* XXXKSE */
|
|
|
|
/* if ((ke->ke_flags & KEF_ONRUNQ) == 0) */
|
|
|
|
if (p->p_stat == SSLEEP || p->p_stat == SSTOP) {
|
|
|
|
ke->ke_slptime++;
|
|
|
|
} else {
|
|
|
|
ke->ke_slptime = 0;
|
|
|
|
awake = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pctcpu is only for ps?
|
|
|
|
* Do it per kse.. and add them up at the end?
|
|
|
|
* XXXKSE
|
|
|
|
*/
|
|
|
|
ke->ke_pctcpu = (ke->ke_pctcpu * ccpu) >> FSHIFT;
|
|
|
|
/*
|
|
|
|
* If the kse has been idle the entire second,
|
|
|
|
* stop recalculating its priority until
|
|
|
|
* it wakes up.
|
|
|
|
*/
|
|
|
|
if (ke->ke_slptime > 1) {
|
|
|
|
continue;
|
|
|
|
}
|
2000-10-06 02:20:21 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#if (FSHIFT >= CCPU_SHIFT)
|
2001-09-12 08:38:13 +00:00
|
|
|
ke->ke_pctcpu += (realstathz == 100) ?
|
|
|
|
((fixpt_t) ke->ke_cpticks) <<
|
|
|
|
(FSHIFT - CCPU_SHIFT) :
|
|
|
|
100 * (((fixpt_t) ke->ke_cpticks) <<
|
|
|
|
(FSHIFT - CCPU_SHIFT)) / realstathz;
|
1994-05-24 10:09:53 +00:00
|
|
|
#else
|
2001-09-12 08:38:13 +00:00
|
|
|
ke->ke_pctcpu += ((FSCALE - ccpu) *
|
|
|
|
(ke->ke_cpticks * FSCALE / realstathz)) >>
|
|
|
|
FSHIFT;
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
2001-09-12 08:38:13 +00:00
|
|
|
ke->ke_cpticks = 0;
|
|
|
|
} /* end of kse loop */
|
|
|
|
if (awake == 0) {
|
|
|
|
kg->kg_slptime++;
|
|
|
|
} else {
|
|
|
|
kg->kg_slptime = 0;
|
|
|
|
}
|
|
|
|
kg->kg_estcpu = decay_cpu(loadfac, kg->kg_estcpu);
|
|
|
|
resetpriority(kg);
|
2002-02-11 20:37:54 +00:00
|
|
|
td = FIRST_THREAD_IN_PROC(p);
|
|
|
|
if (td->td_priority >= PUSER &&
|
2001-09-12 08:38:13 +00:00
|
|
|
(p->p_sflag & PS_INMEM)) {
|
|
|
|
int changedqueue =
|
2002-02-11 20:37:54 +00:00
|
|
|
((td->td_priority / RQ_PPQ) !=
|
|
|
|
(kg->kg_user_pri / RQ_PPQ));
|
2001-09-12 08:38:13 +00:00
|
|
|
|
2002-02-11 20:37:54 +00:00
|
|
|
td->td_priority = kg->kg_user_pri;
|
2001-09-12 08:38:13 +00:00
|
|
|
FOREACH_KSE_IN_GROUP(kg, ke) {
|
2002-02-11 20:37:54 +00:00
|
|
|
if ((ke->ke_oncpu == NOCPU) &&
|
2001-09-12 08:38:13 +00:00
|
|
|
(p->p_stat == SRUN) && /* XXXKSE */
|
|
|
|
changedqueue) {
|
|
|
|
remrunqueue(ke->ke_thread);
|
|
|
|
setrunqueue(ke->ke_thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* end of ksegrp loop */
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2001-09-12 08:38:13 +00:00
|
|
|
} /* end of process loop */
|
2001-03-28 11:52:56 +00:00
|
|
|
sx_sunlock(&allproc_lock);
|
1998-03-08 09:59:44 +00:00
|
|
|
wakeup((caddr_t)&lbolt);
|
2000-11-27 22:52:31 +00:00
|
|
|
callout_reset(&schedcpu_callout, hz, schedcpu, NULL);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recalculate the priority of a process after it has slept for a while.
|
|
|
|
* For all load averages >= 1 and max p_estcpu of 255, sleeping for at
|
|
|
|
* least six times the loadfactor will decay p_estcpu to zero.
|
|
|
|
*/
|
2001-01-16 01:00:43 +00:00
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
updatepri(td)
|
|
|
|
register struct thread *td;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
register struct ksegrp *kg;
|
|
|
|
register unsigned int newcpu;
|
1994-05-24 10:09:53 +00:00
|
|
|
register fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
if (td == NULL)
|
|
|
|
return;
|
|
|
|
kg = td->td_ksegrp;
|
|
|
|
newcpu = kg->kg_estcpu;
|
|
|
|
if (kg->kg_slptime > 5 * loadfac)
|
|
|
|
kg->kg_estcpu = 0;
|
1994-05-24 10:09:53 +00:00
|
|
|
else {
|
2001-09-12 08:38:13 +00:00
|
|
|
kg->kg_slptime--; /* the first time was done in schedcpu */
|
|
|
|
while (newcpu && --kg->kg_slptime)
|
Scheduler fixes equivalent to the ones logged in the following NetBSD
commit to kern_synch.c:
----------------------------
revision 1.55
date: 1999/02/23 02:56:03; author: ross; state: Exp; lines: +39 -10
Scheduler bug fixes and reorganization
* fix the ancient nice(1) bug, where nice +20 processes incorrectly
steal 10 - 20% of the CPU, (or even more depending on load average)
* provide a new schedclk() mechanism at a new clock at schedhz, so high
platform hz values don't cause nice +0 processes to look like they are
niced
* change the algorithm slightly, and reorganize the code a lot
* fix percent-CPU calculation bugs, and eliminate some no-op code
=== nice bug === Correctly divide the scheduler queues between niced and
compute-bound processes. The current nice weight of two (sort of, see
`algorithm change' below) neatly divides the USRPRI queues in half; this
should have been used to clip p_estcpu, instead of UCHAR_MAX. Besides
being the wrong amount, clipping an unsigned char to UCHAR_MAX is a no-op,
and it was done after decay_cpu() which can only _reduce_ the value. It
has to be kept <= NICE_WEIGHT * PRIO_MAX - PPQ or processes can
scheduler-penalize themselves onto the same queue as nice +20 processes.
(Or even a higher one.)
=== New schedclk() mechansism === Some platforms should be cutting down
stathz before hitting the scheduler, since the scheduler algorithm only
works right in the vicinity of 64 Hz. Rather than prescale hz, then scale
back and forth by 4 every time p_estcpu is touched (each occurance an
abstraction violation), use p_estcpu without scaling and require schedhz
to be generated directly at the right frequency. Use a default stathz (well,
actually, profhz) / 4, so nothing changes unless a platform defines schedhz
and a new clock. Define these for alpha, where hz==1024, and nice was
totally broke.
=== Algorithm change === The nice value used to be added to the
exponentially-decayed scheduler history value p_estcpu, in _addition_ to
be incorporated directly (with greater wieght) into the priority calculation.
At first glance, it appears to be a pointless increase of 1/8 the nice
effect (pri = p_estcpu/4 + nice*2), but it's actually at least 3x that
because it will ramp up linearly but be decayed only exponentially, thus
converging to an additional .75 nice for a loadaverage of one. I killed
this, it makes the behavior hard to control, almost impossible to analyze,
and the effect (~~nothing at for the first second, then somewhat increased
niceness after three seconds or more, depending on load average) pointless.
=== Other bugs === hz -> profhz in the p_pctcpu = f(p_cpticks) calcuation.
Collect scheduler functionality. Try to put each abstraction in just one
place.
----------------------------
The details are a little different in FreeBSD:
=== nice bug === Fixing this is the main point of this commit. We use
essentially the same clipping rule as NetBSD (our limit on p_estcpu
differs by a scale factor). However, clipping at all is fundamentally
bad. It gives free CPU the hoggiest hogs once they reach the limit, and
reaching the limit is normal for long-running hogs. This will be fixed
later.
=== New schedclk() mechanism === We don't use the NetBSD schedclk()
(now schedclock()) mechanism. We require (real)stathz to be about 128
and scale by an extra factor of 2 compared with NetBSD's statclock().
We scale p_estcpu instead of scaling the clock. This is more accurate
and flexible.
=== Algorithm change === Same change.
=== Other bugs === The p_pctcpu bug was fixed long ago. We don't try as
hard to abstract functionality yet.
Related changes: the new limit on p_estcpu must be exported to kern_exit.c
for clipping in wait1().
Agreed with by: dufault
1999-11-28 12:12:13 +00:00
|
|
|
newcpu = decay_cpu(loadfac, newcpu);
|
2001-09-12 08:38:13 +00:00
|
|
|
kg->kg_estcpu = newcpu;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
resetpriority(td->td_ksegrp);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're only looking at 7 bits of the address; everything is
|
|
|
|
* aligned to 4, lots of things are aligned to greater powers
|
|
|
|
* of 2. Shift right by 8, i.e. drop the bottom 256 worth.
|
|
|
|
*/
|
|
|
|
#define TABLESIZE 128
|
2001-09-12 08:38:13 +00:00
|
|
|
static TAILQ_HEAD(slpquehead, thread) slpque[TABLESIZE];
|
1998-07-15 02:32:35 +00:00
|
|
|
#define LOOKUP(x) (((intptr_t)(x) >> 8) & (TABLESIZE - 1))
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1996-07-31 09:26:54 +00:00
|
|
|
void
|
1999-03-03 18:15:29 +00:00
|
|
|
sleepinit(void)
|
1996-07-31 09:26:54 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1999-03-03 18:15:29 +00:00
|
|
|
sched_quantum = hz/10;
|
|
|
|
hogticks = 2 * sched_quantum;
|
1996-07-31 09:26:54 +00:00
|
|
|
for (i = 0; i < TABLESIZE; i++)
|
|
|
|
TAILQ_INIT(&slpque[i]);
|
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* General sleep call. Suspends the current process until a wakeup is
|
|
|
|
* performed on the specified identifier. The process will then be made
|
|
|
|
* runnable with the specified priority. Sleeps at most timo/hz seconds
|
|
|
|
* (0 means no timeout). If pri includes PCATCH flag, signals are checked
|
|
|
|
* before and after sleeping, else signals are not checked. Returns 0 if
|
|
|
|
* awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
|
|
|
|
* signal needs to be delivered, ERESTART is returned if the current system
|
|
|
|
* call should be restarted if possible, and EINTR is returned if the system
|
|
|
|
* call should be interrupted by the signal (return EINTR).
|
2000-09-11 00:20:02 +00:00
|
|
|
*
|
|
|
|
* The mutex argument is exited before the caller is suspended, and
|
|
|
|
* entered before msleep returns. If priority includes the PDROP
|
|
|
|
* flag the mutex is not entered before returning.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
int
|
2000-09-11 00:20:02 +00:00
|
|
|
msleep(ident, mtx, priority, wmesg, timo)
|
1994-05-24 10:09:53 +00:00
|
|
|
void *ident;
|
2000-09-14 20:15:16 +00:00
|
|
|
struct mtx *mtx;
|
1994-05-24 10:09:53 +00:00
|
|
|
int priority, timo;
|
1997-11-21 11:37:03 +00:00
|
|
|
const char *wmesg;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread;
|
2002-05-23 04:14:18 +00:00
|
|
|
struct proc *p = td->td_proc;
|
2001-03-07 03:01:53 +00:00
|
|
|
int sig, catch = priority & PCATCH;
|
2000-09-07 01:33:02 +00:00
|
|
|
int rval = 0;
|
2000-09-11 00:20:02 +00:00
|
|
|
WITNESS_SAVE_DECL(mtx);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#ifdef KTRACE
|
2002-06-07 05:39:16 +00:00
|
|
|
if (KTRPOINT(td, KTR_CSW))
|
|
|
|
ktrcsw(1, 0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
WITNESS_SLEEP(0, &mtx->mtx_object);
|
2001-05-23 19:38:26 +00:00
|
|
|
KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL,
|
|
|
|
("sleeping without a mutex"));
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (cold || panicstr) {
|
|
|
|
/*
|
|
|
|
* After a panic, or during autoconfiguration,
|
|
|
|
* just give interrupts a chance, then just return;
|
|
|
|
* don't run any other procs or panic below,
|
|
|
|
* in case this is the idle process and already asleep.
|
|
|
|
*/
|
2000-11-29 18:32:50 +00:00
|
|
|
if (mtx != NULL && priority & PDROP)
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
mtx_unlock(mtx);
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2000-09-07 01:33:02 +00:00
|
|
|
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
DROP_GIANT();
|
2000-11-29 18:32:50 +00:00
|
|
|
|
|
|
|
if (mtx != NULL) {
|
|
|
|
mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
WITNESS_SAVE(&mtx->mtx_object, mtx);
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
mtx_unlock(mtx);
|
2000-11-29 18:32:50 +00:00
|
|
|
if (priority & PDROP)
|
|
|
|
mtx = NULL;
|
|
|
|
}
|
|
|
|
|
2000-11-15 22:27:38 +00:00
|
|
|
KASSERT(p != NULL, ("msleep1"));
|
2001-09-12 08:38:13 +00:00
|
|
|
KASSERT(ident != NULL && td->td_proc->p_stat == SRUN, ("msleep"));
|
|
|
|
|
|
|
|
td->td_wchan = ident;
|
|
|
|
td->td_wmesg = wmesg;
|
|
|
|
td->td_kse->ke_slptime = 0; /* XXXKSE */
|
|
|
|
td->td_ksegrp->kg_slptime = 0;
|
2002-02-11 20:37:54 +00:00
|
|
|
td->td_priority = priority & PRIMASK;
|
2001-09-12 08:38:13 +00:00
|
|
|
CTR5(KTR_PROC, "msleep: thread %p (pid %d, %s) on %s (%p)",
|
|
|
|
td, p->p_pid, p->p_comm, wmesg, ident);
|
|
|
|
TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], td, td_slpq);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (timo)
|
2001-09-12 08:38:13 +00:00
|
|
|
callout_reset(&td->td_slpcallout, timo, endtsleep, td);
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* We put ourselves on the sleep queue and start our timeout
|
2002-05-29 23:44:32 +00:00
|
|
|
* before calling cursig, as we could stop there, and a wakeup
|
1994-05-24 10:09:53 +00:00
|
|
|
* or a SIGCONT (or both) could occur while we were stopped.
|
|
|
|
* A SIGCONT would cause us to be marked as SSLEEP
|
|
|
|
* without resuming us, thus we must be ready for sleep
|
2002-05-29 23:44:32 +00:00
|
|
|
* when cursig is called. If the wakeup happens while we're
|
|
|
|
* stopped, td->td_wchan will be 0 upon return from cursig.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
if (catch) {
|
2001-06-22 23:11:26 +00:00
|
|
|
CTR3(KTR_PROC, "msleep caught: proc %p (pid %d, %s)", p,
|
|
|
|
p->p_pid, p->p_comm);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_flags |= TDF_SINTR;
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2001-06-22 23:11:26 +00:00
|
|
|
PROC_LOCK(p);
|
2002-05-29 23:44:32 +00:00
|
|
|
sig = cursig(p);
|
2001-06-22 23:11:26 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
PROC_UNLOCK(p);
|
2001-06-22 23:11:26 +00:00
|
|
|
if (sig != 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
if (td->td_wchan != NULL)
|
|
|
|
unsleep(td);
|
|
|
|
} else if (td->td_wchan == NULL)
|
1994-05-24 10:09:53 +00:00
|
|
|
catch = 0;
|
|
|
|
} else
|
|
|
|
sig = 0;
|
2001-09-12 08:38:13 +00:00
|
|
|
if (td->td_wchan != NULL) {
|
|
|
|
td->td_proc->p_stat = SSLEEP;
|
2001-06-22 23:11:26 +00:00
|
|
|
p->p_stats->p_ru.ru_nvcsw++;
|
|
|
|
mi_switch();
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
CTR3(KTR_PROC, "msleep resume: proc %p (pid %d, %s)", td, p->p_pid,
|
2001-06-22 23:11:26 +00:00
|
|
|
p->p_comm);
|
2001-09-12 08:38:13 +00:00
|
|
|
KASSERT(td->td_proc->p_stat == SRUN, ("running but not SRUN"));
|
|
|
|
td->td_flags &= ~TDF_SINTR;
|
|
|
|
if (td->td_flags & TDF_TIMEOUT) {
|
|
|
|
td->td_flags &= ~TDF_TIMEOUT;
|
2001-06-22 23:11:26 +00:00
|
|
|
if (sig == 0)
|
2000-09-07 01:33:02 +00:00
|
|
|
rval = EWOULDBLOCK;
|
2001-09-12 08:38:13 +00:00
|
|
|
} else if (td->td_flags & TDF_TIMOFAIL)
|
|
|
|
td->td_flags &= ~TDF_TIMOFAIL;
|
|
|
|
else if (timo && callout_stop(&td->td_slpcallout) == 0) {
|
2001-08-10 21:08:56 +00:00
|
|
|
/*
|
|
|
|
* This isn't supposed to be pretty. If we are here, then
|
|
|
|
* the endtsleep() callout is currently executing on another
|
|
|
|
* CPU and is either spinning on the sched_lock or will be
|
|
|
|
* soon. If we don't synchronize here, there is a chance
|
|
|
|
* that this process may msleep() again before the callout
|
|
|
|
* has a chance to run and the callout may end up waking up
|
|
|
|
* the wrong msleep(). Yuck.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_flags |= TDF_TIMEOUT;
|
2001-08-10 21:08:56 +00:00
|
|
|
p->p_stats->p_ru.ru_nivcsw++;
|
|
|
|
mi_switch();
|
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2000-11-16 01:07:19 +00:00
|
|
|
|
2001-06-22 23:11:26 +00:00
|
|
|
if (rval == 0 && catch) {
|
2001-03-07 03:01:53 +00:00
|
|
|
PROC_LOCK(p);
|
2002-05-29 23:44:32 +00:00
|
|
|
/* XXX: shouldn't we always be calling cursig() */
|
|
|
|
if (sig != 0 || (sig = cursig(p))) {
|
2001-06-22 23:11:26 +00:00
|
|
|
if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
|
|
|
|
rval = EINTR;
|
|
|
|
else
|
|
|
|
rval = ERESTART;
|
|
|
|
}
|
2001-03-07 03:01:53 +00:00
|
|
|
PROC_UNLOCK(p);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
#ifdef KTRACE
|
2002-06-07 05:39:16 +00:00
|
|
|
if (KTRPOINT(td, KTR_CSW))
|
|
|
|
ktrcsw(0, 0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
2002-06-07 05:39:16 +00:00
|
|
|
PICKUP_GIANT();
|
2000-09-11 00:20:02 +00:00
|
|
|
if (mtx != NULL) {
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock(mtx);
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
WITNESS_RESTORE(&mtx->mtx_object, mtx);
|
2000-09-11 00:20:02 +00:00
|
|
|
}
|
2000-09-07 01:33:02 +00:00
|
|
|
return (rval);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-08-10 06:37:05 +00:00
|
|
|
* Implement timeout for msleep()
|
1998-12-21 07:41:51 +00:00
|
|
|
*
|
1994-05-24 10:09:53 +00:00
|
|
|
* If process hasn't been awakened (wchan non-zero),
|
|
|
|
* set timeout flag and undo the sleep. If proc
|
|
|
|
* is stopped, just unsleep so it will remain stopped.
|
2000-12-01 04:55:52 +00:00
|
|
|
* MP-safe, called without the Giant mutex.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1997-11-22 08:35:46 +00:00
|
|
|
static void
|
1994-05-24 10:09:53 +00:00
|
|
|
endtsleep(arg)
|
|
|
|
void *arg;
|
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
register struct thread *td = arg;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
CTR3(KTR_PROC, "endtsleep: thread %p (pid %d, %s)", td, td->td_proc->p_pid,
|
|
|
|
td->td_proc->p_comm);
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2001-08-10 21:08:56 +00:00
|
|
|
/*
|
|
|
|
* This is the other half of the synchronization with msleep()
|
|
|
|
* described above. If the PS_TIMEOUT flag is set, we lost the
|
|
|
|
* race and just need to put the process back on the runqueue.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
if ((td->td_flags & TDF_TIMEOUT) != 0) {
|
|
|
|
td->td_flags &= ~TDF_TIMEOUT;
|
|
|
|
setrunqueue(td);
|
|
|
|
} else if (td->td_wchan != NULL) {
|
|
|
|
if (td->td_proc->p_stat == SSLEEP) /* XXXKSE */
|
|
|
|
setrunnable(td);
|
1994-05-24 10:09:53 +00:00
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
unsleep(td);
|
|
|
|
td->td_flags |= TDF_TIMEOUT;
|
|
|
|
} else {
|
|
|
|
td->td_flags |= TDF_TIMOFAIL;
|
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a process from its wait queue
|
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
unsleep(struct thread *td)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2001-09-12 08:38:13 +00:00
|
|
|
if (td->td_wchan != NULL) {
|
|
|
|
TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_slpq);
|
|
|
|
td->td_wchan = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-07-06 01:16:43 +00:00
|
|
|
* Make all processes sleeping on the specified identifier runnable.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-07-06 01:16:43 +00:00
|
|
|
wakeup(ident)
|
1994-05-24 10:09:53 +00:00
|
|
|
register void *ident;
|
|
|
|
{
|
1996-07-31 09:26:54 +00:00
|
|
|
register struct slpquehead *qp;
|
2001-09-12 08:38:13 +00:00
|
|
|
register struct thread *td;
|
|
|
|
struct proc *p;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
qp = &slpque[LOOKUP(ident)];
|
|
|
|
restart:
|
2001-09-12 08:38:13 +00:00
|
|
|
TAILQ_FOREACH(td, qp, td_slpq) {
|
|
|
|
p = td->td_proc;
|
|
|
|
if (td->td_wchan == ident) {
|
|
|
|
TAILQ_REMOVE(qp, td, td_slpq);
|
|
|
|
td->td_wchan = NULL;
|
|
|
|
if (td->td_proc->p_stat == SSLEEP) {
|
1994-05-24 10:09:53 +00:00
|
|
|
/* OPTIMIZED EXPANSION OF setrunnable(p); */
|
2001-09-12 08:38:13 +00:00
|
|
|
CTR3(KTR_PROC, "wakeup: thread %p (pid %d, %s)",
|
|
|
|
td, p->p_pid, p->p_comm);
|
|
|
|
if (td->td_ksegrp->kg_slptime > 1)
|
|
|
|
updatepri(td);
|
|
|
|
td->td_ksegrp->kg_slptime = 0;
|
|
|
|
td->td_kse->ke_slptime = 0;
|
|
|
|
td->td_proc->p_stat = SRUN;
|
2001-01-24 11:10:55 +00:00
|
|
|
if (p->p_sflag & PS_INMEM) {
|
2001-09-12 08:38:13 +00:00
|
|
|
setrunqueue(td);
|
2002-02-11 20:37:54 +00:00
|
|
|
maybe_resched(td);
|
1996-07-31 09:26:54 +00:00
|
|
|
} else {
|
2001-01-24 11:10:55 +00:00
|
|
|
p->p_sflag |= PS_SWAPINREQ;
|
1996-07-31 09:26:54 +00:00
|
|
|
wakeup((caddr_t)&proc0);
|
|
|
|
}
|
|
|
|
/* END INLINE EXPANSION */
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1996-07-31 09:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-07-31 10:35:47 +00:00
|
|
|
* Make a process sleeping on the specified identifier runnable.
|
2000-05-07 05:09:45 +00:00
|
|
|
* May wake more than one process if a target process is currently
|
2001-07-06 01:16:43 +00:00
|
|
|
* swapped out.
|
1996-07-31 09:26:54 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-07-06 01:16:43 +00:00
|
|
|
wakeup_one(ident)
|
1996-07-31 09:26:54 +00:00
|
|
|
register void *ident;
|
|
|
|
{
|
|
|
|
register struct slpquehead *qp;
|
2001-09-12 08:38:13 +00:00
|
|
|
register struct thread *td;
|
1996-07-31 09:26:54 +00:00
|
|
|
register struct proc *p;
|
|
|
|
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
1996-07-31 09:26:54 +00:00
|
|
|
qp = &slpque[LOOKUP(ident)];
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
TAILQ_FOREACH(td, qp, td_slpq) {
|
|
|
|
p = td->td_proc;
|
|
|
|
if (td->td_wchan == ident) {
|
|
|
|
TAILQ_REMOVE(qp, td, td_slpq);
|
|
|
|
td->td_wchan = NULL;
|
|
|
|
if (td->td_proc->p_stat == SSLEEP) {
|
1996-07-31 09:26:54 +00:00
|
|
|
/* OPTIMIZED EXPANSION OF setrunnable(p); */
|
2001-06-22 23:11:26 +00:00
|
|
|
CTR3(KTR_PROC, "wakeup1: proc %p (pid %d, %s)",
|
|
|
|
p, p->p_pid, p->p_comm);
|
2001-09-12 08:38:13 +00:00
|
|
|
if (td->td_ksegrp->kg_slptime > 1)
|
|
|
|
updatepri(td);
|
|
|
|
td->td_ksegrp->kg_slptime = 0;
|
|
|
|
td->td_kse->ke_slptime = 0;
|
|
|
|
td->td_proc->p_stat = SRUN;
|
2001-01-24 11:10:55 +00:00
|
|
|
if (p->p_sflag & PS_INMEM) {
|
2001-09-12 08:38:13 +00:00
|
|
|
setrunqueue(td);
|
2002-02-11 20:37:54 +00:00
|
|
|
maybe_resched(td);
|
1996-07-31 10:35:47 +00:00
|
|
|
break;
|
1996-07-31 09:26:54 +00:00
|
|
|
} else {
|
2001-01-24 11:10:55 +00:00
|
|
|
p->p_sflag |= PS_SWAPINREQ;
|
1996-07-31 09:26:54 +00:00
|
|
|
wakeup((caddr_t)&proc0);
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
/* END INLINE EXPANSION */
|
|
|
|
}
|
1996-07-31 09:26:54 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The machine independent parts of mi_switch().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
mi_switch()
|
|
|
|
{
|
2002-02-22 13:32:01 +00:00
|
|
|
struct bintime new_switchtime;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread; /* XXX */
|
|
|
|
register struct proc *p = td->td_proc; /* XXX */
|
2001-01-24 11:10:55 +00:00
|
|
|
#if 0
|
|
|
|
register struct rlimit *rlim;
|
2001-01-23 16:35:33 +00:00
|
|
|
#endif
|
2001-02-20 05:26:15 +00:00
|
|
|
u_int sched_nest;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-01-24 11:10:55 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
|
2001-10-23 17:52:49 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
if (p->p_stat != SMTX && p->p_stat != SRUN)
|
|
|
|
mtx_assert(&Giant, MA_NOTOWNED);
|
|
|
|
#endif
|
2000-09-07 01:33:02 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Compute the amount of time during which the current
|
|
|
|
* process was running, and add that to its total so far.
|
|
|
|
*/
|
2002-02-22 13:32:01 +00:00
|
|
|
binuptime(&new_switchtime);
|
|
|
|
bintime_add(&p->p_runtime, &new_switchtime);
|
|
|
|
bintime_sub(&p->p_runtime, PCPU_PTR(switchtime));
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-08-21 20:09:05 +00:00
|
|
|
#ifdef DDB
|
|
|
|
/*
|
|
|
|
* Don't perform context switches from the debugger.
|
|
|
|
*/
|
2001-08-21 23:10:37 +00:00
|
|
|
if (db_active) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2001-08-21 20:09:05 +00:00
|
|
|
db_error("Context switches not allowed in the debugger.");
|
2001-08-21 23:10:37 +00:00
|
|
|
}
|
2001-08-21 20:09:05 +00:00
|
|
|
#endif
|
|
|
|
|
2001-01-20 02:57:59 +00:00
|
|
|
#if 0
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Check if the process exceeds its cpu resource allocation.
|
1996-09-22 06:35:24 +00:00
|
|
|
* If over max, kill it.
|
2000-09-07 01:33:02 +00:00
|
|
|
*
|
|
|
|
* XXX drop sched_lock, pickup Giant
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1998-11-27 11:44:22 +00:00
|
|
|
if (p->p_stat != SZOMB && p->p_limit->p_cpulimit != RLIM_INFINITY &&
|
|
|
|
p->p_runtime > p->p_limit->p_cpulimit) {
|
2001-01-23 16:35:33 +00:00
|
|
|
rlim = &p->p_rlimit[RLIMIT_CPU];
|
1998-11-26 14:05:58 +00:00
|
|
|
if (p->p_runtime / (rlim_t)1000000 >= rlim->rlim_max) {
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2001-05-15 23:15:06 +00:00
|
|
|
PROC_LOCK(p);
|
1998-05-28 09:30:28 +00:00
|
|
|
killproc(p, "exceeded maximum CPU limit");
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
PROC_UNLOCK(p);
|
1998-11-26 14:05:58 +00:00
|
|
|
} else {
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2001-03-07 03:01:53 +00:00
|
|
|
PROC_LOCK(p);
|
1998-05-28 09:30:28 +00:00
|
|
|
psignal(p, SIGXCPU);
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
PROC_UNLOCK(p);
|
1998-05-28 09:30:28 +00:00
|
|
|
if (rlim->rlim_cur < rlim->rlim_max) {
|
|
|
|
/* XXX: we should make a private copy */
|
|
|
|
rlim->rlim_cur += 5;
|
1994-12-12 06:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2001-01-20 02:57:59 +00:00
|
|
|
#endif
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pick a new current process and record its start time.
|
|
|
|
*/
|
|
|
|
cnt.v_swtch++;
|
2001-01-10 04:43:51 +00:00
|
|
|
PCPU_SET(switchtime, new_switchtime);
|
2001-06-22 23:11:26 +00:00
|
|
|
CTR3(KTR_PROC, "mi_switch: old proc %p (pid %d, %s)", p, p->p_pid,
|
|
|
|
p->p_comm);
|
2001-02-20 05:26:15 +00:00
|
|
|
sched_nest = sched_lock.mtx_recurse;
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_lastcpu = td->td_kse->ke_oncpu;
|
|
|
|
td->td_kse->ke_oncpu = NOCPU;
|
|
|
|
td->td_kse->ke_flags &= ~KEF_NEEDRESCHED;
|
2000-09-07 01:33:02 +00:00
|
|
|
cpu_switch();
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_kse->ke_oncpu = PCPU_GET(cpuid);
|
2001-02-20 05:26:15 +00:00
|
|
|
sched_lock.mtx_recurse = sched_nest;
|
2001-09-12 08:38:13 +00:00
|
|
|
sched_lock.mtx_lock = (uintptr_t)td;
|
2001-06-22 23:11:26 +00:00
|
|
|
CTR3(KTR_PROC, "mi_switch: new proc %p (pid %d, %s)", p, p->p_pid,
|
|
|
|
p->p_comm);
|
2002-02-22 13:32:01 +00:00
|
|
|
if (PCPU_GET(switchtime.sec) == 0)
|
|
|
|
binuptime(PCPU_PTR(switchtime));
|
2001-01-10 04:43:51 +00:00
|
|
|
PCPU_SET(switchticks, ticks);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change process state to be runnable,
|
|
|
|
* placing it on the run queue if it is in memory,
|
|
|
|
* and awakening the swapper if it isn't in memory.
|
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
setrunnable(struct thread *td)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct proc *p = td->td_proc;
|
2001-09-21 19:16:12 +00:00
|
|
|
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
switch (p->p_stat) {
|
2001-09-12 08:38:13 +00:00
|
|
|
case SZOMB: /* not a thread flag XXXKSE */
|
2001-09-21 19:16:12 +00:00
|
|
|
panic("setrunnable(1)");
|
2001-09-12 08:38:13 +00:00
|
|
|
}
|
|
|
|
switch (td->td_proc->p_stat) {
|
1994-05-24 10:09:53 +00:00
|
|
|
case 0:
|
|
|
|
case SRUN:
|
2000-09-07 01:33:02 +00:00
|
|
|
case SWAIT:
|
1994-05-24 10:09:53 +00:00
|
|
|
default:
|
2001-09-12 08:38:13 +00:00
|
|
|
panic("setrunnable(2)");
|
1994-05-24 10:09:53 +00:00
|
|
|
case SSTOP:
|
2001-01-16 01:00:43 +00:00
|
|
|
case SSLEEP: /* e.g. when sending signals */
|
2001-09-12 08:38:13 +00:00
|
|
|
if (td->td_flags & TDF_CVWAITQ)
|
|
|
|
cv_waitq_remove(td);
|
2001-01-16 01:00:43 +00:00
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
unsleep(td);
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SIDL:
|
|
|
|
break;
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_stat = SRUN;
|
|
|
|
if (td->td_ksegrp->kg_slptime > 1)
|
|
|
|
updatepri(td);
|
|
|
|
td->td_ksegrp->kg_slptime = 0;
|
|
|
|
td->td_kse->ke_slptime = 0;
|
2001-01-24 11:10:55 +00:00
|
|
|
if ((p->p_sflag & PS_INMEM) == 0) {
|
|
|
|
p->p_sflag |= PS_SWAPINREQ;
|
1994-05-24 10:09:53 +00:00
|
|
|
wakeup((caddr_t)&proc0);
|
2001-07-03 07:53:35 +00:00
|
|
|
} else {
|
2001-09-12 08:38:13 +00:00
|
|
|
setrunqueue(td);
|
2002-02-11 20:37:54 +00:00
|
|
|
maybe_resched(td);
|
2001-07-03 07:53:35 +00:00
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the priority of a process when running in user mode.
|
|
|
|
* Arrange to reschedule if the resulting priority is better
|
|
|
|
* than that of the current process.
|
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
resetpriority(kg)
|
|
|
|
register struct ksegrp *kg;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
register unsigned int newpriority;
|
2002-02-11 20:37:54 +00:00
|
|
|
struct thread *td;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2002-02-11 20:37:54 +00:00
|
|
|
if (kg->kg_pri_class == PRI_TIMESHARE) {
|
2001-09-12 08:38:13 +00:00
|
|
|
newpriority = PUSER + kg->kg_estcpu / INVERSE_ESTCPU_WEIGHT +
|
|
|
|
NICE_WEIGHT * (kg->kg_nice - PRIO_MIN);
|
2001-02-12 00:20:08 +00:00
|
|
|
newpriority = min(max(newpriority, PRI_MIN_TIMESHARE),
|
|
|
|
PRI_MAX_TIMESHARE);
|
2002-02-11 20:37:54 +00:00
|
|
|
kg->kg_user_pri = newpriority;
|
|
|
|
}
|
|
|
|
FOREACH_THREAD_IN_GROUP(kg, td) {
|
|
|
|
maybe_resched(td);
|
1994-09-01 05:12:53 +00:00
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1997-11-25 07:07:48 +00:00
|
|
|
|
2001-10-20 13:10:43 +00:00
|
|
|
/*
|
|
|
|
* Compute a tenex style load average of a quantity on
|
|
|
|
* 1, 5 and 15 minute intervals.
|
|
|
|
* XXXKSE Needs complete rewrite when correct info is available.
|
|
|
|
* Completely Bogus.. only works with 1:1 (but compiles ok now :-)
|
|
|
|
*/
|
|
|
|
static void
|
2001-10-20 16:07:17 +00:00
|
|
|
loadav(void *arg)
|
2001-10-20 13:10:43 +00:00
|
|
|
{
|
|
|
|
int i, nrun;
|
2001-10-20 16:07:17 +00:00
|
|
|
struct loadavg *avg;
|
2001-10-20 13:10:43 +00:00
|
|
|
struct proc *p;
|
|
|
|
struct ksegrp *kg;
|
|
|
|
|
2001-10-20 16:07:17 +00:00
|
|
|
avg = &averunnable;
|
2001-10-20 13:10:43 +00:00
|
|
|
sx_slock(&allproc_lock);
|
|
|
|
nrun = 0;
|
|
|
|
FOREACH_PROC_IN_SYSTEM(p) {
|
|
|
|
FOREACH_KSEGRP_IN_PROC(p, kg) {
|
|
|
|
switch (p->p_stat) {
|
|
|
|
case SRUN:
|
|
|
|
if ((p->p_flag & P_NOLOAD) != 0)
|
|
|
|
goto nextproc;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case SIDL:
|
|
|
|
nrun++;
|
|
|
|
}
|
|
|
|
nextproc:
|
2002-03-19 11:02:06 +00:00
|
|
|
continue;
|
2001-10-20 13:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sx_sunlock(&allproc_lock);
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
|
|
|
|
nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
|
2001-10-20 16:07:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Schedule the next update to occur after 5 seconds, but add a
|
|
|
|
* random variation to avoid synchronisation with processes that
|
|
|
|
* run at regular intervals.
|
|
|
|
*/
|
|
|
|
callout_reset(&loadav_callout, hz * 4 + (int)(random() % (hz * 2 + 1)),
|
|
|
|
loadav, NULL);
|
2001-10-20 13:10:43 +00:00
|
|
|
}
|
|
|
|
|
1997-11-25 07:07:48 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
|
|
|
sched_setup(dummy)
|
|
|
|
void *dummy;
|
|
|
|
{
|
2000-11-27 22:52:31 +00:00
|
|
|
|
|
|
|
callout_init(&schedcpu_callout, 1);
|
|
|
|
callout_init(&roundrobin_callout, 0);
|
2001-10-20 16:07:17 +00:00
|
|
|
callout_init(&loadav_callout, 0);
|
2000-11-27 22:52:31 +00:00
|
|
|
|
1997-11-25 07:07:48 +00:00
|
|
|
/* Kick off timeout driven events by calling first time. */
|
|
|
|
roundrobin(NULL);
|
|
|
|
schedcpu(NULL);
|
2001-10-20 16:07:17 +00:00
|
|
|
loadav(NULL);
|
1997-11-25 07:07:48 +00:00
|
|
|
}
|
|
|
|
|
1999-11-27 12:32:27 +00:00
|
|
|
/*
|
|
|
|
* We adjust the priority of the current process. The priority of
|
|
|
|
* a process gets worse as it accumulates CPU time. The cpu usage
|
1999-11-27 15:27:11 +00:00
|
|
|
* estimator (p_estcpu) is increased here. resetpriority() will
|
Scheduler fixes equivalent to the ones logged in the following NetBSD
commit to kern_synch.c:
----------------------------
revision 1.55
date: 1999/02/23 02:56:03; author: ross; state: Exp; lines: +39 -10
Scheduler bug fixes and reorganization
* fix the ancient nice(1) bug, where nice +20 processes incorrectly
steal 10 - 20% of the CPU, (or even more depending on load average)
* provide a new schedclk() mechanism at a new clock at schedhz, so high
platform hz values don't cause nice +0 processes to look like they are
niced
* change the algorithm slightly, and reorganize the code a lot
* fix percent-CPU calculation bugs, and eliminate some no-op code
=== nice bug === Correctly divide the scheduler queues between niced and
compute-bound processes. The current nice weight of two (sort of, see
`algorithm change' below) neatly divides the USRPRI queues in half; this
should have been used to clip p_estcpu, instead of UCHAR_MAX. Besides
being the wrong amount, clipping an unsigned char to UCHAR_MAX is a no-op,
and it was done after decay_cpu() which can only _reduce_ the value. It
has to be kept <= NICE_WEIGHT * PRIO_MAX - PPQ or processes can
scheduler-penalize themselves onto the same queue as nice +20 processes.
(Or even a higher one.)
=== New schedclk() mechansism === Some platforms should be cutting down
stathz before hitting the scheduler, since the scheduler algorithm only
works right in the vicinity of 64 Hz. Rather than prescale hz, then scale
back and forth by 4 every time p_estcpu is touched (each occurance an
abstraction violation), use p_estcpu without scaling and require schedhz
to be generated directly at the right frequency. Use a default stathz (well,
actually, profhz) / 4, so nothing changes unless a platform defines schedhz
and a new clock. Define these for alpha, where hz==1024, and nice was
totally broke.
=== Algorithm change === The nice value used to be added to the
exponentially-decayed scheduler history value p_estcpu, in _addition_ to
be incorporated directly (with greater wieght) into the priority calculation.
At first glance, it appears to be a pointless increase of 1/8 the nice
effect (pri = p_estcpu/4 + nice*2), but it's actually at least 3x that
because it will ramp up linearly but be decayed only exponentially, thus
converging to an additional .75 nice for a loadaverage of one. I killed
this, it makes the behavior hard to control, almost impossible to analyze,
and the effect (~~nothing at for the first second, then somewhat increased
niceness after three seconds or more, depending on load average) pointless.
=== Other bugs === hz -> profhz in the p_pctcpu = f(p_cpticks) calcuation.
Collect scheduler functionality. Try to put each abstraction in just one
place.
----------------------------
The details are a little different in FreeBSD:
=== nice bug === Fixing this is the main point of this commit. We use
essentially the same clipping rule as NetBSD (our limit on p_estcpu
differs by a scale factor). However, clipping at all is fundamentally
bad. It gives free CPU the hoggiest hogs once they reach the limit, and
reaching the limit is normal for long-running hogs. This will be fixed
later.
=== New schedclk() mechanism === We don't use the NetBSD schedclk()
(now schedclock()) mechanism. We require (real)stathz to be about 128
and scale by an extra factor of 2 compared with NetBSD's statclock().
We scale p_estcpu instead of scaling the clock. This is more accurate
and flexible.
=== Algorithm change === Same change.
=== Other bugs === The p_pctcpu bug was fixed long ago. We don't try as
hard to abstract functionality yet.
Related changes: the new limit on p_estcpu must be exported to kern_exit.c
for clipping in wait1().
Agreed with by: dufault
1999-11-28 12:12:13 +00:00
|
|
|
* compute a different priority each time p_estcpu increases by
|
|
|
|
* INVERSE_ESTCPU_WEIGHT
|
1999-11-27 15:27:11 +00:00
|
|
|
* (until MAXPRI is reached). The cpu usage estimator ramps up
|
1999-11-27 12:32:27 +00:00
|
|
|
* quite quickly when the process is running (linearly), and decays
|
|
|
|
* away exponentially, at a rate which is proportionally slower when
|
1999-11-27 15:27:11 +00:00
|
|
|
* the system is busy. The basic principle is that the system will
|
1999-11-27 12:32:27 +00:00
|
|
|
* 90% forget that the process used a lot of CPU time in 5 * loadav
|
|
|
|
* seconds. This causes the system to favor processes which haven't
|
|
|
|
* run much recently, and to round-robin among other processes.
|
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
schedclock(td)
|
|
|
|
struct thread *td;
|
1999-11-27 12:32:27 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct kse *ke = td->td_kse;
|
|
|
|
struct ksegrp *kg = td->td_ksegrp;
|
|
|
|
|
|
|
|
if (td) {
|
|
|
|
ke->ke_cpticks++;
|
|
|
|
kg->kg_estcpu = ESTCPULIM(kg->kg_estcpu + 1);
|
|
|
|
if ((kg->kg_estcpu % INVERSE_ESTCPU_WEIGHT) == 0) {
|
|
|
|
resetpriority(td->td_ksegrp);
|
2002-02-11 20:37:54 +00:00
|
|
|
if (td->td_priority >= PUSER)
|
|
|
|
td->td_priority = kg->kg_user_pri;
|
2001-09-12 08:38:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic("schedclock");
|
1999-11-27 12:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
2000-12-02 05:41:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* General purpose yield system call
|
|
|
|
*/
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
yield(struct thread *td, struct yield_args *uap)
|
2000-12-02 05:41:30 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct ksegrp *kg = td->td_ksegrp;
|
2000-12-02 05:41:30 +00:00
|
|
|
|
2001-09-01 03:54:09 +00:00
|
|
|
mtx_assert(&Giant, MA_NOTOWNED);
|
2001-09-21 19:21:18 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2002-02-11 20:37:54 +00:00
|
|
|
td->td_priority = PRI_MAX_TIMESHARE;
|
2001-09-12 08:38:13 +00:00
|
|
|
setrunqueue(td);
|
|
|
|
kg->kg_proc->p_stats->p_ru.ru_nvcsw++;
|
2000-12-02 05:41:30 +00:00
|
|
|
mi_switch();
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2001-09-21 19:21:18 +00:00
|
|
|
td->td_retval[0] = 0;
|
2000-12-02 05:41:30 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2001-09-01 03:54:09 +00:00
|
|
|
|