Linux 内核实时补丁PREEMPT_RT 安装

时间:2023-01-21 08:52:11
Controlling a laser with Linux is crazy, but everyone in this room is crazy in his own way. So if you want to use Linux to control an industrial welding laser, I have no problem with your using PREEMPT_RT." -- Linus Torvalds


一、RTPREEMT_RT简介

标准的Linux内核只能能够满足软中断的要求,为用户空间提供简基本的Posix操作,但是不对固定的时间点做保证。Ingo Molnar's 的实时抢占补丁(文中为:PREEMPT_RT)和Thomas Gleixner的具有高分辨率的通用时钟事件层,Linux增加了硬事实能力。

PREEMPT_RT补丁因起了工业界的关注。由于它简洁的设计和与内核的mainline的一致性,所以,它的硬实时和企业实时应用程序出现在专业的视频到工业控制领域。(根据[1]翻译)


二、获取PREEMPT_RT

方法一:通过git下载最新的代码

http://git.kernel.org/?p=linux/kernel/git/rostedt/linux-rt.git;a=summary 

这样下载后的代码不需要打补丁,已经是最新的代码了。


方法二: 分别下载内核代码和补丁

Kernel:http://www.kernel.org/pub/linux/kernel/

PREEMPT_RT:http://www.kernel.org/pub/linux/kernel/projects/rt/

下载的内核和补丁要对应,如kernellinux-2.6.33.7.tar.bz2,补丁为patch-2.6.33.7.2-rt30.bz2


三、打补丁的方法(通过git获取的不需要打补丁)

$tar xfv linux-2.6.33.7.tar.bz2 $ cd linux-2.6.33.7 $ bzcat ../patch-2.6.33.7.2-rt30.bz2 | patch -p1

四、内核配置

$make menuconfig

4.1在“Processor type and features”

(1)选择“Complete Preemption(Real-Time)”

Linux 内核实时补丁PREEMPT_RT 安装

(2)选择“Thread Softirqs”和“Thread Hardirqs”

Linux 内核实时补丁PREEMPT_RT 安装

2. 在”Device Drivers”中,去掉“Staging Drivers”

Linux 内核实时补丁PREEMPT_RT 安装

由于Staging不稳定,导致编译报错[2]

LD [M] drivers/net/bnx2x.o

make: *** [drivers]错误2

unanao@debian:~/Experiment/linux/linux-2.6.33.7$ make

CHK include/linux/version.h

CHK include/generated/utsrelease.h

CALL scripts/checksyscalls.sh

CHK include/generated/compile.h

VDSOSYM arch/x86/vdso/vdso-syms.lds

VDSOSYM arch/x86/vdso/vdso32-int80-syms.lds

VDSOSYM arch/x86/vdso/vdso32-syscall-syms.lds

VDSOSYM arch/x86/vdso/vdso32-sysenter-syms.lds

VDSOSYM arch/x86/vdso/vdso32-syms.lds

LD arch/x86/vdso/built-in.o

LD arch/x86/built-in.o

LD drivers/staging/built-in.o

CC [M] drivers/staging/comedi/drivers/quatech_daqp_cs.o

drivers/staging/comedi/drivers/quatech_daqp_cs.c:70: error: field ‘eos’ has incomplete type

drivers/staging/comedi/drivers/quatech_daqp_cs.c: In function ‘daqp_interrupt’:

drivers/staging/comedi/drivers/quatech_daqp_cs.c:291: error: implicit declaration of function ‘up’

drivers/staging/comedi/drivers/quatech_daqp_cs.c: In function ‘daqp_ai_insn_read’:

drivers/staging/comedi/drivers/quatech_daqp_cs.c:405: error: implicit declaration of function ‘sema_init’

drivers/staging/comedi/drivers/quatech_daqp_cs.c:418: error: implicit declaration of function ‘down_interruptible’

drivers/staging/comedi/drivers/quatech_daqp_cs.c: In function ‘daqp_cs_attach’:

drivers/staging/comedi/drivers/quatech_daqp_cs.c:1045: warning: assignment from incompatible pointer type

make[4]: *** [drivers/staging/comedi/drivers/quatech_daqp_cs.o]错误1

make[3]: *** [drivers/staging/comedi/drivers]错误2

make[2]: *** [drivers/staging/comedi]错误2

make[1]: *** [drivers/staging]错误2

make: *** [drivers]错误2


五、编译

$make -jn

(n是要衍生的作业书,在实际中,每个处理器上一般衍生一个活两个作业。例如在一个双和处理器上可以使用$make -j4)[3]

make install

make modules_install

#cd /boot

#mkinitramfs -k -o initrd.img-2.6.33.7.2-rt 2.6.33.7.2-rt30

(这一步是必须的,否则是起不来系统的)


六、修改grub.cfg

grub.cfg中找到debian原有的一段配置文件,修改红色部分就可以了。

menuentry 'Debian GNU/Real Time Linux, with Linux 2.6.33.7.2-rt30' --class debian --class gnu-linux --class gnu --class os {

insmod part_msdos

insmod ext2

set root='(hd0,msdos2)'

search --no-floppy --fs-uuid --set a9f7fdc4-3975-4e23-aa13-6e22d35d6ad9

echo 'Loading Linux 2.6.33.7.2--rt30 ...'

linux /boot/vmlinuz-2.6.33.7.2-rt30 root=UUID=a9f7fdc4-3975-4e23-aa13-6e22d35d6ad9 ro quiet

echo 'Loading initial ramdisk ...'

initrd /boot/initrd.img-2.6.33.7.2-rt

}

修改后,重启就可以选择进入系统了


七、基准测试

在这里下载基准测试程序:

http://www.kernel.org/pub/linux/kernel/people/tglx/rt-tests/

下载后解压,

$make all

测试:

非实时内核:

root@debian:/home/unanao/Experiment/linux/rt-tests# ./cyclictest -t1 -p 80 -n -i 10000 -l 10000

policy: fifo: loadavg: 0.02 0.16 0.14 2/303 3109

T: 0 ( 2599) P:80 I:10000 C: 10000 Min: 8 Act: 48 Avg: 108 Max: 8294

root@debian:/home/unanao/Experiment/linux/rt-tests# ./cyclictest -t1 -p 80 -i 500 -l 100000

policy: fifo: loadavg: 0.21 0.14 0.12 1/306 3129

T: 0 ( 2690) P:80 I:500 C: 10000 Min: 10 Act: 31 Avg: 77 Max: 7943


实时内核:

root@debian:/home/unanao/Experiment/linux/rt-tests# ./cyclictest -t1 -p 80 -n -i 10000 -l 10000

policy: fifo: loadavg: 0.02 0.16 0.14 2/303 3109

T: 0 ( 3107) P:80 I:10000 C: 10000 Min: 6 Act: 54 Avg: 65 Max: 282

root@debian:/home/unanao/Experiment/linux/rt-tests# ./cyclictest -t1 -p 80 -i 500 -l 100000

policy: fifo: loadavg: 0.21 0.14 0.12 1/306 3129

T: 0 ( 3129) P:80 I:500 C: 100000 Min: 12 Act: 75 Avg: 91 Max: 418

比较可知实时内核的实时性更好。

八、参考文档

[1]https://rt.wiki.kernel.org/index.php/RT_PREEMPT_HOWTO

[2]http://comments.gmane.org/gmane.linux.rt.user/5780

[3]Linux内核设计与实现》