Skip to content

uspace: pin the NIC IRQs before the first scan, rescan unscanned slaves, idle timeout 2 ms - #5

Merged
sittner merged 3 commits into
mainfrom
irq-pin-at-startup
Nov 2, 2026
Merged

sittner merged 3 commits into
mainfrom
irq-pin-at-startup

Conversation

@sittner

@sittner sittner commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

A coating machine failed its first two or three starts after every reboot. Debug logs showed the bus scan's replies reaching the master 0.5–1.8 ms after sending (every timed-out datagram's reply arrived as UNMATCHED), past the 500 µs datagram timeout: the NIC IRQ sat on the housekeeping CPUs (irqaffinity=0,1) until the first cyclic receive moved it. Slaves were left without their SII and the application's configuration failed. With the IRQ pinned to the RT CPU by hand the first start came up with no timeouts at all.

Changes

  • ecrt_startup_master(..., run_on_cpu, irq_cpu): pins the main and backup transports' IRQs once, after the transports are open and before the idle phase starts the scan. -1 leaves them alone. The dynamic pinning (ec_pal_record_rt_cpu in ecrt_master_receive(), ec_pal_check_irq_affinity in the operation thread) is removed: one decision where the application knows its RT CPU, and sched_getcpu() leaves the cyclic receive path. The standalone daemon gets --irq-cpu. API note in ecrt.h.in.
  • Rescan at startup: a startup scan that left slaves unscanned is repeated, up to EC_INITIAL_SCAN_ATTEMPTS (3) scans, then reported done with an error naming the slave positions. Later rescans unchanged. The startup wait watches a progress counter instead of the slave count (a rescan finds the same count).
  • EC_IDLE_IO_TIMEOUT (2 ms) for datagrams before activation; the operation phase keeps EC_IO_TIMEOUT (500 µs).

Tests

The sim transport records set_cpu_affinity, can lose a slave's SII replies during the first N scans, and can delay every reply.

  • test_sim_irq_pin: pinned before the scan's first frame; not at all with -1 (fails with "150 frames" when the pin is moved after the idle phase).
  • test_sim_scan_retry: clean bus 1 scan; slave lost in the first scan identified after the second; lost in every scan → 3 attempts, unscanned, startup returns.
  • test_sim_scan_slow_replies: replies 800 µs late → one scan, all identified (4 scans, none identified, with 500 µs).

make -C tests check 21 pass / 2 skip, script/rt-effects-check.sh OK. Kernel module build and distcheck not run locally.

The stratumak side (deriving the CPU from the HAL config, plugin passing irq_cpu) follows in a stratumak PR.

🤖 Generated with Claude Code

@sittner
sittner force-pushed the irq-pin-at-startup branch from fcb1c56 to c030779 Compare November 2, 2026 16:47
sittner and others added 3 commits November 2, 2026 17:47
The transport IRQs were moved to the realtime CPU only once the cyclic
thread had run (recorded in ecrt_master_receive(), re-pinned by the
operation thread).  Until then they stay wherever the system put them;
on a machine with irqaffinity=0,1 that is the housekeeping CPUs, and
under the load right after a reboot the scan's replies reached the
master 0.5-1.8 ms after sending -- past the 500 us datagram timeout.
Slaves were left without their SII and the application's configuration
failed (coating machine, 2026-11-02; a start with the IRQ pinned by
hand came up at once, without a single timeout).

ecrt_startup_master() takes irq_cpu and pins the main and backup
transports' IRQs once, after the transports are open and before the
idle phase starts the scan.  -1 leaves them alone.  The dynamic pinning
is gone: one decision, made where the application knows its realtime
CPU, and sched_getcpu() leaves the cyclic receive path.  The standalone
daemon gets --irq-cpu.

