Skip to content

Instantly share code, notes, and snippets.

@marianposaceanu
Last active January 30, 2024 06:47
Show Gist options
  • Star 76 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save marianposaceanu/5629740 to your computer and use it in GitHub Desktop.
Save marianposaceanu/5629740 to your computer and use it in GitHub Desktop.
Linux simple performance tweaks

Linux simple performance tweaks

Change the I/O Scheduler

Open $ vim /etc/default/grub then add elevator=noop next to GRUB_CMDLINE_LINUX_DEFAULT. Run $ update-grub and $ cat /sys/block/sda/queue/scheduler to be sure that noop is being used:

$ vim /etc/default/grub
$ update-grub
$ cat /sys/block/sda/queue/scheduler
[noop] deadline cfq

noop : a trivial scheduler that just passes down the I/O that comes to it. Useful for checking whether complex I/O scheduling decisions of other schedulers are not causing I/O performance regressions.

In some cases it can be helpful for devices that do I/O scheduling themselves, as intelligent storage, or devices that do not depend on mechanical movement, like SSDs. Usually, the DEADLINE I/O scheduler is a better choice for these devices, but due to less overhead NOOP may produce better performance on certain workloads.

Enable KSM

Open $ vim /sys/kernel/mm/ksm/run and change 0 to 1

Kernel Samepage Merging (KSM) is a feature of the Linux kernel introduced in the 2.6.32 kernel. KSM allows for an application to register with the kernel to have its pages merged with other processes that also register to have their pages merged. For KVM, the KSM mechanism allows for guest virtual machines to share pages with each other. In an environment where many of the guest operating systems are similar, this can result in significant memory savings.

note : this might be useful when running multiple VMs on the same machine

Swappiness and cache pressure

Edit /etc/sysctl.conf and change the values for swappiness and vfs_cache_pressure.

vm.swappiness=10
vm.vfs_cache_pressure=50

You can check current values with:

sudo cat /proc/sys/vm/swappiness
sudo cat /proc/sys/vm/vfs_cache_pressure

swappiness this control is used to define how aggressively the kernel swaps out anonymous memory relative to pagecache and other caches. Increasing the value increases the amount of swapping. The default value is 60.

vfs_cache_pressure this variable controls the tendency of the kernel to reclaim the memory which is used for caching of VFS caches, versus pagecache and swap. Increasing this value increases the rate at which VFS caches are reclaimed.

Mentions

  • disable pagefile not recommanded : swapoff -a

credits

@patadejaguar
Copy link

Good tips... Thanks for share.

@dianjuar
Copy link

dianjuar commented May 5, 2016

Here you can make some test to see the effectiveness of Swappiness and cache pressure
https://rudd-o.com/linux-and-free-software/tales-from-responsivenessland-why-linux-feels-slow-and-how-to-fix-that

Works for Arch Based distributions too.

@ivantomica
Copy link

ivantomica commented May 15, 2016

For I/O scheduler I have following entry in /etc/udev/rules.d/90-schedulers.rules

# set noop scheduler for non-rotating disks
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="noop"

as comment suggests it will enable noop I/O scheduler for non-rotating disks (SSD).

@Quix0r
Copy link

Quix0r commented Apr 8, 2017

@ivantomica what do you recommend/use for rotational (HDDs) disks? I use deadline here to void "hicks" on playing DVDs.

@jthill
Copy link

jthill commented Jun 5, 2017

I tend to have some idle ram, and I do nice -n 19 find $(df |awk '/^\//{print $NF}') -xdev -type c &>- & disown to preload the dentry cache in .xinitrc. I'm no longer the least bit dissatisfied with performance on my hdd-based systems, though I'm about to experiment with dropping vfs_cache_pressure from 50 to 10.

@garynych
Copy link

garynych commented Feb 3, 2018

For I/O scheduler I have following entry in /etc/udev/rules.d/60-schedulers.rules

system default : set cfq scheduler for rotating disks

ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"

SSD specific : set deadline scheduler for non-rotating disks

ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="deadline"

@jessuppi
Copy link

A few years old but still some great tips here, thanks! I do think a 10 value for vm.swappiness is fine for SSD drives these days, even though some guides still recommend 1 to "prolong" your SSD lifetime (not so relevant on newer tech or cloud hosting).

I referenced this Gist in our SlickStack project too:
https://github.com/littlebizzy/slickstack/blob/master/ubuntu/sysctl.txt

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