System Administration Guide, Volume I
只搜尋這本書
以 PDF 格式下載這本書

Configuring Additional Swap Space

37

This is a list of the overview conceptual information and step-by-step instructions in this chapter.
Swap Space and Virtual Memorypage 614
Swap Space and the tmpfs File Systempage 615
How Do I Know If I Need More Swap Space?page 616
How Swap Space Is Allocatedpage 617
Planning for Swap Spacepage 618
Monitoring Swap Resourcespage 619
Adding More Swap Spacepage 620
Removing a Swap File From Usepage 623

About Swap Space

This section provides a conceptual overview of swap space and briefly discusses the differences between Solaris 1.x (SunOS 4.x) and Solaris 2.x (SunOS 5.x) swap requirements. If you are already familiar with the Solaris 2.x swap mechanism, proceed to the section called "Planning for Swap Space" on page 618.
It is important for administrators to understand the features of the Solaris 2.x swap mechanism in determining:
  • Swap space requirements
  • The relationship with the tmpfs file system
  • Recovery from error messages related to swap space

Swap Space and Virtual Memory

The Solaris 2.x system software uses some disk slices for temporary storage rather than for file systems. These slices are called swap slices. Swap slices are used as virtual memory storage areas when the system does not have enough physical memory to handle current processes.
The Solaris 2.x virtual memory system maps physical copies of files on disk to virtual addresses in memory. Physical memory pages which hold the data for these mappings can be backed by regular files in the file system or by swap space. If the memory is backed by swap space it is referred to as anonymous memory because the user doesn't know the names of the files backing the memory.
Solaris 1.x anonymous memory pages are mapped using randomly assigned names from the system's swap space pool. These memory pages are used for:
  • Private copies of data created during copy-on-write operations
  • Process and stack segments
  • The tmpfs file system storage resources
The limitations of the Solaris 1.x anonymous memory implementation are:
  • Physical storage (disk-backed swap) must always be reserved for anonymous memory mappings even if the application doesn't use it.

    For example, applications with large data segments must be configured with lots of swap space even if the pages are not written out to physical storage.

  • The formula used to associate an anonymous memory page with physical storage is limited and inflexible because the backing store is chosen at random and can never be changed.
The Solaris 2.x software environment introduces the concept of virtual swap space, a layer between anonymous memory pages and the physical storage (or disk-backed swap space) that actually back these pages. A system's virtual swap space is equal to the sum of all its physical (disk-backed) swap space plus a portion of the currently available physical memory.
Virtual swap space has these advantages:
  • The need for large amounts of physical swap space is reduced because virtual swap space does not necessarily correspond to physical (disk) storage.
  • A pseudo file system called swapfs provides addresses for anonymous memory pages. Because swapfs controls the allocation of memory pages, it has greater flexibility in deciding what happens to a page. For example, it may change the page's requirements for disk-backed swap storage.

Swap Space and the tmpfs File System

The tmpfs file system is activated automatically in the Solaris 2.x environment by an entry in the /etc/vfstab file. The tmpfs file system stores files and their associated information in memory (in the /tmp directory) rather than on disk, which speeds access to those files. This results in a major performance enhancement for applications such as compilers and DBMS products that use /tmp heavily.
The tmpfs file system allocates space in the /tmp directory from the system's swap resources. This means that as you use up space in /tmp, you are also using up swap space. So if your applications use /tmp heavily and you do not monitor swap space usage, your system could run out of swap space.
Use the following if you want to use tmpfs but your swap resources are limited:
  • Mount the tmpfs file system with the size option (-o size) to control how much of the swap resources tmpfs can use.
  • If you are close to running out of swap space, you can use your compiler's TMPDIR environment variable to point to a larger temporary directory.

    Using your compiler's TMPDIR variable only controls whether the compiler is using /tmp or not. It has not effect on other programs' use of /tmp.

How Do I Know If I Need More Swap Space?

This section lists several possible error messages displayed when you run out of swap space.

Swap-Related Error Messages

