<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>
"); //-->

博客專(zhuān)欄

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

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

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

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

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


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

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

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

比如,一個(gè)電力監測系統必須在10ms內運行一次任務(wù)對電力運行狀況進(jìn)行監測,

一旦時(shí)間不準,調度不到該程序運行,則無(wú)法保證對電力故障的及時(shí)響應。

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

實(shí)時(shí)操作系統相關(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í)現實(shí)時(shí)性系統?

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

3.PREEMPT_RT

PREEMPT_RT是Linux內核的一個(gè)實(shí)時(shí)補丁。得到Linus的高度評價(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


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


具體怎么用可以參考:

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

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



4.編譯內核方法

4.1下載linux內核源碼

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


國內鏡像****:http://mirror.bjtu.edu.cn/kernel/linux/kernel/  ,

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


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

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


4.2下載RT patch

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

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


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

4.3 編譯內核方法

1.解壓內核源碼

2.打patch

3.編譯linux內核并安裝 

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模塊

編譯內核需要libssl-dev模塊

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


 cp /boot/config-4.15.18 ./

make menuconfig

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

             




              



——


                        

編譯內核,再安裝,更新grub.

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

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



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


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


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

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

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

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

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

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

sudo apt-get install rt-tests 

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

sudo cyclictest -t 5 -p 80 -n 
注釋?zhuān)?nbsp;運行五個(gè)線(xiàn)程,線(xiàn)程優(yōu)先級為80,無(wú)限循環(huán)
————————————————

cyclictest運行結果詳解

   

T: 0     序號為0的線(xiàn)程

P: 0     線(xiàn)程優(yōu)先級為0

C: 9397  計數器。線(xiàn)程的時(shí)間間隔每達到一次,計數器加1

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

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

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

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

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

測試結果示例:

1.實(shí)體機測試結果

# /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.虛擬機測試結果 

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

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)行多次運行并統計結果
sudo  cyclictest -l10000000 -m -n -t1 -p99 -i2000 -h100

  

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


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


-n :指定使用 clock_nanosleep,


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


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


-i2000 :指定基本線(xiàn)程間隔,單位是us,


-h100 :指定統計結果的分布情況。


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

大多集中在1-7 us以?xún)取?/span>

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

# /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.總結

實(shí)時(shí)性補丁能夠較好地滿(mǎn)足我們的需求(1ms),實(shí)現較強的實(shí)時(shí)性。

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

ubuntu 和centos 都提供了預編譯的prempt 內核, 只是ubuntu的是lowlatency 

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

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

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

apt-get install linux-lowlatency

7.2 centos  安裝預編譯的實(shí)時(shí)內核

官方 redhat 8 安裝rt內核文檔 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 查看內核當前的實(shí)時(shí)性信息

首先是用 uname -an 查詢(xún)內核信息,本例中可以看到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ò)查看當前內核的配置文件,一般在/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

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



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

相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>