You are not logged in.

#1 2020-12-19 04:44:58

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Effective ZRAM Scripts and Service File

NOTE: The most up-to-date version of the scripts and service file can be found here:
zram-config.

I've been fiddling, tinkering, and experimenting with ZRAM, in various configurations for a few years now, and I think I finally hit on a very useful (for me at least) implementation. I present it here for others to test, if they like, and to give feedback.

My computer is an old 2010 Dell Latitude E6410 with 8GB of maxed RAM, hence system resources often bottleneck whenever I try to do too much at once, which with 8GB of RAM, isn't much. Utilizing a swap partition or file is highly undesirable for me, as my system grids to a crawl whenever it is utilized more than a small bit.

These scripts and their service file were created by integrating information from one very useful online article, the Arch Wiki, and my own experiments and experiences.

The two scripts go in:

/usr/local/bin

zramswap-on

#!/bin/bash

# Disable zswap
echo 0 > /sys/module/zswap/parameters/enabled

modprobe zram

#zram0
echo zstd > /sys/block/zram0/comp_algorithm
echo 4G > /sys/block/zram0/disksize
mkswap --label zram0 /dev/zram0
swapon --priority 32767 /dev/zram0

#zram1
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram1/comp_algorithm
echo 4G > /sys/block/zram1/disksize
mkswap --label zram1 /dev/zram1
swapon --priority 32767 /dev/zram1

#zram2
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram2/comp_algorithm
echo 4G > /sys/block/zram2/disksize
mkswap --label zram2 /dev/zram2
swapon --priority 32767 /dev/zram2

#zram3
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram3/comp_algorithm
echo 4G > /sys/block/zram3/disksize
mkswap --label zram3 /dev/zram3
swapon --priority 32767 /dev/zram3

zramswap-off

#!/bin/bash
swapoff /dev/zram0
swapoff /dev/zram1
swapoff /dev/zram2
swapoff /dev/zram3

echo 0 > /sys/class/zram-control/hot_remove
# Not required, but creating a blank uninitalzed drive
# after removing one may be desired
cat /sys/class/zram-control/hot_add

Make both script files executable.

The service file goes in:

/etc/systemd/system

create-zram-swap.service

[Unit]
Description=Configures zram swap device
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/zramswap-on
ExecStop=/usr/local/bin/zramswap-off
RemainAfterExit=yes

[Install]
WantedBy = multi-user.target

The only other things that I did, aside from enabling the service file, are adding the zram module to the modules section of mkinitcpio.conf, and recreating the initramfs. I'm not certain that adding the module to mkinitcpio.conf is necessary (I have't tested that just yet). I just try to cover all bases without stepping on my own toes.

The only way I've found ZRAM to really be noticeably useful is when in total it equals 2X to 2.5X total physical RAM, hence four 4GB zstd-compressed ZRAM drives, with the highest possible priority. Also, I do have a swap file, however I set it to the lowest priority, so it is basically never used (performance is hideous if it is). With just this ZRAM setup, all three installations of Arch where I've implemented it are running quite a bit smoother, so the system bogs down noticeably less often. I've tested it for about 2 to 3 weeks now, and have been sufficiently impressed with it that I thought I'd share it in hopes others with low maxed-RAM systems might find it useful. Obviously any aspect of it can be modified for your own particular systems and needs.

NOTE: I wasn't sure whether this should be best posted here or in the Programming and Scripting subforum. I am hesitant to put it here, as most other posts here seem to be actual programs, whereas I just have a silly script. ;-P

Last edited by tydynrain (2020-12-29 08:00:39)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#2 2020-12-19 07:27:52

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 557

Re: Effective ZRAM Scripts and Service File

Or use systemd-swap, disabling zswap, enabling zram and tadaa? neutral

The big problem with that script is that it doesn’t do math. tongue

Last edited by GaKu999 (2020-12-19 07:31:10)


My reposSome snippets

Heisenberg might have been here.

Offline

#3 2020-12-19 13:09:39

Everette88
Member
Registered: 2018-02-17
Posts: 41

Re: Effective ZRAM Scripts and Service File

Just ditch that homemade tools and use this: https://aur.archlinux.org/packages/zram-generator/

Offline

#4 2020-12-19 17:15:19

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

GaKu999 wrote:

Or use systemd-swap, disabling zswap, enabling zram and tadaa? neutral

The big problem with that script is that it doesn’t do math. tongue

@GaKu999,  I'm aware of, and have used systemd-swap for quite some time now, with various settings, as a part of my tinkering and experimentation. One thing I have noticed about it is that it's somewhat inconsistent. I finally disabled it when I set up the current iteration. My version seems to work notably (as in I can see and feel the difference in how my system is operating) better for me at least, and it is completely consistent (it hasn't failed to load yet across three Arch installs).

Everette88 wrote:

Just ditch that homemade tools and use this: https://aur.archlinux.org/packages/zram-generator/

@Everett88, I've heard of, but so far have not explored zram-generator. I will likely fiddle with it to see what it can do and how well.

Last edited by tydynrain (2020-12-19 17:33:12)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#5 2020-12-19 17:23:10

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,081

Re: Effective ZRAM Scripts and Service File

Out of curiosity, why do you prefer zram over zswap?
I find zswap with an userspace tool like early-oom much more convenient, maybe you do not use a swap partition?


Help me to improve ssh-rdp !

Offline

#6 2020-12-19 17:44:24

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

kokoko3k wrote:

Out of curiosity, why do you prefer zram over zswap?
I find zswap with an userspace tool like early-oom much more convenient, maybe you do not use a swap partition?

@kokoko3k, I was exploring zswap too, though since it works with a swap partition or file, and I'm doing my best NOT to use said swap partition or file, as pretty much any use of actual physical swap annihilates my system's usefulness, as it grinds to a halt. Zswap with early-oom is not something with which I have experience yet. So much to explore...

Last edited by tydynrain (2020-12-20 02:51:46)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#7 2020-12-20 06:49:48

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,081

Re: Effective ZRAM Scripts and Service File

Yes, that's why i use early-oom too smile


Help me to improve ssh-rdp !

Offline

#8 2020-12-20 15:31:44

adventurer
Member
Registered: 2014-05-04
Posts: 68

Re: Effective ZRAM Scripts and Service File

GaKu999 wrote:

Or use systemd-swap, disabling zswap, enabling zram and tadaa? neutral

I had been using that but it caused problems for me. The boot process was stuck for about 20 sec and I couldn't switch to a TTY with Ctrl-Alt-F3. I hadn't the time to properly check what exactly caused those problems (journalctl didn't present an obvious culprit - perhaps one of the sandboxing entries in systemd-swap.service).

Anyway, tydynrain's approach works well for me. I'm not sure, though, why zramswap-off is needed at all.

Offline

#9 2020-12-21 03:34:22

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

adventurer wrote:
GaKu999 wrote:

Or use systemd-swap, disabling zswap, enabling zram and tadaa? neutral

I had been using that but it caused problems for me. The boot process was stuck for about 20 sec and I couldn't switch to a TTY with Ctrl-Alt-F3. I hadn't the time to properly check what exactly caused those problems (journalctl didn't present an obvious culprit - perhaps one of the sandboxing entries in systemd-swap.service).

Anyway, tydynrain's approach works well for me. I'm not sure, though, why zramswap-off is needed at all.

That's why I started fiddling with ZRAM again after a period where I didn't think about it so much. Then I realized that the swap on my system was going a bit haywire; unstable, inconsistent. That's exactly why I started looking for another, better, reliable, and consistent method of managing ZRAM where I actually notice the difference. With this method, it works for me 100% of the time, and watching system resources, and ZRAM allocation and balance happening in a way that the system maintains and regains stability and usability more easily and quickly.

I think the larger ZRAM device sizes and the implementation of Zstandard (zstd) are what made the difference. Adjust the ZRAM device size in relation to your system's physical RAM (2-2.5X).

Regarding the reason for the zramzwap-off script, that's how the author of the original base script that I built on implemented it. It may not be necessary at all. Perhaps experiment with disabling it to to see if there is any noticeable difference.

I'm happy to hear that it worked for you.

Last edited by tydynrain (2020-12-21 03:58:15)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#10 2020-12-22 12:20:55

adventurer
Member
Registered: 2014-05-04
Posts: 68

Re: Effective ZRAM Scripts and Service File

tydynrain wrote:

I think the larger ZRAM device sizes and the implementation of Zstandard (zstd) are what made the difference. Adjust the ZRAM device size in relation to your system's physical RAM (2-2.5X).

Hm, have you done any performance tests? Because what you write does not seem to confirm the tests in that article you linked to in your first post, particularly the second test therein (Chromium 85 Compile Times Under Memory Pressure).

Btw.: I wrote in my previous post that one of the sandboxing options in systemd-swap.service might have caused the problems I ran into. Does anyone understand why it should be sandboxed at all? Sandboxing processes with network access (e.g. dnscrypt-proxy) or processes that can be used as an attack vector somehow makes absolutely sense. But what's the rationale of sandboxing systemd-swap?

Offline

#11 2020-12-23 21:59:38

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

adventurer wrote:
tydynrain wrote:

I think the larger ZRAM device sizes and the implementation of Zstandard (zstd) are what made the difference. Adjust the ZRAM device size in relation to your system's physical RAM (2-2.5X).

Hm, have you done any performance tests? Because what you write does not seem to confirm the tests in that article you linked to in your first post, particularly the second test therein (Chromium 85 Compile Times Under Memory Pressure).

Btw.: I wrote in my previous post that one of the sandboxing options in systemd-swap.service might have caused the problems I ran into. Does anyone understand why it should be sandboxed at all? Sandboxing processes with network access (e.g. dnscrypt-proxy) or processes that can be used as an attack vector somehow makes absolutely sense. But what's the rationale of sandboxing systemd-swap?

Correction: The ideal ZRAM size range seems to be between .5X to 2X of physical RAM, not 2-2.5X. In all my previous experiments with ZRAM, the sizes were notably smaller (4KB to 512MB) and I was always using some version of lz* (lzo, lzo-rle, or lz4) compression. After enabling the above setup, my system has had quite a bit (very noticeable) more long-duration stability and responsiveness, even under heavy loads. I've had 8GB swapped to ZRAM, which is equal to my system's physical RAM, and my system is still responsive. I have never had that before.

I'm now experimenting with modifying the vm.swappiness, vm.vfs_cache_pressure, vm.dirty_background_ratio, and vm.dirty_ratio values to see how that intersects with the above, based on information from these two excellent articles by the same author:


Linux Performance: Why You Should Almost Always Add Swap Space

Linux Performance: Almost Always Add Swap. Part 2: ZRAM

Last edited by tydynrain (2020-12-24 00:40:44)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#12 2020-12-26 02:13:00

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

OK, so after using and integrating the information from all the previously mentioned sources, as well as this ZRAM article at kernel.org, I've continued to tinker and refine the scripts. Lots of experimentation at the moment. I changed the name of both scripts and their service file, as now they are managing more than ZRAM as just swap.

Since implementing this second iteration on all three of my Arch installations, and testing with all sorts of activities and loads, across several different kernels (I have 11 installed presently), I have been honestly astonished observing how my system now manages its resources. Even at the highest load I've seen since testing (18+) and with multiple GB in ZRAM swap (2-8GB), the system remains responsive and generally quite smooth. I've been so excited watching it work, that I've been hesitant to post about it yet, as the very noticeable improvement I've been experiencing on my lil'ol 2010 laptop is...just wow. Also, when I'm not actively doing anything, and even with windows open, I've seen the lowest loads I've ever seen (.5-1). Now, in saying that, I make no claims or promises about what it will do. I don't know how it will work for you. I'd take it and start tweeking it to your own system. It's a starting point. I simply offer it to my community (that I deeply love) as something that has been a game-changer for me on my low-memory (8GB/7.7GB usable).

I originally implemented the kernel-parameter tweaks via 99-sysctl.conf in /etc/sysctl.d, but I changed that so that I can keep everything more self-contained within the scripts themselves, to keep it simple.

Me, being a tinkerer and experimenter, I've already been seeing how the above plays out with multiple kernels (presently playing with three different versions of the XanMod kernel patchset) compiled for my laptop's old westmere architecture, and with localmodconfig, so that I only include the modules and drivers for what my system actually has, but I digress.

I'm very curious how this might work for different people, as the script can be easily adapted. Should I update the original post with the new scripts and service file, or just keep it as is down here in the comments? Let me know, if possible.

This my Christmas present for y'all in the Arch universe! Merry Christmas!

By the way, /tmp mounted on a zstd-compressed ZRAM device is awesomeness in action.

zram-config-on:

#!/bin/bash

#ZRAM Config 

#Disable zswap
echo 0 > /sys/module/zswap/parameters/enabled

modprobe zram

#ZRAM swap
#zram0
echo zstd > /sys/block/zram0/comp_algorithm
echo 4G > /sys/block/zram0/disksize
mkswap --label zram0 /dev/zram0
swapon --priority 32767 /dev/zram0

#zram1
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram1/comp_algorithm
echo 4G > /sys/block/zram1/disksize
mkswap --label zram1 /dev/zram1
swapon --priority 32767 /dev/zram1

#zram2
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram2/comp_algorithm
echo 4G > /sys/block/zram2/disksize
mkswap --label zram2 /dev/zram2
swapon --priority 32767 /dev/zram2

#zram3
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram3/comp_algorithm
echo 4G > /sys/block/zram3/disksize
mkswap --label zram3 /dev/zram3
swapon --priority 32767 /dev/zram3

# /tmp on ZRAM
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram4/comp_algorithm
echo 6G > /sys/block/zram4/disksize
mkfs.ext4 /dev/zram4
mount /dev/zram4 /tmp
chmod 1777 /tmp

#Virtual Memory Kernel Tweaks default for low-memory systems
sysctl -w vm.swappiness=100
sysctl -w vm.vfs_cache_pressure=500
sysctl -w vm.dirty_background_ratio=1
sysctl -w vm.dirty_ratio=50

zram-config-off:

#!/bin/bash

#ZRAM Config

#ZRAM Swap
swapoff /dev/zram0
swapoff /dev/zram1
swapoff /dev/zram2
swapoff /dev/zram3
# /tmp on ZRAM
umount /dev/zram4

echo 0 > /sys/class/zram-control/hot_remove
# Not required, but creating a blank uninitalzed drive
# after removing one may be desired
cat /sys/class/zram-control/hot_add

zram-config.service:

[Unit]
Description=ZRAM Config (swap, /tmp, and kernel parameter tweaks)
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/zram-config-on
ExecStop=/usr/local/bin/zram-config-off
RemainAfterExit=yes

[Install]
WantedBy = multi-user.target

Last edited by tydynrain (2020-12-26 05:49:57)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#13 2020-12-29 07:57:13

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

OK, after a few more days of intense focus, research, experimentation, and testing (I am so flipping exhausted), I have made some more changes to the script. The last posted version worked really well on my system, except when I was trying to compile the linux-uksm kernel, which employs sphinx 3 parallel builder to create the documentation. It kept failing when sphinx was started, complaining that there was no more space left on the device (/tmp on ZRAM), even though there seemed to be plenty of room on it. I kept researching, tweaking various parameters and testing, until I finally got it working with linux-uksm kernel once again.

This is what I added to allow the linux-uksm kernel to be built again:

mount -t tmpfs -o size=0,nr_inodes=0 tmpfs /tmp

The zram device was still being given a size of 8GB. Now this worked, but it was not a good solution, as with both size and nr_inodes set to zero,which is no limit, an errant process could run away with resources and the system would completely lock up. So once I got it working, I started to test the parameters between system lockup and failed kernel build. Today I finally found good parameters for my system, where everything seems to work well. This 10-year-old laptop feels new (it definitely doesn't look new, however).

mount -t tmpfs -o size=75%,nr_inodes=524288 tmpfs /tmp

I have a lot of references to post, as I've been reading myself silly and frying my brain putting it all together. The changes were in the zram-config-on script:

#!/bin/bash

#ZRAM Config 

#Disable zswap
echo 0 > /sys/module/zswap/parameters/enabled

modprobe zram

#swap on ZRAM
#zram0
echo zstd > /sys/block/zram0/comp_algorithm
echo 4G > /sys/block/zram0/disksize
echo 1873M > /sys/block/zram0/mem_limit
mkswap --label zram0 /dev/zram0
swapon --priority 32767 /dev/zram0

#zram1
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram1/comp_algorithm
echo 4G > /sys/block/zram1/disksize
echo 1873M > /sys/block/zram1/mem_limit
mkswap --label zram1 /dev/zram1
swapon --priority 32767 /dev/zram1

#zram2
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram2/comp_algorithm
echo 4G > /sys/block/zram2/disksize
echo 1873M > /sys/block/zram2/mem_limit
mkswap --label zram2 /dev/zram2
swapon --priority 32767 /dev/zram2

#zram3
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram3/comp_algorithm
echo 4G > /sys/block/zram3/disksize
echo 1873M > /sys/block/zram3/mem_limit
mkswap --label zram3 /dev/zram3
swapon --priority 32767 /dev/zram3

# /tmp on ZRAM
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram4/comp_algorithm
echo 8G > /sys/block/zram4/disksize
mkfs.ext4 /dev/zram4
#mount -t tmpfs -o size=0,nr_inodes=0 tmpfs /tmp
mount -t tmpfs -o size=75%,nr_inodes=524288 tmpfs /tmp
mount /dev/zram4 /tmp
chmod 1777 /tmp
 
#Virtual Memory Kernel Tweaks default for low-memory systems)
# swappiness is optimal between 80-100, depending on load.
sysctl -w vm.swappiness=80
sysctl -w vm.vfs_cache_pressure=500
sysctl -w vm.dirty_background_ratio=1
sysctl -w vm.dirty_ratio=50
sysctl -w vm.page-cluster=0

The commented out /tmp mount is what I got it to work, then I began finding a practical limit that worked. Hence my particular values. Adjust to your system. I also lowered swappiness to 80, which seems to work well. I also put a memory limit on each zram device, to help lower the potential of system lockup, and I added page-cache/page-cluster, which should help with responsiveness.

I am sure there will be many more edits to this, but this version works consistently well on my system. I welcome advice, suggestions, and (hopefully gentle) criticisms. I am still learning. I learn fast, though.

The references that I used to create and refine these scripts are here. aside from the Arch Wiki:

https://linuxreviews.org/Zram

https://haydenjames.io/linux-performanc … wap-space/

https://haydenjames.io/linux-performanc … wap-space/

https://www.kernel.org/doc/Documentatio … v/zram.txt

https://www.kernel.org/doc/Documentatio … /tmpfs.txt

https://wiki.gentoo.org/wiki/Zram

https://wiki.gentoo.org/wiki/Portage_TMPDIR_on_tmpfs

https://wiki.gentoo.org/wiki/Talk:Porta … R_on_tmpfs

https://www.agix.com.au/set-inode-limit … at-server/

https://man7.org/linux/man-pages/man5/proc.5.html

https://unix.stackexchange.com/question … filesystem

https://hackaday.com/2020/05/20/zram-bo … rformance/

https://github.com/ecdye/zram-config

Finally, here is a cloud storage link to the most up-to-date version:

zram-config

I am going to sleep now. So tired. It was worth it. I hope it's useful for other archers.


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#14 2020-12-29 13:09:05

adventurer
Member
Registered: 2014-05-04
Posts: 68

Re: Effective ZRAM Scripts and Service File

Thanks for your update. I still have to read all those references in order to understand what, e.g., mem_limit exactly means.

Regarding performance, I can't notice any significant differences on my system. But this might be due to the fact that it has 16 GB RAM anyhow.

What irritates me is that I'm running into trouble whenever I use the lines for "/tmp on ZRAM". The SDDM login screen is never reached but I can switch to a TTY via Ctrl-Alt-F3. Without those lines your code runs very smoothly.

Offline

#15 2020-12-29 17:55:22

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

adventurer wrote:

Thanks for your update. I still have to read all those references in order to understand what, e.g., mem_limit exactly means.

Regarding performance, I can't notice any significant differences on my system. But this might be due to the fact that it has 16 GB RAM anyhow.

What irritates me is that I'm running into trouble whenever I use the lines for "/tmp on ZRAM". The SDDM login screen is never reached but I can switch to a TTY via Ctrl-Alt-F3. Without those lines your code runs very smoothly.

@adventurer, here is a bit on mem_limit from the zram document at kernel.org:

Set memory limit: Optional
Set memory limit by writing the value to sysfs node 'mem_limit'.
The value can be either in bytes or you can use mem suffixes.
In addition, you could change the value in runtime.
Examples:

 
	# limit /dev/zram0 with 50MB memory
	echo $((50*1024*1024)) > /sys/block/zram0/mem_limit

	# Using mem suffixes
	echo 256K > /sys/block/zram0/mem_limit
	echo 512M > /sys/block/zram0/mem_limit
	echo 1G > /sys/block/zram0/mem_limit

	# To disable memory limit
	echo 0 > /sys/block/zram0/mem_limit

I'm still testing to see if this option is still fully necessary. Under most situations it wouldn't be. It's more of a safeguard. Most of the time I've been running the script I've done so without the limit.

Yes, given that your system has twice the physical RAM that mine does, I would figure that you wouldn't notice it as much as I do on my system with greater memory-constraints. I'd definitely adjust the values to fit your system's resources.

That's very curious about the #swap on ZRAM comment, as it shouldn't be read at all as long as the # is in front of it. I use sddm as well.

Last edited by tydynrain (2020-12-29 17:57:02)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#16 2020-12-29 18:31:02

adventurer
Member
Registered: 2014-05-04
Posts: 68

Re: Effective ZRAM Scripts and Service File

Thanks for your reply!

tydynrain wrote:

That's very curious about the #swap on ZRAM comment, as it shouldn't be read at all as long as the # is in front of it. I use sddm as well.

Ah - no, what I meant: If I use these lines of your code:

# /tmp on ZRAM
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram4/comp_algorithm
echo 8G > /sys/block/zram4/disksize
mkfs.ext4 /dev/zram4
#mount -t tmpfs -o size=0,nr_inodes=0 tmpfs /tmp
mount -t tmpfs -o size=75%,nr_inodes=524288 tmpfs /tmp
mount /dev/zram4 /tmp
chmod 1777 /tmp

I'm having those troubles. After commenting them all is well. I couldn't find anything in journalctl that might cause that.

Offline

#17 2020-12-31 04:43:55

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

adventurer wrote:

Thanks for your reply!



tydynrain wrote:

That's very curious about the #swap on ZRAM comment, as it shouldn't be read at all as long as the # is in front of it. I use sddm as well.

Ah - no, what I meant: If I use these lines of your code:

# /tmp on ZRAM
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram4/comp_algorithm
echo 8G > /sys/block/zram4/disksize
mkfs.ext4 /dev/zram4
#mount -t tmpfs -o size=0,nr_inodes=0 tmpfs /tmp
mount -t tmpfs -o size=75%,nr_inodes=524288 tmpfs /tmp
mount /dev/zram4 /tmp
chmod 1777 /tmp

I'm having those troubles. After commenting them all is well. I couldn't find anything in journalctl that might cause that.

Of course. I want anyone that tries this out to have a good experience.

Hrmm...I've been tweaking this setting a bit lately, with more tests. I've been considering running some tests where I use mem_limit on the zram device before I mount it to /tmp, hence removing the need to constrain it with those two settings.


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#18 2020-12-31 05:14:03

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

More testing, more tweaks. I got failures while building the behemoth of that is the mainline kernel, which also uses the sphinx builder for documentation. This kernel had not previously given me issues, but I also hadn't tried to build it since implementing this way of managing virtual memory. I upped the size /tmp to 10GB, kept size=75%, and put nr_inodes=655360. This worked, and very well. Very little was pushed into swap (zram) until some strong cpu and memory spikes as it continued building. I was getting slightly concerned watching the resources, especially memory, at 2 or 3 points, but each time, just as it looked like memory might be exhausted, what was in memory was unloaded to swap, and quickly, with very good compression. The /tmp directory got to 70% full at maximum, and I had 6.5GB of data compressed to 2.1 GB. It succeeded better than I had expected.

The new settings  for the # swap on ZRAM section, which works well on my specific system, is the following.

# /tmp on ZRAM
cat /sys/class/zram-control/hot_add
echo zstd > /sys/block/zram4/comp_algorithm
echo 10G > /sys/block/zram4/disksize
mkfs.ext4 /dev/zram4
#mount -t tmpfs -o size=0,nr_inodes=0 tmpfs /tmp
mount -t tmpfs -o size=75%,nr_inodes=655360 tmpfs /tmp
mount /dev/zram4 /tmp
chmod 1777 /tmp

The script has been updated in the MEGA share. Just remember to change the permissions.

As I mentioned in my previous reply, I will be testing a setup where I use mem_limit on the zram device before I mount it on /tmp. Doing it this way keeps the memory management of all zram devices uniform. I'll post more after further testing.

Last edited by tydynrain (2021-01-05 09:09:17)


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#19 2021-01-02 08:17:15

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

The present version of the script has been working so well for me across my three Arch installs, handling very well whatever I toss at it, that I haven't really had any need to test other parameters yet, though I'm sure I will at some point soon. I've been compiling lots of system-tuned kernels, and the present parameters work for everything. I love compiling in zstd-compressed /tmp in zram .

One of the really cool things I've continued to notice is that when the system is not actively doing something, just running my usual background stuff, and in KDE Plasma, I've seen the lowest load I've ever seen, .23, which is astounding.

This script has, and continues to make a huge, very tangible difference on my system, and with tweaks to the values it could easily be adapted to systems with greater or fewer resources.


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#20 2021-01-02 21:50:47

xerxes_
Member
Registered: 2018-04-29
Posts: 328

Re: Effective ZRAM Scripts and Service File

For me your ZRAM setup looks too complicated; I don't understand why it have to be so complicated.

I tried ZRAM setup some time ago when I have only 2GB of RAM in system. I setup one big 3GB zram0 with lz4 compression.
It was good until I don't use it too much. If I use something more than 500MB of ZRAM I had sometimes stalled system when browsing heavy sites (for something about 20 sec. I couldn't do anything). Also ZRAM hardly recover it's used space.

Next I tried ZSWAP and it was better for me: never had so long stalled system and it better recover it's space. It also limit hdd i/o when using swap. So I ended  with zsawp and bought another 2GB of RAM to even less use zswap.

Last edited by xerxes_ (2021-01-02 21:56:09)

Offline

#21 2021-01-03 22:28:37

tydynrain
Member
From: Lower Puna, Big Island Hawai'i
Registered: 2017-10-26
Posts: 111
Website

Re: Effective ZRAM Scripts and Service File

xerxes_ wrote:

For me your ZRAM setup looks too complicated; I don't understand why it have to be so complicated.

I tried ZRAM setup some time ago when I have only 2GB of RAM in system. I setup one big 3GB zram0 with lz4 compression.
It was good until I don't use it too much. If I use something more than 500MB of ZRAM I had sometimes stalled system when browsing heavy sites (for something about 20 sec. I couldn't do anything). Also ZRAM hardly recover it's used space.

Next I tried ZSWAP and it was better for me: never had so long stalled system and it better recover it's space. It also limit hdd i/o when using swap. So I ended  with zsawp and bought another 2GB of RAM to even less use zswap.

@xeres_, None of the previous methods and scripts I had tried out for while actually worked the way I wanted, regardless of how simple or complex they were. I made this script only as complex as I needed to do what I wanted it to do, with the knowledge and understanding that I presently have. I'm still learning, and will modify thing to be as simple as possible as I come to understand the frameworks in which I'm working more through time.

As it stands now, this script has made the largest, most noticeable difference in the performance of my system that I've experienced, on par with adding more RAM. I'm simply suggesting that there may be others who could benefit from it as well. It can and should be adapted to each specific system.


Registered Linux User: #623501 | Arch Linux Principles: Simplicity - Modernity - Pragmatism - User Centrality - Versatility => KISS
Arch Linux, the most exciting thing since Linus created Linux and married it with GNU/GPL.
Arch Linux for Life, Arch Linux Forever!

Offline

#22 2021-01-04 23:07:02

xerxes_
Member
Registered: 2018-04-29
Posts: 328

Re: Effective ZRAM Scripts and Service File

@tydynrain
So, how many ZRAMs do you have now (4, 5 or more)? How big they are and how much full they can be? Did they recover from that full state to free state, when you have free space in RAM? Do you notice any system hang/stall when ZRAM is heavy used or full of data? What will happen when you exceed your ZRAMs capacity? What compression algorithm do you recommend?

Sorry for so much questions, but it is interesting for me if and how you really managed to configure ZRAM so well, how you say.

Offline

Board footer

Powered by FluxBB