---
title: "Rust EtherCAT on PREEMPT_RT Linux: Jitter and DC Sync"
url: https://www.induqora.com/blog/rust-ethercat-preempt-rt-linux-jitter-guide
publication: Induqora
type: Insight
category: Motion Control & Drives
author: Edward (Editor, Induqora)
author_url: https://www.induqora.com/author/edward
published: 2026-11-28
updated: 2026-11-28
reading_minutes: 7
tags: ["EtherCAT", "Industrial Ethernet", "Motion control"]
---

# Rust EtherCAT on PREEMPT_RT Linux: Jitter and DC Sync

> A software EtherCAT master on general-purpose Linux is a latency engineering problem before it is a Rust problem. This guide covers cycle jitter on PREEMPT_RT versus stock kernels, core isolation and interrupt affinity, zero-allocation cycle loops with mlockall, and distributed-clock drift on SYNC0.

## Key takeaways

- Worst-case cycle jitter, not average, decides whether a userspace EtherCAT master can drive servo motion; stock kernels land in the hundreds of microseconds to milliseconds.
- isolcpus, nohz_full and rcu_nocbs remove the scheduler and the tick from the cycle cores, but only if the NIC interrupt is also moved to a housekeeping core.
- mlockall with MCL_FUTURE is required: locking only current pages leaves every later allocation able to fault inside the cycle.
- ethercrab sizes subdevices and the process data image at compile time; keep the cycle body free of formatting and allocation, and include timer and executor latency in the deadline budget.
- Distributed clocks compensate frame-arrival jitter, not master cycle drift, and SYNC0 must match the cycle period exactly.
- Below roughly 250 µs with a hard worst-case bound, a commercial master or dedicated motion controller is the correct answer.

An EtherCAT master implemented in userspace is a hard real-time problem with a soft real-time operating system underneath it. The fieldbus itself is deterministic: frames pass through every slave in a few microseconds and the working counter tells you exactly what was processed. What is not deterministic is when your process gets scheduled to send the next frame. On a stock kernel that moment can move by hundreds of microseconds; on a correctly configured PREEMPT_RT kernel it can be held to single-digit microseconds, and the difference decides whether a servo drive sees a smooth profile or a stutter.

This guide is about the Linux side. The Rust side — `ethercrab` and its zero-allocation cycle loop — is comparatively easy once the platform behaves.

## What the numbers actually look like

Measure scheduling latency and actual frame transmit timing under the intended workload. Record CPU, firmware, kernel, cycle period, NIC driver and test duration with every result. The diagram illustrates a possible distribution. Capture a fresh trace on the target platform.

![Histogram of EtherCAT cycle jitter in microseconds on a 1 millisecond cycle comparing a tuned PREEMPT_RT kernel concentrated below 40 microseconds with a stock kernel having a long tail beyond 200 microseconds, a dashed servo tolerance line at 25 microseconds, and a six-step tuning ladder from isolcpus through mlockall to zero-allocation loops and NIC offload settings](/media/2026/11/diagram-rust-ethercat-preempt-rt-linux-jitter-guide-v2.png "Figure 1: Illustrative timing distributions. Compare measured worst-case timing with the configured device deadline.")

| Measurement | What to record | Acceptance criterion |
| --- | --- | --- |
| Scheduler wake-up latency | Maximum and percentile values under CPU, disk and network load | Stay within the allocated cycle jitter budget |
| Cycle start lateness | Actual start minus the scheduled monotonic deadline, in µs | Compare with the device watchdog and application deadline |
| Exchange duration | Time from sending to completion of the process-data exchange, in µs | Complete before the next application deadline |
| Distributed-clock offset | Reference-clock error after the specified settling time, in ns or µs | Meet the device documentation and motion requirements |

The important column is not the typical value. A drive tolerates a late frame; it does not tolerate one that arrives after the next frame, because that produces a velocity discontinuity the current loop cannot absorb. That is why the worst case, not the average, is the design number.

## Core isolation and interrupt affinity

The single biggest improvement comes from getting the cycle thread away from everything else. Three kernel parameters do most of the work:

```
isolcpus=2,3 nohz_full=2,3 rcu_nocbs=2,3
```

- `isolcpus` removes the listed cores from the general scheduler so nothing else is placed there.
- `nohz_full` stops the tick on those cores when a single task is running, removing a periodic interrupt that would otherwise land in the middle of a cycle.
- `rcu_nocbs` moves RCU callbacks off the isolated cores.

Isolation only helps if the NIC interrupt is also moved. On a system where the EtherCAT NIC shares a core with the cycle thread, every frame triggers an interrupt on the core that is trying to send the next frame:

```bash
# Find the IRQ for the EtherCAT NIC
grep eth1 /proc/interrupts

# Move it to a housekeeping core (core 0 here), away from isolated 2,3
echo 0 > /proc/irq/<irq>/smp_affinity_list
```

Disable the interrupt-coalescing features that were designed for throughput and add latency on a real-time link:

```bash
ethtool -K eth1 gro off gso off tso off lro off
ethtool -C eth1 rx-usecs 0 tx-usecs 0
```

Then pin the cycle thread, raise its priority, and lock its memory so a page fault can never occur mid-cycle:

```rust
// compile-check: library
// Call on the cycle thread after reserving stack and process-data storage.
pub fn configure_cycle_thread(cpu: usize) -> std::io::Result<()> {
    if cpu >= libc::CPU_SETSIZE as usize {
        return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "CPU index exceeds cpu_set_t"));
    }
    unsafe {
        let mut set = std::mem::zeroed::<libc::cpu_set_t>();
        libc::CPU_SET(cpu, &mut set);
        if libc::sched_setaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &set) != 0 {
            return Err(std::io::Error::last_os_error());
        }
        let param = libc::sched_param { sched_priority: 80 };
        if libc::sched_setscheduler(0, libc::SCHED_FIFO, &param) != 0 {
            return Err(std::io::Error::last_os_error());
        }
        if libc::mlockall(libc::MCL_CURRENT | libc::MCL_FUTURE) != 0 {
            return Err(std::io::Error::last_os_error());
        }
    }
    Ok(())
}
```

`MCL_FUTURE` is the part people forget. Without it, the heap is locked at the moment of the call but every later allocation can still fault — and a fault on an isolated core under `SCHED_FIFO` is a multi-millisecond stall.

## The zero-allocation cycle loop

`ethercrab` is written for a `no_std` target and sized at compile time: subdevice count and PDI length are const generics, and the process data image is a fixed array. That design is what makes it usable in a cycle loop, but only if your code around it follows the same rule.

The example targets `ethercrab 0.7.1` on Linux. Its timer paces the asynchronous loop; measure that executor on the target system. The loop reads process data and leaves outputs unchanged. Configure device-specific PDOs, watchdogs and DC separately.

```toml
[dependencies]
ethercrab = { version = "=0.7.1", default-features = false, features = ["std"] }
smol = "=2.0.2"
futures-lite = "=2.6.1"
libc = "=0.2.180"
```

```rust
// compile-check: library
use ethercrab::{MainDevice, MainDeviceConfig, PduStorage, Timeouts, std::ethercat_now};
use futures_lite::StreamExt;
use std::time::{Duration, Instant};

const MAX_SUBDEVICES: usize = 16;
const MAX_PDU_DATA: usize = PduStorage::element_size(1100);
const MAX_FRAMES: usize = 16;
const PDI_LEN: usize = 64;
static PDU_STORAGE: PduStorage<MAX_FRAMES, MAX_PDU_DATA> = PduStorage::new();

pub fn run(interface: &str) -> Result<(), ethercrab::error::Error> {
    smol::block_on(async {
        let (tx, rx, pdu_loop) = PDU_STORAGE.try_split().expect("can only split once");
        let maindevice = MainDevice::new(
            pdu_loop,
            Timeouts {
                wait_loop_delay: Duration::from_millis(2),
                mailbox_response: Duration::from_millis(1000),
                ..Default::default()
            },
            MainDeviceConfig::default(),
        );
        let transport = smol::spawn(
            ethercrab::std::tx_rx_task(interface, tx, rx).expect("create TX/RX task"),
        );
        transport.detach();

        let group = maindevice
            .init_single_group::<MAX_SUBDEVICES, PDI_LEN>(ethercat_now)
            .await?;
        let group = group.into_op(&maindevice).await?;
        let period = Duration::from_millis(1);
        let late_budget = Duration::from_micros(50);
        let mut tick = smol::Timer::interval(period);
        let mut late_starts = 0_u64;
        let mut last_input_byte = 0_u8;

        loop {
            let scheduled = tick.next().await.expect("interval timer remains active");
            if Instant::now().saturating_duration_since(scheduled) > late_budget {
                late_starts = late_starts.saturating_add(1);
            }
            group.tx_rx(&maindevice).await?;

            for subdevice in group.iter(&maindevice) {
                // Drop this read guard before taking any mutable PDI guard.
                let first_input = {
                    let inputs = subdevice.inputs_raw();
                    inputs.first().copied()
                };
                if let Some(value) = first_input {
                    last_input_byte = value;
                }
            }
            // A diagnostic task can consume snapshots outside the cycle.
            std::hint::black_box((late_starts, last_input_byte));
        }
    })
}
```

