/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/sysmacros.h>
#include <sys/tuneable.h>
#include <sys/tnf_probe.h>
#include <vm/seg_kmem.h>
/*
* The swapper sleeps on runout when there is no one to swap in.
* It sleeps on runin when it could not find space to swap someone
* in or after swapping someone in.
*/
char runout;
char runin;
/*
* The swapper swaps processes to reduce memory demand and runs
* when avefree < desfree. The swapper resorts to SOFTSWAP when
* avefree < desfree which results in swapping out all processes
* sleeping for more than maxslp seconds. HARDSWAP occurs when the
* system is on the verge of thrashing and this results in swapping
* out runnable threads or threads sleeping for less than maxslp secs.
*
* The swapper runs through all the active processes in the system
* for every thread in the process to obtain an effective priority
* for the process. A priority of -1 implies that the thread isn't
* swappable. This effective priority is used to find the most
* eligible process to swapout or swapin.
*
* NOTE: Threads which have been swapped are not linked on any
* queue and their dispatcher lock points at the "swapped_lock".
*
* Processes containing threads with the TS_DONT_SWAP flag set cannot be
* swapped out immediately by the swapper. This is due to the fact that
* such threads may be holding locks which may be needed by the swapper
* to push its pages out. The TS_SWAPENQ flag is set on such threads
* to prevent them running in user mode. When such threads reach a
* safe point (i.e., are not holding any locks - CL_TRAPRET), they
* queue themseleves onto the swap queue which is processed by the
* swapper. This results in reducing memory demand when the system
* is desparate for memory as the thread can't run in user mode.
*
* The swap queue consists of threads, linked via t_link, which are
* haven't been swapped, are runnable but not on the run queue. The
* swap queue is protected by the "swapped_lock". The dispatcher
* lock (t_lockp) of all threads on the swap queue points at the
* queue can be locked by acquiring "swapped_lock".
*/
int maxslp = 0;
/*
* Minimum size used to decide if sufficient memory is available
* before a process is swapped in. This is necessary since in most
* cases the actual size of a process (p_swrss) being swapped in
* is usually 2 pages (kernel stack pages). This is due to the fact
* almost all user pages of a process are stolen by pageout before
* the swapper decides to swapout it out.
*/
static void process_swap_queue();
#ifdef __sparc
extern void lwp_swapin(kthread_t *);
#endif /* __sparc */
/*
* Counters to keep track of the number of swapins or swapouts.
*/
/*
* Macro to determine if a process is eligble to be swapped.
*/
#define not_swappable(p) \
/*
* Memory scheduler.
*/
void
sched()
{
kthread_id_t t;
int desperate;
int divisor;
int chosen_pri;
if (maxslp == 0)
loop:
needs = 0;
desperate = 0;
swapin_pri = v.v_nglobpris;
swapin_prp = NULL;
chosen_pri = -1;
/*
* Set desperate if
* 1. At least 2 runnable processes (on average).
* 2. Short (5 sec) and longer (30 sec) average is less
* than minfree and desfree respectively.
* 3. Pagein + pageout rate is excessive.
*/
"desp:avefree: %d, avefree30: %d, freemem: %d"
desperate = 1;
goto unload;
}
/*
* Search list of processes to swapin and swapout deadwood.
*/
swapin_proc_time = 0;
top:
if (not_swappable(prp))
continue;
/*
* Look at processes with at least one swapped lwp.
*/
/*
* Higher priority processes are good candidates
* to swapin.
*/
proc_pri = -1;
proc_time = 0;
do {
if (t->t_schedflag & TS_LOAD)
continue;
thread_lock(t);
thread_pri = CL_SWAPIN(t, 0);
thread_unlock(t);
if (thread_pri > proc_pri)
if (proc_pri == -1)
continue;
"prp %p epri %d proc_time %d",
/*
* Swapin processes with a high effective priority.
*/
swapin_prp = prp;
}
} else {
/*
* No need to soft swap if we have sufficient
* memory.
*/
continue;
/*
* Skip processes that are exiting
* or whose address spaces are locked.
*/
continue;
}
/*
* Softswapping to kick out deadwood.
*/
proc_pri = -1;
do {
if ((t->t_schedflag & (TS_SWAPENQ |
continue;
thread_lock(t);
thread_unlock(t);
if (thread_pri > proc_pri)
if (proc_pri != -1) {
"softswap:prp %p", prp);
softswap++;
goto top;
}
}
}
if (swapin_prp != NULL)
if (swapin_prp == NULL) {
"schedrunout:runout nswapped: %d, avefree: %ld freemem: %ld",
t = curthread;
thread_lock(t);
runout++;
t->t_whystop = PR_SUSPENDED;
t->t_whatstop = SUSPEND_NORMAL;
(void) new_mstate(t, LMS_SLEEP);
thread_stop(t); /* change state and drop lock */
swtch();
goto loop;
}
/*
* Decide how deserving this process is to be brought in.
* Needs is an estimate of how much core the process will
* need. If the process has been out for a while, then we
* will bring it in with 1/2 the core needed, otherwise
* we are conservative.
*/
divisor = 1;
divisor = 2;
/*
* Use freemem, since we want processes to be swapped
* in quickly.
*/
if (swapin(swapin_prp)) {
goto loop;
}
} else {
/*
* If deficit is high, too many processes have been
* swapped in so wait a sec before attempting to
* swapin more.
*/
goto block;
}
}
/*
* Unload all unloadable modules, free all other memory
* resources we can find, then look for a thread to hardswap.
*/
modreap();
swapout_prp = NULL;
/*
* No need to soft swap if we have sufficient
* memory.
*/
if (not_swappable(prp))
continue;
swapout_prp = NULL;
break;
}
/*
* Skip processes that are exiting
* or whose address spaces are locked.
*/
continue;
}
proc_pri = -1;
do {
if ((t->t_schedflag & (TS_SWAPENQ |
continue;
thread_lock(t);
thread_unlock(t);
if (thread_pri > proc_pri)
if (proc_pri == -1)
continue;
/*
* Swapout processes sleeping with a lower priority
* than the one currently being swapped in, if any.
*/
swapout_prp = prp;
}
}
}
/*
* Acquire the "p_lock" before dropping "pidlock"
* to prevent the proc structure from being freed
* if the process exits before swapout completes.
*/
if (swapout_prp != NULL)
int swapped;
if (swapped) {
/*
* If desperate, we want to give the space obtained
* by swapping this process out to processes in core,
* so we give them a chance by increasing deficit.
*/
if (desperate)
hardswap++;
}
if (swapped)
goto loop;
}
/*
* Delay for 1 second and look again later.
*/
"schedrunin:runin nswapped: %d, avefree: %ld freemem: %ld",
t = curthread;
thread_lock(t);
runin++;
t->t_whystop = PR_SUSPENDED;
t->t_whatstop = SUSPEND_NORMAL;
(void) new_mstate(t, LMS_SLEEP);
thread_stop(t); /* change to stop state and drop lock */
swtch();
goto loop;
}
/*
* Remove the specified thread from the swap queue.
*/
static void
{
tpp = &tswap_queue;
for (;;) {
break;
}
}
/*
* Swap in lwps. Returns nonzero on success (i.e., if at least one lwp is
* swapped in) and 0 on failure.
*/
static int
{
int err;
int num_swapped_in = 0;
top:
do {
/*
* Only swapin eligible lwps (specified by the scheduling
* class) which are unloaded and ready to run.
*/
/*
* Now drop the p_lock since the stack needs
* to brought in.
*/
/* Kernel probe */
/*
* Re-acquire the p_lock.
*/
if (err) {
num_swapped_in = 0;
break;
} else {
#ifdef __sparc
lwp_swapin(tp);
#endif /* __sparc */
dq_sruninc(tp);
/* set swapin time */
nswapped--;
"swapin: pp %p stack_pages %lu",
pp, stack_pages);
goto top;
}
}
return (num_swapped_in);
}
/*
* Swap out lwps. Returns nonzero on success (i.e., if at least one lwp is
* swapped out) and 0 on failure.
*/
static int
{
int err;
int swapped_lwps = 0;
return (0);
top:
do {
/*
* Swapout eligible lwps (specified by the scheduling
* class) which don't have TS_DONT_SWAP set. Set the
* "intent to swap" flag (TS_SWAPENQ) on threads
* which have TS_DONT_SWAP set so that they can be
* swapped if and when they reach a safe point.
*/
if (thread_pri != -1) {
} else {
/*
* Remove thread from the swap_queue.
*/
TS_SWAPENQ));
dq_srundec(tp);
tp->t_schedflag &=
~(TS_LOAD | TS_SWAPENQ);
/*
* Set swapout time if the thread isn't
* sleeping.
*/
nswapped++;
/*
* Now drop the p_lock since the
* stack needs to pushed out.
*/
ws_pages += stack_pages;
/* Kernel probe */
"vm swap swapout",
/* CSTYLED */,
if (err) {
"swapout: segkp_fault "
"failed err: %d", err);
}
swapped_lwps++;
goto top;
}
}
}
/*
* Unload address space when all lwps are swapped out.
*/
/*
* Avoid invoking as_swapout() if the process has
* no MMU resources since pageout will eventually
* steal pages belonging to this address space. This
* saves CPU cycles as the number of pages that are
* potentially freed or pushed out by the segment
* swapout operation is very small.
*/
/* Kernel probe */
}
return (swapped_lwps);
}
void
{
/*
* Don't insert the thread onto the swap queue if
* sufficient memory is available.
*/
return;
}
/*
* Lock the thread, then move it to the swapped queue from the
* onproc queue and set its state to be TS_RUN.
*/
/*
* Set the state of this thread to be runnable
* and move it from the onproc queue to the swap queue.
*/
/*
* Insert the thread onto the swap queue.
*/
tswap_queue = tp;
swtch();
} else {
}
}
/*
* Swap all threads on the swap queue.
*/
static void
process_swap_queue(void)
{
int err;
if (tswap_queue == NULL)
return;
/*
* Acquire the "swapped_lock" which locks the swap queue,
* and unload the stacks of all threads on it.
*/
/*
* Drop the "dispatcher lock" before acquiring "t_lock"
* to avoid spinning on it since the thread at the front
* of the swap queue could be pinned before giving up
* its "t_lock" in resume.
*/
/*
* Now, re-acquire the "swapped_lock". Acquiring this lock
* results in locking the thread since its dispatcher lock
* (t_lockp) is the "swapped_lock".
*/
/* Kernel probe */
if (err) {
"process_swap_list: segkp_fault failed err: %d", err);
}
nswapped++;
swapqswap++;
/*
* Don't need p_lock since the swapper is the only
* thread which increments/decrements p_swapcnt and p_swrss.
*/
/*
* Unload address space when all lwps are swapped out.
*/
"swaplist_proc: pp %p pages_pushed: %lu",
/* Kernel probe */
/* CSTYLED */,
}
}
}