
构建高性能Linux内核!
!!📢硬件平台说明!!
- 💗 平台 X86_64.
- 💗 CPU AMD Ryzen 9 3900X.
- 💗 GPU GTX1660Ti.
- 💗 ODM模具 Clevo NH5xAx.
- 💗 EC Firwmare XMG-APEX15.
- 💗 Bios Firwmare XMG-APEX15.
- 💗 操作系统Arch Linux.
- 💗 Linux内核源码版本,5.16.12
获取Linux内核源码,并使用sha256校验.
pacman -Syy wget
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.tar.{xz,sign}
for i in *xz *sign; do
sha256sum ${i}
done获取Linux-xanmod补丁,并使用sha256校验.
wget https://github.com/xanmod/linux/releases/download/5.16.12-xanmod1/patch-5.16.12-xanmod1.xz
for i in *xz *sign; do
sha256sum ${i}
done添加gpg密钥;
gpg --recv-keys ABAF11C65A2970B130ABE3C479BE3E4300411886 #Linux Torvalds
gpg --recv-keys 647F28654894E3BD457199BE38DBBDC86092693E #Greg Kroah-HartmanCPU微架构性能优化选项脚本.
#!/usr/bin/env bash
. /usr/share/makepkg/util/message.sh
colorize
Detect_CPU=$(gcc -c -Q -march=native --help=target | grep march | awk '{print $2}' | head -1)
msg "Detected CPU architecture: $Detect_CPU"
cat << EOF
Available CPU microarchitectures:
1) AMD K6/K6-II/K6-III
2) AMD Athlon/Duron/K7
3) AMD Opteron/Athlon64/Hammer/K8
4) AMD Opteron/Athlon64/Hammer/K8 with SSE3
5) AMD 61xx/7x50/PhenomX3/X4/II/K10
6) AMD Family 10h (Barcelona)
7) AMD Family 14h (Bobcat)
8) AMD Family 16h (Jaguar)
9) AMD Family 15h (Bulldozer)
10) AMD Family 15h (Piledriver)
11) AMD Family 15h (Steamroller)
12) AMD Family 15h (Excavator)
13) AMD Family 17h (Zen)
14) AMD Family 17h (Zen 2)
15) AMD Family 19h Zen 3 processors (Zen 3)
16) Transmeta Crusoe
17) Transmeta Efficeon
18) IDT Winchip C6
19) Winchip-2/Winchip-2A/Winchip-3
20) AMD Elan
21) Geode GX1 (Cyrix MediaGX)
22) AMD Geode GX and LX
23) Cyrix III or C3
24) VIA C3 "Nehemiah"
25) VIA C7
26) Intel Pentium 4, Pentium D and older Nocona/Dempsey Xeon CPUs with Intel 64bit
27) Intel Atom
28) Intel Core 2 and newer Core 2 Xeons (Xeon 51xx and 53xx)
29) Intel 1st Gen Core i3/i5/i7-family (Nehalem)
30) Intel 1.5 Gen Core i3/i5/i7-family (Westmere)
31) Intel Silvermont
32) Intel Goldmont (Apollo Lake and Denverton)
33) Intel Goldmont Plus (Gemini Lake)
34) Intel 2nd Gen Core i3/i5/i7-family (Sandybridge)
35) Intel 3rd Gen Core i3/i5/i7-family (Ivybridge)
36) Intel 4th Gen Core i3/i5/i7-family (Haswell)
37) Intel 5th Gen Core i3/i5/i7-family (Broadwell)
38) Intel 6th Gen Core i3/i5/i7-family (Skylake)
39) Intel 6th Gen Core i7/i9-family (Skylake X)
40) Intel 8th Gen Core i3/i5/i7-family (Cannon Lake)
41) Intel 8th Gen Core i7/i9-family (Ice Lake)
42) Xeon processors in the Cascade Lake family
43) Intel Xeon (Cooper Lake)
44) Intel 3rd Gen 10nm++ i3/i5/i7/i9-family (Tiger Lake)
45) Intel Sapphire Rapids
46) Intel Rocket Lake
47) Intel Alder Lake
92) Generic-x86-64-v2 (Nehalem and newer)
93) Generic-x86-64-v3 (Haswell and newer)
94) Generic-x86-64-v4 (AVX512 CPUs)
98) Intel-Native optimizations autodetected by GCC
99) AMD-Native optimizations autodetected by GCC
0) Generic (default)
EOF
sleep 1
answer=$1
case $answer in
1) Microarchitecture=CONFIG_MK6 ;;
2) Microarchitecture=CONFIG_MK7 ;;
3) Microarchitecture=CONFIG_MK8 ;;
4) Microarchitecture=CONFIG_MK8SSE3 ;;
5) Microarchitecture=CONFIG_MK10 ;;
6) Microarchitecture=CONFIG_MBARCELONA ;;
7) Microarchitecture=CONFIG_MBOBCAT ;;
8) Microarchitecture=CONFIG_MJAGUAR ;;
9) Microarchitecture=CONFIG_MBULLDOZER ;;
10) Microarchitecture=CONFIG_MPILEDRIVER ;;
11) Microarchitecture=CONFIG_MSTEAMROLLER ;;
12) Microarchitecture=CONFIG_MEXCAVATOR ;;
13) Microarchitecture=CONFIG_MZEN ;;
14) Microarchitecture=CONFIG_MZEN2 ;;
15) Microarchitecture=CONFIG_MZEN3 ;;
16) Microarchitecture=CONFIG_MCRUSOE ;;
17) Microarchitecture=CONFIG_MEFFICEON ;;
18) Microarchitecture=CONFIG_MWINCHIPC6 ;;
19) Microarchitecture=CONFIG_MWINCHIP3D ;;
20) Microarchitecture=CONFIG_MELAN ;;
21) Microarchitecture=CONFIG_MGEODEGX1 ;;
22) Microarchitecture=CONFIG_MGEODE_LX ;;
23) Microarchitecture=CONFIG_MCYRIXIII ;;
24) Microarchitecture=CONFIG_MVIAC3_2 ;;
25) Microarchitecture=CONFIG_MVIAC7 ;;
26) Microarchitecture=CONFIG_MPSC ;;
27) Microarchitecture=CONFIG_MATOM ;;
28) Microarchitecture=CONFIG_MCORE2 ;;
29) Microarchitecture=CONFIG_MNEHALEM ;;
30) Microarchitecture=CONFIG_MWESTMERE ;;
31) Microarchitecture=CONFIG_MSILVERMONT ;;
32) Microarchitecture=CONFIG_MGOLDMONT ;;
33) Microarchitecture=CONFIG_MGOLDMONTPLUS ;;
34) Microarchitecture=CONFIG_MSANDYBRIDGE ;;
35) Microarchitecture=CONFIG_MIVYBRIDGE ;;
36) Microarchitecture=CONFIG_MHASWELL ;;
37) Microarchitecture=CONFIG_MBROADWELL ;;
38) Microarchitecture=CONFIG_MSKYLAKE ;;
39) Microarchitecture=CONFIG_MSKYLAKEX ;;
40) Microarchitecture=CONFIG_MCANNONLAKE ;;
41) Microarchitecture=CONFIG_MICELAKE ;;
42) Microarchitecture=CONFIG_MCASCADELAKE ;;
43) Microarchitecture=CONFIG_MCOOPERLAKE ;;
44) Microarchitecture=CONFIG_MTIGERLAKE ;;
45) Microarchitecture=CONFIG_MSAPPHIRERAPIDS ;;
46) Microarchitecture=CONFIG_MROCKETLAKE ;;
47) Microarchitecture=CONFIG_MALDERLAKE ;;
92) Microarchitecture=CONFIG_GENERIC_CPU2 ;;
93) Microarchitecture=CONFIG_GENERIC_CPU3 ;;
94) Microarchitecture=CONFIG_GENERIC_CPU4 ;;
98) Microarchitecture=CONFIG_MNATIVE_INTEL ;;
99) Microarchitecture=CONFIG_MNATIVE_AMD ;;
*) default=CONFIG_GENERIC_CPU ;;
esac
warning "According to PKGBUILD variable _microarchitecture, your choice is $answer"
msg "Building this package for microarchitecture: $Microarchitecture$default"
sleep 5
sed -e 's|^CONFIG_GENERIC_CPU=y|# CONFIG_GENERIC_CPU is not set|g' -i .config
sed -e "s|^# $Microarchitecture is not set|$Microarchitecture=y|g" -i .config
echo编写PKGBUILD.
# Maintainer: BadBoy <luckmelove2@gmail.com>
#------------------------------#
# #
# Arch Linux Xanmod DiY Kernel #
# #
#------------------------------#
#根据CPU微架构性能优化选项脚本,来选择你的微架构,对CPU性能进行优化.
#有效数字介于:0 ~ 99 之间.
#默认为:0 => 通用
#如果您的软件包适用于一台机器,这是一个不错的选择:
#98(英特尔本机)或99(AMD 本机)
#此机器CPU AMD Zen2 R9-3900X.
if [ -z ${_microarchitecture+x} ]; then
_microarchitecture=14
fi
#禁用NUMA,因为大多数用户没有多个处理器,破坏CUDA/NvEnc.
## Archlinux 和 Xanmod 默认启用它;
## 将变量'use_numa'设置为:n 禁用(可能提高性能)
if [ -z ${use_numa+x} ]; then
use_numa=n
fi
##为了提高性能,您可以禁用 FUNCTION_TRACER/GRAPH_TRACER,限制内核的调试和分析.
##Stock Archlinux 和 Xanmod 已启用此功能.
##将变量"use_tracers"设置为:n 禁用(可能提高性能)
##y 启用(库存默认)
if [ -z ${use_tracers+x} ]; then
use_tracers=n
fi
#在 GCC 和 CLANG 配置之间选择(默认为 GCC)
if [ -z ${_compiler+x} ]; then
_compiler=gcc
fi
#使用 ZSTD 压缩模块(以节省磁盘空间)
if [ -z ${_compress_modules+x} ]; then
_compress_modules=n
fi
#只编译使用过的模块来大大减少构建的模块数量.
if [ -z ${_localmodcfg} ]; then
_localmodcfg=n
fi
#在构建之前通过 nconfig 调整内核选项;
if [ -z ${_makenconfig} ] &&! [ -f myconfig ]; then
_makenconfig=y
fi
### !!!<--警告-->!!! 除非您知道自己在做什么,否则请勿在此行下方进行编辑;
pkgbase='linux-xanmod-lht'
_major='5.16'
pkgver="${_major}.12"
_branch='5.x'
xanmod='1'
pkgrel="${xanmod}"
pkgdesc='Linux Xanmod DIY Kernel'
url='http://www.xanmod.org/'
arch=('x86_64')
license=('GPL2')
makedepends=(
bc cpio kmod libelf perl tar xz
)
if [ "${_compiler}" = "clang" ]; then
makedepends+=(clang llvm lld python)
fi
options=('!strip')
_srcname="linux-${pkgver}-xanmod${xanmod}"
source=("https://cdn.kernel.org/pub/linux/kernel/v${_branch}/linux-${_major}.tar."{xz,sign}
"https://github.com/xanmod/linux/releases/download/${pkgver}-xanmod${xanmod}/patch-${pkgver}-xanmod${xanmod}.xz"
'choose-gcc-optimization.sh'
)
#"patch-${pkgver}-xanmod${xanmod}.xz::https://sourceforge.net/projects/xanmod/files/releases/stable/${pkgver}-xanmod${xanmod}/patch-${pkgver}-xanmod${xanmod}.xz/download"
validpgpkeys=(
'ABAF11C65A2970B130ABE3C479BE3E4300411886' # Linux Torvalds
'647F28654894E3BD457199BE38DBBDC86092693E' # Greg Kroah-Hartman
)
# Arch Linux patchers
_commit="ec9e9a4219fe221dec93fa16fddbe44a34933d8d"
_patches=()
for _patch in ${_patches[@]}; do
#source+=("${_patch}::https://git.archlinux.org/svntogit/packages.git/plain/trunk/${_patch}?h=packages/linux&id=${_commit}")
source+=("${_patch}::https://raw.githubusercontent.com/archlinux/svntogit-packages/${_commit}/trunk/${_patch}")
done
sha256sums=('027d7e8988bb69ac12ee92406c3be1fe13f990b1ca2249e226225cd1573308bb'
'SKIP'
'd6d162990832239a4ed28452e761a9c8e1c79b422551ecada1dc311b8d9c89fa'
'1ac18cad2578df4a70f9346f7c6fccbb62f042a0ee0594817fdef9f2704904ee'
)
export KBUILD_BUILD_HOST=${KBUILD_BUILD_HOST:-L-H-T}
export KBUILD_BUILD_USER=${KBUILD_BUILD_USER:-ArchLinux}
export KBUILD_BUILD_TIMESTAMP=${KBUILD_BUILD_TIMESTAMP:-$(date "+%F %T %Z") Made by L-H-T}
prepare() {
cd linux-${_major}
patch -Np1 -i ../patch-${pkgver}-xanmod${xanmod}
msg2 "Setting version..."
scripts/setlocalversion --save-scmversion
echo "-${pkgrel}" > localversion.10-pkgrel
echo "${pkgbase#linux-xanmod-lht}" > localversion.20-pkgname
local src
for src in "${source[@]}"; do
src="${src%%::*}"
src="${src##*/}"
[[ ${src} = *.patch ]] || continue
msg2 "Applying patch ${src}..."
patch -Np1 < "../${src}"
done
cp -vf CONFIGS/xanmod/${_compiler}/config .config
# enable LTO_CLANG_THIN
if [ "${_compiler}" = "clang" ]; then
scripts/config --disable LTO_CLANG_FULL
scripts/config --enable LTO_CLANG_THIN
_LLVM=1
fi
scripts/config --enable CONFIG_STACK_VALIDATION
scripts/config --enable CONFIG_IKCONFIG \
--enable CONFIG_IKCONFIG_PROC
if [ "${use_tracers}" = "n" ]; then
msg2 "Disabling FUNCTION_TRACER/GRAPH_TRACER only if we are not compiling with clang..."
if [ "${_compiler}" = "gcc" ]; then
scripts/config --disable CONFIG_FUNCTION_TRACER \
--disable CONFIG_STACK_TRACER
fi
fi
if [ "${use_numa}" = "n" ]; then
msg2 "Disabling NUMA..."
scripts/config --disable CONFIG_NUMA
fi
if [ "${_compress_modules}" = "y" ]; then
scripts/config --enable CONFIG_MODULE_COMPRESS_ZSTD
fi
sh ${srcdir}/choose-gcc-optimization.sh ${_microarchitecture}
[ "${_microarchitecture}" = "0" ] || scripts/config --disable CONFIG_GENERIC_CPU2
#这适用于想要使用自己的配置构建此包的人,
#将文件"myconfig"放在包文件夹(这将优先)或"${XDG_CONFIG_HOME}/linux-xanmod/myconfig"
#如果我们使用脚本/配置命令检测到部分文件,我们将作为脚本执行,
#如果不是,它是一个完整的配置,将被替换;
for _myconfig in "${SRCDEST}/myconfig" "${HOME}/.config/linux-xanmod/myconfig" "${XDG_CONFIG_HOME}/linux-xanmod/myconfig" ; do
if [ -f "${_myconfig}" ] && [ "$(wc -l <"${_myconfig}")" -gt "0" ]; then
if grep -q 'scripts/config' "${_myconfig}"; then
# myconfig 是一个部分文件,作为脚本执行;
msg2 "Applying myconfig..."
bash -x "${_myconfig}"
else
#myconfig 是一个完整的配置文件,替换默认的 .config
msg2 "Using user CUSTOM config..."
cp -f "${_myconfig}" .config
fi
echo
break
fi
done
#精简内核配置;
###可选地为 make localmodconfig 加载所需的模块;
# See https://aur.archlinux.org/packages/modprobed-db
if [ "${_localmodcfg}" = "y" ]; then
if [ -f ${HOME}/.config/modprobed.db ]; then
msg2 "Running Steven Rostedt's make localmodconfig now"
make LLVM=${_LLVM} LLVM_IAS=${_LLVM} LSMOD=${HOME}/.config/modprobed.db localmodconfig
else
msg2 "No modprobed.db data found"
exit 1
fi
fi
make LLVM=${_LLVM} LLVM_IAS=${_LLVM} olddefconfig
make -s kernelrelease > version
msg2 "Prepared %s version %s" "${pkgbase}" "$(<version)"
if [ "${_makenconfig}" = "y" ]; then
make LLVM=${_LLVM} LLVM_IAS=${_LLVM} nconfig
fi
cat .config > "${SRCDEST}/config.last"
}
build() {
cd linux-${_major}
make LLVM=${_LLVM} LLVM_IAS=${_LLVM} all
}
_package() {
pkgdesc="The Linux kernel and modules with Xanmod patches"
depends=(coreutils kmod initramfs)
optdepends=('crda: to set the correct wireless channels of your country'
'linux-firmware: firmware images needed for some devices')
provides=(VIRTUALBOX-GUEST-MODULES
WIREGUARD-MODULE
KSMBD-MODULE
NTFS3-MODULE)
cd linux-${_major}
local kernver="$(<version)"
local modulesdir="${pkgdir}/usr/lib/modules/${kernver}"
msg2 "Installing boot image..."
install -Dm644 "$(make -s image_name)" "${modulesdir}/vmlinuz"
# Used by mkinitcpio to name the kernel
echo "${pkgbase}" | install -Dm644 /dev/stdin "${modulesdir}/pkgbase"
msg2 "Installing modules..."
make INSTALL_MOD_PATH="${pkgdir}/usr" INSTALL_MOD_STRIP=1 modules_install
rm "${modulesdir}"/{source,build}
}
_package-headers() {
pkgdesc="Headers and scripts for building modules for the ${pkgdesc} kernel"
depends=(pahole)
cd linux-${_major}
local builddir="${pkgdir}/usr/lib/modules/$(<version)/build"
msg2 "Installing build files..."
install -Dt "${builddir}" -m644 .config Makefile Module.symvers System.map \
localversion.* version vmlinux
install -Dt "${builddir}/kernel" -m644 kernel/Makefile
install -Dt "${builddir}/arch/x86" -m644 arch/x86/Makefile
cp -t "${builddir}" -a scripts
install -Dt "${builddir}/tools/objtool" tools/objtool/objtool
if [ -f "${builddir}/tools/bpf/resolve_btfids" ]; then install -Dt "${builddir}/tools/bpf/resolve_btfids" tools/bpf/resolve_btfids/resolve_btfids ; fi
msg2 "Installing headers..."
cp -t "${builddir}" -a include
cp -t "${builddir}/arch/x86" -a arch/x86/include
install -Dt "${builddir}/arch/x86/kernel" -m644 arch/x86/kernel/asm-offsets.s
install -Dt "${builddir}/drivers/md" -m644 drivers/md/*.h
install -Dt "${builddir}/net/mac80211" -m644 net/mac80211/*.h
install -Dt "${builddir}/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h
# https://bugs.archlinux.org/task/20402
install -Dt "${builddir}/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
install -Dt "${builddir}/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
install -Dt "${builddir}/drivers/media/tuners" -m644 drivers/media/tuners/*.h
install -Dt "${builddir}/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h
echo "Installing KConfig files..."
find . -name 'Kconfig*' -exec install -Dm644 {} "${builddir}/{}" \;
msg2 "Removing unneeded architectures..."
local arch
for arch in "${builddir}"/arch/*/; do
[[ ${arch} = */x86/ ]] && continue
echo "Removing $(basename "${arch}")"
rm -r "${arch}"
done
msg2 "Removing documentation..."
rm -r "${builddir}/Documentation"
msg2 "Removing broken symlinks..."
find -L "${builddir}" -type l -printf 'Removing %P\n' -delete
msg2 "Removing loose objects..."
find "${builddir}" -type f -name '*.o' -printf 'Removing %P\n' -delete
msg2 "Stripping build tools..."
local file
while read -rd '' file; do
case "$(file -bi "${file}")" in
application/x-sharedlib\;*) # Libraries (.so)
strip -v ${STRIP_SHARED} "${file}" ;;
application/x-archive\;*) # Libraries (.a)
strip -v ${STRIP_STATIC} "${file}" ;;
application/x-executable\;*) # Binaries
strip -v ${STRIP_BINARIES} "${file}" ;;
application/x-pie-executable\;*) # Relocatable binaries
strip -v ${STRIP_SHARED} "${file}" ;;
esac
done < <(find "${builddir}" -type f -perm -u+x ! -name vmlinux -print0)
msg2 "Stripping vmlinux..."
strip -v ${STRIP_STATIC} "${builddir}/vmlinux"
msg2 "Adding symlink..."
mkdir -p "${pkgdir}/usr/src"
ln -sr "${builddir}" "${pkgdir}/usr/src/${pkgbase}"
}
pkgname=("${pkgbase}" "${pkgbase}-headers")
for _p in "${pkgname[@]}"; do
eval "package_${_p}() {
$(declare -f "_package${_p#${pkgbase}}")
_package${_p#${pkgbase}}
}"
done
# vim:set ts=8 sts=2 sw=2 et:配置MAKEFLAGS变量,使用所有CPU线程编译.
export MAKEFLAGS="-j$(nproc)"构建软件包.
makepkg
配置抢占式内核(低延迟桌面).

设置Linux内核时钟频率.

去掉nouveau.

去掉Nvidia I2C驱动.

去掉不需要的内核模块.
由于每台计算机的硬件配置都可能不同,所以,这里不能给出自己的私有配置.
因为这并不通用,很有可能,你使用了我的配置而导致内核崩溃.
总之一句话,除非具有特殊需求,否则不要过度极端的剪裁内核,.
内核并非越小越好,在适用自己的硬件的前提之上,也得顾及其他外部硬件设备.
所以当你决定使用精简内核时,请仔细参考Modprobed-db.
将Google BBR2TCP拥塞算法编译到内核中.

将KVM虚拟化模块编译到内核中.

安装软件包.
for i in *tar.zst; do
sudo pacman -U ${i}
doen修改配置Grub.
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 processor.max_cstate=1 zswap.enabled=1 zswap.compressor=lz4 nvidia-drm.modeset=1"生成Grub配置文件.
grub-mkconfig -o /boot/grub/grub.cfg最终的Linux xanmod内核 .



