Certification Objective 12.01Working with Virtual Memory


Certification Objective 12.01—Working with Virtual Memory

Exam Objective 2.1: Explain virtual memory concepts and given a scenario, configure, and manage swap space.

Solaris software uses some disk slices for temporary storage to support the physical memory rather than for regular file systems for the end users. These disk slices are called swap slices. Swap slices are used as virtual memory storage areas when the system does not have enough physical memory to support processes currently running on the system. The term virtual refers to the memory, because it is storage on disk as compared with physical memory. The virtual memory system maps physical copies of files on disk to virtual addresses in memory. The addresses are virtual because they give the impression to the process that it is a memory address, even though the data may be on the disk. In addition to virtual memory and swap space, you also need to learn how two file systems—TMPFS and SWAPFS—are related to the virtual memory.

Understanding Virtual Memory

Let us put the concept of virtual memory in the right context by exploring the basic concepts related to it:

  • Secondary storage. This is the storage that is remote to the CPU and is usually used to store the programs and data that are currently not being used by the CPU. This space is provided by devices such as hard drives and CD-ROM. Access to the data on the secondary storage is slow, but the data survives even if the computer shuts down. For this reason it is also called permanent storage.

  • Physical memory. The physical memory of a computer is the storage space provided by a physical device called random access memory (RAM) located on the motherboard (and in other circuit boards). This is also called primary storage because this is the storage that running programs often use and prefer, given that it is faster than secondary storage (disk). It holds the programs and the data currently being used by the CPU. When the computer is shut down or rebooted, the data in the physical memory is lost; this is why it is also called volatile memory or temporary storage.

  • Virtual memory. The word virtual refers to something that is not really what it pretends to be. Virtual memory is the method of using the space on the hard drive as though it were the physical memory. It is used by the OS when it runs low on physical memory.

  • Swap space. The disk slices being used for storing virtual memory are called the swap space or the swap slices. The address of the data in the virtual memory referred to by the running program is not the real address in the physical memory; this is why it is called a virtual address. The virtual memory system maps the data file on disk to the virtual addresses in the physical memory.

  • TMPFS file system. The TMPFS is the memory-based file system that maps the /tmp directory on the hard disk to the memory. If there is sufficient RAM, the reads and writes will be faster. In other words, the TMPFS file system stores its files and the related information in the /tmp directory, which speeds up access to those files. As a result, the applications (such as compiler and database management systems products) that use the /tmp directory for storing and retrieving their data improve performance. However, TMPFS allocates space in the /tmp directory from the system's swap space resources. Consequently, if you are using the space in the /tmp directory, you are using the system's swap space. If the system starts getting low on memory, the files will be swapped to the disk, and then the read and writes will obviously be as slow as the disk. An entry in the /etc/vfstab file automatically activates the TMPFS file system. Because TMPFS is a memory-based file system, you will lose all the files in and underneath the /tmp directory when the system is shut down or rebooted.

  • Virtual swap space. Virtual swap space is the opposite of virtual memory. Suppose your computer has enough memory (physical memory) that you do not need the swap space. Now, consider that you have a large number of processes running on your computer, and a swap space has to be allocated to each process to be on the safe side. That means that a large amount of physical swap space will have to be configured even if your computer has a large amount of physical memory. The solution to this problem is the space in the physical memory that acts as swap space—the virtual swap space. It is equivalent to saying, yes there is a swap space and here is the address for it, and the address actually lives in the memory; hence, the name virtual swap space address. This space is controlled by the SWAPFS file system, which provides virtual swap space addresses as opposed to physical swap space addresses in response to requests for reserving swap space, thereby reducing the need to configure a large amount of physical swap space.

  • Paging. The memory space is allocated in units called pages—for example, a page is a chunk of 8KB memory in UltraSPARC architecture. When a program needs the data that has been stored into the swap space, the data cannot be found in the memory; this condition is called page fault. When the page fault occurs, the virtual memory system brings the page containing the requested data into the physical memory; this process is called paging.

Note that even if the virtual swap space is being used for virtual space addresses. When page faults start to occur, that indicates that the system is running low on physical memory. In this case, the SWAPFS converts the virtual swap space addresses to physical swap space addresses. Therefore, virtual swap space can be looked on as the sum of the actual physical swap space plus a portion of the remaining physical memory space.

