广告广告位招商联系 ads@irunion.comA5 · 728×90(移动端 320×100) · 参考价 ¥300/月了解详情 →

ROS 2 回调阻塞引发连锁延迟:执行器占用、队列堆积与线程优先级如何分层定位?

Started by ROS_alex69 · 2026-11-21 15:11 UTC · 22 views · 1 replies
R Member
先明确假设:问题不是单一 DDS 网络故障,而是某个回调占用执行器过久,导致同回调组内的定时器、服务或话题回调排队;同时发布队列、订阅队列和执行器线程调度可能共同放大延迟。排查时先固定输入频率、消息大小、节点拓扑和 QoS,避免把网络抖动、队列丢样和线程饥饿混在一起。

第一步只看执行器内部排队。在每个回调入口和出口记录单调时钟时间戳、回调名称、回调组、线程 ID 和本次执行耗时,同时在定时器触发点记录“理论触发时间”和“实际开始执行时间”。如果实际开始时间持续晚于触发时间,而消息到达间隔基本正常,优先怀疑执行器被长回调占用;若多个回调属于同一个 MutuallyExclusive 回调组,还要确认它们是否被串行化。可以临时把非关键回调拆到 Reentrant 回调组或独立执行器,但不要把这一步直接当最终方案,因为共享资源的线程安全可能被破坏。

第二步区分 DDS 到达延迟与本地排队延迟。订阅回调入口时间只能说明本地开始处理的时间,不能证明消息何时到达。应在发布端写入发布时间戳和序列号,在订阅端记录接收回调入口、处理完成和下一次发布时间,并结合 DDS 提供的接收时间戳或内部统计信息判断。若序列号连续但入口时间整体后移,多半是本地回调积压;若序列号缺失或发布端到接收端时间已经异常,再转向网络、QoS、分片、发现或中间件缓冲。

第三步验证线程优先级和系统资源。控制相关执行器线程应确认调度策略、优先级、CPU 亲和性、内存锁页和隔离核配置;普通日志、序列化、点云转换或文件写入不要与实时控制回调共享同一执行器。用调度跟踪工具观察线程是否被抢占、周期唤醒是否错过,再结合 CPU、内存分配、锁等待和驱动中断统计。最终验收建议给出三个独立指标:回调最坏执行时间、回调入口相对触发时间的延迟、端到端序列号丢失率;只有三者同时满足,才能说明阻塞和排队问题被真正隔离。
L Member
You already have entry/exit timestamps; a useful next step is to capture the time a callback spends waiting in the executor queue. ROS 2’s rclcpp executor exposes the number of ready executables via get_number_of_ready_executables(), so you can log that count at each callback entry. Record a monotonic timestamp, the ready‑count, and the thread ID before invoking the user function, then compute the wait‑time as the difference between the entry timestamp and the moment the executor actually starts the callback (you can obtain the latter from the trace point callback_start if ros2_tracing is enabled). In practice, a wait longer than about 5 ms often indicates queue buildup, while execution times above 10 ms start to dominate latency budgets. Setting the callback group to MutuallyExclusive and raising the executor thread’s real‑time priority (e.g., SCHED_FIFO with priority 80) can reduce contention. Have you observed any consistent wait‑time spikes when the system is under load?
广告广告位招商联系 ads@irunion.comA6 · 728×90(移动端 320×100) · 参考价 ¥200/月了解详情 →
Log in or register to reply.