Three rules make the difference between a working loop and a mystery:

- **No `println!`, no `format!`, no `dbg!`** in the cycle. Formatting allocates, and allocation can fault. Buffer diagnostics into a fixed array and flush them from a non-real-time task.
- **Keep waits explicit.** This sketch uses a timer and the bus exchange. Avoid unrelated channel waits and include timer/executor wake-up latency in the measured cycle budget.
- **Count deadline misses instead of logging them.** A single miss after a heavy network event is information; a log line per miss turns a 5 % problem into a 100 % outage.

## Distributed clocks and SYNC0 drift

Distributed clocks (DC) let every slave derive its local clock from a reference slave, so all devices latch inputs at the same instant regardless of frame arrival time. On a software master the reference clock is also used to schedule SYNC0, the synchronisation pulse that triggers output latching.

The practical behaviour to know:

- **DC compensates frame-arrival jitter, not master jitter.** If the master's cycle drifts by 100 µs, the slaves still latching together does not help — motion becomes uneven because the whole set of setpoints moves.
- **SYNC0 period must match the cycle period exactly.** A 1 ms cycle with a 2 ms SYNC0 period gives every device a fresh setpoint only every other cycle, which shows up as a 500 Hz excitation.
- **Correction of the drift is progressive.** After a master restart the slaves need a few hundred cycles to re-converge; expect a transient and do not start motion during it.
- **Do not enable DC on slaves that do not support it.** A single device without DC support can prevent the drift compensation of an entire group.

The check that exposes most DC problems is to log, on one diagnostic cycle per second, the difference between the master's scheduled send time and the actual send time, plus the DC time offset reported for one servo. If the DC offset wanders by more than a few microseconds while the send jitter stays low, the problem is in the DC configuration. If both wander together, the problem is the kernel.

## When to stop tuning software and buy hardware

A userspace master on PREEMPT_RT is a legitimate engineering choice for machines with 250 µs to 1 ms cycles and moderate axis counts. It stops being the right choice when:

- You need cycle times below 250 µs with hard guarantees.
- Your worst case must be bounded in the low microseconds regardless of system load.
- The design needs a safety function without a validated safety architecture. Safety over EtherCAT (FSoE) uses a black-channel approach with safety-rated endpoints; an ordinary transport master alone provides no safety integrity claim.
- You have no control over the BIOS, the NIC or the OS image in the field.

In those cases the honest answer is a commercial master or a dedicated motion controller. A software master is a good tool; it is not a substitute for hardware determinism, and the marketing material for userspace real-time stacks tends to quote the typical case rather than the worst one.

## Frequently asked questions

### Can I run an EtherCAT master on a stock Ubuntu kernel for a bench test?

Yes, for development and slow I/O. Expect jitter in the hundreds of microseconds with occasional multi-millisecond outliers, which is enough to stall a servo drive but fine for verifying slave detection, PDO mapping and DC configuration.

### Why does moving the NIC interrupt matter so much?

If the EtherCAT NIC raises an interrupt on the same core running the cycle thread, the interrupt preempts the send. Affinity moves both to different cores, which typically removes the largest single source of jitter.

### Do I need PREEMPT_RT or is a low-latency kernel enough?

A low-latency kernel reduces but does not bound latency. For a 1 ms cycle driving servos, PREEMPT_RT is the practical minimum; the low-latency patch leaves worst cases in the hundreds of microseconds.

### What does mlockall with MCL_FUTURE protect against?

Page faults. Any allocation after the call can fault, and a fault inside a SCHED_FIFO loop on an isolated core stalls for milliseconds. MCL_FUTURE keeps all future pages resident.

### Why does my motion look uneven if the slaves are synchronised?

Distributed clocks synchronise when slaves latch, not when the master sends. If the master cycle drifts, the entire set of setpoints moves even though the slaves latch together, and the motion profile inherits the drift.

### Is a userspace master safe for a machine with safety functions?

A transport master alone provides no safety integrity claim. FSoE can use an ordinary EtherCAT network as a black channel, while safety-rated endpoints implement the safety protocol. Validate the complete safety architecture and stop functions for the machine.

## Sources and further reading

- ETG.1000, *EtherCAT Specification*, EtherCAT Technology Group.
- ETG.1020, *EtherCAT Protocol Enhancements*, distributed clocks chapter.
- [ethercrab 0.7.1 published source and examples](https://docs.rs/ethercrab/0.7.1/ethercrab/)
- [Linux kernel documentation, PREEMPT_RT](https://docs.kernel.org/core-api/real-time/index.html)
- [Linux kernel IRQ affinity documentation](https://docs.kernel.org/core-api/irq/irq-affinity.html)
