Enable the magical Multi-Gen LRU AKA MGLRU on openSUSE Tumblewed, stay away from OOM today!

archerallstars

Archer Allstars

Posted on March 31, 2023

Enable the magical Multi-Gen LRU AKA MGLRU on openSUSE Tumblewed, stay away from OOM today!

Why MGLRU 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 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, tested on tens of millions of ChromeOS users and about a million Android users, MGLRU shows an overall 40% decrease in 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.


Enable MGLRU

Abstract

  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

      
     If this returns `y`, your kernel supports `MGLRU`, you can proceed to the next step.
      
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


Enter fullscreen mode Exit fullscreen mode
  
Enter fullscreen mode Exit fullscreen mode
  1. Move the mglru.service file to /etc/systemd/system using GNOME Files in admin mode by using nautilus admin:///etc/systemd/system command.

  2. Open YaST Services Manager and enable the service to run on boot.

  3. Reboot the system.

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

    
    

cat /sys/kernel/mm/lru_gen/enabled

      
     This should return `0x0007`. If it returns `0x0000`, this means it's not enabled yet.

     ```


cat /sys/kernel/mm/lru_gen/min_ttl_ms


Enter fullscreen mode Exit fullscreen mode
  
 This should return `1000` for the optimal thrashing prevention.
Enter fullscreen mode Exit fullscreen mode

There's an open issue to enable MGLRU by default on openSUSE. If you're interesting, you can participate by submitting your test results. See comment #10 in the issue thread for more details.


Cover Photo by Resource Database on Unsplash

Abstract Photo by Resource Database on Unsplash

💖 💪 🙅 🚩
archerallstars
Archer Allstars

Posted on March 31, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related