• <li id="00i08"><input id="00i08"></input></li>
  • <sup id="00i08"><tbody id="00i08"></tbody></sup>
    <abbr id="00i08"></abbr>
  • 博客專欄

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

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

    發布人:電子禪石 時間:2024-01-27 來源:工程師 發布文章

    1.什么是實時性操作系統?

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


    實時性是指調度的時候,任務響應時間。windows一般是15ms,最大的問題是不能保證。

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

    在這工業上是無法使用的。工業上一些應用要求必須有更高的時間精度,

    比如,一個電力監測系統必須在10ms內運行一次任務對電力運行狀況進行監測,

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

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

    實時操作系統相關論文:


    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.怎么實現實時性系統?

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

    3.PREEMPT_RT

    PREEMPT_RT是Linux內核的一個實時補丁。得到Linus的高度評價:


    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


    項目主頁見:https://rt.wiki.kernel.org/index.php/Main_Page


    具體怎么用可以參考:

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

    preemptrt_setup  ,這是官方的說明。



    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/


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

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


    4.2下載RT patch

    patch在https://rt.wiki.kernel.org/index.php/Main_Page 下載,點擊某個版本,

    可能有點舊,進到鏈接里面后,選版本號新的就可以了,與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需要安裝這個libncurses-dev模塊

    編譯內核需要libssl-dev模塊

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


     cp /boot/config-4.15.18 ./

    make menuconfig

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

                 




                  



    ——


                            

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

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

    // 編譯
    make -j2
    // 如果直接安裝到本機上,直接make install 即可
    // make install 會自動執行 update-grub2
    sudo make install -j
    
    
    
    幾個可選的命令和參數:
    // make modules_install 用于安裝/lib/modules目錄下的驅動, 
    安裝的模塊可能含有調試信息,文件非常大,
    可以使用INSTALL_MOD_STRIP選項去除調試信息
    sudo make INSTALL_MOD_STRIP=1 modules_install  
    
    
    如果不希望將內核安裝到本機,比如將內核放到某個目錄,
    然后復制到另一臺機器上,用 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 
    ————————————————
    
    重啟后會多一個  linux-4.4.138-rt  的啟動選項。
    如果要在grub界面停一下,選擇內核,需要修改/etc/default/grub文件
    # 注釋掉下面這行將會顯示引導菜單
    #GRUB_TIMEOUT_STYLE=hidden
    GRUB_TIMEOUT=5
    
    5.cyclictest測試實時性
    可參考以下網頁:
    
    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 
    
    安裝這個工具,運行cyclictest程序進行實時性測試:
    
    sudo cyclictest -t 5 -p 80 -n 
    注釋: 運行五個線程,線程優先級為80,無限循環
    ————————————————

    cyclictest運行結果詳解

       

    T: 0     序號為0的線程

    P: 0     線程優先級為0

    C: 9397  計數器。線程的時間間隔每達到一次,計數器加1

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

    Min:     最小延時(us)

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

    Avg:    平均延時(us)

    Max:    最大延時(us)   

    測試結果示例:

    1.實體機測試結果

    # /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.也可以進行多次運行并統計結果
    sudo  cyclictest -l10000000 -m -n -t1 -p99 -i2000 -h100

      

    -l10000000 :指定1千萬循環,


    -m :鎖定當前和將來的內存分配,


    -n :指定使用 clock_nanosleep,


    -t1 :指定開一個線程進行測試,


    -p99 :指定最高優先級,


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


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


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

    大多集中在1-7 us以內。

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

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

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

    7.無需動手編譯,直接安裝預編譯內核的方法

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

    低延時內核,centos的是rt kernel最高級實時

    7.1 ubuntu 安裝低延時內核

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

    apt-get install linux-lowlatency

    7.2 centos  安裝預編譯的實時內核

    官方 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 查看內核當前的實時性信息

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

    PREEMPT_RT才是最高的實時性。

     
    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

    也可以通過查看當前內核的配置文件,一般在/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 關于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

    *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



    關鍵詞: Linux-RT

    相關推薦

    技術專區

    關閉
    主站蜘蛛池模板: 漠河县| 正镶白旗| 昌宁县| 沁阳市| 霸州市| 库尔勒市| 潜江市| 尤溪县| 时尚| 新龙县| 尼木县| 乌鲁木齐市| 嵊州市| 门头沟区| 申扎县| 攀枝花市| 正镶白旗| 金平| 和平县| 平顺县| 吉安市| 湘潭市| 黄石市| 尖扎县| 灌阳县| 曲阳县| 新巴尔虎左旗| 阿勒泰市| 高雄县| 万宁市| 左云县| 青海省| 华蓥市| 泗洪县| 冷水江市| 昌黎县| 成安县| 阳曲县| 朝阳市| 和田市| 衡山县|