Chapter:Chapter 02 – Inside OS/2 Warp
Subsection: 01. Base Operating System Architecture
Topic: The Swap File
Date Composed: 01-08-97 02:47:05 PM Date Modified: 01-07-99 09:26:01 AM
The swap file is an important part of OS/2 memory management. It allows OS/2 to overcommit memory so that more memory can be used by the operating system and application programs than there is RAM physically present in the computer. Pages of data are swapped out of RAM and onto the disk for storage until they are needed again, when they are swapped back into RAM.
The memory pages stored on disk are located in a file called SWAPPER.DAT, which is usually located in the \OS2\SYSTEM subdirectory on the OS/2 boot drive. The location of this file is specified in the CONFIG.SYS file by the following line, and can be changed if your boot drive is too full to provide sufficient space for the SWAPPER.DAT file to grow.
SWAPPATH=C:\OS2\SYSTEM 4096 5120
The SWAPPATH statement in the CONFIG.SYS file determines the initial size of the SWAPPER.DAT file (5 MB in this example) and the amount of disk space to reserve so that the swap file cannot file up the entire disk. It also determines the location of the swap file.
The strategy used to implement swapping in OS/2 is completely misunderstood by most people. As a result they have certain expectations about how the swap file is supposed to behave, and believe that there is a problem when it does not behave as expected.
OS/2 starts by creating the SWAPPER.DAT file during the kernel initialization. If the swap file previously existed, as is almost always the case, it is erased and recreated. The initial size of the swap file is determined by the second parameter in the SWAPPATH statment, above. Its location is determined by the path specified in the SWAPPATH statement.
Preallocating the swap file helps to ensure that it is contigous. This is especially important for performance reasons on FAT drives. Preallocation also reduces the amount of time required to move data to the swap file because the space on the hard drive has already been allocated. Again, this is especially important on a FAT drive because it can take a great deal of time to allocate disk space. This is a result of the relatively primitive structure of the FAT file system.
Swap File Allocation
Part of the OS/2 swap strategy is to attempt to allocate or deallocate space for SWAPPER.DAT on the hard drive when no other activity is taking place. This helps to ensure that, when additional space needs to be allocated, it is not done at the very time when the data needs to be swapped to the file, thus slowing the actual swapping process.
Whan RAM is full and existing swap file space is nearly full, the memory management portion of the OS/2 kernel allocates more space on the hard drive for the swap file even though it is not yet needed. The trigger point is when 500 KB or less of free space remains in the swap file. When that point is reached, the memory manager watches the hard drive to determine a time when it is not busy responding to read or write requests. When the drive is not busy, the memory manager allocates more space for the swap file in 1 MB increments.
This strategy for swap file allocation not only prevents having to allocate disk space at the moment the swap file needs to be used, it also prevents interrupting other applications which are accessing the disk.
Swap File Deallocation
Deallocation of swap file space is less critical in terms of its impact on the swap process. Deallocation can occur any time. It does not have to be done as a prerequisite to enable some other task to be completed. It is also desirable to maintain much of the data in the swap file for a period of time after the file was closed or the program terminated. This can prevent having to read those same files from the disk if they are needed relatively soon.
Say, for example, that I have just finished a document in a word processor. After I print the document, and while the print spooler is sending the document to the printer and the printer is printing the document, I close the program. I am prone to make mistakes, as are most of us carbon based, humanoid life forms. Since I find the error after the file is closed and the program has been terminated, I have to restart the word processing program and reload the document. It does not take very long because the application and the document have both been retained in the swap file.
Dynamic Link Libraries (DLLs) are also retained in the swap file for a considerable period of time. Many DLLs are used by a lot of different components of OS/2 and by many application programs.
For these reasons swap file deallocation does not take place quickly. Many people I have talked to in my years of supporting OS/2 assumed that as soon as a file was closed or a program terminated that the swapper file should shrink. When it did not, they assumed that there was a problem. This is an erroneous assumption.
Let us assume, then, that several files or programs have been closed and that the requisite amount of time has passed for the memory manager to begin the process of deallocating space from the SWAPPER.DAT file. When the disk is not busy, space belonging to the files and programs which have been least recently closed is marked as unused. It is not possible, however, just to whack a chunk off the end of the file. Most of the empty space in the file is now scattered throughout the SWAPPER.DAT file rather than all being nice and tidy at the end.
The task of the memory manager now becomes one of moving all of the unused space to the end of the swap file so that it can be deallocated. Over a period of time while the disk is not busy with productive tasks, the memory manager moves empty pages to the end of the swap file by moving in-use pages to the empty ones nearer the beginning of the file.
When three conditions are met, the memory manager will deallocate the space.
- The last 1 MB of space in the file must be unused.
- There must be 1.5 MB of total unused space.
- There must be no new pages swapped into the file for a specified period of time. This helps to ensure that there will be no immediate need to reallocate space in the swapper file. Why deallocate space which will be needed again soon anyway?
When these conditions are met, the last 1MB of the SWAPPER.DAT file is deallocated leaving at least 528 KB of empty space available in case additional swapping needs to take place. Because of the need to meet all of these requirements and the time required to move the data around within the swap file, this process can take a long time. It does work, though. My swap file can be as large as 55 MB during a typical day. although I do observe some shrinkage of SWAPPER.DAT during the day, it usually remains over 40 MB until I quit for the evening. By the next morning, it is back down to 25 MB or so, which is just a little larger then the 20 MB default preallocation size I have specified in CONFIG.SYS.
This is very complex, but a large part of this strategy is to perform operations on the swap file in such a way that they affect the productive tasks as little as possible.
See “Tuning the SWAPPER.DAT file” for details of tuning the swap file.