Zswap or Zram?


Pedro Teixeira
 Share

1 1

Recommended Posts

Hi all,

 

I've been searching this topic for a while but haven't found a clear answer to my question.

 

Running an N2+, I've noticed that sometimes, in dmesg, a process will be killed due to lack of ram, this is usually when, for example, plex is busy generating video thumbnails and bazarr is syncing newly downloaded subtitles for example. This isn't as common place as such but it does happen from time to time.

 

My armbian installation (latest ubuntu server image with latest mainline kernel) is installed to an nvme ssd, so I was wondering if enabling zswap and disabling zram would be beneficial in this case? Or can both be used at the same time? That's the part that I'm a bit confused, if both can be used in conjunction or if it's one or the other. Or would tweaking zram be a better solution?

 

Many thanks!

Link to post
Share on other sites

Armbian is a community driven open source project. Do you like to contribute your code?

Yes both can be used at the same time.
If your configuration is set right it first will use memory, then zram. And only after that the swap file.

If you've got the 4GB model it can be good to reinstall zram-config

"sudo apt install zram-config"

It should make 2GB zram for 4GB models but only makes 1GB default.

Link to post
Share on other sites

i think zram is good as long as you want to (and can) live without swap at all - zswap is good if you'll need a bit of swap and i made some quite good experiences especially since it supports zstd and 3fold modes which results in about 1:3 compression ratio usually ... it cannot do any wonders, so it is better to use it with at least a bit of ram (i would recommend 2gb real ram at minimum if you run a desktop as otherwise it will eat quite a bit of cpu for compressing and decompressing the memory all the time, maybe 1gb is ok for a server) ... a good side effect of it is that everything swapped out will be compressed too, so less io to emmc or sd card which is good ... the confusing part about it is that counts the compressed pages before compression and the pages swapped out together as swap in free or top, so it looks like it is using a lot of real swap while in reality it does not. i created a little script for myself to get a better overview of its memory numbers here: https://github.com/hexdump0815/imagebuilder/blob/dev/files/extra-files/root/zswap-stats.sh like and this is the config i'm using:

# do some low memory optimization by using zswap memory compression
echo zstd > /sys/module/zswap/parameters/compressor 
echo z3fold > /sys/module/zswap/parameters/zpool
# default is 20, less means less compressed memory and less cpu usage
# for compression, more is the other way around, depends on actual hw
# with 10 there is no big win on memory, but only compressed pages
# are written to the swap sapce and thus io to emmc/sd is reduced
echo 10 > /sys/module/zswap/parameters/max_pool_percent 
echo 1 > /sys/module/zswap/parameters/enabled

best wishes - hexdump

Link to post
Share on other sites

1 hour ago, NicoD said:

Yes both can be used at the same time.
If your configuration is set right it first will use memory, then zram. And only after that the swap file.

If you've got the 4GB model it can be good to reinstall zram-config

"sudo apt install zram-config"

It should make 2GB zram for 4GB models but only makes 1GB default.

 

Thanks! So, if I just install it again, it will re-create zram to 2GB instead of 1GB? (It's definitely at 1GB right now). Mine is an N2+ so it's 4GB.

 

What about armbian-zram-config? Won't there be a conflict? As it looks like it's not a reinstall at all:

 

The following NEW packages will be installed:
  zram-config
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,312 B of archives.

 

Edited by Pedro Teixeira
Link to post
Share on other sites

37 minutes ago, Pedro Teixeira said:

Thanks! So, if I just install it again, it will re-create zram to 2GB instead of 1GB? (It's definitely at 1GB right now). Mine is an N2+ so it's 4GB.

Indeed. It will just resets the zram configuration.
The armbian-zram-config is old. I think it doesn't need to be used anymore. It used to be a better config. But things have changed since then.
I'll take a look at it when I find time, but I said that a lot before :) 

Link to post
Share on other sites

1 hour ago, NicoD said:

Indeed. It will just resets the zram configuration.
The armbian-zram-config is old. I think it doesn't need to be used anymore. It used to be a better config. But things have changed since then.
I'll take a look at it when I find time, but I said that a lot before :) 

 

Thanks, that worked a treat. It's now showing 2GB of zram. Would it be better in this scenario to disable the armbian-zram-config?

 

# configuration values for the armbian-zram-config service
#
# enable the armbian-zram-config service?
ENABLED=true

 

Link to post
Share on other sites

Thanks both.

 

I've disabled armbian-zram-config service for now.

 

I was about what you said @NicoD regarding using zram then zswap.

 

According to here https://github.com/Nefelim4ag/systemd-swap. Which is the script I use to run zswap + swapfc on my crappy celeron with 2GB running Manjaro and it works incredibly well. Zram and zswap shouldn't be used in conjunction:

 

Quote

Q: Do we need to activate both zram and zswap?
A: Nope, it's useless, as zram is a compressed RAM DISK, but zswap is a compressed "writeback" CACHE on swap file/disk. Also having both activated can lead to inverse LRU as noted here

 

If having 2GB of zram works well I'll keep it of course. Otherwise I'll give this script a go and replicate what I use on my laptop and check the results!

Link to post
Share on other sites

On 9/20/2020 at 5:52 PM, Igor said:


This problem should be fixed by now. At least at build stage.

 

This is probably going to be a stupid question. But since it's been fixed now, should I uninstall zram-config and re-enable armbian-zram-config service and then sudo apt update - upgrade? Or just leave as is?

Edited by Pedro Teixeira
Link to post
Share on other sites

3 hours ago, Igor said:


IMO just leave as is. For the future.

 

I just updated and after reboot both seem to be running:

 

● zram-config.service - Initializes zram swaping
     Loaded: loaded (/lib/systemd/system/zram-config.service; enabled; vendor preset: enabled)
     Active: active (exited) since Tue 2020-09-22 17:51:56 BST; 36min ago
    Process: 1710 ExecStart=/usr/bin/init-zram-swapping (code=exited, status=0/SUCCESS)
   Main PID: 1710 (code=exited, status=0/SUCCESS)

Sep 22 17:51:55 odroidn2 init-zram-swapping[1859]: no label, UUID=339a18b7-f3bf-434d-87c7-59b2080efe0e
Sep 22 17:51:55 odroidn2 init-zram-swapping[1905]: Setting up swapspace version 1, size = 302.8 MiB (317485056 bytes)
Sep 22 17:51:55 odroidn2 init-zram-swapping[1905]: no label, UUID=342ed5cd-4791-4c62-a93a-630837fda499
Sep 22 17:51:55 odroidn2 init-zram-swapping[1965]: Setting up swapspace version 1, size = 302.8 MiB (317485056 bytes)
Sep 22 17:51:55 odroidn2 init-zram-swapping[1965]: no label, UUID=e5029900-bb05-48f3-8f66-ea5b720591c2
Sep 22 17:51:55 odroidn2 init-zram-swapping[1989]: Setting up swapspace version 1, size = 302.8 MiB (317485056 bytes)
Sep 22 17:51:55 odroidn2 init-zram-swapping[1989]: no label, UUID=a3eaf027-1cbc-4346-8ba6-caca455008b5
Sep 22 17:51:55 odroidn2 init-zram-swapping[2022]: Setting up swapspace version 1, size = 302.8 MiB (317485056 bytes)
Sep 22 17:51:55 odroidn2 init-zram-swapping[2022]: no label, UUID=9a4d8876-4fb4-4e94-8b8d-cfeff3371146
Sep 22 17:51:56 odroidn2 systemd[1]: Finished Initializes zram swaping.
sptz@odroidn2:~$ sudo systemctl status armbian-zram-config
● armbian-zram-config.service - Armbian ZRAM config
     Loaded: loaded (/lib/systemd/system/armbian-zram-config.service; enabled; vendor preset: enabled)
     Active: active (exited) since Tue 2020-09-22 17:51:54 BST; 36min ago
    Process: 1505 ExecStart=/usr/lib/armbian/armbian-zram-config start (code=exited, status=0/SUCCESS)
   Main PID: 1505 (code=exited, status=0/SUCCESS)

Warning: journal has been rotated since unit was started, output may be incomplete.

 

Is this "bad" or harmless? Should I remove one of them and if so which one?

 

Thanks!

Link to post
Share on other sites

I think zram and zswap sometimes are not compatible with each other. zram's cold compressed page are not able to be moved out of disk. Because zram is not designed to be swap to outer disk.

If both zram and zswap are enable, there will be a problem:

 

When processes are running, here is the usual order. Lets say there is a Block/Page called blk1

1)blk1 fill the ram 

2)zram start to compress page blk1 inside the ram (note that virtual swapfile is created inside the ram not in the disk). It becomes r-blk1

3)it keeps compressing then lets say the compressed file reach the criteria to activate zswap

4)zswap start to compress page. It become s-r-blk1

5)zswap decompress and move page to disk. In the disk, it is saved as r-blk1

6)a prcoess want to read data from the ram. the ram will try to search the blk1 which was once compressed by zram, and it will try to decompress it for use. 

7)the problem comes here. Since zram only operates on the ram level. The ram will keep searching on ram's virtual swap file with the purpose to decompress it, but unable to find r-blk1 because r-blk1 was swaped to the disk.

8)what happen?

9)the system will assume the 'residual/index' of r-blk1 left on the ram's virtual swap file is the actual r-blk1. The 'residual' gets decompressed. System cannot read it.

10) zram decompression error

 

 

ps: I personally experience zram decompression failed error -22.

plz let me know if you have any thoughts on my comments

 

Feel free to email me as I am quite curious about this problem.

kevinmaplefeiyu@gmail.com

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

1 1