User:Bertini97

From Gentoo Wiki
Jump to:navigation Jump to:search

Foreword

Here are a few words that I think it's best to keep in mind:

  • Optimizing a system is a hobby. If you care about productivity this is not the path: instead of squeezing every bit of performance out of your computer, you are much better off spening this time doing something else. But if you, like me, unreasonably dig this stuff, keep reading. I think people are attracted to this for the same reason car tuners are attracted to DIY modifications more than buyng a new car.
  • Optimizing a system can be brittle. Every package version change within hundreds of packages can potentially break your house of cards of compiler flags.


With this in mind, I tried writing a tutorial for people who want to optimize their Gentoo system really quickly and with the least brittleness (hopefully this is a word).

Important
Distributions like Ubuntu do a great job at optimizing critical packages, and can obtain results sometimes similar to Gentoo. One should evaluate carefully if the time spent is worth.

make.conf

If you used Gentoo for a bit, you are probably used to the /etc/portage/make.conf file. It is used to customize Portage, Gentoo's package manager. Here you can specify build flags for various languages.

CFLAGS and friends

For C, C++ and FORTRAN we set -pipe to speed up compilation, -O2 to select the most reliable and tested level of optimization, and finally -march=native to make the compiler produce binaries fine-tuned to our cpu architecture.

FILE /etc/portage/make.conf
COMMON_FLAGS="-O2 -pipe -march=native"

CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

Rust

Now, for rust we want only

FILE /etc/portage/make.conf
RUSTFLAGS="-C target-cpu=native"

Link time optimizations (LTO)

Enabling Link Time Optimizations (LTO) allows the compiler to perform optimizations at link time, sometimes resulting in better performance and smaller executable size. To enable it we just pass -flto to the compiler. We also add some flags to make the compiler throw error instead of warning in the cases where would cause runtime trouble especially when paired with LTO.

FILE /etc/portage/make.conf
WARNING_FLAGS="-Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing"
COMMON_FLAGS="-O2 -pipe -march=native -flto ${WARNING_FLAGS}"

CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"
Note
Major distributions (Ubuntu, Fedora, Debian, etc) recently started using LTO for their packages.

Kernel tunables

Kernel tunables are used to customize the behavior of the kernel, and can be used to improve energy efficiency or to save some hardware cycles. They are set using the sysctl command, or by manually editing /etc/sysctl.conf or files in /etc/sysctl.d.

Some good options to tune are:

  • vm.swappiness=10 to lower how aggressively the kernel starts using the swap.
  • kernel.nmi_watchdog=0 to disable the NMI watchdog. This is a hard lockup detector, and it monitors each CPU for its ability to respond to timer interrupts.
  • vm.dirty_writeback_centisecs=6000 to make the kernel flusher threads and write `old’ data out to disk every 60s.