* First, if any processes have been sleeping or stopped for at least * "swap_idle_threshold1" seconds, they are swapped out. If, however, * no such processes exist, then the longest-sleeping or stopped * process is swapped out. Finally, and only as a last resort, if * there are no sleeping or stopped processes, the longest-resident * process is swapped out. */
| 778 | * process is swapped out. |
| 779 | */ |
| 780 | static void |
| 781 | swapout_procs(int action) |
| 782 | { |
| 783 | struct proc *p; |
| 784 | struct thread *td; |
| 785 | int slptime; |
| 786 | bool didswap, doswap; |
| 787 | |
| 788 | MPASS((action & (VM_SWAP_NORMAL | VM_SWAP_IDLE)) != 0); |
| 789 | |
| 790 | didswap = false; |
| 791 | sx_slock(&allproc_lock); |
| 792 | FOREACH_PROC_IN_SYSTEM(p) { |
| 793 | /* |
| 794 | * Filter out not yet fully constructed processes. Do |
| 795 | * not swap out held processes. Avoid processes which |
| 796 | * are system, exiting, execing, traced, already swapped |
| 797 | * out or are in the process of being swapped in or out. |
| 798 | */ |
| 799 | PROC_LOCK(p); |
| 800 | if (p->p_state != PRS_NORMAL || p->p_lock != 0 || (p->p_flag & |
| 801 | (P_SYSTEM | P_WEXIT | P_INEXEC | P_STOPPED_SINGLE | |
| 802 | P_TRACED | P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) != |
| 803 | P_INMEM) { |
| 804 | PROC_UNLOCK(p); |
| 805 | continue; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Further consideration of this process for swap out |
| 810 | * requires iterating over its threads. We release |
| 811 | * allproc_lock here so that process creation and |
| 812 | * destruction are not blocked while we iterate. |
| 813 | * |
| 814 | * To later reacquire allproc_lock and resume |
| 815 | * iteration over the allproc list, we will first have |
| 816 | * to release the lock on the process. We place a |
| 817 | * hold on the process so that it remains in the |
| 818 | * allproc list while it is unlocked. |
| 819 | */ |
| 820 | _PHOLD_LITE(p); |
| 821 | sx_sunlock(&allproc_lock); |
| 822 | |
| 823 | /* |
| 824 | * Do not swapout a realtime process. |
| 825 | * Guarantee swap_idle_threshold1 time in memory. |
| 826 | * If the system is under memory stress, or if we are |
| 827 | * swapping idle processes >= swap_idle_threshold2, |
| 828 | * then swap the process out. |
| 829 | */ |
| 830 | doswap = true; |
| 831 | FOREACH_THREAD_IN_PROC(p, td) { |
| 832 | thread_lock(td); |
| 833 | slptime = (ticks - td->td_slptick) / hz; |
| 834 | if (PRI_IS_REALTIME(td->td_pri_class) || |
| 835 | slptime < swap_idle_threshold1 || |
| 836 | !thread_safetoswapout(td) || |
| 837 | ((action & VM_SWAP_NORMAL) == 0 && |