swap space: an idea. Please comment.

Riccardo Facchetti (fizban@mbox.vol.it)
Sun, 23 Feb 1997 14:34:45 +0100 (MET)


Hello,
I was talking this morning at 2 a.m. with a friend in a pub (okay okay
... may be my mind was not so ... how to say it ... clear :)

We was talking about VAX and VMS. At some point he said something like
that VMS is a strong OS. That never crash and never fails in giving
resources to processes that ask for them.
I don't know VMS (I just have an idea about its shell and I definitely
don't like it) but an idea enlightened me ... may be some sort of ethilic
alchool burning on top of my beer glass ... bah :))
But now, that I am (at least I think) lucid, I still think this may be an
interesting idea.

Swap space size is the sum of swap partitions size and swap files size.
These sizes are somewhat constant. When you have configured a swap
partition and even a swap file, you have an amount of swap space and that
is all the extra-memory you have configured for your system.
Of course at run-time you can configure and activate more swap
files/partitions, so the swap space can grow under the control of
sysadmin. Under the control of sysadmin, this is the problem.

Imagine you have an application that is asking for memory and all the
available memory is exhausted. In this case malloc return NULL and your
application will bomb out (gracefully for itself or with SEGV or something
like that). In a case like this, the sysadmin will likely not have time to
add more swap by hand before applications start to bomb out.

Now the idea.
Why not have an (I called) "adaptive" or if you prefer (I don't :)
"dynamic" swap file, managed in kernel space, that can grow and shrink
when memory is needed and/or released by someone ?

This adaptive swap file must be a file in a filesystem, not a
partition, because we can not easily resize on the fly a swap partition,
but we can with a file (or at least, the thing seems to me more easy to
do).
The swap file can start as a regular swap file as tiny as you want. It
is marked as "adaptive", so it can grow when needed and can shrink when
not needed.
You can assign a low priority to this swap file so that it will be used
only as a last resort, only when all the other swaps are full and no more
memory is available.
You can assign a % of free filesystem space to be used for adaptive
swapping, so that the swap file will not fill its filesystem.
The algoritm of resizing should start its work only when the system failed
to allocate resources in the standard way, so the adaptive swap code will
check if there is space on the filesys:

if (No more memory avail) && (No more high priority swap space avail)
&& (A process asked for more memory) {
if (We have enought space on the filesystem) {
Expand the adaptive swap file;
Make the new swap space available;
Retry the memory allocation, return success;
}
}

No memory nor swap space available, return failure;

Of course, the file can shrink when needed:

if (memory is released by some process) {
Try to free adaptive swap space at the end of file;
Shrink adaptive swap file;
}

Okay.
The idea still seems to me a good idea, but I have to say that I don't
have a good knowledge of the mm/ code, so this idea may be only a crazy
thing, I don't know.
I would like to have some opinions about it, so that if I feel you
think it is an interesting idea, I will try to implement it into the
kernel.

Ciao,
Riccardo.