Understanding zswap

I still struggle to understand zswap. My assumption was that before anything goes to the swapfile it is stored in RAM which is 16 GB.

My zswap configuration is:

/sys/module/zswap/parameters/same_filled_pages_enabled:Y
/sys/module/zswap/parameters/enabled:Y
/sys/module/zswap/parameters/max_pool_percent:25
/sys/module/zswap/parameters/compressor:zstd
/sys/module/zswap/parameters/zpool:z3fold
/sys/module/zswap/parameters/accept_threshold_percent:90

Then I see

/sys/kernel/debug/zswap/same_filled_pages:0
/sys/kernel/debug/zswap/stored_pages:177
/sys/kernel/debug/zswap/pool_total_size:245760
/sys/kernel/debug/zswap/duplicate_entry:0
/sys/kernel/debug/zswap/written_back_pages:0
/sys/kernel/debug/zswap/reject_compress_poor:0
/sys/kernel/debug/zswap/reject_kmemcache_fail:0
/sys/kernel/debug/zswap/reject_alloc_fail:0
/sys/kernel/debug/zswap/reject_reclaim_fail:0
/sys/kernel/debug/zswap/pool_limit_hit:0

and swapon shows

NAME           TYPE SIZE USED PRIO
/swap/swapfile file  18G 3.8M   -2

Can anybody explain why there are 3.8M in the swapfile?

zswap still uses swap, it just provides a compressed RAM cache to facilitate making the swap more efficient and less write intensive–sort of like a buffer, to make writing to swap work better.

You may be thinking of zram, which is a swap device unto itself; it essentially replaces swap (except for hibernation).

This is a short explanation from the ArchWiki:

Zswap is a kernel feature that provides a compressed RAM cache for swap pages. Pages which would otherwise be swapped out to disk are instead compressed and stored into a memory pool in RAM. Once the pool is full or the RAM is exhausted, the least recently used (LRU) page is decompressed and written to disk, as if it had not been intercepted. After the page has been decompressed into the swap cache, the compressed version in the pool can be freed.

The difference compared to zram is that zswap works in conjunction with a swap device while zram is a swap device in RAM that does not require a backing swap device.

2 Likes

That’s exactly what baffles me. As in my example above the cache (pool) isn’t full. Why do I see anything in the swapfile?

This means your pool size is 25% of your memory, so in your case the zswap pool is about 4GB. After your system has about 12GB of RAM in use it should start passing pages to swap.

All 4GB don’t need to be full before the swapfile is used:

This means when the pool is 90% full, it will start ignoring new pages (they get passed directly to swap) until there is enough free space to take more pages. So when you have about 3.6GB in zswap, it will start using the swap a little bit.

Certain types of suspend will also use swap on the disk instead of zswap or RAM.

I don’t think that is quite right.

That should mean that once the compressed pool becomes completely full it will write directly to swap until it drops to 90% full. Once it reaches 90%, it will re-engage the compressed pool again.

2 Likes

Gotcha, yes that makes more sense.

Somehow I got a little mixed up reading through this explanation, with the hysteresis and all:

To prevent zswap from shrinking pool when zswap is full and there’s a high pressure on swap (this will result in flipping pages in and out zswap pool without any real benefit but with a performance drop for the system), a special parameter has been introduced to implement a sort of hysteresis to refuse taking pages into zswap pool until it has sufficient space if the limit has been hit.

Now that you explain it, it makes a lot more sense. :bulb:

2 Likes

I have enabled zswap on various distro (Debian, Ubuntu, Linux Mint and Arch Linux derivative) via systemd-swap service. but unfortunately when open Firefox sometimes my HDD makes heavy swapping on 2 GB of RAM lol.

If your home directory is on the HDD you are probably noticing Firefox writing the disk cache (not swapping). You can turn it off or move the disk cache to RAM by following these instructions: