Skip to content

Latest commit

History

192 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

kernel_compiler_patch

Too lazy to update readme

The option to build a kernel with -march=native was merged and included in kernel version 6.16. This makes the patch in section 2 only needed if you want to build an optimzed kernel (say for zen5) on a machine that is not a match to that choice. I am too lazy to re-write this readme.

The ISA patch in secton 1 is still relevant if you want to build with -march=x86-64-v2 or -march=x86-64-v3 for generic kernels that will run on numerous supported CPUs.

Why a specific patch?

The kernel uses its own set of CFLAGS, KCFLAGS. For example, see:

Alternative way to define a -march= option without this patch

As pointed out by codemac in this topic, one can simply export the value/values for the KCFLAGS and KCPPFLAGS before calling make to achieve the same result, see here.

export KCFLAGS=' -march=znver3'
export KCPPFLAGS=' -march=znver3'
make all

New tunings

These patches adds additional tunings via new x86-64 ISA levels and more micro-architecture options to the Linux kernel in three broad classes.

1. New generic x86-64 ISA levels

When compiling the Generic x86-64 Processor family target, these are selectable under:

 Processor type and features ---> x86-64 compiler ISA level
  • x86-64 A value of (1) is the default and builds with the generic x86-64 ISA level
  • x86-64-v2 A value of (2) brings support for vector instructions up to Streaming SIMD Extensions 4.2 (SSE4.2) and Supplemental Streaming SIMD Extensions 3(SSSE3), the POPCNT instruction, and CMPXCHG16B.
  • x86-64-v3 A value of (3) adds vector instructions up to AVX2, MOVBE, and additional bit-manipulation instructions.

x86-64-v4 does exist but it adds vector instructions from some of the AVX-512 variants which the kernel does not use so including it does not make much sense.

Users of glibc 2.33 and above can see which level is supported by running one of the follownig:

/lib/ld-linux-x86-64.so.2 --help | grep supported
/lib64/ld-linux-x86-64.so.2 --help | grep supported

2. New micro-architectures levels

These are selectable under:

 Processor type and features ---> Processor family
CPU Family-march=Min GCC VerMin Clang Ver
AMD Improved K8-familyk8-sse39.39.0
AMD K10-familyamdfam109.39.0
AMD Family 10h (Barcelona)barcelona9.39.0
AMD Family 14h (Bobcat)btver19.39.0
AMD Family 16h (Jaguar)btver29.39.0
AMD Family 15h (Bulldozer)bdver19.39.0
AMD Family 15h (Piledriver)bdver29.39.0
AMD Family 15h (Steamroller)bdver39.39.0
AMD Family 15h (Excavator)bdver49.39.0
AMD Family 17h (Zen)znver19.39.0
AMD Family 17h (Zen 2)znver29.39.0
AMD Family 19h (Zen 3)znver310.312.0
AMD Family 19h (Zen 4)znver413.017.0
AMD Family 19h (Zen 5)znver514.119.1
Intel Bonnell family Atombonnell9.39.0
Intel Silvermont family Atomsilvermont9.39.0
Intel Goldmont family Atom (Apollo Lake and Denverton)goldmont9.39.0
Intel Goldmont Plus family Atom (Gemini Lake)goldmont-plus9.39.0
Intel 1st Gen Core i3/i5/i7-family (Nehalem)nehalem9.39.0
Intel 1.5 Gen Core i3/i5/i7-family (Westmere)westmere9.39.0
Intel 2nd Gen Core i3/i5/i7-family (Sandybridge)sandybridge9.39.0
Intel 3rd Gen Core i3/i5/i7-family (Ivybridge)ivybridge9.39.0
Intel 4th Gen Core i3/i5/i7-family (Haswell)haswell9.39.0
Intel 5th Gen Core i3/i5/i7-family (Broadwell)broadwell9.39.0
Intel 6th Gen Core i3/i5/i7-family (Skylake)skylake9.39.0
Intel 6th Gen Core i7/i9-family (Skylake X)skylake-avx5129.39.0
Intel 8th Gen Core i3/i5/i7-family (Cannon Lake)cannonlake9.39.0
Intel 10th Gen Core i7/i9-family (Ice Lake)icelake-client9.39.0
Intel Xeon (Cascade Lake)cascadelake10.210.0
Intel Xeon (Cooper Lake)cooperlake10.210.0
Intel 3rd Gen 10nm++ i3/i5/i7/i9-family (Tiger Lake)cooperlake10.210.0
Intel 4th Gen 10nm++ Xeon (Sapphire Rapids)sapphirerapids11.112.0
Intel 11th Gen i3/i5/i7/i9-family (Rocket Lake)rocketlake11.112.0
Intel 12th Gen i3/i5/i7/i9-family (Alder Lake)alderlake11.112.0
Intel 13th Gen i3/i5/i7/i9-family (Raptor Lake)raptorlake13.015.0.5
Intel 5th Gen 10nm++ Xeon (Emerald Rapids)emeraldrapids13.0???

3. Auto-detected micro-architecture levels

These are also selectable under:

 Processor type and features ---> Processor family

They have the ability to compile by passing the '-march=native' option which, according to the GCC manual "selects the CPU to generate code for at compilation time by determining the processor type of the compiling machine. Using -march=native enables all instruction subsets supported by the local machine and will produce code optimized for the local machine under the constraints of the selected instruction set."

Users of Intel CPUs should select the 'Intel-Native' option and users of AMD CPUs should select the 'AMD-Native' option.

Benchmarks

Setup

The test machine measured the time it took to make bzImage of the linux kernel source (.config generated by make x86_64_defconfig prior).

Three separate test machines were evaluated:

  1. AMD Ryzen 9 5950X
  2. Intel i7-4790K
  3. Intel N100

Separate kernels were first compiled from source patched with more-uarches-for-kernel-6.8-rc4+.patch.

  • Kernel 1 used the default menu config option for Processor family = Generic x86-64
  • Kernel 2 used the menu config option for Processor family = x86-64-v3
  • Kernel 3 used the menu config option for Processor family = AMD Zen 3 or Intel Haswell or Intel Alder Lake

The make test

Each machine was booted into its respective kernel and the make test was conducted. Then the next kernel was installed and the machine was booted into it and the make test was again conducted.

The stress-ng benchmark

The AMD 5950X ran stress-ng --taskset 0-1 --metrics-brief -t 30s --foo 2 12 times where foo was one of: af-alg, fork, mmap, or pipe under Kernel 1 and then again under Kernel 3.

Conclusion

Consistently across all three test machines, the kernels built with the optimized processor family options introduced by the patch hosted in this repo ran the make test faster than the kernel compiled with the default processor family option by a small (<1% difference) but statistically significant amount as measured by this make compilation.

The stress-ng testing generally showed small improvements (1-2% faster) and one showing no difference.

What does this mean for real-world usage? Maybe nothing. The intent was to see if something easily automatable could show some value in applying these micro-arch tunings. People have historically gravitated to compilation-based benchmarks so that coupled with ease-of-use point is why I settled on it. If someone has a good kernel-centric benchmark, I am interested to see a controlled comparison.

Discussion

  1. All the assumptions for ANOVA are met:
    • Data are normally distributed
    • The population variances are fairly equal
  2. The boxplot plot clearly show significance for either pair-wise comparison
    • Pair-wise analysis by Tukey-Kramer data shown for all pairs (see tables)

In other words, x86-64-v3 is significantly different from generic x86-64. The various subtargets are also significantly different from x86-64.

The make test

Stats for Machine 1. AMD Ryzen 9 X5950

Processor family optionMean compile timeStd dev# of replicates
Generic x86-6479.800 sec0.1076 sec12
x86-64-v379.456 sec0.0772 sec12
AMD Zen 379.440 sec0.0912 sec12

X9550

Treatment pairsTukey HSD Q statTukey HSD p-valueTukey HSD interfence
Generic x86-64 vs x86-64-v312.87710.0010053$${\color{green} \verb|**|p&lt;0.01}$$
Generic x86-64 vs AMD Zen 313.46750.0010053$${\color{green} \verb|**|p&lt;0.01}$$
x86-64-v3 vs AMD Zen 39.65240.8999947$${\color{red}insignificant}$$

