I can add some notes on my experiences activating zswap by default on the
Ubuntu Desktop for Pi images, and the thinking on why I didnβt replicate that
on the Ubuntu Server for Pi images. Firstly though, some basic clarification in
case anyoneβs not aware:
-
zram works with or without a swap file, and effectively acts as compressed
swap media
-
zswap only works with a swap file, and acts as a compressed cache for pages
entering the underlying swap
General consensus at this time I was looking into this (for impish β¦ possibly
jammy, maybe?) was that if a swap file is present, zswap was the preferable
system as it wasnβt independent of the existing swap media (from some recent
reading it appears they may be more nuance there now, though Iβm not sure that
conclusionβs actually changed yet).
Regardless, both systems increase overall available memory by utilizing some
chunk of RAM as a compressed swap space (whether thatβs actual swap or cache
isnβt that relevant). An important facet to acknowledge here is that both
systems reduce available RAM. You canβt directly use pages in swap (either a
real one or a compressed RAM one); they still have to be swapped back in
(albeit much more quickly from compressed RAM than disk), so by dedicating some
of your RAM to compressed swap (of either kind) you do reduce the RAM available
for general usage.
The situation for the Pi images was as follows:
The case for enabling zswap on the Desktop images was fairly clear. The base
model of Pi supported was the Pi 4 with 2GB of RAM (now the Pi 4 with 4GB of
RAM), so we knew that even with 20% (the default) allocated to zswap there
would still be >1.5GB of RAM left, and furthermore a 1GB swap-file was already
allocated on first-boot so there would definitely be backing for the zswap
cache.
On the Ubuntu Desktop for Pi images, enabling zswap with zstd compression
and the 3:1 pool allocator (giving maximum potential compression of 3:1 per
page) changed the memory layout in the 2GB RAM case as follows:
βββββββββββββββββββββββββ¬βββββββββββββ
β 2GB RAM β 1GB Swap β
βββββββββββββββββββββββββ΄βββββββββββββ
βββββββββββββββββββββ¬βββββββββββββββββ¬βββββββββββββ
β 1.6GB RAM β 1.2GB ZSwap β 1GB Swap β
βββββββββββββββββββββ΄βββββββββββββββββ΄βββββββββββββ
(effective)
Hence, we sacrifice 20% of available RAM to increase total (effective)
available memory space by 26% (1.6+1.2+1 / 2+1). Not a huge improvement, but
crucially not worse. Further, it also provides a βsmootherβ progression as
memory pressure increases. Instead of going straight from super-fast RAM to
incredibly slow swap, the system can now go from super-fast RAM, to
βsuspiciously fastβ swap (the compressed portion), and eventually to incredibly
slow swap (and yes, thereβs plenty of subtle details Iβm skipping over here
like uncompressable pages going straight to disk and so on; none of this is
βcut and driedβ).
The 4GB RAM case changed as follows:
ββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β 4GB RAM β 1GB Swap β
ββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
ββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββ¬βββββββββββββ
β 3.2GB RAM β 2.4GB ZSwap β 1GB Swap β
ββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββ΄βββββββββββββ
(effective)
Here, the gain is more prominent. We sacrifice 20% of available RAM to increase
total available by 32% (3.2+2.4+1 / 4+1). I didnβt worry too much about the Pi
4 8GB case because, if you had that model you probably donβt worry much about
swap anyway.
So that was the case for Desktop. How about the Ubuntu Server for Pi images?
Here the story was much more difficult:
-
The minimum supported model was (and still is) the Pi Zero 2W and 3A+ both
of which have a minimal 512MB of RAM. Sacrificing pretty much any of this
is extremely risky, and recall that the smaller the RAM sacrifice the smaller
the overall gains.
-
No swap file is created by default. The server images run in a multitude of
environments and configurations, including netboot, overlayroot, etc. In
several cases, swap was undesirable which more or less ruled out zswap (and
I really didnβt want to contend with two different compression systems across
images).
Suffice it to say, I decided that the case on Server was weaker and too
variable to confidently persue. I havenβt verified this, but personally I
harbour a suspicion that 2GB of RAM is probably the lower bound where these
systems are genuinely useful. Thereβs also the aspect that a user of Server is
probably more confident in configuring zswap or zram themselves, should they
feel it necessary.
Concluding: I suspect that the above reasoning may apply, at least partly, to
the Ubuntu PC images too. I can see a case being made for Desktop fairly easily
(either zram or zswap β Iβm not going into the weeds on that one without some
serious data to back me up!). The case for Server is much harder and, I think,
weaker for similar reasons (wider range of hardware, configuration scenarios,
overruling preferences of server admin, etc. etc.)