Rust in Everything
基于 cu29-runtime 1.3.1

一次控制周期:run_one_iteration 全链路追踪

主线位置:控制周期热路径主线总览。本页顺着 #[copper_runtime] 生成的 run_one_iteration() 函数,逐步追踪一个控制周期内 CopperList 从借出、填充、消费到序列化落盘的完整轨迹。

单周期时序全景

sequenceDiagram
  autonumber
  participant App as CuApplication::run_one_iteration
  participant Lim as LoopRateLimiter
  participant CLM as SyncCopperListsManager
  participant Tasks as Source -> Task -> Sink
  participant Mon as CuMonitor
  participant Log as CompletedCopperListSink

  App->>Lim: wait_until_ready(&clock)
  App->>CLM: create() 借出槽位并 reset_for_runtime_use(id)
  CLM-->>App: &mut CopperList<CuStampedDataSet> (Initialized)
  loop 按编译期拓扑顺序内联执行每个节点
    App->>Tasks: preprocess(&ctx)
    App->>Tasks: process(&ctx, input_msgs, output_msg)
    Note over App,Tasks: 自动记下 before/after RobotClock 时间戳
    App->>Tasks: postprocess(&ctx)
     Tasks-->>Mon: 错误时触发 process_error 决策 (Abort/Ignore/Shutdown)
  end
  App->>Mon: process_copperlist(&msgs)
  App->>CLM: end_of_processing(culistid)
  CLM->>Log: write(&CopperList) 写入 UnifiedLogger
  CLM->>CLM: 状态恢复并归还槽位池

第一步:速率限制器 LoopRateLimiter

若 .ron 配置中声明了目标频率(如 rate_target_hz: 1000),LoopRateLimiter::wait_until_ready 会在周期入口精确对齐时钟周期:

// core/cu29_runtime/src/curuntime.rs#L316-L347
pub fn wait_until_ready(&self, clock: &RobotClock) {
  let Some(mut remaining) = self.remaining(clock) else {
    return;
  };
  #[cfg(all(feature = "std", feature = "high-precision-limiter"))]
  {
    let spin_budget = CuDuration(200_000); // 最后 200 µs 采用自旋忙等消除 OS 睡眠抖动
    if remaining > spin_budget {
      let sleep_for = remaining - spin_budget;
      std::thread::sleep(std::time::Duration::from_nanos(sleep_for.as_nanos()));
    }
    while !self.is_ready(clock) {
      std::hint::spin_loop();
    }
    return;
  }
  // ...
}

第二步:从预分配池借出 CopperList 槽位

随后,生成代码调用 SyncCopperListsManager::create 从容量为 NBCL 的环形池中取出一个空闲槽位,并原地清零元数据:

// core/cu29_runtime/src/curuntime.rs#L631-L640
pub fn create(&mut self) -> CuResult<&mut CopperList<P>>
where
  P: CuListZeroedInit,
{
  let id = self.next_cl_id;
  self.next_cl_id = self.next_cl_id.wrapping_add(1);
  let cl = &mut self.current_cl;
  cl.reset_for_runtime_use(id);
  Ok(cl)
}

第三步:内联执行节点三阶段与时间戳打点

对于拓扑计划中的每个节点,编译期生成的代码直接通过元组下标(如 &mut cl.msgs.0.0)解构出输入输出引用,依次调用:

  1. task.preprocess(&ctx):外设准备或仿真钩子拦截;
  2. 记录起始时刻 let before = clock.now(),调用 task.process(&ctx, input, output),记录结束时刻 let after = clock.now(),写入 output.metadata.process_time(CuMsgMetadata);
  3. task.postprocess(&ctx)。

第四步:end_of_processing 序列化与回收

所有节点执行完毕后,SyncCopperListsManager::end_of_processing 将当前 CopperList 推入 CompletedCopperListSink(即 UnifiedLogger 的 CopperList Section 流),同时将冻结的任务状态写入 KeyFrame 流。

❗IMPORTANT

动脑筋:看上面 LoopRateLimiter::wait_until_ready 的实现,为什么启用 high-precision-limiter 时要先 std::thread::sleep(remaining - 200µs),最后 200 µs 改用 spin_loop()?

动脑筋答案

Linux 普通调度器(CFS)乃至 PREEMPT_RT 内核的 nanosleep 唤醒延迟通常有 50 µs ~ 150 µs 的不确定长尾。如果全程 sleep,唤醒时刻会频繁越过周期边界;如果全程 spin_loop(),又会占满 100% CPU 核心。先睡掉绝大部分剩余时间,仅在最后 200 µs 窗口内自旋等待 RobotClock 跨过目标刻度,兼顾了低 CPU 占用与亚微秒级周期触发精度。

小结

  • 单个控制周期完全由编译期展开的直线代码驱动,无运行期图遍历开销。
  • SyncCopperListsManager 通过原地重置槽位(reset_for_runtime_use)消除了跨周期的堆分配与析构成本。

💬 关联讨论

加载中...