ubuntu 16.04低延迟内核

在某些特殊环境中,比如音视频实时处理(比如MIDI),无线电数据的编解码(比如 OpenAirInterface 明确要求使用低延时内核)等情况下,我们希望系统尽可能的实时,同时又不会降低太多的性能(实时性越高,性能,功耗等的损失越大)。这种情况下,我们可以尝试使用低延时内核。

Linux提供了五种内核类型,对于内核类型的选择,可以参考如下的解释:

*******************************************

  • If you do not require low latency for your system then please use the -generic kernel.
  • If you need a low latency system (e.g. for recording audio) then please use the -preempt kernel as a fist choice. This reduces latency but doesn't sacrifice power saving features. It is available only for 64 bit systems (also called amd64).
  • If the -preempt kernel does not provide enough low latency for your needs (or you have an 32 bit system) then you should try the -lowlatency kernel.
  • If the -lowlatency kernel isn't enough then you should try the -rt kernel
  • If the -rt kernel isn't enough stable for you then you should try the -realtime kernel

*******************************************

目前(2018.02)为止,ubuntu官方库中提供前四种内核,我们一般建议使用lowlatency版本。可以使用如下命令安装:

$ sudo apt-get install linux-lowlatency

$ sudo apt-get install linux-image-`uname -r | cut -d- -f1-2`-lowlatency

$ sudo apt-get install linux-headers-`uname -r | cut -d- -f1-2`-lowlatency

# 重启加载新内核
$ sudo reboot

目前,如果机器上使用了nvidia显卡,并且使用nvidia的闭源驱动的情况下,重启机器会无法进入图形界面,查看系统日志,出现如下错误信息:

$ cat /var/log/syslog | grep dkms
Feb 28 16:23:01 xx-System-Product-Name gpu-manager[831]: Error: can't open /lib/modules/4.13.0-36-lowlatency/updates/dkms

解决方法如下:

#目前最新版本驱动是384版本,如果使用的其他版本,请配置使用的版本

$ sudo dpkg-reconfigure nvidia-384

注意,使用sudo apt-get dist-upgrade升级内核的时候,内核会被替换成generic版本,低延时内核需要手工重新安装一遍。

参考链接


发布者