On the Job 

If the applications on your system make heavy use of the /tmp directory, your system will run out of swap space if you fail to monitor swap space usage. You can set your compiler's TMPDIR environment variable to point to another directory with sufficient space. However, remember that this controls only which directory the compiler is using and has no effect on the use of the /tmp directory by other programs.

Because many applications count on swap space, you, the system administrator, should know how to manage swap space.

Managing Swap Space

Managing swap space includes the initial configuration, monitoring, and adding and removing of the swap space.

The Initial Configuration of the Swap Space

Swap space is initially set up (configured) during the Solaris installation process, and the default value is 512MB (which you can, of course, change). After the system has been installed, swap slices and swap files are activated by the /sbin/swapadd script each time the system is booted. The /etc/vfstab file contains the list of swap devices (slices and files). An entry for a swap device in the /etc/vfstab file contains the following:

  • The full path name of the file or slice

  • The file system type of the file or slice

On the Job 

Before a swap file is activated, the file system that contains the file must be mounted. Therefore, make sure that the entry that mounts the file system appears in the /etc/vfstab file before the entry that activates the swap file.

How much swap space should be set aside during installation? This largely depends on the requirements of the applications that will be running on your system. A complex application in areas such as computer-aided design and database management may require 200 to 1000MB of swap space. You can read the application documentation or consult the application vendor to find out the swap space requirement of a specific application. Overall general guidelines for swap space are presented in Table 12-1.

Table 12-1: General guidelines for setting up the swap space on your system as a function of physical memory space

System Type

Physical Memory Size

Swap Space Size

Workstation

4GB

1GB

Mid-range server

8GB

2GB

High-end servers

16 to 128GB

4GB

You initially configure the swap space based on your best estimate for the system requirements of the swap space. However, you can change it later on by adding or removing the swap space. How would you know you need to add or remove some swap space? You learn that by monitoring the swap space.

Monitoring the Swap Space

If your system does not have sufficient swap space, one or more of the applications running on the system may fail to start as a result of insufficient memory. If that were to happen, it would require manual intervention either to add additional swap space or to reconfigure the memory usage of these applications. Before this happens or to keep it from happening, you should be monitoring the swap space by using the /usr/sbin/swap command. To identify the swap area on your system and obtain some information about it, you can issue the command shown here:

    swap -l 

The output of this command will look like the following:

 swapfile dev swaplo blocks free /dev/dsk/c0t0d0s1 136,1 16 1638608 1600528 

The fields in this output are described here:

  • swapfile. Full path name for the swap area

  • dev. The device number for the swap device

  • swaplo. Offset (in units of 512-byte blocks) where usable swap area begins

  • blocks. Size of the swap area in units of 512-byte blocks

  • free. The total amount of swap space, in units of 512-byte blocks, in this area that is not currently allocated

To get more information about the swap space, issue the command shown here:

    swap -s 

The output will look like the following:

    total: 57416k bytes allocated + 10480k reserved = 67896k used, 833128k available 

The fields in the displayed output are described here:

  • bytes allocated. The total amount of swap space, in units of 1024-byte blocks, that is currently allocated as disk-backed swap space

  • reserved. The total amount of swap space, in units of 1024-byte blocks, that is not currently allocated but is claimed by memory for possible future use

  • used. The total amount of swap space, in units of 1024-byte blocks—that is, allocated plus reserved

  • available. The total amount of swap space, in units of 1024-byte blocks, that is currently available for future use (i.e., for reservation and allocation)

These terms might be confusing if you are not clear about the process of allocating swap space, which is described here:

  1. When a process requests additional memory, the system assigns it the memory pages from the physical memory.

  2. Each page of physical memory is backed by swap space—that is, the swap space is reserved at this time. The reserved space has not really been allocated yet, just reserved for possible use in the future. This is because the use of physical memory is a dynamic thing; there are many other processes running on the system, and when physical memory runs low, some memory pages are swapped out of the physical memory into the swap area.

  3. When the process actually starts writing data to a reserved page, the system assigns a location on the physical swap space to the page. This is referred to as allocating the swap space.