These messages indicate that an application was trying to get more anonymous memory and there was no swap space left to back it.
application is out of memory
malloc error O
WARNING: Sorry, no swap space to grow stack for pid

tmpfs-Related Error Messages

directory: File system full, swap space limit exceeded

  This message is displayed if a page could not be allocated when writing a
  file. This can occur when tmpfs tries to write more than it is allowed or if
  currently executed programs are using a lot of memory.

directory: File system full, memory allocation failed

This messages means tmpfs ran out of physical memory while attempting to create a new file or directory.
See tmpfs(7FS) for information on recovering from the tmpfs-related error messages.

How Swap Space Is Allocated

Initially, swap space is allocated as part of the Solaris installation process. If you use the installation program's automatic layout of disk slices and do not manually change the size of the swap slice, the Solaris installation program allocates default swap slices as shown in Table 37-1.
Table 37-1
If Your System Has n Mbytes of Physical Memory...Then the Default Swap Space Allocated Is ...
16-6432 Mbytes
64-12864 Mbytes
128-512128 Mbytes
greater than 512256 Mbytes

The /etc/vfstab File

After the system is installed, swap slices and files are listed in the /etc/vfstab file and are activated by the /sbin/swapadd script when the system is booted.
An entry for a swap device in the /etc/vfstab file contains:
  • The full path name of the swap slice or file
  • File system type of swap
Because the file system containing a swap file must be mounted before the swap file is activated, make sure that the entry that mounts the file system comes before the entry that activates the swap file in the /etc/vfstab file.

Planning for Swap Space

The most important factors in determining swap space size are the requirements of the system's software applications. For example, large applications such as computer-aided-design simulators, database-management products, transaction monitors, and geologic analysis systems can consume as much as 200-1000 Mbytes of swap space in very large memory systems.
Consult your application vendor for swap space requirements for any application whose data files typically exceed 10-20 Mbytes in size.
If you are unable to determine swap space requirements from the application vendor, use the following guidelines to allocate swap space:
  • To support your applications, allocate:

    · 1 Mbyte per trivial application such as xterm.

    · 2-3 Mbytes per lightweight application such as a calendar or mail application.

    · 20-50 Mbytes for large applications such as desktop publishing software.

  • To save crash dumps, allocate 100% of physical memory to save a worst-case crash dump.
  • If you are unsure of system or application requirements, allocate 50 to 100% of the system's physical memory. For example, allocate 16-32 Mbytes of swap space for a system with 32 Mbytes of physical memory. This will provide 48-64 Mbytes of total virtual swap space.
  • Determine whether large applications (like compilers) will be using the /tmp directory. Then allocate additional swap space to be used by tmpfs. See "Swap Space and the tmpfs File System" for information about tmpfs.

Monitoring Swap Resources

The /usr/sbin/swap command is used to manage swap areas. Two options, -l and -s, are used to display information about swap resources.
Use the swap -l command to identify a system's swap areas. Activated swap devices or files are listed under the swapfile column.

  # swap -l  
  swapfile           dev  swaplo blocks   free  
  /dev/dsk/c0t2d0s1 32,17      8 205624 192704  

Use the swap -s command to monitor swap resources.

  # swap -s  
  total: 10492k bytes allocated + 7840k reserved = 18332k used, \  
  21568k available  

The used plus available figures equals total swap space on the system, which includes a portion of physical memory and swap devices (or files).
You can use the amount of swap space available and used (in the swap -s output) as a way to monitor swap space usage over time. If a system's performance is good, use swap -s to see how much swap space is available. When the performance of a system slows down, check the amount of swap space available to see if it has decreased. Then you can identify what changes to the system might have caused swap space usage to increase.
Keep in mind when using this command that the amount of physical memory available for swap usage changes dynamically as the kernel and user processes lock down and release physical memory.

Note - The swap -l command displays swap space in 512-byte blocks and the swap -s command displays swap space in 1024-byte blocks. If you add up the blocks from swap -l and convert them to Kbytes, it will be less than used + available (in the swap -s output) because swap -l does not include physical memory in its calculation of swap space.

The output from the swap -s command is summarized in Table 37-2.
Table 37-2 swap -s
KeywordDescription
bytes allocatedThe total amount of swap space in 1024-byte blocks that is currently allocated as backing store (disk-backed swap space).
reservedThe total amount of swap space in 1024-byte blocks not currently allocated, but claimed by memory for possible future use.
usedThe total amount of swap space in 1024-byte blocks that is either allocated or reserved.
availableThe total amount of swap space in 1024-byte blocks that is currently available for future reservation and allocation.

Adding More Swap Space

As system configurations change and new software packages are installed, you might need to add more swap space. The preferred way to add more swap space is to use the mkfile and swap commands to designate a part of an existing UFS or NFS file system as a supplementary swap area. These commands, described below, enable you to add more swap space without repartitioning a disk.
An alternative way to add more swap space is to repartition a disk. See "Managing Disks" for your system for information on how to repartition a disk.

Creating a Swap File

The following steps are used to create a swap file:
  • Create a swap file using the mkfile command.
  • Activate the swap file with the swap command.
  • Add an entry for the swap file in the /etc/vfstab file so that its activated automatically when the system is booted.

The mkfile Command

The mkfile command creates a file that is suitable for use either as an NFS-mounted or local swap area. The sticky bit is set, and the file is padded with zeros. You can specify the size of the swap file in bytes (the default) or in kilobytes, blocks, or megabytes using the k, b, or m suffixes, respectively.
Table 37-3 shows the options to the mkfile command.
Table 37-3 mkfile
OptionDescription
-nCreates an empty file. The size is noted, but the disk blocks are not allocated until data is written to them.
-vVerbose. Reports the names and sizes of created files.

CAUTION Caution - Use the -n option only when creating an NFS swap file.

· How to Create a Swap File and Make It Available

  1. Become root.

    You can create a swap file without root permissions, but it is a good idea to have root be the owner of the swap file so that the swap file cannot be inadvertently overwritten.

  2. Create the swap file.


  # mkfile nnn[k|b|m] filename  

The swap file of the size nnn (in Kbytes, bytes, or Mbytes) and file name you specify is created.
In this example, a 20-Mbyte swap file named swapfile is created on a UFS file system named /files:

  # mkfile 20m /files/swapfile  

  1. Activate the swap file.


  # /usr/sbin/swap -a /path/filename  

You must use the absolute path name to specify the swap file. The swap file is added and available until the file system is unmounted or the system is rebooted.
  1. Add an entry for the swap file to the /etc/vfstab file that specifies the full path name of the file, and designates swap as the file system type. For example, to make the swap file /files/swapfile available, add this entry to the /etc/vfstab file:


  /files/swapfile        -      -       swap     -     no     -  

  1. To verify that the swap file is added, type.


  /usr/sbin/swap -l  

Example--Creating a Swap File and Making It Available


  # swap -a /files/swapfile  
  # swap -l  
  swapfile             dev  swaplo blocks   free  
  /dev/dsk/c0t2d0s1   32,17      8 205624 192704  
  /files/swapfile       -        8  40952  40952  

Removing a Swap File From Use

If the user no longer needs the extra swap space, you can remove it.

· How to Remove Extra Swap Space

  1. Become root.

  2. Use the swap -d command to remove swap space..


  # /usr/sbin/swap -d /path/filename  

The swap file name is removed from the list so that it is no longer available for swapping. The file itself is not deleted.
  1. Edit the /etc/vfstab file and delete the entry for the swap file.

  2. Recover the disk space so that you can use it for something else.


  rm swap-filename  

If the swap space is a file, remove it. Or, if the swap space is on a separate slice and you are sure you will not need it again, make a new file system and mount the file system.
See Chapter 34, "Mounting and Unmounting File Systems," for more information.

Example--Removing Extra Swap Space


  # swap -d /files/swapfile  
  # swap -l  
  swapfile             dev  swaplo  blocks   free  
  /dev/dsk/c0t2d0s1   32,17      8  205624  192720  
  # ls -l /files/swapfile  
  -rw------T   1 root   staff   20971520  Jun 2 12:54 /files/swapfile