DocId: KBRC00001262

What is swap and why does a system need it?

HP-UX, like other versions of Unix, use Virtual Memory to load processes into
memory. In simple terms, Virtual Memory consists of two parts, physical memory
or ram and swap. The physical memory is where the programs are running, and
swap is the "spill over". Swap devices are usually parts of a physical hard
drive. Swap allows the total number of processes to exceed the amount of
physical ram, and can be allocated as needed.

When a process is spawned, the kernel will check virtual memory to see if the
process can be directly loaded into physical memory. The kernel also checks to
make sure that the process is able to reserve space in the swap area. If either
of these tests fail, the process will not spawn and the process will be
terminated. The resulting error messages are malloc or fork failed are
produced. All processes have to be able to reserve space in the swap area. To
maintain enough free physical memory for processes to run, a daemon called vhand
scans through the process tables in the kernel and looks for pages of data that
have not been used in a while. If vhand finds any "non-active" pages, vhand
will move those pages to the swap area.  If available memory drops too low,
another daemon called swapper will remove or swap out the entire process.
Swapper will continue to push processes from physical memory to the swap area
until available memory is increased. When a system is swapping, the swapper
process is very active. At HP-UX 10.x and up, the entire process is not swapped
out, only parts of the process are swapped. The term for a portion of a process
that is swapped is called paged.

System performance can be effected greatly by not having enough swap configured
on the system. Some of the effects that a system can have are not being able to
access all of the physical memory that is installed on the system. The kernel
will only allow a process to spawn if there is enough free space within the swap
area for the process. Applications also rely on swap, and will produce memory
related errors such as malloc or fork failed when there is not enough swap
configured. If the system does need to swap processes from physical memory to
swap area, additional disk I/O will be performed. The kernel will also use
additional resources to monitor memory and to handle the movement of the
processes to the swap device. This additional overhead will reduce system
performance. If the problem is serious, it is possible for the system to use all
cpu or other resources on process management. Once the system has reached this
state, it is refereed to as Thrashing..

You should at least configure a 1:1 ratio of swap to physical memory. This is a
fundamental ratio that will allow you to access the entire physical ram and to
handle most of the operating system's needs for swap. However, applications
that you install may need additional swap configured. You should contact the
vendor or supplier for recommendations for additional swap. It is not uncommon
to find systems with 4 times the amount of swap as physical memory.

Using a disk section or logical volume for swap is refered to be device swap.
By default, the system is configured with at least one area of device swap when
the operating system is installed (/dev/vg00/lvol2). Device swap is nothing
more than a logical volume, or a disk section, that is given to the system to
use for swap. Commands like bdf will not show swap on a system, however
swapinfo does. Device swap can be configured in any volume group on the system.
Using a logical volume is the most preferred when it comes to performance, and
should be configured first when the system needs additional swap. Device swap
is also divided into two different terms. The first is primary swap. This swap
device tends to be /dev/vg00/lvol2 and is created when the operating system is
installed. Primary swap can only be found on the boot drive. Any additional
device swap is called secondary swap. Secondary swap devices can be configured
in any volume group.

Filesystem swap allows a system administrator to add more swap to the system
when all of the disk space has already been allocated to filesystems. With
filesystem swap, you can setup and configure available space with in a
filesystem. When you allocate filesystem swap, the system will create a
directory call paging, and will create swap files in the paging directory. The
system performs writes to those files if an only if the system starts
swapping to that swap area. System performance will suffer do to the overhead of
maintaining filesystem swap. This is due to the fact that the operating system
is already removing pages from physical memory and then writing them to a file
in small chunks. If the system only needs the filesystem swap for reserving
space, then the system's performance will not suffer. Filesystem swap should
only be used as temporary solution to a swap problem. Once additional drive(s)
have been added to the system, then filesystem swap should be removed as soon as
possible. For performance reasons, it is suggested to set the priority of
filesystem swap areas numerically higher than device swap.

Pseudo swap is the exception to the rule. Pseudo swap was designed to allow a
system administrator to take advantage of systems with large amounts physical
ram without configuring large swap areas. Pseudo swap is not a substitute for
device swap, but an enhancement to swap. When the system boots, the amount of
pseudo swap is calculated. This calculation is 75% of physical memory and this
value is a non-tunable kernel parameter. The kernel will see this enhancement
as additional swap area that it can allocate when spawning new processes. The
system will use pseudo swap only for reserving space and does not page
processes in and out of pseudo swap. In the event that processes need to be
paged out of physical memory, the kernel will swap to device or filesystem
swap. Pseudo swap is turned on by default and can be turned off my changing the
kernel parameter swapmem_on to off.

