博客專欄

EEPW首頁(yè) > 博客 > 實(shí)時(shí)Linux內(nèi)核(PREEMPT_RT)的編譯安裝以及測(cè)試

實(shí)時(shí)Linux內(nèi)核(PREEMPT_RT)的編譯安裝以及測(cè)試

發(fā)布人:電子禪石 時(shí)間:2024-01-27 來(lái)源:工程師 發(fā)布文章

1.什么是實(shí)時(shí)性操作系統(tǒng)?

https://blog.csdn.net/ywx123_/article/details/53861274


實(shí)時(shí)性是指調(diào)度的時(shí)候,任務(wù)響應(yīng)時(shí)間。windows一般是15ms,最大的問(wèn)題是不能保證。

比如平均值是1ms,但是隨著系統(tǒng)負(fù)載的變化,有時(shí)甚至達(dá)到100ms,

在這工業(yè)上是無(wú)法使用的。工業(yè)上一些應(yīng)用要求必須有更高的時(shí)間精度,

比如,一個(gè)電力監(jiān)測(cè)系統(tǒng)必須在10ms內(nèi)運(yùn)行一次任務(wù)對(duì)電力運(yùn)行狀況進(jìn)行監(jiān)測(cè),

一旦時(shí)間不準(zhǔn),調(diào)度不到該程序運(yùn)行,則無(wú)法保證對(duì)電力故障的及時(shí)響應(yīng)。

————————————————

實(shí)時(shí)操作系統(tǒng)相關(guān)論文:


http://www.cs.columbia.edu/~sedwards/classes/2001/w4995-02/reports/

shamil-kalpen.pdf


https://www.researchgate.net/publication/3151063_Performance_Comparison_

of_VxWorks_Linux_RTAI_and_Xenomai_in_a_Hard_Real-time_Application


https://www.researchgate.net/publication/273759417_Performance_Comparison_

of_Real-Time_and_General-Purpose_Operating_Systems_in_Parallel_Physical_

Simulation_with_High_Computational_Cost


https://elinux.org/images/d/de/Real_Time_Linux_Scheduling_Performance_

Comparison.pdf


https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.21.9705&rep=

rep1&type=pdf

————————————————

2.怎么實(shí)現(xiàn)實(shí)時(shí)性系統(tǒng)?

https://blog.csdn.net/lu_embedded/article/details/52485527

3.PREEMPT_RT

PREEMPT_RT是Linux內(nèi)核的一個(gè)實(shí)時(shí)補(bǔ)丁。得到Linus的高度評(píng)價(jià):


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


項(xiàng)目主頁(yè)見(jiàn):https://rt.wiki.kernel.org/index.php/Main_Page


具體怎么用可以參考:

https://wiki.linuxfoundation.org/realtime/documentation/howto/applications/

preemptrt_setup  ,這是官方的說(shuō)明。



4.編譯內(nèi)核方法

4.1下載linux內(nèi)核源碼

****:https://mirrors.edge.kernel.org/pub/linux/kernel/


國(guó)內(nèi)鏡像****:http://mirror.bjtu.edu.cn/kernel/linux/kernel/  ,

http://mirror.tuna.tsinghua.edu.cn/kernel/


這兩個(gè)國(guó)內(nèi)的可以看一下,速度比較快。

————————————————


4.2下載RT patch

patch在https://rt.wiki.kernel.org/index.php/Main_Page 下載,點(diǎn)擊某個(gè)版本,

可能有點(diǎn)舊,進(jìn)到鏈接里面后,選版本號(hào)新的就可以了,與linux內(nèi)核的版本號(hào)一定要一樣。


更多下載地址: https://mirrors.edge.kernel.org/pub/linux/kernel/projects/rt/

4.3 編譯內(nèi)核方法

1.解壓內(nèi)核源碼

2.打patch

3.編譯linux內(nèi)核并安裝 

tar xzvf  linux-4.4.138.tar.gz
cd linux-4.4.138
patch -p1 < ../patch-4.4.138-rt65.patch

————————————————


make menuconfig需要安裝這個(gè)libncurses-dev模塊

編譯內(nèi)核需要libssl-dev模塊

sudo apt-get install libncurses-dev   bison   flex  bc   libelf-dev
sudo apt-get install libssl-dev
配置linux內(nèi)核
#據(jù)說(shuō)下載包里自帶的.config文件可能導(dǎo)致打補(bǔ)丁失敗,
另一種辦法是將原linux系統(tǒng)中的.config文件拷貝到內(nèi)核文件夾,
這樣編譯出來(lái)的內(nèi)核與本機(jī)兼容性更好


 cp /boot/config-4.15.18 ./

make menuconfig

選擇Processor type and feature   —>   Fully Preemptible Kernel (RT)這個(gè)選項(xiàng),具體路徑如下圖。保存。(2021.3.10更新,內(nèi)核5.0以上的不是這樣的路徑?)

             




              



——


                        

編譯內(nèi)核,再安裝,更新grub.

編譯時(shí)加上 INSTALL_MOD_STRIP=1 可以生成沒(méi)有調(diào)試信息的模塊,大大減小內(nèi)核的大小。

