Is distributing load in a balanced way between swap partitions possible?

1

Let's say I have two swap partitions, /dev/sdb and /dev/sdc, where /dev/sdb is 4 times faster in terms of I/O than /dev/sdc. Is it possible to configure it in such way that when the OS decides that it has 5 MB of RAM content to swap out, it directs 4 MB of it to /dev/sdb and 1 MB to /dev/sdc to provide optimal swapping speed?

Krzysiek Setlak

Posted 2017-01-18T20:55:41.147

Reputation: 57

Answers

2

If /dev/sdb is 4 times faster than /dev/sdc, it'll be faster to fill up /dev/sdb first before starting on /dev/sdc.

Keep in mind that it's not just splitting up sequential reads and writes; there's also seek latencies, disk spin-up times, and synchronizing both disks so that one writes four parts while the other writes one. The sequential throughput may theoretically be higher, but it's not practical.

That said, mounted swap partitions have priorities. If you want to mount your two swap partitions so that /dev/sdb is used first and then /dev/sdc, consider this example:

sudo swapon -p 10 /dev/sdb
sudo swapon -p 5  /dev/sdc

In /etc/fstab, you would pass the pri option to set the priority, like so:

/dev/sda none swap defaults,pri=10 0 0
/dev/sdb none swap defaults,pri=5  0 0

As a practical example, you would want to do this if you were using zram. Since zram is way faster than hard drives and solid-state drives, you would like your zram swap partitions to be used first. Here, you can see the four zram devices preferred to the slow device /dev/zd16:

deltik@node51 [~]$ swapon -s
Filename                Type        Size    Used    Priority
/dev/zd16                               partition   8388604 0   -1
/dev/zram0                              partition   1524800 275252  5
/dev/zram1                              partition   1524800 275400  5
/dev/zram2                              partition   1524800 276296  5
/dev/zram3                              partition   1524800 275392  5

(On Ubuntu, zram swap is conveniently provided by the zram-config package.)

Even though /dev/zram* are orders of magnitude faster than /dev/zd16 sequentially, I would not want to balance the workload because /dev/zd16 seeks way slower than /dev/zram* do.

Deltik

Posted 2017-01-18T20:55:41.147

Reputation: 16 807

See - that's what I don't understand. Looking at our simplified, theoretical case, having 5 MB it's better to load it into both partitions simultaneously, taking up 1 s than to fill up the faster partition first according to priorities given to them as it would take 1.25 s in that case. Why can't multiple devices be used in parallel, RAID-0 style? Because normally if we give both our theoretical partitions the same priority, they will be BOTH filled up with a speed of 1 MB/s, the speed of the slowest partition, why does it work like this? – Krzysiek Setlak – 2017-01-19T17:17:41.577

Can it be configured to always squeeze top performance from such an arrangement? – Krzysiek Setlak – 2017-01-19T17:18:27.227

1

@KrzysiekSetlak: You certainly can do swap on RAID-0. One way to do it is by using md. You can't split the I/O 4 to 1 because striping doesn't work like that. Nobody would bother making your desired implementation because of the shortcomings described in the second paragraph of my answer.

– Deltik – 2017-01-19T17:36:25.367