• Stars
    star
    169
  • Rank 224,453 (Top 5 %)
  • Language
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple guide for optimizing linux ๐Ÿง in detail

Optimizing Linux

I am writing this guide to save my progress and let others contribute to increasing linux 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 fully 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 kernel

By now, everyone agrees that compiling your kernel is one of the best options to get the fastest possible speed. You might want to google How to make custom kernel in <distro> to get the packages required to compile the kernel.

  1. Download the latest kernel or whatever you like. Extract it; I am going to 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 = <yourname>".

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

    • Now, one of the best things 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 is a lot of other stuff you can do too, but you will have to read them and see which suits you best. A simple way might be to just copy clear linux config, but it might disable certain features(see next Applying patches).
  5. Now, you might want to 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 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 CFLAGS='-march=native -O3 -flto -pipe' CXXFLAGS='-march=native -O3 -flto -pipe'
    make -j N CFLAGS='-march=native -O3 -flto -pipe' CXXFLAGS='-march=native -O3 -flto -pipe' modules
    sudo make modules_install
    sudo make install

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

    If any steps fail, 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 kernel.

Applying patches

There are several 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 suit you best. I will be using graysky2 kernel patch here. Download the whole repo or just the file you need. In my case, I have GCC 10 and the latest kernel, so I will be using this .

  1. Copy the desired patch file into the root of the extracted linux dictionary; same 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

There are other patches such as scheduling related that you can apply to. Again try finding your 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). NOTE: Removing the currently running kernel (determined by uname -r) will render your system non-bootable.

  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 an unknown script, and even if I have to, I use containers and firefox with noscript and a few other security add-ons. Nonetheless, if you understand the security concerns, you can disable it and see a substantial boost in performance.

  1. sudo grubby --args "mitigations=off nowatchdog processor.ignore_ppc=1 amdgpu.ppfeaturemask=0xffffffff ec_sys.write_support=1" --update-kernel=ALL

OR

  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

    These 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:

    sudo systemctl disable NetworkManager-wait-online.service
    sudo systemctl disable dnf-makecache.service 
    sudo systemctl disable dnf-makecache.timer
    sudo 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 a laptop, and it gets hot). You might want to 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 the kernel compilation.

Improving graphic card performance

You can find overclocking tools specific to your 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 heat up 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:

More Repositories

1

wasm-template-rust

A wasm ๐Ÿ•ธ template for Rust ๐Ÿฆ€ to publish to gh-pages without npm-deploy
Rust
114
star
2

rust-ffi-examples

FFI examples written in Rust
Makefile
48
star
3

brainhug

A simple brainfuck translator crate in Rust
Rust
20
star
4

pakoda

A hobby programming language ๐Ÿฆ€ inspired by llvm's kaleidoscope
Rust
11
star
5

wasm-conway

Conway game of life in Rust + Wasm
Rust
10
star
6

dota2_webapi_bindings

Dota 2 webapi bindings for rust
Rust
9
star
7

firefox-css

My firefox ๐ŸฆŠ browser config
CSS
8
star
8

Hacktoberfest-2018

Here is a starter project for first time contributors #hacktoberfest
C
7
star
9

sn99

๐Ÿฆ€
7
star
10

DellG5SE-Linux

DellG5SE script for gaming/battery/normal usage
Shell
7
star
11

pokemon-text-game

A pokemon text based fight game written in Rust
Rust
7
star
12

vague-os

A hobby OS written in Rust
Rust
6
star
13

conway-game-of-life

Conway game of life in written in Rust
Rust
6
star
14

sn99_alfred_bot

A bot for telegram
JavaScript
6
star
15

rule_engine

Rust
6
star
16

custom_traceroute

simple tracerouting
Rust
5
star
17

driver

run driver.bat and see logs in debugview
Rust
5
star
18

travis

A repository to fuddle with travis
Rust
5
star
19

mandelbrot

Rust
5
star
20

mandelbrot-vulkan

Mandelbrot set
Rust
5
star
21

weird_os

Just a weird OS
Rust
5
star
22

scan_open_ports

Rust
5
star
23

goal

A rust bot for irc
Rust
5
star
24

grachical-tic-tac-toe

A graphical tic-tac-toe game written in Rust
Rust
5
star
25

sudoku-solver

A sudoku solver
Rust
5
star
26

s-shell

A simple "shell" made in Rust
Rust
5
star
27

maggi

flappy bird
Rust
5
star
28

sam

A bootloader
Assembly
4
star
29

college_thesis_notes

notes
4
star
30

lg-gram

Linux configs and tweaks for lg-gram laptops
3
star
31

minifilter-rust

Pure rust implementation of windows minifilters
Rust
3
star
32

Work-Life-Balance

Personal managed Android WorkSpace
Jupyter Notebook
2
star
33

unix-fsfilter-rs

fsfilter for unix
Rust
1
star
34

location_outliers

Rust
1
star
35

dbms-asap

College project on Employee Database And Payroll Management System
1
star
36

windows-snapshot

windows state
Rust
1
star
37

policy

Google MDM schema maker
Rust
1
star
38

network

Rust
1
star
39

tetragon-rust

Rust ebpf observability with Tetragon
Rust
1
star
40

credential-provider

experimental windows credential provider
Rust
1
star
41

advent-of-code

Advent of Code 2023
Rust
1
star
42

hackathon

Rust
1
star