// 編譯
make -j2
// 如果直接安裝到本機(jī)上,直接make install 即可
// make install 會(huì)自動(dòng)執(zhí)行 update-grub2
sudo make install -j



幾個(gè)可選的命令和參數(shù):
// make modules_install 用于安裝/lib/modules目錄下的驅(qū)動(dòng), 
安裝的模塊可能含有調(diào)試信息,文件非常大,
可以使用INSTALL_MOD_STRIP選項(xiàng)去除調(diào)試信息
sudo make INSTALL_MOD_STRIP=1 modules_install  


如果不希望將內(nèi)核安裝到本機(jī),比如將內(nèi)核放到某個(gè)目錄,
然后復(fù)制到另一臺(tái)機(jī)器上,用 INSTALL_MOD_PATH指定modules安裝目錄, 
INSTALL_PATH 指定內(nèi)核安裝路徑:
make INSTALL_MOD_PATH=~/work/linux/bin   modules_install
make  INSTALL_PATH=~/work/linux/bin/boot  install


// 對(duì)于嵌入式板子還需要安裝dtbs,用INSTALL_DTBS_PATH指定安裝的路徑。
x86上不需要dtbs
make  INSTALL_DTBS_PATH=~/work/linux/bin  dtbs_install 
————————————————

重啟后會(huì)多一個(gè)  linux-4.4.138-rt  的啟動(dòng)選項(xiàng)。
如果要在grub界面停一下,選擇內(nèi)核,需要修改/etc/default/grub文件
# 注釋掉下面這行將會(huì)顯示引導(dǎo)菜單
#GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=5

5.cyclictest測(cè)試實(shí)時(shí)性
可參考以下網(wǎng)頁(yè):

https://blog.csdn.net/kl1125290220/article/details/78560220

https://blog.csdn.net/longerzone/article/details/16897655

cyclictest 講的非常詳細(xì) https://zhuanlan.zhihu.com/p/336381111

sudo apt-get install rt-tests 

安裝這個(gè)工具,運(yùn)行cyclictest程序進(jìn)行實(shí)時(shí)性測(cè)試:

sudo cyclictest -t 5 -p 80 -n 
注釋: 運(yùn)行五個(gè)線程,線程優(yōu)先級(jí)為80,無(wú)限循環(huán)
————————————————

cyclictest運(yùn)行結(jié)果詳解

   

T: 0     序號(hào)為0的線程

P: 0     線程優(yōu)先級(jí)為0

C: 9397  計(jì)數(shù)器。線程的時(shí)間間隔每達(dá)到一次,計(jì)數(shù)器加1

I: 1000  時(shí)間間隔為1000微秒(us)

Min:     最小延時(shí)(us)

Act:     最近一次的延時(shí)(us)

Avg:    平均延時(shí)(us)

Max:    最大延時(shí)(us)   

測(cè)試結(jié)果示例:

1.實(shí)體機(jī)測(cè)試結(jié)果

# /dev/cpu_dma_latency set to 0us
policy: fifo: loadavg: 0.13 0.06 0.02 1/244 19255
 
T: 0 (18989) P:80 I:1000 C: 155947 Min:      1 Act:    1 Avg:    1 Max:      17
T: 1 (18990) P:80 I:1500 C: 103964 Min:      1 Act:    1 Avg:    1 Max:      15
T: 2 (18991) P:80 I:2000 C:  77973 Min:      1 Act:    1 Avg:    1 Max:       7
T: 3 (18992) P:80 I:2500 C:  62378 Min:      1 Act:    1 Avg:    1 Max:      10
T: 4 (18993) P:80 I:3000 C:  51982 Min:      1 Act:    1 Avg:    1 Max:       7

2.虛擬機(jī)測(cè)試結(jié)果 

在虛擬機(jī)中效果一般,受主機(jī)的限制太大。

my@ubuntu:~/rt/linux-4.4.138$ sudo cyclictest -p 80 -t5 -n 
# /dev/cpu_dma_latency set to 0us
policy: fifo: loadavg: 0.69 0.19 0.07 1/726 5825          

T: 0 ( 5821) P:80 I:1000 C:   8168 Min:      7 Act:  445 Avg:  327 Max:    7005
T: 1 ( 5822) P:80 I:1500 C:   5455 Min:      7 Act:  411 Avg:  319 Max:    7516
T: 2 ( 5823) P:80 I:2000 C:   4098 Min:     14 Act:  174 Avg:  320 Max:    2362
T: 3 ( 5824) P:80 I:2500 C:   3275 Min:      5 Act:   52 Avg:  319 Max:    6940
T: 4 ( 5825) P:80 I:3000 C:   2732 Min:      8 Act:  214 Avg:  299 Max:    5198
————————————————
3.也可以進(jìn)行多次運(yùn)行并統(tǒng)計(jì)結(jié)果
sudo  cyclictest -l10000000 -m -n -t1 -p99 -i2000 -h100

  

-l10000000 :指定1千萬(wàn)循環(huán),


-m :鎖定當(dāng)前和將來(lái)的內(nèi)存分配,


-n :指定使用 clock_nanosleep,