The sim transport records set_cpu_affinity calls; test_sim_irq_pin
checks the pin comes before the scan's first frame (fails with 150
frames when moved after the idle phase).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A slave whose scan failed (its SII replies lost or late) stayed on the
bus without identity or PDO information, and ecrt_startup_master()
returned as if the scan had succeeded: the application's configuration
then failed on it ("Invalid mailbox configuration", "does not support
CoE", "PDO entry ... is not mapped").  On the coating machine the
operator restarted until a scan came through clean.

The master now counts failed slave scans; a startup scan with failures
is repeated, up to EC_INITIAL_SCAN_ATTEMPTS (3) scans, before it is
reported done -- then with an error naming the slave positions.  Later
rescans (topology changes) are unchanged.  The startup wait watches a
progress counter (scans begun, slaves scanned) instead of the slave
count, which a repeated scan finds unchanged.

The sim transport can lose a slave's SII replies during the first N
scans (sim_bus_lose_sii) and counts scans; test_sim_scan_retry: a clean
bus scans once, a slave lost in the first scan is there after the
second, one lost in every scan ends after three attempts, unscanned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Before activation the master's idle thread runs on a CPU shared with the
rest of the system, and so may the NIC interrupts.  On the coating
machine the bus scan's replies came back 0.5-1.8 ms after sending, past
the 500 us EC_IO_TIMEOUT tuned for the cyclic exchange, and the scan
failed.  Nothing in the idle phase (scan, slave configuration) is
time-critical: its datagrams now time out after EC_IDLE_IO_TIMEOUT
(2 ms).  The operation phase keeps EC_IO_TIMEOUT.

The sim transport can delay every reply (sim_bus_set_reply_delay);
test_sim_scan_slow_replies scans a bus whose replies take 800 us in one
attempt with every slave identified (4 attempts, none identified, with
500 us).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sittner
sittner force-pushed the irq-pin-at-startup branch from c030779 to 6a86dac Compare November 2, 2026 16:47
@sittner
sittner merged commit 214a1c5 into main Nov 2, 2026
18 checks passed
@sittner
sittner deleted the irq-pin-at-startup branch November 2, 2026 16:55
sittner added a commit to stratuMAK/stratumak that referenced this pull request Nov 2, 2026
* launcher: reserve thread CPUs while parsing; env->sched->funct_cpu (ABI 5)

A module's New() runs before the HAL wiring commands create the threads,
so it could not know where its realtime function will run.  The
EtherCAT master needs that before its bus scan: its NIC IRQs belong on
the realtime CPU, and a scan whose replies are handled on a shared CPU
can miss the datagram timeout (coating machine, 2026-11-02).

Right after parsing the HAL files the launcher reserves every
newthread's CPU from the pool, in command order
(halcmd.ReserveThreadCPU); the newthread command then takes its
reservation instead of a new CPU, so an answer given earlier is the CPU
the thread gets.  Threads not in the parsed files (REST, HALCMD,
halrun) acquire as before.  halcmd also remembers each created
thread's CPU (ThreadCPU).

New sub-API stmak_sched.h: env->sched->funct_cpu(ctx, funct) is the CPU
of the thread the boot HAL files add funct to (-1: not added, or no
affinity).  Appended to cmod_env_t; CMOD_ABI_VERSION 4 -> 5, every cmod
must be rebuilt (config components: install.sh).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ethercat: pin the NIC IRQs where the read function runs, before the scan

The master gets irq_cpu (stratuMAK/ethercat#5): it pins the transports'
IRQs once, before the idle phase starts the bus scan, and no longer
moves them at the first cyclic receive.  The plugin passes the CPU of
the HAL thread that runs <instance>.read-all (or <instance>.<master>.read),
from env->sched; <master irqCpu="N"> overrides it, -1 leaves the IRQs
alone.  The decision is logged.

The submodule moves to stratuMAK/ethercat main 214a1c59 (#5, on top of
the CI fixes #6), which also repeats a startup scan that left slaves
unscanned and gives datagrams 2 ms before activation.

tests/ethercat/sim-irq-cpu: an INI start whose newthread (cpu=0) comes
after the load, as in a config whose threads.hal is last; the plugin
derives CPU 0 and the master pins before "Scanning bus" (fails without
the launcher's parse result).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* build: rebuild the EtherCAT master when its submodule or its tree changed

The master's build stamps (src/.ecmaster-stamps) recorded what was done
once and outlived it.  A submodule moved to another commit kept the old
master installed, so the plugin compiled against the old ecrt.h ("too
many arguments to function ecrt_startup_master"); a master tree that had
lost its generated files kept its stamps, so "make -C" found no Makefile
(both on the coating machine, 2026-11-02, building #53).

The submodule's commit is now a prerequisite of the bootstrap stamp
(.head, rewritten only when it changes), so another commit bootstraps,
configures, builds and installs again; and a stamp whose product is gone
(the master's configure, its Makefile, the installed include/ecrt.h) is
dropped when make reads the Makefile.  An unchanged tree rebuilds
nothing; the first make after this lands rebuilds the master once.

Checked by hand on a worktree: no change -> nothing; submodule to
another commit and back -> full rebuild each way, installed ecrt.h
following; git clean of the master tree with the stamps kept ->
bootstrap .. install; installed ecrt.h removed -> install only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant