How to prioritize swap partition and swap file for better performance

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (RHEL)

Issue

  • How to prioritize the devices used for swap.
  • Performance difference between swap partition and swap file.

Resolution

  1. Check the priority of swap using "swapon -s"
    # swapon -s
    
    Filename		Type		Size	Used	Priority
    /dev/hda2           partition	819304	80	-1
    /swapfile           file		65528	0	-2
  • Notice that the priorities for the two swap spaces are negative. This is the default setting. Since -1 is a higher number than -2, the system will swap to /dev/hda2 before /swapfile. By default the priorities are assigned in the order that the swap spaces are added.

NOTE: kernel-3.10.0-957.el7or later, the priority will start -2 due to Content from git.kernel.org is not included.an enhancement.

    # swapon -s
    
    Filename		Type		Size	Used	Priority
    /dev/hda2           partition	819304	80	-2
    /swapfile           file		65528	0	-3
  1. Customize the priority of swap:

Temporary Setting

(i) Make sure your swap device is not in use by the system.

    # free -m
    total       used       free     shared    buffers     cached
    Swap:         864          0        864
  • From the above output, 0MB of Swap is used.

(ii) Disable the swap device for paging and swapping.

    # swapoff /dev/hda2

(iii) Set the priority to the swap partition.

    # swapon -p 10 /dev/hda2

Note: Higher numbers indicate higher priority

(iv) Now enable the swap device and verify it's priority.

    #swapon -s
    Filename    Type		Size	 Used	 Priority
    /dev/hda2   partition	819304	 0	 10

(v) Now repeat step (i-iv) for swap file. assuming name of swap file is "swapfile" on "/" directory.

(vi) Now enable the swap file and verify it's priority.

    # swapon -s
    Filename	   Type		Size	Used	Priority
    /dev/hda2      partition	819304	0	10
    /swapfile      file		65528	0	9

Note 1: Here we have give priority 9 to swapfile. Now swap device (/dev/hda2) will have high priority then swap file (swapfile). Setting the priority using swapon is nice for testing different swap configurations, but once we know they'll work we'll want to make the changes permanent. To do permanent changes modify /etc/fstab file.

Note 2: Due to kernel specifications, negative numbers (such as '-2', '-3', etc.) are considered to be "automatically allocated by the kernel" and will fluctuate randomly based on the device recognition order at boot time (timing conflicts between LVM and physical partition recognition). Therefore, avoid using negative numbers when customizing settings.


Permanent Setting

Fstab is a text file that tells the system how to automatically mount and use file systems. It also handles swap spaces.

(i) First take the backup of fstab before modifying it hence it some times goes wrong we can revert to previous state.

    # cp -rv /etc/fstab /etc/fstab.backup

(ii) Modify the swap line as shown below:

    # cat /etc/fstab

From:

    UUID=ff859d1c-c833-4e15-b6ba-48eddac87206       none  swap    defaults        0 0
    /swapfile                                       swap  swap    defaults        0 0

To:

    UUID=ff859d1c-c833-4e15-b6ba-48eddac87206 none    swap    pri=10        0 0
    /swapfile	                              swap    swap    pri=9         0 0

(iii) Restart the system or turn on and off swap to take effect with swapon command:

    # sync;sync;swapoff -a
    # swapon -a

(iv) Now enable the swap file and verify it's priority.

    # swapon -s
    Filename				Type		Size	Used	Priority
    /dev/hda2                               partition	819304	0	10
    /swapfile                               file		65528	0	9
  • swap from "free -m" should show the addition of swap partition and swap file.
  • Creating a swap file in a partition does have less performance compared to the swap partition. Swap priority can be set in that case i.e high priority to the swap partition and if swap partition got over-utilized then use swap file.

Root Cause

See also: How can the performance of multiple swap partitions be tuned?

Components
Category
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.