summaryrefslogtreecommitdiff
path: root/posts/linux:using-zram.rst
blob: 93938a1c5deaddd9f57db295186206e9202d21d9 (plain)
    1 Linux:Using ZRam
    2 =================
    3 
    4 I recently discovered zram in Linux, and it felt like a mystical, futuristic
    5 experience. Upon discovering it and learning how to use it, I immediately went
    6 looking for uses.
    7 
    8 
    9 What is Zram?
   10 -------------
   11 
   12 `Zram <https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html>`_
   13 is a feature of the Linux kernel (``modprobe zram``) that allows the user to
   14 create "compressed RAM-based block devices".
   15 
   16 If that doesn't make much sense, it is very similar to RAM drives, but
   17 compressed. I learned about using RAM drives a few years ago (``mount -t tmpfs
   18 ...``) and have been using them for all kinds of things on my systems to reduce
   19 IO load on my storage, while maximizing performance. With RAM drives you
   20 section off a portion of your RAM to use as "storage" [with a read/write speed
   21 of ~6 GB/s]. ZRam is very similar, except that it compresses inline, making
   22 even more "storage" available at the cost of less RAM, but slightly higher CPU.
   23 
   24 
   25 How can we use zram?
   26 --------------------
   27 
   28 A package compiling layer (like /var/pkgmk)
   29 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   30 
   31 Previously, I did this (in fstab, but here's the command)
   32 
   33 .. code-block:: sh
   34 
   35    mount -t tmpfs -o size=10G none /var/pkgmk
   36 
   37 That creates a 10 GB RAM device, mounted at ``/var/pkgmk``, where I compile
   38 packages from source on my system (any Crux users out there?).
   39 
   40 NOW however, I do this:
   41 
   42 .. code-block:: sh
   43 
   44    dev=$(zramctl -f)  # Find the first available zram device
   45    zramctl "${dev}" -s 16384 -t $(( $(nproc) / 2 ))
   46    mkfs.ext4 "${dev}"
   47    mount "${dev}" /var/pkgmk
   48 
   49 It's a bit more complicated to get set up (I have the above in a script), but
   50 the end result is a 16 GB RAM drive, which takes significantly less ram, using
   51 up to half of my CPU's cores to compress. I tend to see about 70% compression
   52 when compiling source code, so while the drive allows for 16 GB of capacity
   53 (the full amount in my laptop in fact), at its maximum it only takes about 5
   54 GB; usually much less.
   55 
   56 
   57 zswap (swapon... swapoff... swapon... swapoff)
   58 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   59 
   60 I haven't used swap on my system in a long time. With computers coming with
   61 more and more RAM, I haven't needed it in a long time. However, I recently ran
   62 into some compiling tasks that, combined with my ram drive for storing
   63 compiling source code, consumes more RAM than my laptop has.
   64 
   65 Enter ``zswap``
   66 
   67 Zswap is basically the same thing as a zram block device, except instead of
   68 being formatted with a real filesystem, it is formatted for swap usage. To set
   69 one of these up, I do this (again, this is distilled from the script I wrote):
   70 
   71 .. code-block:: sh
   72 
   73    dev=$(zramctl -f)  # Find the first available zram device
   74    zramctl "${dev}" -s 10240 -t $(( $(nproc) / 2 ))
   75    mkswap "${dev}"
   76    swapon "${dev}"
   77 
   78 That will create a new zram block device that is 10 GB in size, using up to
   79 half of my CPU's cores for compression. This zram device is then formatted for
   80 swap usage (``mkswap``), and enabled (``swapon``).
   81 
   82 
   83 
   84 Conclusion
   85 ----------
   86 
   87 These commands obviously need to be put into a nice shell script for easy setup
   88 and teardown. I have my in an init script, supporting ``start``, ``stop``, and
   89 ``status`` commands to make things less cumbersome. You can see the full init
   90 script I wrote for zswap setup over `here
   91 <https://oper.io/src/nullspoon/zram-swap.git/>`_.
   92 
   93 Do you have any other ideas for how to use zram? If so, feel free to email me
   94 any ideas you might come up with. I'd love to hear them!

Generated by cgit