Ubuntu 实时性优化方案目标是把延迟抖动压到50us、甚至 30us和你现在的 fast_shm 数据匹配。一、内核选择最关键1低延迟内核先试简单bash运行sudo apt update sudo apt install -y linux-lowlatency linux-headers-lowlatency sudo reboot验证bash运行uname -a | grep lowlatency2PREEMPT_RT 实时内核硬实时推荐Ubuntu 22.04/24.04 可用官方源免费个人用bash运行sudo pro attach # 按提示绑定个人最多5台免费 sudo pro enable realtime-kernel sudo reboot验证看到 PREEMPT_RT 即可bash运行uname -a # 或 cat /proc/version | grep PREEMPT_RT二、内核启动参数/etc/default/grub编辑 grubbash运行sudo nano /etc/default/grub把GRUB_CMDLINE_LINUX改成根据你的 CPU 核心数假设 4 核隔离核心 2、3 给实时任务plaintextGRUB_CMDLINE_LINUXisolcpus2,3 nohz_full2,3 rcu_nocbs2,3 irqaffinity0,1 intel_idle.max_cstate0 processor.max_cstate0 idlepoll参数说明isolcpus2,32、3 核不跑普通进程专给实时任务nohz_full2,3隔离核关闭时钟节拍减少抖动rcu_nocbs2,3RCU 回调不跑在隔离核irqaffinity0,1中断只在 0、1 核处理intel_idle.max_cstate0禁用 CPU 深度休眠减少唤醒抖动更新并重启bash运行sudo update-grub sudo reboot三、调度与优先级chrt nice1给你的 fast_shm 进程设实时优先级bash运行# 查看进程 PID ps aux | grep fast_shm # 设为 FIFO 实时优先级 99最高 sudo chrt -f -p 99 PID # 设 CPU 亲和性绑到隔离核 2 sudo taskset -c 2 -p PID2默认限制调大/etc/security/limits.confbash运行sudo nano /etc/security/limits.conf添加plaintext* soft rtprio 99 * hard rtprio 99 * soft memlock unlimited * hard memlock unlimited四、中断与软中断优化1把网卡 / 外设中断移到非隔离核查看所有中断bash运行cat /proc/interrupts把网卡如 eth0中断绑到 0、1 核bash运行sudo echo 3 /proc/irq/$(cat /proc/interrupts | grep eth0 | awk {print $1} | sed s/://)/smp_affinity2关闭无用服务bash运行sudo systemctl stop apport sudo systemctl disable apport sudo systemctl stop cups sudo systemctl disable cups sudo systemctl stop bluetooth sudo systemctl disable bluetooth五、内存与网络优化1内存锁定避免 swapbash运行sudo sysctl -w vm.swappiness0 sudo sysctl -w kernel.shmmax68719476736 sudo sysctl -w kernel.shmall42949672962网络栈优化减少 UDP / 共享内存抖动bash运行sudo sysctl -w net.ipv4.tcp_low_latency1 sudo sysctl -w net.ipv4.tcp_syn_retries1 sudo sysctl -w net.core.netdev_max_backlog10000六、BIOS 必关项非常重要关闭Intel SpeedStep / Turbo Boost关闭CPU C-State深度休眠关闭省电模式 / Power Saving开启Performance 模式七、效果验证用你之前的方法优化后再跑你的 fast_shm 测试正常应看到平均延迟25–35usP9945us最大抖动50us八、一键优化脚本复制即用bash运行#!/bin/bash # 1. 安装低延迟内核 sudo apt update sudo apt install -y linux-lowlatency linux-headers-lowlatency # 2. 写入 grub 参数 sudo sed -i s/^GRUB_CMDLINE_LINUX.*/GRUB_CMDLINE_LINUXisolcpus2,3 nohz_full2,3 rcu_nocbs2,3 irqaffinity0,1 intel_idle.max_cstate0 processor.max_cstate0 idlepoll/ /etc/default/grub sudo update-grub # 3. limits.conf echo * soft rtprio 99 | sudo tee -a /etc/security/limits.conf echo * hard rtprio 99 | sudo tee -a /etc/security/limits.conf echo * soft memlock unlimited | sudo tee -a /etc/security/limits.conf echo * hard memlock unlimited | sudo tee -a /etc/security/limits.conf # 4. sysctl sudo sysctl -w vm.swappiness0 sudo sysctl -w kernel.shmmax68719476736 sudo sysctl -w kernel.shmall4294967296 sudo sysctl -w net.ipv4.tcp_low_latency1 # 5. 关闭服务 sudo systemctl stop apport cups bluetooth sudo systemctl disable apport cups bluetooth echo 优化完成请重启