《ubuntu 16.04低延迟内核》上有9条评论

  1. 您好,我原本在TX2上的内核版本是
    Linux tegra-ubuntu 4.4.38+ #43 SMP PREEMPT Wed Dec 13 10:21:54 CST 2017 aarch64 aarch64 aarch64 GNU/Linux

    为实时性需求,我下载了linux-4.4.15并安装了实时补丁patch-4.4.15-rt23。完成后用update-grub2命令显示
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-4.4.15-rt23
    Found initrd image: /boot/initrd.img-4.4.15-rt23
    Found linux image: /boot/vmlinuz-4.4.15-rt23.old
    Found initrd image: /boot/initrd.img-4.4.15-rt23
    Found Ubuntu 16.04 LTS (16.04) on /dev/mmcblk0p1
    done

    但是重启后不能进入选择系统界面,直接默认进入了原来的ubuntu系统。请问您上面说的是我遇到的这个问题么?您看我这种现象的问题大概在什么地方呢?

        1. 正常情况下,不应该进入备选内核的,应该会进入配置的内核,有种情况会发生内核不进入的情况,就是安装的内核不是对应CPU的内核,比如在ARM上强制安装了X86的内核,在运行之前GRUB会判断CPU然后强制用旧版本内核。
          但是你这个内核在编译的时候最好自定义一下名字,比如“4.13.0-36-lowlatency”,会不会是已经是打过补丁的内核了,但是名字迷惑了呢?

          1. 您说的架构和内核不匹配的情况我觉得有可能存在。
            关于您说的第二点现在我不是很确定,用uname -a显示
            Linux tegra-ubuntu 4.4.38+ #43 SMP PREEMPT Wed Dec 13 10:21:54 CST 2017 aarch64 aarch64 aarch64 GNU/Linux

            其中的PREEMPT让我有些困惑,我不知道这个在我打补丁之前有没有。但我感觉应该这还是原来的内核。我的/boot/下有下面这些:
            drwxr-xr-x 6 root root 4096 Mar 1 15:21 ./
            drwxr-xr-x 22 root root 4096 Dec 13 10:26 ../
            -rw-r--r-- 1 root root 28902 Mar 1 15:04 config-4.4.15-rt23
            -rw-r--r-- 1 root root 28527 Mar 1 14:39 config-4.4.15-rt23.old
            drwxr-xr-x 2 root root 4096 Dec 13 10:23 dtb/
            drwxr-xr-x 3 root root 4096 May 4 2016 efi/
            drwxr-xr-x 2 root root 4096 Dec 13 10:23 extlinux/
            drwxr-xr-x 2 root root 4096 Mar 1 17:28 grub/
            -rw-r--r-- 1 root root 20216320 Dec 13 10:23 Image
            -rw-r--r-- 1 root root 0 Dec 13 10:23 initrd
            -rw-r--r-- 1 root root 7266570 Mar 1 15:09 initrd.img-4.4.15-rt23
            -rw-r--r-- 1 root root 827331 Mar 1 15:04 System.map-4.4.15-rt23
            -rw-r--r-- 1 root root 527021 Mar 1 14:39 System.map-4.4.15-rt23.old
            -rw-r--r-- 1 ubuntu ubuntu 267595 Jul 20 2017 tegra186-quill-p3310-1000-a00-00-base.dtb
            -rw-r--r-- 1 ubuntu ubuntu 267760 Dec 13 10:23 tegra186-quill-p3310-1000-c03-00-base.dtb
            -rw-r--r-- 1 ubuntu ubuntu 267816 Jul 20 2017 tegra186-quill-p3310-1000-c03-00-dsi-hdmi-dp.dtb
            -rw-r--r-- 1 root root 2990592 Mar 1 15:04 vmlinuz-4.4.15-rt23
            -rw-r--r-- 1 root root 2727936 Mar 1 14:39 vmlinuz-4.4.15-rt23.old

            按说我的命名是4.4.15版本。您说的改变名称是在grub.cfg里面的?

          2. $ make oldconfig

            $ make-kpkg --initrd kernel-image

            一般是用上面的命令编译内核的,上面命令中 make-kpkg 可以增加一个参数指定,如果没有指定,默认出来的内核名字是“linux-image-4.13.0-36.Custom_amd64.deb” 这个样子的“Custom”部分就是内核名字,比如指定成“lowlatency”
            那“uname -r” 输出就从“4.13.0-36-Custom”变成了“4.13.0-36-lowlatency” ,另外注意最后的“amd64” 部分,就是CPU型号了,如果你编译正确,那最后应该是“aarch64”或者“arm”或者"arm64"都是可能的。

        2. 另外内核我们都是编译成DEB安装包,然后使用dpkg命令安装的,如果手工替换,可能会遗漏一些东西,导致不正常

          1. 我觉得是之前的内核的原因是我的RT测试结果和原先的改变不大。
            nvidia@tegra-ubuntu:~$ sudo cyclictest -t5 -p80 -m -n -l100000
            # /dev/cpu_dma_latency set to 0us
            policy: fifo: loadavg: 2.53 1.84 0.89 1/508 2423

            T: 0 ( 2149) P:80 I:1000 C: 99999 Min: 5 Act: 25 Avg: 29 Max: 8848
            T: 1 ( 2150) P:80 I:1500 C: 66681 Min: 5 Act: 23 Avg: 31 Max: 325
            T: 2 ( 2151) P:80 I:2000 C: 49976 Min: 5 Act: 19 Avg: 34 Max: 8976
            T: 3 ( 2152) P:80 I:2500 C: 40000 Min: 5 Act: 25 Avg: 35 Max: 8237
            T: 4 ( 2153) P:80 I:3000 C: 33335 Min: 5 Act: 23 Avg: 28 Max: 6860

            这是插拔USB 硬中断时的抖动。

          2. 本身延时抖动就不会变化太大,内核只是把中断周期修改了一些,中断的频率更快,有个"CONFIG_HZ"的参数需要调整一下的,这个根据你需要的精度调整,默认这个很保守,我记得就仅仅是从100修改成了250的样子,默认修改很少的,需要根据实际需求修改。要注意的是,数字越大,切换的频率越快,CPU会更多的浪费在上下文切换上,性能会降低的更多,但是响应会更积极,这是一个取舍的过程。

回复 郭萌 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注