![]() |
John VanDyk has been innovating with information technology for more than 20 years. Read more... |
Moving Swap on OS X 10.5 Leopard
By default, OS X 10.5 stores its swapfiles in /private/var/vm:
$ ls -lh /private/var/vm
total 131072
-rw------T 1 root wheel 64M May 28 10:32 swapfile0
Recently I've been having some fun with a Mac Pro that has 32GB (sic) of RAM. The machine is being used for some bioinformatics work which involves huge chunks of memory. Naturally we ran out of memory and started swapping, but we wanted to swap faster.
I created a RAID 0 (OS X calles it a striped set) using 32GB partitions across four disks for a 128GB RAID volume. Creatively, I called this volume swapraid0
. I chose that size because Leopard can have a maximum of 64 2GB swap files (at least that's what Yves wrote). The first few swap files are smaller (Leopard creates 64MB, 128MB, ... 1GB files) so 128GB should be plenty.
I made this RAID 0 volume the first volume (and put the OS on the second volume) because some people reported trouble if swap was not on the first volume.
Then I copied the existing paging directory to the new volume:
$ sudo cp -Rp /private/var/vm /Volumes/swapraid0/.vm
I used a dot in the name so it wouldn't show up in the Finder. -R is recursive and -p means "preserve permissions".
If you simply edit /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist to point to your swap on the new volume, you run the risk of the volume not being mounted in time during startup. So I put the following at /sbin/dynamic_pager_init:
#!/bin/bash
#launch Apple's dynamic_pager only when the swap volume is mounted
#see http://forums.macosxhints.com/showpost.php?p=452409&postcount=14
if [ "x`df -H | grep /Volumes/swapraid0`" = "x" ]; then
echo "Waiting for swap volume to mount";
else
echo "Launching dynamic pager on volume swapraid0";
/sbin/dynamic_pager -F /Volumes/swapraid0/.vm/swapfile;
fi
Changed permissions on /sbin/dynamic_pager_init to make it executable:
sudo chmod ugo+x-w /sbin/dynamic_pager_init
sudo chown root:wheel /sbin/dynamic_pager_init
Double-checked:
$ ls -l /sbin/dynamic_pager_init
-r-xr-xr-x 1 root wheel 388 Jun 1 20:41 /sbin/dynamic_pager_init
Next, I made a backup of the plist that launches the dynamic pager:
$ sudo cp -p /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist /Users/jvandyk/com.apple.dynamic_pager.plist
Then I edited the plist so that it would run the script at /sbin/dynamic_pager_init:
$ sudo nano -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
The result:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.apple.dynamic_pager</string>
<key>ProgramArguments</key>
<array>
<string>/sbin/dynamic_pager_init</string>
</array>
<key>OnDemand</key>
<false/>
</dict>
</plist>
After rebooting, I ensured that the pager had actually launched:
$ ps aux | grep swap
root 161 0.0 0.0 610348 3504 ?? S 10:00AM 0:00.01 /sbin/dynamic_pager -F /Volumes/swapraid0/.vm/swapfile
Warnings/Caveats
1. You'd have to be nuts to do this. The existing pager settings work fine.
2. I am not responsible if you blow up your machine.
3. When I made a typo while I was experimenting, I found out what happens when the pager isn't actually running and you use all the memory. Ugly.
4. Certainly there are ways to improve this.
Sources
Pretty much all of this approach was taken from the posts by E James, whom I thank.
Also, read the dynamic pager man page:
$ man dynamic_pager
- Log in to post comments
Comments
Great article on moving the swap
Thanks for pointing out that there is a possibility that the drive you are moving the swap file(s) to, may not be mounted when the swap is turned on. I successfully moved my swap to another drive, and it works great. I have noticed a big speed improvement, as my swap space is no longer competing with the OS hard drive access. It really does make a big difference.
I do this for many linux servers, and sometimes I will put swap on a whole other RAID controller in addition to another drive. For other people who may be reading this, that are not familiar with HD and partitioning setup of workstations, and servers; to get the most performance, you want to split up things across as many drives as you can. For example, many database servers have multiple RAID controller cards. One controller controls a RAID array of drive, and ONLY the OS and related files live on that. Another controller will host an array of drives just for the data files, another controller will host drives with just temp files and log files, and another controller will host swap files or partitions (partitions have a speed advantage over files, because there is no filesystem layer to bottleneck things)
The moral of the story is that the reason why you would move your swap to another drive, is to gain speed. OS X likes to utilize swap, and by moving it to another drive, you free up your main drive to launch apps and OS related stuff, where the swap lives on another drive and does not have to compete with the OS and application access.
Xupport
I should point out that Xupport does this now easily, with a few clicks.