DOCUMENT ID:  1538-02

SYNOPSIS:     Brief description of how swap is handled with Solaris          

OS RELEASE:   2.x

PRODUCT:      Solaris x86

KEYWORDS:     swap virtual memory


DESCRIPTION: 

Brief description of how swap is handled with Solaris          


SOLUTION:

The swapper process

Before the advent of demand paged virtual memory, operating systems
swapped out whole processes from memory to disk to make room for other
processes that want to run.  These are called swapping systems. 
Historically, UNIX was a swapping system and the swapping was carried
out by a special process called the swapper or the scheduler.  Under
UNIX System V Release 4, there is still a swapper process, it had the
name sched (short for scheduler) and always runs as process 0. 

There are times when memory load is so high that pageout cannot maintain
a large enough freelist.  When memory balls below the threshold set by
GPGSLO, sched() is invoked.  It in turn calls the process class
dependent function CL_SWAPOUT() to select a process to swap out to
secondary storage.  The function swapout() is then called to free all
the pages associated with the process The SLOAD bit in this process's
proc structure is cleared indicating that the process has been swapped
and is no longer eligible to run. 

Some time later, sched() will run again.  If the amount of free memory
is above GPGSLO, the class dependent function CL_SWAPIN() is called to
select a process to swap back into memory, The SLOAD bit of the chosen
process is set, making it eligible to run again.  As this process runs,
its pages are faulted back in, in the usual way. 

There are two functions that support the operation of the process
swapper.  They are described as follows:

*  sched() - has two distinct modes of operation:  swap in mode and swap
   out  mode.  In swap in mode, it calls  the  process  class  dependent
   CL_SWAPIN()   function   to  select  a  process  to  swap  in.  Under
   conditions of low load, all processes are swapped in, and CL_SWAPIN()
   will not be able to find any more eligible processes.  At this point,
   sched()  sleeps.  When  clock() is  invoked,  it checks the amount of
   free  memory in the system and if free  memory is below  GPGSLO  then
   sched() is awoken.  When sched()  wakes up, it starts  again from the
   beginning.

*  If there are no more  processes  to swap in, but free  memory is less
   than GPGSLO,  sched() changes to swap out mode.  The class  dependent
   CL_SWAPOUT()  function is called to select a process to swap out.  If
   a process is suitable to be swapped out, the  function  swapout()  is
   called  to free all the pages  associated  with it.  A  process  is a
   suitable  candidate for swapping if it is not a system process, it is
   not locked in memory, it is not  already  swapped,  and it has no I/O
   operations pending on it via the /proc file system.

*  After  freeing  the pages,  sched()  sleeps.  It stays  asleep  until
   awoken by clock(), or until awoken by another  process.  Once sched()
   runs again, it starts from the beginning in swap in mode.

*  There is a  possibility  of  thrashing in sched().  The process  just
   swapped in could be the one  selected  to be swapped  out at the next
   run.  To avoid this,  sched()  keeps a record of the last  process it
   swapped in.  When it calls  CL_SWAPOUT()  it passes the most recently
   swapped in process as  parameter.  This gives  CL_SWAPOUT()  the hint
   that this process should not be selected this time for swapping out.

*  swapout()  - is called by  sched()  to remove all the pages  from the
   address  space of the process  via  as_swapout().  This in turn calls
   the segment  specific  swapout  functions  for all the segments  that
   comprise the address  space.  In general,  these are vnode  segments,
   and the function segvn_swapout() is invoked.

*  segvn_swapout()  scans all the pages in the  segment.  Pages that are
   locked,  marked  as being  kept in  memory,  or  marked  as  having a
   copy-on-write  operation in progress are not removed.  Any pages that
   are not dirty are  returned  to the  freelist,  and  dirty  pages are
   written to swap space before being freed.


DATE APPROVED: 10/18/95