Note that the used space plus the available space is equal to the total swap space on the system, which includes a portion of physical memory (think of virtual swap space) and swap files.

On the Job 

First, note that the swap -l command displays swap space in units of 512-byte blocks, whereas the swap -s command displays swap space in units of 1024-byte blocks—that is, in KB. Second, if you add up the blocks from the output of the swap -l and convert them to KB, the result is less than used + available (in the swap -s output). The reason is that swap -l does not include the physical memory part (virtual swap space) in its calculation of swap space.

Here is an example showing how you can use the amount of available and used swap space displayed by the swap -s command to monitor swap space usage. When system performance is good, use the swap -s command to determine how much swap space is available. Watch the system, and when performance slows down, check the amount of available swap space again and determine whether it has decreased. If it has, you have found a correlation between swap space and system performance, and you can identify the changes to the system that might have increased swap space usage.

Your analysis of swap space monitoring may suggest either that you need to add some swap space or there is too much unnecessary swap space that you should remove.

Adding and Removing Swap Space

The changing nature and load of the work on your system may require adding or removing swap space. For example, as new software packages are installed on the system, you may find that you need to add swap space. You can accomplish this without repartitioning a disk by using the mkfile and swap commands.

You basically designate a part of the exiting UFS or NFS file system as a secondary swap area. Here is the process:

  • Create a swap file by issuing the mkfile command.

  • Activate (add) the swap file by issuing the swap command.

  • Add an entry for the swap file in the /etc/vfstab file so that the swap file is automatically activated when the system is booted.

Of course, an alternative is to repartition the disk or to use a new disk to add more swap space, using the format command. The following exercise demonstrates how to add swap space by using the mkfile and swap commands.

Exercise 12-1: How to Add More Swap Space without Repartitioning the Disk

image from book

To add more swap space, perform the following steps:

  1. Become superuser.

  2. Create a directory for the swap file, if needed—for example:

        mkdir /moreswap 

  3. Create the swap file by using the mkfile command, which has the following syntax:

        mkfile <number> [k|b|m] <filename> 

    The <number> specifies the file size in KB, bytes, or MB specified by k, b, or m. For example, to create 500MB swap space, issue the following command:

        mkfile 500m /moreswap/swapFile 

  4. Activate the swap file by carrying on the same example:

        # /usr/sbin/swap -a /moreswap/swapFile 

    You must use the absolute path name to specify the swap file. The swap file is now added, and activated. It is available until the file system is unmounted, the system is rebooted, or the swap file is removed. The swap file will be automatically activated each time the system is booted, if you perform the next step.

  5. Add the following entry for the swap file to the /etc/vfstab file, again using the running example:

        /moreswap/swapFile—- swap—no - 

  6. Issue the following command to verify that the swap file has actually been added:

        /usr/sbin/swap -l 

image from book

You can create a swap file without root permissions. However, if root is not the owner of the swap file, it could be easily overwritten by accident. You can remove a swap file by issuing the following command:

    swap -d <swapFileName> 

This will not remove the file, but it will make it unavailable for swapping. However, if you want the space occupied by the file, you can remove it by using the good old rm command:

    rm <swapFileName> 

You should also remove the entry for the removed swap file from the /etc/vfstab file.

We have used the swap command at several places so far. Now, let's take a look at its syntax:

    swap <option> [<fileName>] 

The options are described here:

  • -a <filename>. Activate a swap file whose full path is specified by <filename>.

  • -d <filename>. Make the file specified by <fileName> unavailable to swapping without removing the file.

  • -l. Identify the swap space on the system: the device name and the total physical swap space—that is, it does not include any space from the physical memory.

  • -s. Display the allocated, reserved, and available swap space. This swap space may include part of available physical memory (the virtual swap space).

The virtual memory and swap space make the location of the data storage transparent to the processes running on the system, whether it's stored in memory or on disk. What about making the location of the file systems transparent to the users and the processes: whether a file system is on the local machine or a remote machine? This is accomplished by using the network file system (NFS).




Sun Certified System Administrator for Solaris 10 Study Guide Exams 310-XXX & 310-XXX
Sun Certified System Administrator for Solaris 10 Study Guide Exams 310-XXX & 310-XXX
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net