swapfile vs swap partition

3 views
Skip to first unread message

Robert Citek

unread,
Nov 22, 2007, 11:44:16 AM11/22/07
to Central West End Linux Users Group

For many of the machines at ByteWorks, including the classroom machines
and the EAC student machines, I've configured them to use a swapfile
instead of a swap partition. This greatly simplifies maintenance of the
machine and cloning of the disks. However, I've been wondering if there
was any performance penalty for using a swapfile instead of a swap
partition. After doing a few tests, I came to the conclusion that a
swapfile is no worse than using a swap partition. In fact, according to
the tests I did, using a swapfile is about 20% faster than using a swap
partition.

Below are the methods and results. Feel free to provide feedback on the
validity of the process.

Regards,
- Robert

-----

Method:

1) Booted Ubuntu 7.04 into recovery mode with the added kernel option
"mem=64M". This was small enough to cause even a simple program to
almost immediately start to swap.

2) using a swapfile (>128 MB), ran a test script 40 times

3) turning the swapfile off and turning the swap partition on (>128 MB),
re-ran the test script 40 times.

The script:

#!/bin/bash

dt=$(date +%s)
{
date
vmstat -n 1 &
pid=$!

foo () {
< file perl -le '@foo=<>; print $#foo'
}

foo=$( (time -p foo) 2>&1 )
sleep 3
kill $pid
echo -e '\n\n\n'
echo $foo
date
} >& swap.$dt.txt

The test file "file" was 10M in size and contained 1,000,000 lines of
"0123456789"

Results:

Swapfile real time values:

$ grep -h real swap.*.txt | cut -d' ' -f 3 | sort -n | xargs | tr ' ' +
38.73+38.76+38.80+38.91+39.02+39.13+39.17+39.56+39.61+39.67+39.78+39.83+39.95+39.95+40.07+40.09+40.09+40.18+40.19+40.33+40.39+40.59+40.62+40.62+40.80+40.80+41.44+41.51+41.73+41.79+41.86+42.11+42.27+43.34+44.25+45.43+46.86+47.90+49.71+76.86

Average=42 seconds

Swap partition real time values:

$ grep -h real swap.*.txt | cut -d' ' -f 3 | sort -n | xargs | tr ' ' +
39.23+39.86+39.93+40.19+40.24+40.78+40.82+41.32+41.46+42.03+42.12+42.33+42.34+42.47+42.58+43.16+43.22+43.73+44.93+45.55+46.55+47.64+48.25+48.37+49.53+50.51+50.84+54.41+57.60+58.41+61.63+65.47+71.14+74.18+75.59+75.71+81.54+81.92+90.54+149.42

Average=54 seconds

Robert Citek

unread,
Nov 25, 2007, 10:19:30 PM11/25/07
to Central West End Linux Users Group
On Nov 22, 2007 10:44 AM, Robert Citek <robert...@gmail.com> wrote:
> Below are the methods and results. Feel free to provide feedback on the
> validity of the process.

I repeated the experiment on another machine using only 10 replicates:

Swapfile:
87.36+88.09+85.27+85.94+86.03+91.28+87.56+88.21+90.75+88.15

88 seconds average

Swap partition:
105.71+103.45+101.47+99.83+100.64+99.10+98.54+102.57+99.75+103.51

101 seconds average

I then ran the script again on that same machine 40 times, alternating
between swapfile and swap partition (see script below). Switching swap
types had the advantage of reseting the amount of swap used to zero:

$ grep swapfile swap.file.* | tr -s ' ' '\t' | cut -f 4 | sort | uniq
0
$ grep sda5 swap.partition.* | tr -s ' ' '\t' | cut -f 4 | sort | uniq
0

Swapfile:
91.69+95.89+97.01+92.31+91.33+92.10+92.05+93.80+93.49+93.29+98.18+95.94+93.24+93.26+160.97+94.42+95.34+92.71+95.67+94.90+96.53+94.31+96.56+94.61+93.35+95.14+95.84+95.55+92.45+97.57+92.35+90.63+99.79+94.18+94.02+93.70+153.28+97.40+92.91+95.04

97 seconds average

Swap partition:
106.22+102.96+104.19+110.58+104.21+108.43+109.98+105.22+104.52+107.46+103.32+108.06+108.63+109.08+104.86+109.68+107.16+106.65+106.37+104.51+104.63+105.42+102.78+106.76+110.28+107.66+109.06+107.41+104.53+107.31+107.23+107.58+103.12+104.12+106.61+111.69+107.05+107.77+106.96+106.46

106 seconds average

With both of these new tests on a new machine the conclusion seems to
be the same: using a swapfile seems to be no worse than using a swap
partition.

You can find the raw data and scripts here:

http://cwelug.org/~rwcitek/swap/

I'm curious to know if others have the same or different experience.

Regards,
- Robert

------

#!/bin/bash

swapon /swapfile
swapoff /dev/sda5
dt=$(date +%s)
{
date
swapon -s


vmstat -n 1 &
pid=$!

foo () {
< file perl -le '@foo=<>; print $#foo'
}

foo=$( (time -p foo) 2>&1 )
sleep 3
kill $pid
echo -e '\n\n\n'
echo $foo
date

} >& swap.file.$dt.txt

swapon /dev/sda5
swapoff /swapfile
dt=$(date +%s)
{
date
swapon -s


vmstat -n 1 &
pid=$!

foo () {
< file perl -le '@foo=<>; print $#foo'
}

foo=$( (time -p foo) 2>&1 )
sleep 3
kill $pid
echo -e '\n\n\n'
echo $foo
date

} >& swap.partition.$dt.txt

Reply all
Reply to author
Forward
0 new messages