-t1 :指定開(kāi)一個(gè)線程進(jìn)行測(cè)試,


-p99 :指定最高優(yōu)先級(jí),


-i2000 :指定基本線程間隔,單位是us,


-h100 :指定統(tǒng)計(jì)結(jié)果的分布情況。


程序執(zhí)行結(jié)束后,輸出顯示平均延時(shí)1 us,最大延時(shí)15 us,通過(guò)直方分布圖察看,

大多集中在1-7 us以內(nèi)。

————————————————

# /dev/cpu_dma_latency set to 0us
policy: fifo: loadavg: 0.36 0.33 0.28 1/246 32690
 
T: 0 (32688) P:99 I:2000 C:10000000 Min:      0 Act:    2 Avg:    1 Max:       15
# Histogram
000000 000051
000001 6635143
000002 3352561
000003 008966
000004 002414
000005 000648
000006 000180
000007 000025
000008 000002
000009 000001
000010 000000
000011 000000
000012 000001
000013 000001
000014 000006
000015 000001
…………(中間都是0,省略)
000097 000000
000098 000000
000099 000000
# Total: 010000000
# Min Latencies: 00000
# Avg Latencies: 00001
# Max Latencies: 00009
# Histogram Overflows: 00000
# Histogram Overflow at cycle number:
# Thread 0:
6.總結(jié)

實(shí)時(shí)性補(bǔ)丁能夠較好地滿足我們的需求(1ms),實(shí)現(xiàn)較強(qiáng)的實(shí)時(shí)性。

7.無(wú)需動(dòng)手編譯,直接安裝預(yù)編譯內(nèi)核的方法

ubuntu 和centos 都提供了預(yù)編譯的prempt 內(nèi)核, 只是ubuntu的是lowlatency 

低延時(shí)內(nèi)核,centos的是rt kernel最高級(jí)實(shí)時(shí)

7.1 ubuntu 安裝低延時(shí)內(nèi)核

參考https://linuxmusicians.com/viewtopic.php?t=18536

apt-get install linux-lowlatency

7.2 centos  安裝預(yù)編譯的實(shí)時(shí)內(nèi)核

官方 redhat 8 安裝rt內(nèi)核文檔 https://access.redhat.com/documentation/en-us/

red_hat_enterprise_linux_for_real_time/8/html-single/installation_guide/index


參考https://unix.stackexchange.com/questions/341933/install-a-real-time-kernel-on-

centos

————————————————

sudo tee /etc/yum.repos.d/CentOS-rt.repo >/dev/null <<EOF
# CentOS-rt.repo
[rt]
name=CentOS-7 - rt
baseurl=http://mirror.centos.org/centos/\$releasever/rt/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF
 
 
sudo yum update -y
sudo yum install -y kernel-rt rt-tests tuned-profiles-realtime
sudo reboot
8 查看內(nèi)核當(dāng)前的實(shí)時(shí)性信息

首先是用 uname -an 查詢內(nèi)核信息,本例中可以看到PREEMPT,但這不是最高的實(shí)時(shí)性。

PREEMPT_RT才是最高的實(shí)時(shí)性。

 
ubuntu@ubuntu:~$ uname -an
Linux ubuntu 5.4.0-1045-raspi #49-Ubuntu SMP PREEMPT Wed 
Sep 29 17:49:16 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux

也可以通過(guò)查看當(dāng)前內(nèi)核的配置文件,一般在/boot目錄下,或/proc目錄下

/boot/config-***

/proc/config.gz

查找以下的字符串,不同的字符串有不同的含義:

PREEMPT_NONE
        bool "No Forced Preemption (Server)"
 
PREEMPT_VOLUNTARY
        bool "Voluntary Kernel Preemption (Desktop)"
 
PREEMPT
        bool "Preemptible Kernel (Low-Latency Desktop)"
 
PREEMPT_RT
        bool "Fully Preemptible Kernel (Real-Time)"

9 關(guān)于PREEMPT的理論方面的介紹

A realtime preemption overview [LWN.net]


realtime:documentation:technical_details:start [Wiki]


https://elinux.org/images/d/de/Real_Time_Linux_Scheduling_

Performance_Comparison.pdf

————————————————

用到的命令行
mkdir -p /usr/src/kernels
cd /usr/src/kernels
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.9.47.tar.xz
wget https://cdn.kernel.org/pub/linux/kernel/projects/rt/4.9/ 
older/patch-4.9.47-rt37.patch.xz
tar xf linux-4.9.47.tar.xz
mv linux-4.9.47 linux-4.9.47-rt37
cd linux-4.9.47-rt37
xz -d ../patch-4.9.47-rt37.patch.xz
patch -p1 <../patch-4.9.47-rt37.patch

cp /boot/config-4.9.0-4-amd64 .config

In the last step, before the kernel can be compiled, the new kernel has
 to be configured so that the functionality imported with the RT patch 
 is also used. The command make menuconfig is called and we select Processor 
 type and features -> Preemption Model -> Fully Preemptible Kernel (RT).
————————————————

原文鏈接:https://blog.csdn.net/v6543210/article/details/80941906

*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



關(guān)鍵詞: Linux-RT

相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