Swap priority during boot

Technical issues/questions of an intermediate or advanced nature.
User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Swap priority during boot

Post#1 by Rava » 07 Sep 2023, 11:12

How do I fine-tune the swap priority during boot.
In cheatcodes.txt there is only noswap and zram=some_value% mentioning swap, and both are not about the priority.

Details: I have one larger swap partition from my internal drive (8703 MB) - and currently also one from an external USB3-drive (4095 MB) and I would like to give the internal drive a higher priority.

man swapon says

Code: Select all

Calls to swapon normally occur in the system boot scripts making all
swap devices available, so that the paging and swapping activity is
interleaved across several devices and files.
Is there a way to use some kernel cheat code to adjust that or would I have to put code into /etc/rc.d/rc.local to swapoff the external drive swap partition and swapon that partition with lower priority. Default for the harddrive swap partitions is 1, so I could use e.g. -p -5

But that would mean when the order of the drives change, I would have to use another method to make sure the correct drive is handled, e.g. via UUID and not via /dev/sdc1
Cheers!
Yours Rava

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Swap priority during boot

Post#2 by rych » 08 Sep 2023, 10:38

Rava, Sorry, Rava, can I ask you a (stupid) basic question: why would one need to have a swap partition in Porteus?

User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Swap priority during boot

Post#3 by Rava » 08 Sep 2023, 11:41

rych wrote:
08 Sep 2023, 10:38
Rava, Sorry, Rava, can I ask you a (stupid) basic question: why would one need to have a swap partition in Porteus?
When the RAM is limited and when you still need to run RAM hungry applications, e.g. your standard browser.

My solution for the topic issue for now is to change the priority of the internal drive - since the /dev/ of the external drive could change, the device of the internal one will always be sda6; so I put this into my /etc/rc.d/rc.local

Code: Select all

#set internal swap partition higher so that external partitions keep their priority of "1" and thus are used later or not at all.
echo "swapoff and swapon of /dev/sda6:"
swapoff -v /dev/sda6
swapon -v -p 5 /dev/sda6
I post the result at next boot. Hopefully it works as I intended it to do.

Added in 2 minutes 8 seconds:
when doing it manually swapon reports all kind of stuff but not the priority: :(

Code: Select all

root@rava:~# swapon -v -p 5 /dev/sda6
swapon: /dev/sda6: found signature [pagesize=4096, signature=swap]
swapon: /dev/sda6: pagesize=4096, swapsize=9126805504, devsize=9126805504
swapon /dev/sda6
Added in 57 seconds:
still the priority is set as I wanted it:

Code: Select all

root@rava:~# cat /proc/swaps
Filename				Type		Size	Used	Priority
/dev/sdb1                               partition	4194300	0	1
/dev/sda6                               partition	8912892	0	5
/dev/zram0                              partition	317348	0	100
Cheers!
Yours Rava

rych
Warlord
Warlord
Posts: 622
Joined: 04 Jan 2014, 04:27
Distribution: Porteus 5.0 x64 OpenBox
Location: NZ
Contact:

Swap priority during boot

Post#4 by rych » 10 Sep 2023, 10:32

Rava wrote:
08 Sep 2023, 11:44
When the RAM is limited and when you still need to run RAM hungry applications, e.g. your standard browser.
I also read your scenario here: Porteus 5.0rc2 XFCE - Swap file with issues
Where you said that creating swap partition is better than swapfile. But in my case I'd be creating that partition on my USB! I don't want to swap to USB storage. Alternatively, a swapfile on the host NTFS disk, which should be still better than a linux native swap partition on USB, right?

I'm asking for future needs. I've never had to use any swap under Porteus, and now machines I run Porteus mostly have 8 or even 16GB of RAM. So, if I'm to run a RAM-greedy app, the app would crush when running out of that huge RAM (never happened to me so far, I guess) and I'd take it as I'd need to run it on a newer machine with more RAM :)

User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Swap priority during boot

Post#5 by Rava » 10 Sep 2023, 13:31

rych wrote:
10 Sep 2023, 10:32
Alternatively, a swapfile on the host NTFS disk, which should be still better than a linux native swap partition on USB, right?
Depends.
when you have an external drive on either eSATA or USB-3 (which internally is also eSATA) a swap partition would be better performance-wise than a swap file on the internal drive.
When you can move partitions around on your internal drive having a swap partition there is the best. (Also, to have rely on an external drive is a bother, what if there is data on there you need to move to some other place? E.g. umount and swapoff all devices. swapoff can take a while to free the drive, and usually that would be less than ideal.)

Like I did, making my sda7 smaller by approx 4GB to double the size of my sda6 swap. :)

Added in 5 minutes 37 seconds:
Just to show how my swap fills up after a while (mainly due to Palemoon, but also a bit due to interlink and other programs)

Code: Select all

root@rava:/# type sx
sx is a function
sx () 
{ 
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    { 
        read firstLine;
        echo "$firstLine";
        while read f t s u p; do
            let "s2 = $s / 1024";
            let "u2 = $u / 1024";
            printf '%-40s%-16s%-8s%-8s%-8s\n' $f $t $s2 $u2 $p;
        done
    } < /proc/swaps
}

Code: Select all

root@rava:/# uptime
 15:38:25 up 2 days,  2:11,  4 users,  load average: 0.99, 1.02, 1.12

root@rava:/# sx
10.09.2023 15:31:33 ____________________________________________________________
Filename				Type		Size	Used	Priority
/dev/sdb1                               partition       4095    0       1       
/dev/sda6                               partition       8703    934     5       
/dev/zram0                              partition       309     309     100     
As you can see, the sda6 starts filling up. The zram0 is already full, and only as a last resort would the system use sdb1 (but that hardly ever happens now that I set the priority during boot to have the internal swap sda6 of higher priority than the external ones (these would all get 1 by default)

FYI: "priority is a value between -1 and 32767. Higher numbers indicate higher priority. See swapon(2) for a full description of swap priorities. "
(quote from man swapon(8))

Added in 4 minutes 39 seconds:
And last but not least:
I realized that my approx 4 GB internal swap partition is not enough was when I used that very external drive with also 4 GB swap partition, all in a sudden my system had less issues going into suspend due to running out of RAM.
That was the main reason why I doubled my internal swap partition so that I get the same performance even without the external drive.
Cheers!
Yours Rava

Post Reply