Skip to content

Instantly share code, notes, and snippets.

@ahydronous
Last active December 3, 2024 13:49
Show Gist options
  • Save ahydronous/7ceaa00df96ef99131600edd4c2c73f2 to your computer and use it in GitHub Desktop.
Save ahydronous/7ceaa00df96ef99131600edd4c2c73f2 to your computer and use it in GitHub Desktop.
# Virtual memory settings.
# Kernel documentation: https://docs.kernel.org/admin-guide/sysctl/vm.html.
# Arch zram: https://wiki.archlinux.org/title/zram#Optimizing_swap_on_zram.
# Gaming tuning: https://pastebin.com/fwzW9whL.
# PopOS tuning: https://github.com/pop-os/default-settings/pull/163.
# MaxPerformanceWizard (MPW) https://gitlab.com/cscs/maxperfwiz/-/blob/master/maxperfwiz.
boot.kernel.sysctl = {
# Tunes how aggressively kernel evicts memory pages until a specific amount of free memory is left for your active working set
## Sweet spot for gaming is 125-200, near 200 makes kswapd swap too aggressive
"vm.watermark_scale_factor" = 125;
# The value simply represents the kernel's tendency to swap out anonymous memory pages relative to other pages, such as file ones
# Most working sets have a ~5:1 ratio of anonymous pages vs. other pages, and we don't want to swap out anonymous pages in favor of other pages
# We also want many Transparent Huge Pages (THPs), swapping those out is detrimental for gaming performance
## THPs also don't get reduced to normal size on swap, which hinders zram compression
# This value should be ~200 for workstation workloads
"vm.swappiness" = 40;
# Watermark boosting pre-emptively frees up memory to prevent a page allocation miss
# Broken feature that causes page thrashing, disabled by setting to "0"
# https://groups.google.com/g/linux.debian.user/c/YcDYu-jM-to
"vm.watermark_boost_factor" = 0;
# As percentage of memory, number of free + reclaimable pages at which background kernel flusher threads will start writing out dirty data.
# From MPW: 8GB = 3, bytewise above that, should amount to 50MB-400MB
# vm.dirty_background_bytes is the bytewise counterpart to this, only one of the two can be set
/*"vm.dirty_background_ratio" = 2;*/
"vm.dirty_background_bytes" = 209715200;
# As percentage of memory, number of free + reclaimable pages at which a process will start writing out its dirty data.
# From MPW: 8GB = 6, >12GB should be set bytewise, should amount to 100MB-600MB.
# vm.dirty_bytes is the bytewise counterpart to this, only one of the two can be set.
# "btrfs" filesystems requires "268435456" minimum or performance will tank.
/*"vm.dirty_ratio" = 3;*/
"vm.dirty_bytes" = 419430400;
# Lowering it from the default value of 100 makes the kernel less inclined to reclaim VFS cache, improved responsiveness
# "66" is between Arch (50) and Maxperfwiz (75) optimum, "0" causes Out of Memory errors
"vm.vfs_cache_pressure" = 66;
# Tunes when data is eligble to be written out by kernel flusher, per 10 milliseconds
# From MPW: SSD = 500, HDD/SD = 3000
"vm.dirty_expire_centisecs" = 500;
# Kernel flusher sleep timer, per 10 milliseconds
# From MPW: SSD = 250, HDD/SD = 1500
"vm.dirty_writeback_centisecs" = 250;
# Convention says KiB of RAM * 0.01
# From MPW: RAM / num_of_logical_cores * 0.058
# 16777216 KiB / 8 threads * 0.058 = 121634
"vm.min_free_kbytes" = 121634;
# Amount of pages to swap in/out
# Logarithmic, "3" = 8, 8 pages is default but tuned for SSD/HDD swap
# "0" notably decreases latency with RAM-based swap
"vm.page-cluster" = 0;
# This is for compatibility with certain games
# SteamOS default
# https://fedoraproject.org/wiki/Changes/IncreaseVmMaxMapCount
"vm.max_map_count"= 2147483642;
};
@fiftydinar
Copy link

According to the MPW, you should account core threads instead of just cores with vm.min_free_kbytes

They use nproc to determine number of core threads.

On my Ryzen 5 5600X, I get 12, as expected.

@ahydronous
Copy link
Author

Good point!

Since it seems more people are looking at the gist, here are the sources I used:

Kernel documentation: https://docs.kernel.org/admin-guide/sysctl/vm.html
Arch zram: https://wiki.archlinux.org/title/zram#Optimizing_swap_on_zram
Gaming tuning: https://pastebin.com/fwzW9whL
PopOS tuning: pop-os/default-settings#163
MaxPerformanceWizard (MPW) https://gitlab.com/cscs/maxperfwiz/-/blob/master/maxperfwiz

The jury is still kind of out on vm.swappiness, because of the way Transparent Huge Pages work. Will update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment