Restructure swapout tests after vm map locking was removed.
Consolidate the regions covered by the process lock. Combine similar conditions tests into one, e.g. all process flags can be test with one logical operation. Add check for in-exec state, since p_vmspace is dererenced. Remove labels and goto by explicitly tracking state. Update comments. Reviewed by: alc, markj (previous version) Tested by: pho (previous version) Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D13693
This commit is contained in:
parent
940b8981c3
commit
565ce2a986
@ -734,126 +734,73 @@ swapout_procs(int action)
|
|||||||
struct proc *p;
|
struct proc *p;
|
||||||
struct thread *td;
|
struct thread *td;
|
||||||
int slptime;
|
int slptime;
|
||||||
bool didswap;
|
bool didswap, doswap;
|
||||||
|
|
||||||
|
MPASS((action & (VM_SWAP_NORMAL | VM_SWAP_IDLE)) != 0);
|
||||||
|
|
||||||
didswap = false;
|
didswap = false;
|
||||||
retry:
|
|
||||||
sx_slock(&allproc_lock);
|
sx_slock(&allproc_lock);
|
||||||
FOREACH_PROC_IN_SYSTEM(p) {
|
FOREACH_PROC_IN_SYSTEM(p) {
|
||||||
|
/*
|
||||||
|
* Filter out not yet fully constructed processes. Do
|
||||||
|
* not swap out held processes. Avoid processes which
|
||||||
|
* are system, exiting, execing, traced, already swapped
|
||||||
|
* out or are in the process of being swapped in or out.
|
||||||
|
*/
|
||||||
PROC_LOCK(p);
|
PROC_LOCK(p);
|
||||||
/*
|
if (p->p_state != PRS_NORMAL || p->p_lock != 0 || (p->p_flag &
|
||||||
* Watch out for a process in
|
(P_SYSTEM | P_WEXIT | P_INEXEC | P_STOPPED_SINGLE |
|
||||||
* creation. It may have no
|
P_TRACED | P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) !=
|
||||||
* address space or lock yet.
|
P_INMEM) {
|
||||||
*/
|
|
||||||
if (p->p_state == PRS_NEW) {
|
|
||||||
PROC_UNLOCK(p);
|
PROC_UNLOCK(p);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* An aio daemon switches its
|
* Further consideration of this process for swap out
|
||||||
* address space while running.
|
* requires iterating over its threads. We release
|
||||||
* Perform a quick check whether
|
* allproc_lock here so that process creation and
|
||||||
* a process has P_SYSTEM.
|
* destruction are not blocked while we iterate.
|
||||||
* Filter out exiting processes.
|
*
|
||||||
|
* To later reacquire allproc_lock and resume
|
||||||
|
* iteration over the allproc list, we will first have
|
||||||
|
* to release the lock on the process. We place a
|
||||||
|
* hold on the process so that it remains in the
|
||||||
|
* allproc list while it is unlocked.
|
||||||
*/
|
*/
|
||||||
if ((p->p_flag & (P_SYSTEM | P_WEXIT)) != 0) {
|
|
||||||
PROC_UNLOCK(p);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_PHOLD_LITE(p);
|
_PHOLD_LITE(p);
|
||||||
PROC_UNLOCK(p);
|
|
||||||
sx_sunlock(&allproc_lock);
|
sx_sunlock(&allproc_lock);
|
||||||
|
|
||||||
PROC_LOCK(p);
|
|
||||||
if (p->p_lock != 1 || (p->p_flag & (P_STOPPED_SINGLE |
|
|
||||||
P_TRACED | P_SYSTEM)) != 0)
|
|
||||||
goto nextproc;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Only aiod changes vmspace. However, it will be
|
* Do not swapout a realtime process.
|
||||||
* skipped because of the if statement above checking
|
* Guarantee swap_idle_threshold1 time in memory.
|
||||||
* for P_SYSTEM.
|
* If the system is under memory stress, or if we are
|
||||||
|
* swapping idle processes >= swap_idle_threshold2,
|
||||||
|
* then swap the process out.
|
||||||
*/
|
*/
|
||||||
if ((p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) !=
|
doswap = true;
|
||||||
P_INMEM)
|
FOREACH_THREAD_IN_PROC(p, td) {
|
||||||
goto nextproc;
|
thread_lock(td);
|
||||||
|
slptime = (ticks - td->td_slptick) / hz;
|
||||||
switch (p->p_state) {
|
if (PRI_IS_REALTIME(td->td_pri_class) ||
|
||||||
default:
|
slptime < swap_idle_threshold1 ||
|
||||||
/*
|
!thread_safetoswapout(td) ||
|
||||||
* Don't swap out processes in any sort
|
((action & VM_SWAP_NORMAL) == 0 &&
|
||||||
* of 'special' state.
|
slptime < swap_idle_threshold2))
|
||||||
*/
|
doswap = false;
|
||||||
break;
|
thread_unlock(td);
|
||||||
|
if (!doswap)
|
||||||
case PRS_NORMAL:
|
break;
|
||||||
/*
|
|
||||||
* do not swapout a realtime process
|
|
||||||
* Check all the thread groups..
|
|
||||||
*/
|
|
||||||
FOREACH_THREAD_IN_PROC(p, td) {
|
|
||||||
thread_lock(td);
|
|
||||||
if (PRI_IS_REALTIME(td->td_pri_class)) {
|
|
||||||
thread_unlock(td);
|
|
||||||
goto nextproc;
|
|
||||||
}
|
|
||||||
slptime = (ticks - td->td_slptick) / hz;
|
|
||||||
/*
|
|
||||||
* Guarantee swap_idle_threshold1
|
|
||||||
* time in memory.
|
|
||||||
*/
|
|
||||||
if (slptime < swap_idle_threshold1) {
|
|
||||||
thread_unlock(td);
|
|
||||||
goto nextproc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Do not swapout a process if it is
|
|
||||||
* waiting on a critical event of some
|
|
||||||
* kind or there is a thread whose
|
|
||||||
* pageable memory may be accessed.
|
|
||||||
*
|
|
||||||
* This could be refined to support
|
|
||||||
* swapping out a thread.
|
|
||||||
*/
|
|
||||||
if (!thread_safetoswapout(td)) {
|
|
||||||
thread_unlock(td);
|
|
||||||
goto nextproc;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* If the system is under memory stress,
|
|
||||||
* or if we are swapping
|
|
||||||
* idle processes >= swap_idle_threshold2,
|
|
||||||
* then swap the process out.
|
|
||||||
*/
|
|
||||||
if ((action & VM_SWAP_NORMAL) == 0 &&
|
|
||||||
((action & VM_SWAP_IDLE) == 0 ||
|
|
||||||
slptime < swap_idle_threshold2)) {
|
|
||||||
thread_unlock(td);
|
|
||||||
goto nextproc;
|
|
||||||
}
|
|
||||||
|
|
||||||
thread_unlock(td);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* If the pageout daemon didn't free enough pages,
|
|
||||||
* or if this process is idle and the system is
|
|
||||||
* configured to swap proactively, swap it out.
|
|
||||||
*/
|
|
||||||
_PRELE(p);
|
|
||||||
if (swapout(p) == 0)
|
|
||||||
didswap = true;
|
|
||||||
PROC_UNLOCK(p);
|
|
||||||
goto retry;
|
|
||||||
}
|
}
|
||||||
nextproc:
|
if (doswap && swapout(p) == 0)
|
||||||
|
didswap = true;
|
||||||
|
|
||||||
PROC_UNLOCK(p);
|
PROC_UNLOCK(p);
|
||||||
sx_slock(&allproc_lock);
|
sx_slock(&allproc_lock);
|
||||||
PRELE(p);
|
PRELE(p);
|
||||||
}
|
}
|
||||||
sx_sunlock(&allproc_lock);
|
sx_sunlock(&allproc_lock);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we swapped something out, and another process needed memory,
|
* If we swapped something out, and another process needed memory,
|
||||||
* then wakeup the sched process.
|
* then wakeup the sched process.
|
||||||
|
Loading…
Reference in New Issue
Block a user