Ubuntu22.04编译安装实时内核
下载安装
1.查看自己系统目前的内核版本
uname -r
2.下载安装实时内核文件
目前系统版本是Ubuntu 22.04.5 LTS,内核版本是 6.8.0-40。因此我需要安装6.8.2版本的PREEMPT_RT内核,在这个网站。(安装和当前默认内核最接近的RT内核,遇到问题的可能性最小)
下载 patch-6.8.2-rt11.patch.gz
…/
incr/ 03-Apr-2024 15:36 -
older/ 03-Apr-2024 15:36 -
patch-6.8.2-rt11.patch.gz 03-Apr-2024 15:36 59K
patch-6.8.2-rt11.patch.sign 03-Apr-2024 15:36 438
patch-6.8.2-rt11.patch.xz 03-Apr-2024 15:36 51K
patches-6.8.2-rt11.tar.gz 03-Apr-2024 15:36 106K
patches-6.8.2-rt11.tar.sign 03-Apr-2024 15:36 438
patches-6.8.2-rt11.tar.xz 03-Apr-2024 15:36 83K
sha256sums.asc 03-Apr-2024 15:40 1253
这个网站linuxfoundation可以查看最新的PREEMPT_RT内核。
创建一个新的文件夹:
mkdir ~/kernel
进入这个文件夹:
cd ~/kernel
从这个网站 https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/ 去下载 6.8.2版本的内核文件:
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/linux-6.8.2.tar.gz
你也可以手动下载这个文件,然后复制到kernel文件夹。
解压:
tar -xzf linux-6.8.2.tar.gz
下载对应的rt_preempt patch, 对我们来说就是刚刚的patch-6.8.2-rt11.patch.gz
wget https://cdn.kernel.org/pub/linux/kernel/projects/rt/6.8/patch-6.8.2-rt11.patch.gz
你也可以手动下载,然后拷贝到kernel文件夹。
解压
gunzip patch-6.8.2-rt11.patch.gz
切换目录,打实时补丁
cd linux-*/
patch -p1 < ../patch-*.patch
复制当前引导的内核配置作为新实时内核的默认配置
cp -v /boot/config-$(uname -r) .config
打开“软件和更新”。在 Ubuntu 软件菜单中勾选“源代码”框
我们需要一些工具来构建内核,安装它们:
sudo apt-get build-dep linux
sudo apt-get install zstd
sudo apt-get install libncurses-dev flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf fakeroot
启用所有 Ubuntu 配置,我们只需使用:
yes '' | make oldconfig
然后我们需要在内核中启用rt_preempt:
make menuconfig
并设置以下内容:
# Enable CONFIG_PREEMPT_RT
-> General Setup
-> Preemption Model (Fully Preemptible Kernel (Real-Time))
(X) Fully Preemptible Kernel (Real-Time)
# Enable CONFIG_HIGH_RES_TIMERS
-> General setup
-> Timers subsystem
[*] High Resolution Timer Support
# Enable CONFIG_NO_HZ_FULL
-> General setup
-> Timers subsystem
-> Timer tick handling (Full dynticks system (tickless))
(X) Full dynticks system (tickless)
# Set CONFIG_HZ_1000 (note: this is no longer in the General Setup menu, go back twice)
-> Processor type and features
-> Timer frequency (1000 HZ)
(X) 1000 HZ
# Set CPU_FREQ_DEFAULT_GOV_PERFORMANCE [=y]
-> Power management and ACPI options
-> CPU Frequency scaling
-> CPU Frequency scaling (CPU_FREQ [=y])
-> Default CPUFreq governor ( [=y])
(X) performance
-> Cryptographic API
-> Certificates for signature checking (at the very bottom of the list)
-> Provide system-wide ring of trusted keys
-> Additional X.509 keys for default system keyring
Remove the “debian/canonical-certs.pem” from the prompt and press Ok
-> Cryptographic API
-> Certificates for signature checking (at the very bottom of the list)
-> Provide system-wide ring of trusted keys
-> X.509 certificates to be preloaded into the system blacklist keyring
Remove the “debian/canonical-certs.pem” from the prompt and press Ok
保存以上配置,退出 。
编译内核:
make -j `nproc` bindeb-pkg
如果需要编译src pkg,请运行"make deb-pkg",但是你必须通过git拉取linux kernel的源代码树,并checkout v6.8.2 tag,然后按上面的过程打实时补丁,并修改内核配置文件。
git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/stable/linux.git
git checkout v6.8.2
根据CPU性能大概需要10-30分钟。
然后我们安装所有内核 debian 软件包
ls -l ../*.deb
-rw-r--r-- 1 zt zt 9881878 10月 27 14:10 ../linux-headers-6.8.2-rt11_6.8.2-5_amd64.deb
-rw-r--r-- 1 zt zt 173899106 10月 27 14:09 ../linux-image-6.8.2-rt11_6.8.2-5_amd64.deb
-rw-r--r-- 1 zt zt 1320705494 10月 27 14:44 ../linux-image-6.8.2-rt11-dbg_6.8.2-5_amd64.deb
-rw-r--r-- 1 zt zt 1398982 10月 27 14:09 ../linux-libc-dev_6.8.2-5_amd64.deb
sudo dpkg -i ../*.deb
现在应该安装实时内核了。重新启动系统并检查新的内核版本
sudo reboot
zt@zt-ROScube-I-ET:~$ uname -a
Linux zt-ROScube-I-ET 6.8.2-rt11 #5 SMP PREEMPT_RT Sun Oct 27 13:59:50 CST 2024 x86_64 x86_64 x86_64 GNU/Linux
测试实时性
1.git 仓库源码安装
git clone git://git.kernel.org/pub/scm/linux/kernel/git/clrkwllms/rt-tests.git
2.进入git仓库
cd rt-tests
3.安装依赖并使用make编译
sudo apt-get install apt-file libnuma-dev
4.编译
make
4.测试
sudo ./cyclictest -t1 -p 80 -n -i 10000 -l 10000
我使用这个命令测试的结果如下:
# /dev/cpu_dma_latency set to 0us
policy: fifo: loadavg: 0.28 0.31 0.19 1/685 2625
T: 0 ( 2587) P:80 I:10000 C: 10000 Min: 2 Act: 2 Avg: 2 Max: 11
输出结果含义:
T: 0 序号为0的线程
P: 80 线程优先级为80
C: 7348 计数器。线程的时间间隔每达到一次,计数器加1
I: 10000 时间间隔为10000微秒(us)
Min: 最小延时(us)
Act: 最近一次的延时(us)
Avg:平均延时(us)
Max: 最大延时(us)
所以我们当前的机器上最小延时为2,平均为2,最大的为 11。