Stats for Machine 2. Intel i7-4790K

Processor family optionMean compile timeStd dev# of replicates
Generic x86-64344.280 sec0.6455 sec12
x86-64-v3342.035 sec0.4971 sec12
Intel Haswell342.189 sec0.2415 sec12

i7-4790k

Treatment pairsTukey HSD Q statTukey HSD p-valueTukey HSD interfence
Generic x86-64 vs x86-64-v328.96520.0010053$${\color{green} \verb|**|p&lt;0.01}$$
Generic x86-64 vs Intel Haswell24.83350.0010053$${\color{green} \verb|**|p&lt;0.01}$$
x86-64-v3 vs Intel Haswell4.13170.0167155$${\color{lightgreen} \verb|*|p&lt;0.05}$$

Stats for Machine 3. Intel N100

Processor family optionMean compile timeStd dev# of replicates
Generic x86-64589.457 sec0.1596 sec12
x86-64-v3589.217 sec0.1382 sec12
Intel Alder Lake588.797 sec0.1532 sec12

N100

Treatment pairsTukey HSD Q statTukey HSD p-valueTukey HSD interfence
Generic x86-64 vs x86-64-v35.50760.0012818$${\color{green} \verb|**|p&lt;0.01}$$
Generic x86-64 vs Intel Alder Lake15.16000.0010053$${\color{green} \verb|**|p&lt;0.01}$$
x86-64-v3 vs Intel Alder Lake9.65240.0010053$${\color{green} \verb|**|p&lt;0.01}$$

Comparing GCC to Clang

The Ryzen 9 5950X was used to compare kernels built with GCC and Clang each with Generic x86-64 and x86-64-v3. The results are consistent for both compilers.

Processor family optionCompilerMean compile timeStd dev# of replicates
Generic x86-64GCC79.4569 sec0.0664 sec5
x86-64-v3GCC79.1403 sec0.0580 sec5
Generic x86-64Clang79.8398 sec0.0629 sec5
x86-64-v3Clang79.0975 sec0.0711 sec5

X9550

The stress-ng benchmarks

Here, stress-ng microbenchmark improvements or regressions (or neutral changes) were as follows (average from 12 x 30 sec runs):

af-alg: +2.7% (kernel AL_ALG crypto)
fork: * (process fork/exit)
mmap: +1.6% (memory mapping)
pipe: +1.3% (pipe + context switch)
*no statistically significant difference at p<0.05
unitsbenchmarkoptimizationmeanstd dev
bogo ops/s (real time)af-algx86-64104,320.21168.61
x86-64-v3107,154.54127.73
pipex86-641,535,225.43,624.5
x86-64-v31,555,824.24,212.6
forkx86-643,964.1421.02
x86-64-v33,953.517.44
mmapx86-6435.720.28
x86-64-v336.310.26

af-alg

fork

mmap

pipe

Software versions used

All machines ran Arch Linux with the all stock repo packages with the exception of the kernel (see below). At the time of work, the following the toolchain versions were used:

  • binutils 2.43+r4+g7999dae6961-1
  • clang 18.0.1-1
  • gcc 14.2.1+r134+gab884fffe3fc-1
  • gcc-libs 14.2.1+r134+gab884fffe3fc-1
  • glibc 2.40+r16+gaa533d58ff-2
  • linux-api-headers 6.10-1
  • stress-ng 0.18.04-1

The kernel packages were built on the official Arch Linux PKGBUILD for kernel version 6.10.10-arch1-1 applying the distro config differing only by the modifications introduced by the aforementioned patch from this repo.

The benchmark was compiling the vanilla Linux kernel version 6.10.10 and as mentioned above, the .config used was generated by running make x86_64_defconfig.

References

Credit

Legacy support

Find support for older version of the linux kernel and of gcc in the outdated_versions directory.

About

Kernel patch enables compiler optimizations for additional CPUs.

Resources

Stars

719 stars

Watchers

65 watching

Forks

Releases

Packages

Used by

Contributors

Languages