A simple guide for optimizing linux in detail

Overview

Optimizing Linux

I am writing this guide as to save my progress and let others contribute to increase the performance even further, after all many are better than one. You can use all of them or just a few of them. Read a topic full before starting.

I am currently on fedora so some steps may vary from distro to distro.

NOTE: This guide is not for beginners who are new to linux but a few of them can be used safely by them.

Index


Compiling your own kernel

I think by now everyone agrees that compiling your own kernel is one of the best options to get fastest possible speed. You might wanna google How to make custom kernel in to get the packages required to compile the kernel.

  1. Download the latest kernel or whatever you might like. Extract it, I am gonna assume a generic name from now on linux-x.x.x.

  2. The next step is finding the config file. Most of the time you can just run:

    cp -v /boot/config-$(uname -r) .config

    from inside linux-x.x.x which should give an output like:

    '/boot/config-y.y.y-generic' -> '.config'

    if it fails you can find config in /proc/config.gz or simple run make listnewconfig OR make oldconfig(it usually starts a long process, try finding your config in your distro source code too).

  3. Edit Makefile and change EXTRAVERSION to add something. For example, "EXTRAVERSION = ".

  4. (You might wanna see next subtopic before doing this) Now run make xconfig. Now a lot of optimizations are possible here and a lot of dead code and modules can be removed and enabled. Let's go the safe road for now.

    • Now one of the best thing you can do is no longer build for a generic kernel. Select
      -  Processor type and features
          - Processor family
              - [x] Core2/newer Xeon
      It should have been Generic-x86-64 by default.
    • There are a lot of other stuff you can do too but then you will have to yourself read them and see which suits best. A simple way might be to just copy clear linux config but it might disable certain features(see next).
  5. Now you might wanna run:

    dmesg --level=err
    dmesg --level=warn   

    to see if you can enable some extra flags for extra features. For example psmouse serio1: elantech: The touchpad can support a better bus than the too old PS/2 protocol. Make sure MOUSE_PS2_ELANTECH_SMBUS and MOUSE_ELAN_I2C_SMBUS are enabled to get a better touchpad experience. can be solved by enabling both of them.

  6. Finally compiling the kernel:

    # sed -ri '/CONFIG_SYSTEM_TRUSTED_KEYS/s/=.+/=""/g' .config
    make -j N "KCFLAGS=-g -O3 -march=native -pipe"
    make -j N "KCFLAGS=-g -O3 -march=native -pipe" modules
    sudo make -j N "KCFLAGS=-g -O3 -march=native -pipe" modules_install
    sudo make -j N "KCFLAGS=-g -O3 -march=native -pipe" install

    Where N is the number of cores you have, alternatively use $(getconf _NPROCESSORS_ONLN).

    If any of the step fails run make clean and start again.

  7. Making it default in grub(I am using grub2, your process might vary):

    sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    sudo grubby --set-default /boot/vmlinuz-x.x.x-x

    You can find yours vmlinuz-x.x.x-x in /boot/

Now restart and run uname -r to see your own kernel.

Applying patches

There are a number of patches that you can use to increase performance or to make life simpler.

There are a lot of patches available and you will have to find those that suite you best. I will be using graysky2 kernel patch here. Download the whole repo or just the file that you need. In my case I have gcc 10 and latest kernel so I will be using this.

  1. Copy the desired patch file into the root of the extracted linux dictionary, ame place as .config.

  2. patch -p1 < enable_additional_cpu_optimizations_for_gcc_v10.1+_kernel_v5.8+.patch

    You should see an output like this:

    patching file arch/x86/Kconfig.cpu
    patching file arch/x86/Makefile
    patching file arch/x86/Makefile_32.cpu
    patching file arch/x86/include/asm/vermagic.h
  3. Now you can start from step 4 in the previous setup and will see:

    -  Processor type and features
        - Processor family
            - [x] Native optimizations autodetected by GCC

You should no longer need the -march=native from step 6 while building.

There are other patches such as scheduling related that you can apply too. Again try finding your own patches that suits your system.

Removing your own compiled kernel

