实时 Linux:PREEMPT_RT、延迟来源与观测

软实时/硬实时需求里,主线内核默认抢占往往不够。PREEMPT_RT 把自旋锁等路径改成可抢占,降低最坏延迟,但要配合隔离核、关机外干扰与实测。

源码锚点

路径 / 手册 作用
Documentation/scheduler/ 调度说明
kernel/sched/ 调度实现
tools/rt-tests/ cyclictest 等
man chrt 设调度策略

调用链

#mermaid-svg-ZVrdPXB3Ry4A2tCu{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .error-icon{fill:#552222;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .marker.cross{stroke:#333333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu p{margin:0;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .cluster-label text{fill:#333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .cluster-label span{color:#333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .cluster-label span p{background-color:transparent;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .label text,#mermaid-svg-ZVrdPXB3Ry4A2tCu span{fill:#333;color:#333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .node rect,#mermaid-svg-ZVrdPXB3Ry4A2tCu .node circle,#mermaid-svg-ZVrdPXB3Ry4A2tCu .node ellipse,#mermaid-svg-ZVrdPXB3Ry4A2tCu .node polygon,#mermaid-svg-ZVrdPXB3Ry4A2tCu .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .rough-node .label text,#mermaid-svg-ZVrdPXB3Ry4A2tCu .node .label text,#mermaid-svg-ZVrdPXB3Ry4A2tCu .image-shape .label,#mermaid-svg-ZVrdPXB3Ry4A2tCu .icon-shape .label{text-anchor:middle;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .rough-node .label,#mermaid-svg-ZVrdPXB3Ry4A2tCu .node .label,#mermaid-svg-ZVrdPXB3Ry4A2tCu .image-shape .label,#mermaid-svg-ZVrdPXB3Ry4A2tCu .icon-shape .label{text-align:center;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .node.clickable{cursor:pointer;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .arrowheadPath{fill:#333333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ZVrdPXB3Ry4A2tCu .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZVrdPXB3Ry4A2tCu .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ZVrdPXB3Ry4A2tCu .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .cluster text{fill:#333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .cluster span{color:#333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZVrdPXB3Ry4A2tCu rect.text{fill:none;stroke-width:0;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .icon-shape,#mermaid-svg-ZVrdPXB3Ry4A2tCu .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .icon-shape p,#mermaid-svg-ZVrdPXB3Ry4A2tCu .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .icon-shape .label rect,#mermaid-svg-ZVrdPXB3Ry4A2tCu .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZVrdPXB3Ry4A2tCu .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZVrdPXB3Ry4A2tCu .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZVrdPXB3Ry4A2tCu :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 外部事件/定时
中断
唤醒实时线程
调度器选中 SCHED_FIFO/DEADLINE
用户态循环
延迟直方图

重点知识

延迟从哪来

中断下半部、关抢占临界区、同核干扰、电源管理休眠、SMI(x86)、显卡锁。RT 内核降低部分最坏情况,不能替代 dual-OS/AMP 硬实时方案。

观测

bash 复制代码
cyclictest -p 90 -i 1000 -l 100000
chrt -f 90 ./my_rt_app
# isolcpus / cpuset 隔离

实践要点

  • 实时线程绑核,避开 IRQ 重载核。
  • 避免在 RT 线程里写磁盘、打日志刷屏。
  • 先测空载基线,再加业务。
相关推荐
高山有多高16 分钟前
【Linux笔记】Linux自定义Shell
linux·c++
姜鱼问生22 分钟前
HTTPS 部署实战:未备案域名 + 非标端口的完整方案
linux·https
jackletter41 分钟前
linux:安装 ubuntu 26.04.1
linux·ubuntu·ubuntu26.04.1
打工仔折腾 AI1 小时前
飞牛OS上用Docker Compose部署ExerciseDiary运动记录并配置远程访问
运维·人工智能·后端·python·docker·容器·ai agent 实战
2401_869769591 小时前
make 进度条
linux
天远数科1 小时前
零信任架构实战:基于天远运营商三要素简版V即时版查询构建自动化买手实名核验网关
运维·人工智能·架构·自动化
天衍四九-1 小时前
Docker Compose企业实战系列(八·终章):多环境隔离实战,开发/测试/生产一键切换
运维·docker·容器
C++ 老炮儿的技术栈2 小时前
有符号变量与无符号变量的区别
服务器·开发语言·前端·数据结构·c++·算法·c
IT大白鼠2 小时前
搜索系列 · 第 04 篇——部署实操:内网集群落地
运维·elasticsearch·nosql
Elastic 中国社区官方博客2 小时前
使用 Jev 作为 search reranker:基准测试与实现方法
大数据·运维·elasticsearch·全文检索