Here is an example of the benefits of using pseudo swap; Lets assume that we
have a system that has 1 gigabyte of physical ram. In order for the operating
system to use all of the memory, the operating system needs at least 1 gigabyte
of swap. The system administrator configures a 1-gigabyte logical volume for
swap. In addition, the system administrator leaves pseudo swap enabled. When
the system boots up, it will configure 75% of physical memory as pseudo swap.
We would have about 750(1000 * .75) megs of additional swap for the system.
Now, the system will see a total 1.75 gigs of swap, or 2.75 gigs of virtual
memory. Pseudo swap does inflate the total amount of swap.  The kernel will see
that the system has 1.75 gig's of swap and will use the swap the same way as if
the system was configured with 1.75 gigs of device swap, however, there is only
1 gig of device swap configured.

Because pseudo swap inflates the total of swap on the system, some system
administrators may want to reduce the amount of device swap and use that space
for data. In some cases the system administrator can do this. A system
administrator also needs to plan for dump space. This dump space is used
by the system to write a system crash. By default, the primary swap
(/dev/vg00/lvol2) is used for both swap and dump. On a 10.X system, the system
administrator has to configure at least a 1:1 ratio of device swap to physical
memory in root volume group or /dev/vg00. This will handle the minimum swap and
dump needs of the operating system.  At 11.0 and up, it was no longer necessary
to configure a 1:1 ratio of swap/dump space to physical memory. System crashes
have been configured only to save the parts that are needed to determine the
root cause of the system crash. This comes in handy since it is not uncommon to
find systems with 16 gig's of ram. To configure the correct amount of dump space
at 11.X, refer to the man page on crashconf(1m).

By far, the simplest command to use to monitor swap on a 10.X and 11.X system
is swapinfo. With one command the system administrator can see how much swap is
configured, how much swap is being reserved for processes, or even how much
swap is being actively used for swapped processes. Here is an example :

#swapinfo -tam
           Mb       Mb       Mb   PCT   START/   Mb
TYPE      AVAIL    USED    FREE  USED   LIMIT RESERVE  PRI  NAME
dev         128     10      118   8%      0       -    1   /dev/vg00/lvol2
localfs      60      0       60   0%     60       0    4   /var/paging
reserve       -     52      -52
memory       91     68       23  75%
total       279    130      149  47%      -     0      -

This output shows that device swap(dev), filesystem swap(localfs), and pseudo
swap(memory) are configured on this system. The first thing that I would like
to point out is the total line. From left to right, you can get a quick
reference of how swap is doing on this system. Under the Mb Avail column, it
shows that this system has a total of 279 megs of configured swap. Of the 279
megs configured, 130 megs are used for something. By that, I mean that the
total line does not show if swap is used for reservation or paging. With 130
megs being used for "something", that leaves a total of 149 megs of swap that
are not in use. The Pct Used column just shows a handy percentage of swap that
is already used.

The "memory" line shows pseudo swap usage, and tends to be the most confusing of
all of the swapinfo output. As I documented before, pseudo swap is only used
for reserving a process. So from this example, there is a total of 91 megs of
pseudo swap configured, and of that 91 megs, 68 megs of pseudo swap are being
used by processes running in memory. The remainder of pseudo swap that it is
not used is 23 megs. One of the reasons why pseudo swap is so confusing is the
fact pseudo swap usage does not degrade system performance like device swap or
filesystem swap. In another words, system performance would be the same on a
system that had 3% or 99% percent of pseudo swap used.  It is usually
recommended that the "memory" line simpily be ignored when looking at swapinfo.

The "reserve" line only deals with amount of swap we are using for reserving
processes in device and filesystem swap areas. From this example, we have a
total of 188 megs of combined device and filesystem swap and of that, only 52
megs are being used for reserving processes. Now if you take the total of megs
used by memory and reserve, we have 120(52+68) megs of swap allocated to
reserving space for active processes. So, from two lines, we have accounted for
about 92% of swap, which is being used only for reserve for running processes.

The "localfs" line shows some information on much space the system will use
for filesystem swap that was configured on /var. An interesting thing
about that line is the priority of the swap, which is 4. This means that all
of device swap (/dev/vg00/lvol2), which is set to priority 1, will be used
before this filesystem swap area is used.

The "dev" line is one of the most important pieces of information that the
swapinfo command can show. If the percent used line is greater that 0, then the
system is swapping. This is a clear sign that there is not enough physical ram
installed on the system. There are only two methods to make a system stop
swapping, the first is install more physical memory and the other is to reduce
the processes running on the system. The dev line that was used in this example
was altered to show what the output would look like if the system was paging.
It is unlikely that a system would page processes to the swap area if it still
had 149 megs of reserve space left.

If you were interested in learning more about swap, I would suggest reading
through the memory managment white paper. This document is stored on the system
in /usr/shar/doc and is called mem_mgt.txt. There is also information about swap
from the system administration tasks manaul(10.x) and from the configuring
systems for work groups(11.x). Both manuals can be seen at :
http://www.docs.hp.com.