{"id":13510539,"url":"https://github.com/sn99/Optimizing-linux","last_synced_at":"2025-03-30T16:33:42.368Z","repository":{"id":38788223,"uuid":"332890788","full_name":"sn99/Optimizing-linux","owner":"sn99","description":"A simple guide for optimizing linux 🐧 in detail","archived":false,"fork":false,"pushed_at":"2024-07-01T07:57:46.000Z","size":38,"stargazers_count":186,"open_issues_count":0,"forks_count":14,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-10-13T07:42:17.961Z","etag":null,"topics":["amd","boot","boot-time","btrfs","compiling-kernels","cpu","fedora","gpu","graphic-card","grub2","improving","linux","linux-desktop","linux-kernel","optimization","patches","processor","swappiness"],"latest_commit_sha":null,"homepage":"https://sn99.github.io/Optimizing-linux/","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sn99.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-01-25T21:38:10.000Z","updated_at":"2024-09-30T16:24:49.000Z","dependencies_parsed_at":"2023-11-27T07:28:43.330Z","dependency_job_id":"1ae2765c-ba5a-4c13-a9e0-c8c3aade2aea","html_url":"https://github.com/sn99/Optimizing-linux","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sn99%2FOptimizing-linux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sn99%2FOptimizing-linux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sn99%2FOptimizing-linux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sn99%2FOptimizing-linux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sn99","download_url":"https://codeload.github.com/sn99/Optimizing-linux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222177445,"owners_count":16943745,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amd","boot","boot-time","btrfs","compiling-kernels","cpu","fedora","gpu","graphic-card","grub2","improving","linux","linux-desktop","linux-kernel","optimization","patches","processor","swappiness"],"created_at":"2024-08-01T02:01:43.062Z","updated_at":"2024-11-01T11:30:29.077Z","avatar_url":"https://github.com/sn99.png","language":null,"readme":"# Optimizing Linux\n\nI am writing this guide to save my progress and let others contribute to increasing linux performance even further;\nafter\nall, many are better than one. You can use all of them or just a few of them. **Read a topic fully before starting**.\n\nI am currently on [Nobara](https://nobaraproject.org/), so some steps may vary from distro to distro.\n\n**NOTE: This guide is not for beginners who are new to Linux** but a few of them can be used safely by them.\n\n## Index\n\n- [Compiling your kernel](#compiling-your-kernel)\n    - [Applying patches](#applying-patches)\n    - [Removing your own compiled kernel](#removing-your-own-compiled-kernel)\n- [Btrfs filesystem optimizations](#btrfs-filesystem-optimizations)\n- [Changing boot parameters](#changing-boot-parameters)\n- [Improving boot time](#improving-boot-time)\n- [Changing swappiness](#changing-swappiness)\n- [Changing scaling_governor to performance](#changing-scaling_governor-to-performance)\n- [Improving graphic card performance](#improving-graphic-card-performance)\n- [Some other tweaks](#some-other-tweaks)\n\n---------------------------------------------------\n\n## Compiling your kernel\n\nBy now, everyone agrees that compiling your kernel is one of the best options to get the fastest possible speed.\nYou might want to google `How to make custom kernel in \u003cdistro\u003e` to get the packages required to compile the kernel.\n\n1. Download the [latest kernel](https://www.kernel.org/) or whatever you like. Extract it; I am going to assume a\n   generic name from now on `linux-x.x.x`.\n\n\n2. The next step is finding the `config` file. Most of the time, you can run:\n    ```shell\n    cp -v /boot/config-$(uname -r) .config\n    ```\n   From inside `linux-x.x.x`, which should give an output like:\n    ```shell\n    '/boot/config-y.y.y-generic' -\u003e '.config'\n    ```\n   if it fails, you can find config in `/proc/config.gz` or simple run `make listnewconfig` OR `make oldconfig`(it\n   usually starts a long process; try finding your config in your distro source code too).\n\n\n3. Edit `Makefile` and change `EXTRAVERSION` to add something. For example, \"EXTRAVERSION = \\\u003cyourname\u003e\".\n\n\n4. (You might want to see the next subtopic before doing this) Now run `make xconfig`. Now a lot of optimizations are\n   possible\n   Here, many dead codes and modules can be removed and enabled. Let's go the safe road for now.\n    - Now, one of the best things you can do is no longer build for a generic kernel. Select\n        ```markdown\n        - Processor type and features\n            - Processor family\n                - [x] Core2/newer Xeon\n        ```\n      It should have been `Generic-x86-64` by default.\n    - There is a lot of other stuff you can do too, but you will have to read them and see which suits\n      you best. A simple way might be to just copy [clear linux config](https://github.com/clearlinux-pkgs/linux), but\n      it might disable certain features (see next [Applying patches](#applying-patches)).\n\n\n5. Now, you might want to run:\n    ```shell\n    dmesg --level=err\n    dmesg --level=warn   \n    ```\n   To see if you can enable some extra flags for extra features. For\n   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.`\n   can be solved by enabling both of them.\n\n\n6. Finally, compiling the kernel:\n    ```shell\n    # sed -ri '/CONFIG_SYSTEM_TRUSTED_KEYS/s/=.+/=\"\"/g' .config\n    make -j N CFLAGS='-march=native -O3 -flto -pipe' CXXFLAGS='-march=native -O3 -flto -pipe'\n    make -j N CFLAGS='-march=native -O3 -flto -pipe' CXXFLAGS='-march=native -O3 -flto -pipe' modules\n    sudo make modules_install\n    sudo make install\n    ```\n\n   Where `N` is the number of `cores` you have, alternatively use `$(getconf _NPROCESSORS_ONLN)`.\n\n   If any steps fail, run `make clean` and start again.\n\n\n7. Making it default in grub (I am using grub2, your process might vary):\n    ```shell\n    sudo grub2-mkconfig -o /boot/grub2/grub.cfg\n    sudo grubby --set-default /boot/vmlinuz-x.x.x-x\n    ```\n   You can find yours `vmlinuz-x.x.x-x` in `/boot/`\n\nNow restart and run `uname -r` to see your kernel.\n\n### Applying patches\n\nThere are several patches that you can use to increase performance or to make life simpler.\n\nThere are a lot of patches available, and you will have to find those that suit you best. I will be\nusing [graysky2](https://github.com/graysky2/kernel_gcc_patch) kernel patch here. Download the\nwhole [repo](https://github.com/graysky2/kernel_gcc_patch) or just the file you need. In my case, I have GCC 10 and\nthe latest kernel, so I will be\nusing [this](https://github.com/graysky2/kernel_gcc_patch/blob/master/enable_additional_cpu_optimizations_for_gcc_v10.1%2B_kernel_v5.8%2B.patch)\n.\n\n1. Copy the desired patch file into the root of the extracted linux dictionary; same place as `.config`.\n\n\n2. `patch -p1 \u003c enable_additional_cpu_optimizations_for_gcc_v10.1+_kernel_v5.8+.patch`\n\n   You should see an output like this:\n\n   ```shell\n   patching file arch/x86/Kconfig.cpu\n   patching file arch/x86/Makefile\n   patching file arch/x86/Makefile_32.cpu\n   patching file arch/x86/include/asm/vermagic.h\n   ```\n\n\n3. Now, you can start from step 4 in the previous setup and will see:\n   ```markdown\n   -  Processor type and features\n       - Processor family\n           - [x] Native optimizations autodetected by GCC\n   ```\n\nThere are other patches such as [scheduling related](https://cchalpha.blogspot.com/) that you can apply to. Again, try\nfinding your patches that suits your system.\n\n### Removing your own compiled kernel\n\nTry to keep the last working kernel, i.e., have a minimum of two kernels (the one you are using and the previous one).\n**NOTE:** Removing the currently running kernel (determined by `uname -r`) will render your system\nnon-bootable.\n\n1. These entries need to be removed:\n   ```shell\n   /boot/vmlinuz-x.x.x-x\n   /boot/initrd-x.x.x-x\n   /boot/System-map-x.x.x-x\n   /boot/config-x.x.x-x\n   /lib/modules/x.x.x-x/\n   /var/lib/initramfs/x.x.x-x/\n   /boot/loader/entries/*x.x.x-x\n   ```\n\n\n2. `sudo grub2-mkconfig -o /boot/grub2/grub.cfg` or `sudo update-grub2`\n\n## Btrfs filesystem optimizations\n\n1. `sudo gedit /etc/fstab`, change it to look something like this (this is on fedora, yours might vary):\n    ```shell\n    UUID=\u003cdo-not-change\u003e /                       btrfs   subvol=root,x-systemd.device-timeout=0,ssd,noatime,space_cache,commit=120,compress=zstd,discard=async,lazytime 0 0\n    UUID=\u003cdo-not-change\u003e /boot                   ext4    defaults        1 2\n    UUID=\u003cdo-not-change\u003e          /boot/efi               vfat    umask=0077,shortname=winnt 0 2\n    UUID=\u003cdo-not-change\u003e /home                   btrfs   subvol=home,x-systemd.device-timeout=0,ssd,noatime,space_cache,commit=120,compress=zstd,discard=async,lazytime 0 0\n    ```\n    \u003e Optional : `nobarrier`\n    \n    `nobarrier` option is safe as long you didn't expect sudden powerloss happens or has battery-backed.\n   \n    _On a device with a volatile battery-backed write-back cache, the nobarrier option will not lead to filesystem corruption as the pending blocks are supposed to make it to the permanent storage._ [man 5 btrfs](https://btrfs.readthedocs.io/en/latest/btrfs-man5.html)\n\n2. `sudo systemctl daemon-reload`\n\n\n3. `sudo systemctl enable fstrim.timer`\n\n## Changing boot parameters\n\n**Important:** I usually like disabling `mitigations`, but then again, I am on `AMD` based CPU and do not\nhave `Meltdown`\nonly `Spectre`, I do not run an unknown script, and even if I have to, I use containers and firefox with `noscript` and\na\nfew other security add-ons. Nonetheless, if you understand the security concerns, you can disable it and see a\nsubstantial\nboost in performance.\n\n1. `sudo grubby --args \"mitigations=off nowatchdog processor.ignore_ppc=1 amdgpu.ppfeaturemask=0xffffffff ec_sys.write_support=1 split_lock_detect=off\" --update-kernel=ALL`\n\nOR\n\n1. `sudo gedit /etc/default/grub`\n\n\n2. You will find a line `GRUB_CMDLINE_LINUX=\" ... rhgb quiet` change it to (`...` signifies other parameters):\n    ```shell\n    GRUB_CMDLINE_LINUX=\"... rhgb quiet mitigations=off nowatchdog processor.ignore_ppc=1 split_lock_detect=off\"\n    ```\n\n3. Also, edit `GRUB_TIMEOUT=5` to `GRUB_TIMEOUT=1.`\n\n\n4. `sudo grub2-mkconfig -o /etc/grub2-efi.cfg`\n\n   OR\n\n   `sudo grub2-mkconfig -o /etc/grub2.cfg`\n\nAfter rebooting, you can run `cat /proc/cmdline` to see your boot options.\n\n## Improving boot time\n\nOur last tweak kinda improved it, but let's try something more.\n\n1. Remove startup applications; I use `gnome-tweaks` for a GUI-like experience.\n\n\n2. Run the following to find what service is taking the longest:\n\n   ```shell\n   systemd-analyze\n   systemd-analyze blame\n   systemd-analyze critical-chain\n   ```\n\n   These might vary from system to system and distro to distro; in my case(fedora), I disabled `dnf-makecache.service.`\n   which took around `32s`. To do so:\n    ```shell\n    sudo systemctl disable NetworkManager-wait-online.service\n    sudo systemctl disable dnf-makecache.service \n    sudo systemctl disable dnf-makecache.timer\n    sudo gsettings set org.gnome.software download-updates false\n    ```\n\n   You might want to google every service that you think about disabling and what it does; in my case, it just updates\n   dnf\n   cache, which I usually like to do manually.\n\n## Changing swappiness\n\nIf you have 8GB or more ram, you might benefit from it; otherwise, leave it as it is.\n\n1. To see current swappiness, enter `cat /proc/sys/vm/swappiness`; it should print `60`; we want to make it 10.\n\n\n2. `sudo gedit /etc/sysctl.conf`\n\n\n3. Enter `vm.swappiness=10` and reboot; now step 1 should print 10.\n\n## Changing `scaling_governor` to `performance`\n\nDo not change it to `performance` on Ryzen based CPUs as it **_might_**(I seem to get better performance on AC, but then\nagain, `performance` does not seem to allow turbo boost in some cases) hurt their performance, using `ondemand`.\nor `schedutil` is better (more leaning towards `schedutil` as soon as it\ngets [fixed](https://www.phoronix.com/scan.php?page=article\u0026item=linux511-amd-patch\u0026num=1)).\n\n1. Run `cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor` to see your current governor.\n\n\n2. `echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor`\n\n   This setting most likely will not persist during the next boot; I like to change it manually rather than making a\n   systemd service (I am a laptop, and it gets hot). You might want to google how to make it persistent for your distro\n   if\n   you like OR:\n    ```shell\n    echo 'GOVERNOR=\"performance\"' | sudo tee /etc/default/cpufrequtils\n    sudo systemctl disable schedutil \n    ```\n\n   The default is `schedutil`; you can see\n   others [here](https://www.kernel.org/doc/html/v4.14/admin-guide/pm/cpufreq.html).\n\n**Note**: You can also change the default during the kernel compilation.\n\n## Improving graphic card performance\n\nYou can find overclocking tools specific to your GPU(s), but to make sure your graphics card isn’t being suppressed by\nthe OS (especially AMD):\n\n1. Checking whether it is `auto`:\n\n    ```shell\n    cat /sys/class/drm/card0/device/power_dpm_force_performance_level\n    cat /sys/class/drm/card1/device/power_dpm_force_performance_level\n    ```\n\n2. Check the parameters of GPU by:\n\n    ```shell\n    sudo cat /sys/kernel/debug/dri/0/amdgpu_pm_info\n    sudo cat /sys/kernel/debug/dri/1/amdgpu_pm_info\n    ```\n\n3. Now set everything to high:\n\n    ```shell\n    sudo su\n    echo high \u003e /sys/class/drm/card0/device/power_dpm_force_performance_level\n    echo high \u003e /sys/class/drm/card1/device/power_dpm_force_performance_level\n    ```\n\n   You can change them back to `auto` if your system overheats.\n\n## Some other tweaks\n\n- [ArchWiki/Improving performance](https://wiki.archlinux.org/index.php/Improving_performance)\n\n- Disabling `Cool'n'Quiet` or `speedstep` or `PowerNow!` from bios (will cause heat up on laptops, only enable it during\n  gaming)\n\n- Check other bios features, too; they vary from system to system but should have a significant boost in performance\n\n- Using `X` instead of `Wayland` (may vary game to game)\n\n- Using `Opengl` backend in games instead of `Vulkun` (may vary game to game)\n\n---------------------------------------------------\n\n## Contributing\n\nFeel free to open an [issue](https://github.com/sn99/Optimizing-linux/issues/new)\nor [editing the README](https://github.com/sn99/Optimizing-linux/edit/master/README.md) yourself.\n\n---------------------------------------------------\n\n## License\n\nLicensed under either of these:\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)\n* MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)\n","funding_links":[],"categories":["Others","optimization"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsn99%2FOptimizing-linux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsn99%2FOptimizing-linux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsn99%2FOptimizing-linux/lists"}