| 1 |
swapspace - dynamic swap manager for Linux |
|---|
| 2 |
|
|---|
| 3 |
http://pqxx.org/development/swapspace/ |
|---|
| 4 |
|
|---|
| 5 |
What it does for you |
|---|
| 6 |
|
|---|
| 7 |
This system daemon for the Linux kernel aims to do away with the need for large, |
|---|
| 8 |
fixed swap partitions or swap files. |
|---|
| 9 |
|
|---|
| 10 |
When installing a Linux-based system (invariably GNU/Linux) with swapspace, the |
|---|
| 11 |
usual swap partition can be omitted, or it can be kept quite small. Whenever |
|---|
| 12 |
swapspace finds, during normal system usage, that more virtual memory is needed, |
|---|
| 13 |
it will automatically claim space from the hard disk. Conversely, swap space |
|---|
| 14 |
that is no longer needed is freed up again for regular use by the filesystem. |
|---|
| 15 |
|
|---|
| 16 |
This means that with swapspace installed, sizing the system's available swap |
|---|
| 17 |
space during installation is no longer a life-or-death choice. It now becomes |
|---|
| 18 |
practical to run GNU/Linux off just a single, big partition--with no disk space |
|---|
| 19 |
lost to regrettable installation choices. The system should also be able to |
|---|
| 20 |
handle the occasional memory-intensive task that takes much more swap space than |
|---|
| 21 |
was originally foreseen, without leaving the same swap space unused and unusable |
|---|
| 22 |
during normal operation as is normally the case. |
|---|
| 23 |
|
|---|
| 24 |
Swapspace is made available for use under the GNU General Public License (GPL). |
|---|
| 25 |
See the file COPYING for an open copyright license. |
|---|
| 26 |
|
|---|
| 27 |
Copyright (C) 2005 Software Industry Promotion Agency (SIPA), Thailand. Written |
|---|
| 28 |
by Jeroen T. Vermeulen <jtv@xs4all.nl> |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
How it compares |
|---|
| 32 |
|
|---|
| 33 |
Unlike similar programs such as dynswapd and the older (and more portable) |
|---|
| 34 |
swapd, swapspace also adapts the sizes of the swap files it creates to meet |
|---|
| 35 |
demand. This means it is less dependent on limits that the kernel may impose on |
|---|
| 36 |
the total number of swapfiles, while reducing the need for manual configuration. |
|---|
| 37 |
If the daemon finds that more and more swap files are needed, it will start |
|---|
| 38 |
creating larger ones to anticipate demand. While demand for swap files is |
|---|
| 39 |
modest, it will stick to smaller ones that can be initialized more quickly and |
|---|
| 40 |
so respond more fluently to present requirements. |
|---|
| 41 |
|
|---|
| 42 |
Robustness and user-friendliness are the first priorities in developing this |
|---|
| 43 |
program. For example, all alternatives we looked at perversely needed to |
|---|
| 44 |
allocate multiple chunks of memory in dealing with low-memory situations; |
|---|
| 45 |
allocation failure would typically crash these programs. It turned out that |
|---|
| 46 |
none of these allocations were really necessary, and swapspace manages to avoid |
|---|
| 47 |
them categorically. This kills two birds with one stone when it comes to |
|---|
| 48 |
reliability: (i) the program doesn't ask for memory just when the least memory |
|---|
| 49 |
is available and (ii) it eliminates one of the most important causes of |
|---|
| 50 |
programming bugs as a risk factor. |
|---|
| 51 |
|
|---|
| 52 |
User-friendliness primarily means that no silly questions are asked of the user. |
|---|
| 53 |
The daemon tries to be sensible and figure out what is needed at runtime, by |
|---|
| 54 |
itself, and without user intervention. You should not have any need to learn |
|---|
| 55 |
about the configuration parameters, or tweak them. They exist mostly for |
|---|
| 56 |
development purposes. |
|---|
| 57 |
|
|---|
| 58 |
The swapspace daemon has been in production use for several months on various |
|---|
| 59 |
32-bit architectures before it was first released to the public, and has been |
|---|
| 60 |
tested with swapfiles larger than can be addressed in 32 bits. |
|---|
| 61 |
|
|---|
| 62 |
Swapspace itself is a small program: about 50 kilobytes on my system--or even |
|---|
| 63 |
less in a special version that only accepts the most basic configuration options |
|---|
| 64 |
and ignores its configuration file. On top of that it allocates no memory at |
|---|
| 65 |
runtime (although the system will allocate some, of course) and does not use |
|---|
| 66 |
much stack space. |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
When not to use it |
|---|
| 70 |
|
|---|
| 71 |
In its current form, swapspace is probably not a good choice for systems that |
|---|
| 72 |
need to remain responsive at all times; depending on the system and the |
|---|
| 73 |
cicrumstances, the creation a large new swapfile can take as long as half a |
|---|
| 74 |
minute and occupy quite a lot of the system's attention. The program minimizes |
|---|
| 75 |
the number of times swapfiles are created, but it wouldn't be very useful if it |
|---|
| 76 |
never created any swapfiles at all! |
|---|
| 77 |
|
|---|
| 78 |
We are hoping to bring further improvements in the future. Since the problem |
|---|
| 79 |
appears to be caused mostly by system code, however, it's hard to be sure that |
|---|
| 80 |
this is really possible. It may turn out to be possible for the kernel, with |
|---|
| 81 |
some modifications, to extend an existing swapfile while it is already in use. |
|---|
| 82 |
That would probably help a great deal, but we don't know at the moment how much |
|---|
| 83 |
work it would take. |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
Where to start |
|---|
| 87 |
|
|---|
| 88 |
The program is available both as a source archive and as a Debian package built |
|---|
| 89 |
from that same source archive. |
|---|
| 90 |
|
|---|
| 91 |
To build and install from source, enter the main swapspace source directory and |
|---|
| 92 |
run "make". Some editing of the Makefile may, but generally shouldn't, be |
|---|
| 93 |
required as long as gcc is used as the C compiler. The program code is written |
|---|
| 94 |
in standard C99 (the 1999 edition of the C standard), plus one POSIX extension. |
|---|
| 95 |
The easiest mode of installation is by running "make install" with root |
|---|
| 96 |
privileges. |
|---|
| 97 |
|
|---|
| 98 |
See the provided manpage for details on how to run swapspace for troubleshooting |
|---|
| 99 |
or debugging purposes. A sample configuration file is also provided, but the |
|---|
| 100 |
average user should not need to take an interest. An init script is provided to |
|---|
| 101 |
start and stop swapspace as a regular system service. |
|---|
| 102 |
|
|---|
| 103 |
The "make install" procedure installs the init script in /etc/init.d, but does |
|---|
| 104 |
not currently ensure that swapspace is run on system startup. |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
Technical details: Installation |
|---|
| 108 |
|
|---|
| 109 |
The installation procedure creates a directory /var/lib/swapspace, which must be |
|---|
| 110 |
accessible to the system's superuser (root) only; granting any kind of access |
|---|
| 111 |
for this directory to an untrusted user is likely to constitute a serious |
|---|
| 112 |
security hole. |
|---|
| 113 |
|
|---|
| 114 |
According to the Filesystem Hierarchy Standard, other appropriate places for |
|---|
| 115 |
this kind of file might be /var/tmp or /var/run. The former was not deemed |
|---|
| 116 |
appropriate because it is accessible to all users, and the latter because most |
|---|
| 117 |
system administrators would probably expect it to occupy very little space and |
|---|
| 118 |
may confine it to a partition that isn't large enough to hold useful swap space. |
|---|
| 119 |
|
|---|
| 120 |
Also, files in /var/lib survive reboots whereas those in /var/tmp and /var/run |
|---|
| 121 |
need not. Obviously any data in swap files can be safely erased on reboot (it's |
|---|
| 122 |
even tempting to think that that would be safer, though I don't think it really |
|---|
| 123 |
is). But consider a system consistently short on physical memory: during boot, |
|---|
| 124 |
swapspace will see the swapfiles left by the previous session, and reinstate |
|---|
| 125 |
them immediately at very little cost, ready for use when they are needed. If |
|---|
| 126 |
they had been erased during reboot, swapspace would have to allocate new ones on |
|---|
| 127 |
disk when it recognized the need for more virtual memory, which takes much more |
|---|
| 128 |
time and resources. |
|---|
| 129 |
|
|---|
| 130 |
Technical details: Algorithm |
|---|
| 131 |
|
|---|
| 132 |
Choices of allocation and deallocation are driven by a "finite state machine" |
|---|
| 133 |
consisting of four states: steady, hungry, overfed, and diet. The program will |
|---|
| 134 |
alternate through these states as circumstances dictate, applying different |
|---|
| 135 |
policies depending on current state. This was done to achieve a good tradeoff |
|---|
| 136 |
between willingness to free up unused swap space on the one hand, and avoidance |
|---|
| 137 |
of "thrashing" between deallocation and re-allocation of swapfiles on the other. |
|---|
| 138 |
|
|---|
| 139 |
For those interested, here is a quick description of these states and their |
|---|
| 140 |
associated policies: |
|---|
| 141 |
|
|---|
| 142 |
Steady - normal operation; no additional swap space is needed, nor do we have |
|---|
| 143 |
more than is needed. However, the program can go into the hungry or overfed |
|---|
| 144 |
states at the drop of a hat. |
|---|
| 145 |
|
|---|
| 146 |
Hungry - more swap space was recently allocated. The program is willing to |
|---|
| 147 |
allocate even more if needed, but it will not consider dropping unneeded swap |
|---|
| 148 |
files. After a certain timeout period, the program reverts to the steady state. |
|---|
| 149 |
|
|---|
| 150 |
Overfed - significantly more virtual memory is available than is needed. If |
|---|
| 151 |
this situation persists for a certain timeout period, a swapfile is deallocated |
|---|
| 152 |
and the program returns to the steady state. |
|---|
| 153 |
|
|---|
| 154 |
Diet - a recent allocation attempt has run into resource limits, e.g. because |
|---|
| 155 |
the filesystem used for swap files was full. No more swapspace can be allocated |
|---|
| 156 |
but excess files can be freed up very rapidly. Afer timeout, reverts to steady. |
|---|
| 157 |
|
|---|
| 158 |
State information can be queried by sending the program the SIGUSR1 signal (see |
|---|
| 159 |
"man 7 signal" to get the number for your architecture) which will cause it to |
|---|
| 160 |
log debug information to the system's daemon log and/or standard output, as |
|---|
| 161 |
appropriate. The program can also be made to log state transitions by starting |
|---|
| 162 |
it with the -v or --verbose option. |
|---|
| 163 |
|
|---|