Enable the magical Multi-Gen LRU AKA MGLRU on openSUSE Tumblewed, stay away from OOM today!
Tags: linux, opensource, computerscience, productivity
Published: 2023-03-31 22:38:01

Why **[MGLRU](https://www.kernel.org/doc/html/next/admin-guide/mm/multigen_lru.html)** is so special? First, we need to know that there is an algorithm for managing the page cache in the Linux kernel. It works by keeping track of which pages have been used most recently and which ones have not. When the system needs to free up memory, it selects the page that has been used the least recently and swaps it out to disk. This is called _LRU_ algorithm, which stands for _Least Recently Used_ algorithm, used in the memory management system.

However, as we know it, the **[traditional active/inactive LRU algorithm](https://www.kernel.org/doc/gorman/html/understand/understand013.html)** used in Linux kernel is not doing a very good job. The common issue is high CPU usage in OOM situations. Therefore, we need a better LRU algorithm, MGLRU? **[According to Google](https://lore.kernel.org/lkml/20220104202247.2903702-1-yuzhao@google.com/T/#m8fd2a29bc557d27d1000f837f65b6c930eef9dff:~:text=Large%2Dscale%20deployments%0A%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%2D%0AWe%27ve,at%20the%2050th%20percentile.)**, tested on tens of millions of ChromeOS users and about a million Android users, MGLRU shows an overall 40% decrease in **[kswapd](https://www.kernel.org/doc/gorman/html/understand/understand013.html#:~:text=Pageout%20Daemon%20(kswapd))** CPU usage, in addition to improvements in other UX metrics, e.g., an 85% decrease in the number of low-memory kills and an 18% decrease in rendering latency.

To put it simply, just by enabling the magical MGLRU, you can save yourself to some extent from the OOM kills today! 

> Note, you need to use Linux kernel 6.1 or above to enable this feature. As of this writing, openSUSE Tumbleweed runs on Linux kernel 6.2.8. It's an advantage of running on a rolling release, you don't have to wait for months or even years for some major improvements.

> Linux kernel 6.3 will bring further improvements to MGLRU also. See **[the news on Phoronix](https://www.phoronix.com/news/Linux-6.3-MM)**.

---

## Enable MGLRU

![Abstract](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3wf0ljcvls1guv82852.jpg)

1. Check whether your kernel enable `CONFIG_LRU_GEN`. You'll need to know your current kernel name in _GNOME about page_ in your system settings. Or run `uname -r` in the terminal. Then, go to `/boot` and copy your kernel config's file name. For example, `config-6.2.8-1-default` which is my current kernel config file. Open the terminal and type:

     ```
cat /boot/config-6.2.8-1-default | grep CONFIG_LRU_GEN
     ```
     &nbsp;
     If this returns `y`, your kernel supports `MGLRU`, you can proceed to the next step.
     &nbsp;
2. Open plain text editor, entering this service file code and save it as `mglru.service`:

     ```
[Unit]
Description=Multi-Gen LRU Enabler Service
ConditionPathExists=/sys/kernel/mm/lru_gen/enabled
[Service]
Type=oneshot
# Turn on MGLRU, valid values: [0; 7] and [yYnN]
ExecStart=/bin/bash -c "/bin/echo y > /sys/kernel/mm/lru_gen/enabled"
# Set the thrashing prevention vaule in milliseconds, valid values: >= 0
ExecStartPost=/bin/bash -c "/bin/echo 1000 > /sys/kernel/mm/lru_gen/min_ttl_ms"
[Install]
WantedBy=default.target
     ```
     &nbsp;
3. Move the `mglru.service` file to `/etc/systemd/system` using _GNOME Files_ in admin mode by using `nautilus admin:///etc/systemd/system` command.

4. Open _YaST Services Manager_ and enable the service to **run on boot**.

5. Reboot the system.

6. Check whether MGLRU is enabled on your system by:

     ```
cat /sys/kernel/mm/lru_gen/enabled
     ```
     &nbsp;
     This should return `0x0007`. If it returns `0x0000`, this means it's not enabled yet.

     ```
cat /sys/kernel/mm/lru_gen/min_ttl_ms
     ```
     &nbsp;
     This should return `1000` for the optimal thrashing prevention.

---

There's **[an open issue](https://bugzilla.suse.com/show_bug.cgi?id=1206937)** to enable MGLRU by default on openSUSE. If you're interesting, you can participate by submitting your test results. See **[comment #10](https://bugzilla.suse.com/show_bug.cgi?id=1206937#c10)** in the issue thread for more details.

---

**Cover** Photo by **[Resource Database](https://unsplash.com/@resourcedatabase?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)** on **[Unsplash](https://unsplash.com/photos/TIUyoNGM0no?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)**

**Abstract** Photo by **[Resource Database](https://unsplash.com/@resourcedatabase?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)** on **[Unsplash](https://unsplash.com/photos/GaBX_ooh8-g?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)**