10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51483Svb70745 * Common Development and Distribution License (the "License").
61483Svb70745 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21390Sraf
220Sstevel@tonic-gate /*
23*11066Srafael.vanoni@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/cpuvar.h>
360Sstevel@tonic-gate #include <sys/var.h>
370Sstevel@tonic-gate #include <sys/tuneable.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/buf.h>
400Sstevel@tonic-gate #include <sys/disp.h>
410Sstevel@tonic-gate #include <sys/vmsystm.h>
420Sstevel@tonic-gate #include <sys/vmparam.h>
430Sstevel@tonic-gate #include <sys/class.h>
440Sstevel@tonic-gate #include <sys/vtrace.h>
450Sstevel@tonic-gate #include <sys/modctl.h>
460Sstevel@tonic-gate #include <sys/debug.h>
470Sstevel@tonic-gate #include <sys/tnf_probe.h>
480Sstevel@tonic-gate #include <sys/procfs.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include <vm/seg.h>
510Sstevel@tonic-gate #include <vm/seg_kp.h>
520Sstevel@tonic-gate #include <vm/as.h>
530Sstevel@tonic-gate #include <vm/rm.h>
540Sstevel@tonic-gate #include <vm/seg_kmem.h>
550Sstevel@tonic-gate #include <sys/callb.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * The swapper sleeps on runout when there is no one to swap in.
590Sstevel@tonic-gate * It sleeps on runin when it could not find space to swap someone
600Sstevel@tonic-gate * in or after swapping someone in.
610Sstevel@tonic-gate */
620Sstevel@tonic-gate char runout;
630Sstevel@tonic-gate char runin;
640Sstevel@tonic-gate char wake_sched; /* flag tells clock to wake swapper on next tick */
650Sstevel@tonic-gate char wake_sched_sec; /* flag tells clock to wake swapper after a second */
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * The swapper swaps processes to reduce memory demand and runs
690Sstevel@tonic-gate * when avefree < desfree. The swapper resorts to SOFTSWAP when
700Sstevel@tonic-gate * avefree < desfree which results in swapping out all processes
710Sstevel@tonic-gate * sleeping for more than maxslp seconds. HARDSWAP occurs when the
720Sstevel@tonic-gate * system is on the verge of thrashing and this results in swapping
730Sstevel@tonic-gate * out runnable threads or threads sleeping for less than maxslp secs.
740Sstevel@tonic-gate *
750Sstevel@tonic-gate * The swapper runs through all the active processes in the system
760Sstevel@tonic-gate * and invokes the scheduling class specific swapin/swapout routine
770Sstevel@tonic-gate * for every thread in the process to obtain an effective priority
780Sstevel@tonic-gate * for the process. A priority of -1 implies that the thread isn't
790Sstevel@tonic-gate * swappable. This effective priority is used to find the most
800Sstevel@tonic-gate * eligible process to swapout or swapin.
810Sstevel@tonic-gate *
820Sstevel@tonic-gate * NOTE: Threads which have been swapped are not linked on any
830Sstevel@tonic-gate * queue and their dispatcher lock points at the "swapped_lock".
840Sstevel@tonic-gate *
850Sstevel@tonic-gate * Processes containing threads with the TS_DONT_SWAP flag set cannot be
860Sstevel@tonic-gate * swapped out immediately by the swapper. This is due to the fact that
870Sstevel@tonic-gate * such threads may be holding locks which may be needed by the swapper
880Sstevel@tonic-gate * to push its pages out. The TS_SWAPENQ flag is set on such threads
890Sstevel@tonic-gate * to prevent them running in user mode. When such threads reach a
900Sstevel@tonic-gate * safe point (i.e., are not holding any locks - CL_TRAPRET), they
910Sstevel@tonic-gate * queue themseleves onto the swap queue which is processed by the
920Sstevel@tonic-gate * swapper. This results in reducing memory demand when the system
930Sstevel@tonic-gate * is desparate for memory as the thread can't run in user mode.
940Sstevel@tonic-gate *
950Sstevel@tonic-gate * The swap queue consists of threads, linked via t_link, which are
960Sstevel@tonic-gate * haven't been swapped, are runnable but not on the run queue. The
970Sstevel@tonic-gate * swap queue is protected by the "swapped_lock". The dispatcher
980Sstevel@tonic-gate * lock (t_lockp) of all threads on the swap queue points at the
990Sstevel@tonic-gate * "swapped_lock". Thus, the entire queue and/or threads on the
1000Sstevel@tonic-gate * queue can be locked by acquiring "swapped_lock".
1010Sstevel@tonic-gate */
1020Sstevel@tonic-gate static kthread_t *tswap_queue;
1030Sstevel@tonic-gate extern disp_lock_t swapped_lock; /* protects swap queue and threads on it */
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate int maxslp = 0;
1060Sstevel@tonic-gate pgcnt_t avefree; /* 5 sec moving average of free memory */
1070Sstevel@tonic-gate pgcnt_t avefree30; /* 30 sec moving average of free memory */
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * Minimum size used to decide if sufficient memory is available
1110Sstevel@tonic-gate * before a process is swapped in. This is necessary since in most
1120Sstevel@tonic-gate * cases the actual size of a process (p_swrss) being swapped in
1130Sstevel@tonic-gate * is usually 2 pages (kernel stack pages). This is due to the fact
1140Sstevel@tonic-gate * almost all user pages of a process are stolen by pageout before
1150Sstevel@tonic-gate * the swapper decides to swapout it out.
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate int min_procsize = 12;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static int swapin(proc_t *);
1200Sstevel@tonic-gate static int swapout(proc_t *, uint_t *, int);
1210Sstevel@tonic-gate static void process_swap_queue();
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate #ifdef __sparc
1240Sstevel@tonic-gate extern void lwp_swapin(kthread_t *);
1250Sstevel@tonic-gate #endif /* __sparc */
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate * Counters to keep track of the number of swapins or swapouts.
1290Sstevel@tonic-gate */
1300Sstevel@tonic-gate uint_t tot_swapped_in, tot_swapped_out;
1310Sstevel@tonic-gate uint_t softswap, hardswap, swapqswap;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * Macro to determine if a process is eligble to be swapped.
1350Sstevel@tonic-gate */
1360Sstevel@tonic-gate #define not_swappable(p) \
1370Sstevel@tonic-gate (((p)->p_flag & SSYS) || (p)->p_stat == SIDL || \
1380Sstevel@tonic-gate (p)->p_stat == SZOMB || (p)->p_as == NULL || \
1390Sstevel@tonic-gate (p)->p_as == &kas)
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * Memory scheduler.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate void
sched()1450Sstevel@tonic-gate sched()
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate kthread_id_t t;
1480Sstevel@tonic-gate pri_t proc_pri;
1490Sstevel@tonic-gate pri_t thread_pri;
1500Sstevel@tonic-gate pri_t swapin_pri;
1510Sstevel@tonic-gate int desperate;
1520Sstevel@tonic-gate pgcnt_t needs;
1530Sstevel@tonic-gate int divisor;
1540Sstevel@tonic-gate proc_t *prp;
1550Sstevel@tonic-gate proc_t *swapout_prp;
1560Sstevel@tonic-gate proc_t *swapin_prp;
1570Sstevel@tonic-gate spgcnt_t avail;
1580Sstevel@tonic-gate int chosen_pri;
1590Sstevel@tonic-gate time_t swapout_time;
1600Sstevel@tonic-gate time_t swapin_proc_time;
1610Sstevel@tonic-gate callb_cpr_t cprinfo;
1620Sstevel@tonic-gate kmutex_t swap_cpr_lock;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate mutex_init(&swap_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
1650Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &swap_cpr_lock, callb_generic_cpr, "sched");
1660Sstevel@tonic-gate if (maxslp == 0)
1670Sstevel@tonic-gate maxslp = MAXSLP;
1680Sstevel@tonic-gate loop:
1690Sstevel@tonic-gate needs = 0;
1700Sstevel@tonic-gate desperate = 0;
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate swapin_pri = v.v_nglobpris;
1730Sstevel@tonic-gate swapin_prp = NULL;
1740Sstevel@tonic-gate chosen_pri = -1;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate process_swap_queue();
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate * Set desperate if
1800Sstevel@tonic-gate * 1. At least 2 runnable processes (on average).
1810Sstevel@tonic-gate * 2. Short (5 sec) and longer (30 sec) average is less
1820Sstevel@tonic-gate * than minfree and desfree respectively.
1830Sstevel@tonic-gate * 3. Pagein + pageout rate is excessive.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate if (avenrun[0] >= 2 * FSCALE &&
1860Sstevel@tonic-gate (MAX(avefree, avefree30) < desfree) &&
1870Sstevel@tonic-gate (pginrate + pgoutrate > maxpgio || avefree < minfree)) {
1880Sstevel@tonic-gate TRACE_4(TR_FAC_SCHED, TR_DESPERATE,
1890Sstevel@tonic-gate "desp:avefree: %d, avefree30: %d, freemem: %d"
1900Sstevel@tonic-gate " pginrate: %d\n", avefree, avefree30, freemem, pginrate);
1910Sstevel@tonic-gate desperate = 1;
1920Sstevel@tonic-gate goto unload;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate * Search list of processes to swapin and swapout deadwood.
1970Sstevel@tonic-gate */
1980Sstevel@tonic-gate swapin_proc_time = 0;
1990Sstevel@tonic-gate top:
2000Sstevel@tonic-gate mutex_enter(&pidlock);
2010Sstevel@tonic-gate for (prp = practive; prp != NULL; prp = prp->p_next) {
2020Sstevel@tonic-gate if (not_swappable(prp))
2030Sstevel@tonic-gate continue;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * Look at processes with at least one swapped lwp.
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate if (prp->p_swapcnt) {
2090Sstevel@tonic-gate time_t proc_time;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * Higher priority processes are good candidates
2130Sstevel@tonic-gate * to swapin.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate mutex_enter(&prp->p_lock);
2160Sstevel@tonic-gate proc_pri = -1;
2170Sstevel@tonic-gate t = prp->p_tlist;
2180Sstevel@tonic-gate proc_time = 0;
2190Sstevel@tonic-gate do {
2200Sstevel@tonic-gate if (t->t_schedflag & TS_LOAD)
2210Sstevel@tonic-gate continue;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate thread_lock(t);
2240Sstevel@tonic-gate thread_pri = CL_SWAPIN(t, 0);
2250Sstevel@tonic-gate thread_unlock(t);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (t->t_stime - proc_time > 0)
2280Sstevel@tonic-gate proc_time = t->t_stime;
2290Sstevel@tonic-gate if (thread_pri > proc_pri)
2300Sstevel@tonic-gate proc_pri = thread_pri;
2310Sstevel@tonic-gate } while ((t = t->t_forw) != prp->p_tlist);
2320Sstevel@tonic-gate mutex_exit(&prp->p_lock);
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate if (proc_pri == -1)
2350Sstevel@tonic-gate continue;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate TRACE_3(TR_FAC_SCHED, TR_CHOOSE_SWAPIN,
2380Sstevel@tonic-gate "prp %p epri %d proc_time %d",
2390Sstevel@tonic-gate prp, proc_pri, proc_time);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate * Swapin processes with a high effective priority.
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate if (swapin_prp == NULL || proc_pri > chosen_pri) {
2450Sstevel@tonic-gate swapin_prp = prp;
2460Sstevel@tonic-gate chosen_pri = proc_pri;
2470Sstevel@tonic-gate swapin_pri = proc_pri;
2480Sstevel@tonic-gate swapin_proc_time = proc_time;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate } else {
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate * No need to soft swap if we have sufficient
2530Sstevel@tonic-gate * memory.
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate if (avefree > desfree ||
2560Sstevel@tonic-gate avefree < desfree && freemem > desfree)
2570Sstevel@tonic-gate continue;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
260390Sraf * Skip processes that are exiting
261390Sraf * or whose address spaces are locked.
2620Sstevel@tonic-gate */
2630Sstevel@tonic-gate mutex_enter(&prp->p_lock);
264390Sraf if ((prp->p_flag & SEXITING) ||
2650Sstevel@tonic-gate (prp->p_as != NULL && AS_ISPGLCK(prp->p_as))) {
2660Sstevel@tonic-gate mutex_exit(&prp->p_lock);
2670Sstevel@tonic-gate continue;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate * Softswapping to kick out deadwood.
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate proc_pri = -1;
2740Sstevel@tonic-gate t = prp->p_tlist;
2750Sstevel@tonic-gate do {
2760Sstevel@tonic-gate if ((t->t_schedflag & (TS_SWAPENQ |
2770Sstevel@tonic-gate TS_ON_SWAPQ | TS_LOAD)) != TS_LOAD)
2780Sstevel@tonic-gate continue;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate thread_lock(t);
2810Sstevel@tonic-gate thread_pri = CL_SWAPOUT(t, SOFTSWAP);
2820Sstevel@tonic-gate thread_unlock(t);
2830Sstevel@tonic-gate if (thread_pri > proc_pri)
2840Sstevel@tonic-gate proc_pri = thread_pri;
2850Sstevel@tonic-gate } while ((t = t->t_forw) != prp->p_tlist);
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate if (proc_pri != -1) {
2880Sstevel@tonic-gate uint_t swrss;
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate mutex_exit(&pidlock);
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate TRACE_1(TR_FAC_SCHED, TR_SOFTSWAP,
2930Sstevel@tonic-gate "softswap:prp %p", prp);
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate (void) swapout(prp, &swrss, SOFTSWAP);
2960Sstevel@tonic-gate softswap++;
2970Sstevel@tonic-gate prp->p_swrss += swrss;
2980Sstevel@tonic-gate mutex_exit(&prp->p_lock);
2990Sstevel@tonic-gate goto top;
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate mutex_exit(&prp->p_lock);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate if (swapin_prp != NULL)
3050Sstevel@tonic-gate mutex_enter(&swapin_prp->p_lock);
3060Sstevel@tonic-gate mutex_exit(&pidlock);
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate if (swapin_prp == NULL) {
3090Sstevel@tonic-gate TRACE_3(TR_FAC_SCHED, TR_RUNOUT,
3100Sstevel@tonic-gate "schedrunout:runout nswapped: %d, avefree: %ld freemem: %ld",
3110Sstevel@tonic-gate nswapped, avefree, freemem);
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate t = curthread;
3140Sstevel@tonic-gate thread_lock(t);
3150Sstevel@tonic-gate runout++;
3160Sstevel@tonic-gate t->t_schedflag |= (TS_ALLSTART & ~TS_CSTART);
3170Sstevel@tonic-gate t->t_whystop = PR_SUSPENDED;
3180Sstevel@tonic-gate t->t_whatstop = SUSPEND_NORMAL;
3190Sstevel@tonic-gate (void) new_mstate(t, LMS_SLEEP);
3200Sstevel@tonic-gate mutex_enter(&swap_cpr_lock);
3210Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
3220Sstevel@tonic-gate mutex_exit(&swap_cpr_lock);
3230Sstevel@tonic-gate thread_stop(t); /* change state and drop lock */
3240Sstevel@tonic-gate swtch();
3250Sstevel@tonic-gate mutex_enter(&swap_cpr_lock);
3260Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &swap_cpr_lock);
3270Sstevel@tonic-gate mutex_exit(&swap_cpr_lock);
3280Sstevel@tonic-gate goto loop;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate * Decide how deserving this process is to be brought in.
3330Sstevel@tonic-gate * Needs is an estimate of how much core the process will
3340Sstevel@tonic-gate * need. If the process has been out for a while, then we
3350Sstevel@tonic-gate * will bring it in with 1/2 the core needed, otherwise
3360Sstevel@tonic-gate * we are conservative.
3370Sstevel@tonic-gate */
3380Sstevel@tonic-gate divisor = 1;
339*11066Srafael.vanoni@sun.com swapout_time = (ddi_get_lbolt() - swapin_proc_time) / hz;
3400Sstevel@tonic-gate if (swapout_time > maxslp / 2)
3410Sstevel@tonic-gate divisor = 2;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate needs = MIN(swapin_prp->p_swrss, lotsfree);
3440Sstevel@tonic-gate needs = MAX(needs, min_procsize);
3450Sstevel@tonic-gate needs = needs / divisor;
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate * Use freemem, since we want processes to be swapped
3490Sstevel@tonic-gate * in quickly.
3500Sstevel@tonic-gate */
3510Sstevel@tonic-gate avail = freemem - deficit;
3520Sstevel@tonic-gate if (avail > (spgcnt_t)needs) {
3530Sstevel@tonic-gate deficit += needs;
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate TRACE_2(TR_FAC_SCHED, TR_SWAPIN_VALUES,
3560Sstevel@tonic-gate "swapin_values: prp %p needs %lu", swapin_prp, needs);
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate if (swapin(swapin_prp)) {
3590Sstevel@tonic-gate mutex_exit(&swapin_prp->p_lock);
3600Sstevel@tonic-gate goto loop;
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate deficit -= MIN(needs, deficit);
3630Sstevel@tonic-gate mutex_exit(&swapin_prp->p_lock);
3640Sstevel@tonic-gate } else {
3650Sstevel@tonic-gate mutex_exit(&swapin_prp->p_lock);
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate * If deficit is high, too many processes have been
3680Sstevel@tonic-gate * swapped in so wait a sec before attempting to
3690Sstevel@tonic-gate * swapin more.
3700Sstevel@tonic-gate */
3710Sstevel@tonic-gate if (freemem > needs) {
3720Sstevel@tonic-gate TRACE_2(TR_FAC_SCHED, TR_HIGH_DEFICIT,
3730Sstevel@tonic-gate "deficit: prp %p needs %lu", swapin_prp, needs);
3740Sstevel@tonic-gate goto block;
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate TRACE_2(TR_FAC_SCHED, TR_UNLOAD,
3790Sstevel@tonic-gate "unload: prp %p needs %lu", swapin_prp, needs);
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate unload:
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate * Unload all unloadable modules, free all other memory
3840Sstevel@tonic-gate * resources we can find, then look for a thread to hardswap.
3850Sstevel@tonic-gate */
3860Sstevel@tonic-gate modreap();
3870Sstevel@tonic-gate segkp_cache_free();
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate swapout_prp = NULL;
3900Sstevel@tonic-gate mutex_enter(&pidlock);
3910Sstevel@tonic-gate for (prp = practive; prp != NULL; prp = prp->p_next) {
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate * No need to soft swap if we have sufficient
3950Sstevel@tonic-gate * memory.
3960Sstevel@tonic-gate */
3970Sstevel@tonic-gate if (not_swappable(prp))
3980Sstevel@tonic-gate continue;
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate if (avefree > minfree ||
4010Sstevel@tonic-gate avefree < minfree && freemem > desfree) {
4020Sstevel@tonic-gate swapout_prp = NULL;
4030Sstevel@tonic-gate break;
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /*
407390Sraf * Skip processes that are exiting
408390Sraf * or whose address spaces are locked.
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate mutex_enter(&prp->p_lock);
411390Sraf if ((prp->p_flag & SEXITING) ||
4120Sstevel@tonic-gate (prp->p_as != NULL && AS_ISPGLCK(prp->p_as))) {
4130Sstevel@tonic-gate mutex_exit(&prp->p_lock);
4140Sstevel@tonic-gate continue;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate proc_pri = -1;
4180Sstevel@tonic-gate t = prp->p_tlist;
4190Sstevel@tonic-gate do {
4200Sstevel@tonic-gate if ((t->t_schedflag & (TS_SWAPENQ |
4210Sstevel@tonic-gate TS_ON_SWAPQ | TS_LOAD)) != TS_LOAD)
4220Sstevel@tonic-gate continue;
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate thread_lock(t);
4250Sstevel@tonic-gate thread_pri = CL_SWAPOUT(t, HARDSWAP);
4260Sstevel@tonic-gate thread_unlock(t);
4270Sstevel@tonic-gate if (thread_pri > proc_pri)
4280Sstevel@tonic-gate proc_pri = thread_pri;
4290Sstevel@tonic-gate } while ((t = t->t_forw) != prp->p_tlist);
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate mutex_exit(&prp->p_lock);
4320Sstevel@tonic-gate if (proc_pri == -1)
4330Sstevel@tonic-gate continue;
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * Swapout processes sleeping with a lower priority
4370Sstevel@tonic-gate * than the one currently being swapped in, if any.
4380Sstevel@tonic-gate */
4390Sstevel@tonic-gate if (swapin_prp == NULL || swapin_pri > proc_pri) {
4400Sstevel@tonic-gate TRACE_2(TR_FAC_SCHED, TR_CHOOSE_SWAPOUT,
4410Sstevel@tonic-gate "hardswap: prp %p needs %lu", prp, needs);
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (swapout_prp == NULL || proc_pri < chosen_pri) {
4440Sstevel@tonic-gate swapout_prp = prp;
4450Sstevel@tonic-gate chosen_pri = proc_pri;
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate * Acquire the "p_lock" before dropping "pidlock"
4520Sstevel@tonic-gate * to prevent the proc structure from being freed
4530Sstevel@tonic-gate * if the process exits before swapout completes.
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate if (swapout_prp != NULL)
4560Sstevel@tonic-gate mutex_enter(&swapout_prp->p_lock);
4570Sstevel@tonic-gate mutex_exit(&pidlock);
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate if ((prp = swapout_prp) != NULL) {
4600Sstevel@tonic-gate uint_t swrss = 0;
4610Sstevel@tonic-gate int swapped;
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate swapped = swapout(prp, &swrss, HARDSWAP);
4640Sstevel@tonic-gate if (swapped) {
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate * If desperate, we want to give the space obtained
4670Sstevel@tonic-gate * by swapping this process out to processes in core,
4680Sstevel@tonic-gate * so we give them a chance by increasing deficit.
4690Sstevel@tonic-gate */
4700Sstevel@tonic-gate prp->p_swrss += swrss;
4710Sstevel@tonic-gate if (desperate)
4720Sstevel@tonic-gate deficit += MIN(prp->p_swrss, lotsfree);
4730Sstevel@tonic-gate hardswap++;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate mutex_exit(&swapout_prp->p_lock);
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate if (swapped)
4780Sstevel@tonic-gate goto loop;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate * Delay for 1 second and look again later.
4830Sstevel@tonic-gate */
4840Sstevel@tonic-gate TRACE_3(TR_FAC_SCHED, TR_RUNIN,
4850Sstevel@tonic-gate "schedrunin:runin nswapped: %d, avefree: %ld freemem: %ld",
4860Sstevel@tonic-gate nswapped, avefree, freemem);
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate block:
4890Sstevel@tonic-gate t = curthread;
4900Sstevel@tonic-gate thread_lock(t);
4910Sstevel@tonic-gate runin++;
4920Sstevel@tonic-gate t->t_schedflag |= (TS_ALLSTART & ~TS_CSTART);
4930Sstevel@tonic-gate t->t_whystop = PR_SUSPENDED;
4940Sstevel@tonic-gate t->t_whatstop = SUSPEND_NORMAL;
4950Sstevel@tonic-gate (void) new_mstate(t, LMS_SLEEP);
4960Sstevel@tonic-gate mutex_enter(&swap_cpr_lock);
4970Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
4980Sstevel@tonic-gate mutex_exit(&swap_cpr_lock);
4990Sstevel@tonic-gate thread_stop(t); /* change to stop state and drop lock */
5000Sstevel@tonic-gate swtch();
5010Sstevel@tonic-gate mutex_enter(&swap_cpr_lock);
5020Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &swap_cpr_lock);
5030Sstevel@tonic-gate mutex_exit(&swap_cpr_lock);
5040Sstevel@tonic-gate goto loop;
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate * Remove the specified thread from the swap queue.
5090Sstevel@tonic-gate */
5100Sstevel@tonic-gate static void
swapdeq(kthread_id_t tp)5110Sstevel@tonic-gate swapdeq(kthread_id_t tp)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate kthread_id_t *tpp;
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(tp));
5160Sstevel@tonic-gate ASSERT(tp->t_schedflag & TS_ON_SWAPQ);
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate tpp = &tswap_queue;
5190Sstevel@tonic-gate for (;;) {
5200Sstevel@tonic-gate ASSERT(*tpp != NULL);
5210Sstevel@tonic-gate if (*tpp == tp)
5220Sstevel@tonic-gate break;
5230Sstevel@tonic-gate tpp = &(*tpp)->t_link;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate *tpp = tp->t_link;
5260Sstevel@tonic-gate tp->t_schedflag &= ~TS_ON_SWAPQ;
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate /*
5300Sstevel@tonic-gate * Swap in lwps. Returns nonzero on success (i.e., if at least one lwp is
5310Sstevel@tonic-gate * swapped in) and 0 on failure.
5320Sstevel@tonic-gate */
5330Sstevel@tonic-gate static int
swapin(proc_t * pp)5340Sstevel@tonic-gate swapin(proc_t *pp)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate kthread_id_t tp;
5370Sstevel@tonic-gate int err;
5380Sstevel@tonic-gate int num_swapped_in = 0;
5390Sstevel@tonic-gate struct cpu *cpup = CPU;
5400Sstevel@tonic-gate pri_t thread_pri;
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pp->p_lock));
5430Sstevel@tonic-gate ASSERT(pp->p_swapcnt);
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate top:
5460Sstevel@tonic-gate tp = pp->p_tlist;
5470Sstevel@tonic-gate do {
5480Sstevel@tonic-gate /*
5490Sstevel@tonic-gate * Only swapin eligible lwps (specified by the scheduling
5500Sstevel@tonic-gate * class) which are unloaded and ready to run.
5510Sstevel@tonic-gate */
5520Sstevel@tonic-gate thread_lock(tp);
5530Sstevel@tonic-gate thread_pri = CL_SWAPIN(tp, 0);
5540Sstevel@tonic-gate if (thread_pri != -1 && tp->t_state == TS_RUN &&
5550Sstevel@tonic-gate (tp->t_schedflag & TS_LOAD) == 0) {
5560Sstevel@tonic-gate size_t stack_size;
5570Sstevel@tonic-gate pgcnt_t stack_pages;
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate ASSERT((tp->t_schedflag & TS_ON_SWAPQ) == 0);
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate thread_unlock(tp);
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate * Now drop the p_lock since the stack needs
5640Sstevel@tonic-gate * to brought in.
5650Sstevel@tonic-gate */
5660Sstevel@tonic-gate mutex_exit(&pp->p_lock);
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate stack_size = swapsize(tp->t_swap);
5690Sstevel@tonic-gate stack_pages = btopr(stack_size);
5700Sstevel@tonic-gate /* Kernel probe */
5710Sstevel@tonic-gate TNF_PROBE_4(swapin_lwp, "vm swap swapin", /* CSTYLED */,
572*11066Srafael.vanoni@sun.com tnf_pid, pid, pp->p_pid,
573*11066Srafael.vanoni@sun.com tnf_lwpid, lwpid, tp->t_tid,
574*11066Srafael.vanoni@sun.com tnf_kthread_id, tid, tp,
575*11066Srafael.vanoni@sun.com tnf_ulong, page_count, stack_pages);
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate rw_enter(&kas.a_lock, RW_READER);
5780Sstevel@tonic-gate err = segkp_fault(segkp->s_as->a_hat, segkp,
5790Sstevel@tonic-gate tp->t_swap, stack_size, F_SOFTLOCK, S_OTHER);
5800Sstevel@tonic-gate rw_exit(&kas.a_lock);
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate * Re-acquire the p_lock.
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate mutex_enter(&pp->p_lock);
5860Sstevel@tonic-gate if (err) {
5870Sstevel@tonic-gate num_swapped_in = 0;
5880Sstevel@tonic-gate break;
5890Sstevel@tonic-gate } else {
5901483Svb70745 #ifdef __sparc
5911483Svb70745 lwp_swapin(tp);
5921483Svb70745 #endif /* __sparc */
5930Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, swapin, 1);
5940Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgswapin,
5950Sstevel@tonic-gate stack_pages);
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate pp->p_swapcnt--;
5980Sstevel@tonic-gate pp->p_swrss -= stack_pages;
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate thread_lock(tp);
6010Sstevel@tonic-gate tp->t_schedflag |= TS_LOAD;
6020Sstevel@tonic-gate dq_sruninc(tp);
6030Sstevel@tonic-gate
604*11066Srafael.vanoni@sun.com /* set swapin time */
605*11066Srafael.vanoni@sun.com tp->t_stime = ddi_get_lbolt();
6060Sstevel@tonic-gate thread_unlock(tp);
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate nswapped--;
6090Sstevel@tonic-gate tot_swapped_in++;
6100Sstevel@tonic-gate num_swapped_in++;
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate TRACE_2(TR_FAC_SCHED, TR_SWAPIN,
6130Sstevel@tonic-gate "swapin: pp %p stack_pages %lu",
6140Sstevel@tonic-gate pp, stack_pages);
6150Sstevel@tonic-gate goto top;
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate thread_unlock(tp);
6190Sstevel@tonic-gate } while ((tp = tp->t_forw) != pp->p_tlist);
6200Sstevel@tonic-gate return (num_swapped_in);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate * Swap out lwps. Returns nonzero on success (i.e., if at least one lwp is
6250Sstevel@tonic-gate * swapped out) and 0 on failure.
6260Sstevel@tonic-gate */
6270Sstevel@tonic-gate static int
swapout(proc_t * pp,uint_t * swrss,int swapflags)6280Sstevel@tonic-gate swapout(proc_t *pp, uint_t *swrss, int swapflags)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate kthread_id_t tp;
6310Sstevel@tonic-gate pgcnt_t ws_pages = 0;
6320Sstevel@tonic-gate int err;
6330Sstevel@tonic-gate int swapped_lwps = 0;
6340Sstevel@tonic-gate struct as *as = pp->p_as;
6350Sstevel@tonic-gate struct cpu *cpup = CPU;
6360Sstevel@tonic-gate pri_t thread_pri;
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pp->p_lock));
6390Sstevel@tonic-gate
640390Sraf if (pp->p_flag & SEXITING)
6410Sstevel@tonic-gate return (0);
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate top:
6440Sstevel@tonic-gate tp = pp->p_tlist;
6450Sstevel@tonic-gate do {
6460Sstevel@tonic-gate klwp_t *lwp = ttolwp(tp);
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate /*
6490Sstevel@tonic-gate * Swapout eligible lwps (specified by the scheduling
6500Sstevel@tonic-gate * class) which don't have TS_DONT_SWAP set. Set the
6510Sstevel@tonic-gate * "intent to swap" flag (TS_SWAPENQ) on threads
6520Sstevel@tonic-gate * which have TS_DONT_SWAP set so that they can be
6530Sstevel@tonic-gate * swapped if and when they reach a safe point.
6540Sstevel@tonic-gate */
6550Sstevel@tonic-gate thread_lock(tp);
6560Sstevel@tonic-gate thread_pri = CL_SWAPOUT(tp, swapflags);
6570Sstevel@tonic-gate if (thread_pri != -1) {
6580Sstevel@tonic-gate if (tp->t_schedflag & TS_DONT_SWAP) {
6590Sstevel@tonic-gate tp->t_schedflag |= TS_SWAPENQ;
6600Sstevel@tonic-gate tp->t_trapret = 1;
6610Sstevel@tonic-gate aston(tp);
6620Sstevel@tonic-gate } else {
6630Sstevel@tonic-gate pgcnt_t stack_pages;
6640Sstevel@tonic-gate size_t stack_size;
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate ASSERT((tp->t_schedflag &
6670Sstevel@tonic-gate (TS_DONT_SWAP | TS_LOAD)) == TS_LOAD);
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate if (lock_try(&tp->t_lock)) {
6700Sstevel@tonic-gate /*
6710Sstevel@tonic-gate * Remove thread from the swap_queue.
6720Sstevel@tonic-gate */
6730Sstevel@tonic-gate if (tp->t_schedflag & TS_ON_SWAPQ) {
6740Sstevel@tonic-gate ASSERT(!(tp->t_schedflag &
6750Sstevel@tonic-gate TS_SWAPENQ));
6760Sstevel@tonic-gate swapdeq(tp);
6770Sstevel@tonic-gate } else if (tp->t_state == TS_RUN)
6780Sstevel@tonic-gate dq_srundec(tp);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate tp->t_schedflag &=
6810Sstevel@tonic-gate ~(TS_LOAD | TS_SWAPENQ);
6820Sstevel@tonic-gate lock_clear(&tp->t_lock);
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate * Set swapout time if the thread isn't
6860Sstevel@tonic-gate * sleeping.
6870Sstevel@tonic-gate */
6880Sstevel@tonic-gate if (tp->t_state != TS_SLEEP)
689*11066Srafael.vanoni@sun.com tp->t_stime = ddi_get_lbolt();
6900Sstevel@tonic-gate thread_unlock(tp);
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate nswapped++;
6930Sstevel@tonic-gate tot_swapped_out++;
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate lwp->lwp_ru.nswap++;
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate /*
6980Sstevel@tonic-gate * Now drop the p_lock since the
6990Sstevel@tonic-gate * stack needs to pushed out.
7000Sstevel@tonic-gate */
7010Sstevel@tonic-gate mutex_exit(&pp->p_lock);
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate stack_size = swapsize(tp->t_swap);
7040Sstevel@tonic-gate stack_pages = btopr(stack_size);
7050Sstevel@tonic-gate ws_pages += stack_pages;
7060Sstevel@tonic-gate /* Kernel probe */
7070Sstevel@tonic-gate TNF_PROBE_4(swapout_lwp,
708*11066Srafael.vanoni@sun.com "vm swap swapout",
709*11066Srafael.vanoni@sun.com /* CSTYLED */,
710*11066Srafael.vanoni@sun.com tnf_pid, pid, pp->p_pid,
711*11066Srafael.vanoni@sun.com tnf_lwpid, lwpid, tp->t_tid,
712*11066Srafael.vanoni@sun.com tnf_kthread_id, tid, tp,
713*11066Srafael.vanoni@sun.com tnf_ulong, page_count,
714*11066Srafael.vanoni@sun.com stack_pages);
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate rw_enter(&kas.a_lock, RW_READER);
7170Sstevel@tonic-gate err = segkp_fault(segkp->s_as->a_hat,
7180Sstevel@tonic-gate segkp, tp->t_swap, stack_size,
7190Sstevel@tonic-gate F_SOFTUNLOCK, S_WRITE);
7200Sstevel@tonic-gate rw_exit(&kas.a_lock);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate if (err) {
7230Sstevel@tonic-gate cmn_err(CE_PANIC,
7240Sstevel@tonic-gate "swapout: segkp_fault "
7250Sstevel@tonic-gate "failed err: %d", err);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate CPU_STATS_ADDQ(cpup,
7280Sstevel@tonic-gate vm, pgswapout, stack_pages);
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate mutex_enter(&pp->p_lock);
7310Sstevel@tonic-gate pp->p_swapcnt++;
7320Sstevel@tonic-gate swapped_lwps++;
7330Sstevel@tonic-gate goto top;
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate thread_unlock(tp);
7380Sstevel@tonic-gate } while ((tp = tp->t_forw) != pp->p_tlist);
7390Sstevel@tonic-gate
7400Sstevel@tonic-gate /*
7410Sstevel@tonic-gate * Unload address space when all lwps are swapped out.
7420Sstevel@tonic-gate */
7430Sstevel@tonic-gate if (pp->p_swapcnt == pp->p_lwpcnt) {
7440Sstevel@tonic-gate size_t as_size = 0;
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate /*
7470Sstevel@tonic-gate * Avoid invoking as_swapout() if the process has
7480Sstevel@tonic-gate * no MMU resources since pageout will eventually
7490Sstevel@tonic-gate * steal pages belonging to this address space. This
7500Sstevel@tonic-gate * saves CPU cycles as the number of pages that are
7510Sstevel@tonic-gate * potentially freed or pushed out by the segment
7520Sstevel@tonic-gate * swapout operation is very small.
7530Sstevel@tonic-gate */
7540Sstevel@tonic-gate if (rm_asrss(pp->p_as) != 0)
7550Sstevel@tonic-gate as_size = as_swapout(as);
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgswapout, btop(as_size));
7580Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, swapout, 1);
7590Sstevel@tonic-gate ws_pages += btop(as_size);
7600Sstevel@tonic-gate
7610Sstevel@tonic-gate TRACE_2(TR_FAC_SCHED, TR_SWAPOUT,
7620Sstevel@tonic-gate "swapout: pp %p pages_pushed %lu", pp, ws_pages);
7630Sstevel@tonic-gate /* Kernel probe */
7640Sstevel@tonic-gate TNF_PROBE_2(swapout_process, "vm swap swapout", /* CSTYLED */,
765*11066Srafael.vanoni@sun.com tnf_pid, pid, pp->p_pid,
766*11066Srafael.vanoni@sun.com tnf_ulong, page_count, ws_pages);
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate *swrss = ws_pages;
7690Sstevel@tonic-gate return (swapped_lwps);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate void
swapout_lwp(klwp_t * lwp)7730Sstevel@tonic-gate swapout_lwp(klwp_t *lwp)
7740Sstevel@tonic-gate {
7750Sstevel@tonic-gate kthread_id_t tp = curthread;
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate ASSERT(curthread == lwptot(lwp));
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate * Don't insert the thread onto the swap queue if
7810Sstevel@tonic-gate * sufficient memory is available.
7820Sstevel@tonic-gate */
7830Sstevel@tonic-gate if (avefree > desfree || avefree < desfree && freemem > desfree) {
7840Sstevel@tonic-gate thread_lock(tp);
7850Sstevel@tonic-gate tp->t_schedflag &= ~TS_SWAPENQ;
7860Sstevel@tonic-gate thread_unlock(tp);
7870Sstevel@tonic-gate return;
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate * Lock the thread, then move it to the swapped queue from the
7920Sstevel@tonic-gate * onproc queue and set its state to be TS_RUN.
7930Sstevel@tonic-gate */
7940Sstevel@tonic-gate thread_lock(tp);
7950Sstevel@tonic-gate ASSERT(tp->t_state == TS_ONPROC);
7960Sstevel@tonic-gate if (tp->t_schedflag & TS_SWAPENQ) {
7970Sstevel@tonic-gate tp->t_schedflag &= ~TS_SWAPENQ;
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate /*
8000Sstevel@tonic-gate * Set the state of this thread to be runnable
8010Sstevel@tonic-gate * and move it from the onproc queue to the swap queue.
8020Sstevel@tonic-gate */
8030Sstevel@tonic-gate disp_swapped_enq(tp);
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate * Insert the thread onto the swap queue.
8070Sstevel@tonic-gate */
8080Sstevel@tonic-gate tp->t_link = tswap_queue;
8090Sstevel@tonic-gate tswap_queue = tp;
8100Sstevel@tonic-gate tp->t_schedflag |= TS_ON_SWAPQ;
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate thread_unlock_nopreempt(tp);
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate TRACE_1(TR_FAC_SCHED, TR_SWAPOUT_LWP, "swapout_lwp:%x", lwp);
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate swtch();
8170Sstevel@tonic-gate } else {
8180Sstevel@tonic-gate thread_unlock(tp);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate /*
8230Sstevel@tonic-gate * Swap all threads on the swap queue.
8240Sstevel@tonic-gate */
8250Sstevel@tonic-gate static void
process_swap_queue(void)8260Sstevel@tonic-gate process_swap_queue(void)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate kthread_id_t tp;
8290Sstevel@tonic-gate uint_t ws_pages;
8300Sstevel@tonic-gate proc_t *pp;
8310Sstevel@tonic-gate struct cpu *cpup = CPU;
8320Sstevel@tonic-gate klwp_t *lwp;
8330Sstevel@tonic-gate int err;
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate if (tswap_queue == NULL)
8360Sstevel@tonic-gate return;
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate /*
8390Sstevel@tonic-gate * Acquire the "swapped_lock" which locks the swap queue,
8400Sstevel@tonic-gate * and unload the stacks of all threads on it.
8410Sstevel@tonic-gate */
8420Sstevel@tonic-gate disp_lock_enter(&swapped_lock);
8430Sstevel@tonic-gate while ((tp = tswap_queue) != NULL) {
8440Sstevel@tonic-gate pgcnt_t stack_pages;
8450Sstevel@tonic-gate size_t stack_size;
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate tswap_queue = tp->t_link;
8480Sstevel@tonic-gate tp->t_link = NULL;
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate * Drop the "dispatcher lock" before acquiring "t_lock"
8520Sstevel@tonic-gate * to avoid spinning on it since the thread at the front
8530Sstevel@tonic-gate * of the swap queue could be pinned before giving up
8540Sstevel@tonic-gate * its "t_lock" in resume.
8550Sstevel@tonic-gate */
8560Sstevel@tonic-gate disp_lock_exit(&swapped_lock);
8570Sstevel@tonic-gate lock_set(&tp->t_lock);
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate /*
8600Sstevel@tonic-gate * Now, re-acquire the "swapped_lock". Acquiring this lock
8610Sstevel@tonic-gate * results in locking the thread since its dispatcher lock
8620Sstevel@tonic-gate * (t_lockp) is the "swapped_lock".
8630Sstevel@tonic-gate */
8640Sstevel@tonic-gate disp_lock_enter(&swapped_lock);
8650Sstevel@tonic-gate ASSERT(tp->t_state == TS_RUN);
8660Sstevel@tonic-gate ASSERT(tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ));
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate tp->t_schedflag &= ~(TS_LOAD | TS_ON_SWAPQ);
869*11066Srafael.vanoni@sun.com tp->t_stime = ddi_get_lbolt(); /* swapout time */
8700Sstevel@tonic-gate disp_lock_exit(&swapped_lock);
8710Sstevel@tonic-gate lock_clear(&tp->t_lock);
8720Sstevel@tonic-gate
8730Sstevel@tonic-gate lwp = ttolwp(tp);
8740Sstevel@tonic-gate lwp->lwp_ru.nswap++;
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate pp = ttoproc(tp);
8770Sstevel@tonic-gate stack_size = swapsize(tp->t_swap);
8780Sstevel@tonic-gate stack_pages = btopr(stack_size);
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate /* Kernel probe */
8810Sstevel@tonic-gate TNF_PROBE_4(swapout_lwp, "vm swap swapout", /* CSTYLED */,
882*11066Srafael.vanoni@sun.com tnf_pid, pid, pp->p_pid,
883*11066Srafael.vanoni@sun.com tnf_lwpid, lwpid, tp->t_tid,
884*11066Srafael.vanoni@sun.com tnf_kthread_id, tid, tp,
885*11066Srafael.vanoni@sun.com tnf_ulong, page_count, stack_pages);
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate rw_enter(&kas.a_lock, RW_READER);
8880Sstevel@tonic-gate err = segkp_fault(segkp->s_as->a_hat, segkp, tp->t_swap,
8890Sstevel@tonic-gate stack_size, F_SOFTUNLOCK, S_WRITE);
8900Sstevel@tonic-gate rw_exit(&kas.a_lock);
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate if (err) {
8930Sstevel@tonic-gate cmn_err(CE_PANIC,
8940Sstevel@tonic-gate "process_swap_list: segkp_fault failed err: %d", err);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgswapout, stack_pages);
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate nswapped++;
8990Sstevel@tonic-gate tot_swapped_out++;
9000Sstevel@tonic-gate swapqswap++;
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate /*
9030Sstevel@tonic-gate * Don't need p_lock since the swapper is the only
9040Sstevel@tonic-gate * thread which increments/decrements p_swapcnt and p_swrss.
9050Sstevel@tonic-gate */
9060Sstevel@tonic-gate ws_pages = stack_pages;
9070Sstevel@tonic-gate pp->p_swapcnt++;
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate TRACE_1(TR_FAC_SCHED, TR_SWAPQ_LWP, "swaplist: pp %p", pp);
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate /*
9120Sstevel@tonic-gate * Unload address space when all lwps are swapped out.
9130Sstevel@tonic-gate */
9140Sstevel@tonic-gate if (pp->p_swapcnt == pp->p_lwpcnt) {
9150Sstevel@tonic-gate size_t as_size = 0;
9160Sstevel@tonic-gate
9170Sstevel@tonic-gate if (rm_asrss(pp->p_as) != 0)
9180Sstevel@tonic-gate as_size = as_swapout(pp->p_as);
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgswapout,
9210Sstevel@tonic-gate btop(as_size));
9220Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, swapout, 1);
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate ws_pages += btop(as_size);
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate TRACE_2(TR_FAC_SCHED, TR_SWAPQ_PROC,
9270Sstevel@tonic-gate "swaplist_proc: pp %p pages_pushed: %lu",
9280Sstevel@tonic-gate pp, ws_pages);
9290Sstevel@tonic-gate /* Kernel probe */
9300Sstevel@tonic-gate TNF_PROBE_2(swapout_process, "vm swap swapout",
931*11066Srafael.vanoni@sun.com /* CSTYLED */,
932*11066Srafael.vanoni@sun.com tnf_pid, pid, pp->p_pid,
933*11066Srafael.vanoni@sun.com tnf_ulong, page_count, ws_pages);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate pp->p_swrss += ws_pages;
9360Sstevel@tonic-gate disp_lock_enter(&swapped_lock);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate disp_lock_exit(&swapped_lock);
9390Sstevel@tonic-gate }
940