Try to keep the last working kernel i.e. have a minimum of 2 kernels (the one you are using and the previous one).

  1. These entries need to be removed:

    /boot/vmlinuz-x.x.x-x
    /boot/initrd-x.x.x-x
    /boot/System-map-x.x.x-x
    /boot/config-x.x.x-x
    /lib/modules/x.x.x-x/
    /var/lib/initramfs/x.x.x-x/
    /boot/loader/entries/*x.x.x-x
  2. sudo grub2-mkconfig -o /boot/grub2/grub.cfg or sudo update-grub2

Btrfs filesystem optimizations

  1. sudo gedit /etc/fstab, change it to look something like this(this is on fedora, yours might vary):

    UUID=<do-not-change> /                       btrfs   subvol=root,x-systemd.device-timeout=0,ssd,noatime,space_cache,commit=120,compress=zstd,discard=async 0 0
    UUID=<do-not-change> /boot                   ext4    defaults        1 2
    UUID=<do-not-change>          /boot/efi               vfat    umask=0077,shortname=winnt 0 2
    UUID=<do-not-change> /home                   btrfs   subvol=home,x-systemd.device-timeout=0,ssd,noatime,space_cache,commit=120,compress=zstd,discard=async 0 0
  2. sudo systemctl daemon-reload

  3. sudo systemctl enable fstrim.timer

Changing boot parameters

Important: I usually like disabling mitigations, but then again I am on AMD based cpu and do not have Meltdown only Spectre, I do not run unknown script and even if I have to I use containers and use firefox with noscript and a few other security addons. Nonetheless if you understand the security concerns you can disable it and see a substantial boost in performance.

  1. sudo gedit /etc/default/grub

  2. You will find a line GRUB_CMDLINE_LINUX=" ... rhgb quiet change it to (... signifies other parameters):

    GRUB_CMDLINE_LINUX="... rhgb quiet mitigations=off nowatchdog processor.ignore_ppc=1"
  3. Also edit GRUB_TIMEOUT=5 to GRUB_TIMEOUT=1

  4. sudo grub2-mkconfig -o /etc/grub2-efi.cfg

    OR

    sudo grub2-mkconfig -o /etc/grub2.cfg

After rebooting you can run cat /proc/cmdline to see your boot options.

Improving boot time

Our last tweak kinda improved it but let's try something more.

  1. Remove startup applications, I use gnome-tweaks for a GUI like experience.

  2. Run the following to find what service is taking the longest:

    systemd-analyze
    systemd-analyze blame
    systemd-analyze critical-chain

    This might vary from system to system and distro to distro, in my case(fedora) I disabled dnf-makecache.service which took around 32s. To do so:

    systemctl disable dnf-makecache.service 
    systemctl disable dnf-makecache.timer
    gsettings set org.gnome.software download-updates false

    You might wanna google every service that you think about disabling and what it does, in my case it just updates dnf cache which I usually like to do manually.

Changing swappiness

If you have 8GB or more ram you might benefit from it otherwise leave it as it is.

  1. To see current swappiness enter cat /proc/sys/vm/swappiness, it should print 60, we wanna make it 10.

  2. sudo gedit /etc/sysctl.conf

  3. Enter vm.swappiness=10 and reboot, now step 1 should print 10.

Changing scaling_governor to performance

Do not change it to performance on Ryzen based CPUs as it might(I seem to get better performance on AC, but then again performance does not seem to allow turbo boost in some cases) hurt their performance, using ondemand or schedutil is better(more leaning towards schedutil as soon as it gets fixed).

  1. Run cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor to see your current governor.

  2. echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

    This setting most likely will not persist during the next boot, I like to change it manually rather than making a systemd service(I am an laptop and it gets hot). You might wanna google how to make it persistent for your distro if you like OR:

    echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
    sudo systemctl disable schedutil 

    The default is schedutil, you can see others here.

Note: You can also change the default during compilation of the kernel.

Improving graphic card performance

You can find overclocking tools specific to you GPU(s), but to make sure your graphics card isn’t being suppressed by the OS(especially AMD):

  1. Checking whether it is auto:

    cat /sys/class/drm/card0/device/power_dpm_force_performance_level
    cat /sys/class/drm/card1/device/power_dpm_force_performance_level
  2. Check the parameters of GPU by:

    sudo cat /sys/kernel/debug/dri/0/amdgpu_pm_info
    sudo cat /sys/kernel/debug/dri/1/amdgpu_pm_info
  3. Now set everything to high:

    sudo su
    echo high > /sys/class/drm/card0/device/power_dpm_force_performance_level
    echo high > /sys/class/drm/card1/device/power_dpm_force_performance_level

    You can change them back to auto if your system overheats.

Some other tweaks

  • ArchWiki/Improving performance

  • Disabling Cool'n'Quiet or speedstep or PowerNow! from bios (will cause heatup on laptops, only enable it during gaming)

  • Check other bios features too, they vary from system to system but should have a significant boost in performance

  • Using X instead of Wayland (may vary game to game)

  • Using Opengl backend in games instead of Vulkun (may vary game to game)


Contributing

Feel free to open an issue or editing the README yourself.


License

Licensed under either of these:

You might also like...
🦀️atos for linux by rust - A partial replacement for Apple's atos tool for converting addresses within a binary file to symbols.

atosl-rs 🦀️ atos for linux by rust - A partial replacement for Apple's atos tool for converting addresses within a binary file to symbols. tested on

Shared memory - A Rust wrapper around native shared memory for Linux and Windows

shared_memory A crate that allows you to share memory between processes. This crate provides lightweight wrappers around shared memory APIs in an OS a

Use Git installed in Bash on Windows/Windows Subsystem for Linux (WSL) from Windows and Visual Studio Code (VSCode)

WSLGit This project provides a small executable that forwards all arguments to git running inside Bash on Windows/Windows Subsystem for Linux (WSL). T

Sytéme d'AutoCompletion de texte pour HeficienceOS est autres distributions linux.

AutoCompletion Pour linux Version: 0.1.0-dev.1 - Note : L'application est encore instable, il est donc recommandé d'être indulgent, l'application risq

A CLI for configuring a GoXLR on Linux

GoXLR configuration utility A tool to configure a GoXLR without requiring a Windows VM. At this time, full device initialization is not possible on Li

stat-rs is a copy of linux command stat

stat-rs is a copy of linux command stat

A clone of linux cal command, using rust programming language

CLI Calendar command What the project does This command was inspired by Linux Cal command that shows the current month calendar as output (by default)

A collection of semi-useful tools made for GNU/Linux

DECTOOLS A collection of semi-useful tools made for GNU/Linux. Some may work on macOS, though functionality isn't a priority. Depenencies: python, bas

Figma Agent for Linux (a.k.a. Font Helper)

Figma Agent for Linux (a.k.a. Font Helper)

Comments
Owner
Siddharth Naithani
potatoes
Siddharth Naithani
An optimizing IK solver based on the Lie group of rigid transforms SE(3)

OptIK A fast inverse kinematics solver for arbitrary serial chains, providing Rust and Python programming interfaces. The implementation is similar to

Kyle Cesare 17 Oct 5, 2023
visual guide for writing Intrinsics(SIMD)

wasm-pack-template A template for kick starting a Rust and WebAssembly project using wasm-pack. Tutorial | Chat Built with ?? ?? by The Rust and WebAs

Ric Li 1 Apr 6, 2022
How to learn modern Rust - A guide to the adventurer.

How to learn modern Rust A guide to the adventurer. Table of Contents Learn Rust deeply one step after the other Text Processing in Rust How Rust maps

João Nuno Carvalho 2.5k Jan 4, 2023
TMM is a Linux native game modding tool. it allows to install and depoly mods for Linux native and wine games.

Tux Mod Manager TMM is a Linux native mod manager made with the Tauri toolkit. It can install, load, remove and deploy mods for both Linux native and

Mathiew May 119 Dec 27, 2022
Wallrus is a simple wallpaper manager for linux.

wallrus Wallrus is a simple and blazing-fast wallpaper manager for linux. It wraps around feh to provide a simple, fast, feature-rich and user-friendl

Akshay Rajput 9 Oct 10, 2022
A super simple /sbin/init for Linux which allows running one and only one program

Summary High-performance /sbin/init program for Linux This is designed to do literally nothing but accept binaries over the network and run them as a

null 19 Dec 4, 2023
Simple template to use csr and ssr leptos with tauri for ios/android/windows/macos/linux and web dev

Tailwind-Leptos-Tauri Template Simple template to use csr and ssr leptos with tauri for ios/android/windows/macos/linux and web dev Just clone the rep

Victor Batarse 11 Mar 10, 2024
Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using ttf-parser and allsorts

rust-fontconfig Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using allsorts as a font parser in order to parse .woff,

Felix Schütt 28 Oct 29, 2022
Check the reproducibility status of your Arch Linux packages (read-only mirror)

arch-repro-status A CLI tool for querying the reproducibility status of the Arch Linux packages using data from a rebuilderd instance such as reproduc

Arch Linux 12 Nov 16, 2022
Basic template for an out-of-tree Linux kernel module written in Rust.

Rust out-of-tree module This is a basic template for an out-of-tree Linux kernel module written in Rust. Please note that: The Rust support is experim

Rust for Linux 118 Dec 26, 2022