diff options
author | Aaron Ball <nullspoon@oper.io> | 2021-07-14 17:34:52 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2021-07-14 17:35:49 -0600 |
commit | 4deaadff1d559659fc5fc94c3093f4e0aebbda39 (patch) | |
tree | fae1d8eb48da22c555a4f59c9c0cefe7b753fe33 | |
parent | a4d0f5ecae14240ce72df4074bbf39553dedbd83 (diff) | |
download | oper.io-4deaadff1d559659fc5fc94c3093f4e0aebbda39.tar.gz oper.io-4deaadff1d559659fc5fc94c3093f4e0aebbda39.tar.xz |
Add post: linux:using zram
-rw-r--r-- | posts/index.adoc | 3 | ||||
-rw-r--r-- | posts/linux:using-zram.rst | 94 |
2 files changed, 96 insertions, 1 deletions
diff --git a/posts/index.adoc b/posts/index.adoc index 47d6fc0..601a259 100644 --- a/posts/index.adoc +++ b/posts/index.adoc @@ -7,6 +7,7 @@ Home [role="index"] New Posts --------- +* link:?p=linux:using-zram[Linux:Using ZRam] * link:?p=Circumventing_MacOS_FileVault_Autologin_Restrictions[Circumventing MacOS FileVault Autologin Restrictions] * link:?p=bash:handy_bashrc_bits[Bash:Handy Bashrc Bits] * link:?p=librem5-cant-update-404-not-found[Librem 5:Can't Update - 404 Not found] @@ -19,7 +20,6 @@ New Posts * link:?p=how_to_uninterest_me_in_your_job_opening[How to Uninterest Me in Your Job Opening] * link:?p=why-linux-is-hard[Why Linux is Hard] * link:?p=checking-dnsbl-lists[Checking DNSBL Lists] -* link:?p=linux:non-root-suspend[Linux:Non-root Suspend] [role="index"] Purism Librem 5 @@ -32,6 +32,7 @@ Purism Librem 5 [role="index"] Linux ----- +* link:?p=linux:using-zram[Linux:Using ZRam] * link:?p=why-linux-is-hard[Why Linux is Hard] * link:?p=linux:manual_restart_with_sysrq-trigger[Linux:Manual Restart with sysrq-trigger] * link:?p=crux_linux:faster_builds[Crux Linux:Faster Builds] diff --git a/posts/linux:using-zram.rst b/posts/linux:using-zram.rst new file mode 100644 index 0000000..93938a1 --- /dev/null +++ b/posts/linux:using-zram.rst @@ -0,0 +1,94 @@ +Linux:Using ZRam +================= + +I recently discovered zram in Linux, and it felt like a mystical, futuristic +experience. Upon discovering it and learning how to use it, I immediately went +looking for uses. + + +What is Zram? +------------- + +`Zram <https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html>`_ +is a feature of the Linux kernel (``modprobe zram``) that allows the user to +create "compressed RAM-based block devices". + +If that doesn't make much sense, it is very similar to RAM drives, but +compressed. I learned about using RAM drives a few years ago (``mount -t tmpfs +...``) and have been using them for all kinds of things on my systems to reduce +IO load on my storage, while maximizing performance. With RAM drives you +section off a portion of your RAM to use as "storage" [with a read/write speed +of ~6 GB/s]. ZRam is very similar, except that it compresses inline, making +even more "storage" available at the cost of less RAM, but slightly higher CPU. + + +How can we use zram? +-------------------- + +A package compiling layer (like /var/pkgmk) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Previously, I did this (in fstab, but here's the command) + +.. code-block:: sh + + mount -t tmpfs -o size=10G none /var/pkgmk + +That creates a 10 GB RAM device, mounted at ``/var/pkgmk``, where I compile +packages from source on my system (any Crux users out there?). + +NOW however, I do this: + +.. code-block:: sh + + dev=$(zramctl -f) # Find the first available zram device + zramctl "${dev}" -s 16384 -t $(( $(nproc) / 2 )) + mkfs.ext4 "${dev}" + mount "${dev}" /var/pkgmk + +It's a bit more complicated to get set up (I have the above in a script), but +the end result is a 16 GB RAM drive, which takes significantly less ram, using +up to half of my CPU's cores to compress. I tend to see about 70% compression +when compiling source code, so while the drive allows for 16 GB of capacity +(the full amount in my laptop in fact), at its maximum it only takes about 5 +GB; usually much less. + + +zswap (swapon... swapoff... swapon... swapoff) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +I haven't used swap on my system in a long time. With computers coming with +more and more RAM, I haven't needed it in a long time. However, I recently ran +into some compiling tasks that, combined with my ram drive for storing +compiling source code, consumes more RAM than my laptop has. + +Enter ``zswap`` + +Zswap is basically the same thing as a zram block device, except instead of +being formatted with a real filesystem, it is formatted for swap usage. To set +one of these up, I do this (again, this is distilled from the script I wrote): + +.. code-block:: sh + + dev=$(zramctl -f) # Find the first available zram device + zramctl "${dev}" -s 10240 -t $(( $(nproc) / 2 )) + mkswap "${dev}" + swapon "${dev}" + +That will create a new zram block device that is 10 GB in size, using up to +half of my CPU's cores for compression. This zram device is then formatted for +swap usage (``mkswap``), and enabled (``swapon``). + + + +Conclusion +---------- + +These commands obviously need to be put into a nice shell script for easy setup +and teardown. I have my in an init script, supporting ``start``, ``stop``, and +``status`` commands to make things less cumbersome. You can see the full init +script I wrote for zswap setup over `here +<https://oper.io/src/nullspoon/zram-swap.git/>`_. + +Do you have any other ideas for how to use zram? If so, feel free to email me +any ideas you might come up with. I'd love to hear them! |