Skip to content

Instantly share code, notes, and snippets.

@l0k18
Created April 18, 2021 00:28
Show Gist options
  • Save l0k18/857a1bdd7265fde1e280a6c137b3fbbd to your computer and use it in GitHub Desktop.
Save l0k18/857a1bdd7265fde1e280a6c137b3fbbd to your computer and use it in GitHub Desktop.
script for increasing linux memory caching

The following is a bundle of sysctl and other things to make linux keep more data from disk in memory.

The last line specifically precaches a huge amount of file data immediately to memory, you should use du -h -d0 /path/to/somewhere' to find out how big the paths you set for this. A typical /usr` folder on current linux will be around 7gb, and my go compilation cache sits at around 11gb, which is comfortable in 32gb and it definitely speeds things up.

#!/bin/bash
modprobe bfq
for d in /sys/block/nvme0*
do
        # HDD (tuned for Seagate SMR drive)
        echo bfq > "$d/queue/scheduler"
        echo 4 > "$d/queue/nr_requests"
        echo 32000 > "$d/queue/iosched/back_seek_max"
        echo 3 > "$d/queue/iosched/back_seek_penalty"
        echo 80 > "$d/queue/iosched/fifo_expire_sync"
        echo 1000 > "$d/queue/iosched/fifo_expire_async"
        echo 5300 > "$d/queue/iosched/slice_idle_us"
        echo 1 > "$d/queue/iosched/low_latency"
        echo 200 > "$d/queue/iosched/timeout_sync"
        echo 0 > "$d/queue/iosched/max_budget"
        echo 1 > "$d/queue/iosched/strict_guarantees"

        # additional tweaks for SSD (tuned for Samsung EVO 850):
        if test $(cat "$d/queue/rotational") = "0"
        then
                echo 36 > "$d/queue/nr_requests"
                echo 1 > "$d/queue/iosched/back_seek_penalty"
                # slice_idle_us should be ~ 0.7/IOPS in µs
                echo 16 > "$d/queue/iosched/slice_idle_us"
                echo 10 > "$d/queue/iosched/fifo_expire_sync"
                echo 250 > "$d/queue/iosched/fifo_expire_async"
                echo 10 > "$d/queue/iosched/timeout_sync"
                echo 0 > "$d/queue/iosched/strict_guarantees"
        fi
	
	echo 9 > /proc/sys/vm/swappiness
	echo 10 > /proc/sys/vm/vfs_cache_pressure
	echo 99 > /proc/sys/vm/dirty_ratio
	echo 50 > /proc/sys/vm/dirty_background_ratio
	echo 18000 > /proc/sys/vm/dirty_expire_centisecs
	echo 18000 > /proc/sys/vm/dirty_writeback_centisecs
	nice find /home/loki/.cache/go-build /usr -type f -print0 | nice ionice -c 3 wc -l --files0-from - > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment