-
Notifications
You must be signed in to change notification settings - Fork 996
Add goroutine core affinity support for RP2040/RP2350 systems #5092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 1 commit
16409a2
870af4f
c00fc84
9025053
8820b4e
3ca2b71
217adfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,46 @@ package machine | |
|
|
||
| const numCPU = 2 // RP2040 and RP2350 both have 2 cores | ||
|
|
||
| // LockCore implementation for the cores scheduler. | ||
| // LockCore sets the affinity for the current goroutine to the specified core. | ||
| // This does not immediately migrate the goroutine; migration occurs at the next | ||
| // scheduling point. See machine_rp2.go for full documentation. | ||
| // Important: LockCore sets the affinity but does not immediately migrate the | ||
| // goroutine to the target core. The actual migration happens at the next | ||
| // scheduling point (e.g., channel operation, time.Sleep, or Gosched). After | ||
| // that point, the goroutine will wait in the target core's queue if that core | ||
| // is busy running another goroutine. | ||
| // | ||
| // To avoid potential blocking on a busy core, consider calling LockCore in an | ||
| // init function before any other goroutines have started. This guarantees the | ||
| // target core is available. | ||
|
Comment on lines
+11
to
+13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be a hard requirements; that is,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding "panic if goroutines started" - I have a specific use case for dynamic pinning: Motion control board where: Core 0: Communications and non-critical tasks func main() { Would you accept one of these:
The deadlock risk is manageable if users follow the pattern of pinning early and using one goroutine per core for pinned work.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, if we do this However, given your use case: is it possible to run the step generation off a hardware timer and an interrupt handler? That seems a better fit, and you can keep both cores running non-critical code.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, That is precisely the implementation that I am trying to get away from.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate why? Assuming your
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interrupt-based approach has several issues for precision step generation:
func stepGenerationLoop() { The core isn't idle - it's maintaining precise timing. An interrupt would add latency between "timer fired" and "step pin toggled."
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I believe I'm doing something similar on a PIO-capable chip (rp2350). My solution is a 3-layer architecture:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is always multiple ways to skin a cat. I am using an approach similar to what you described as well. But having core pinning has its advantages as well. Also, It seems like several people have asked for it previously. Appreciate your reviews and responses |
||
| // | ||
| // This is useful for: | ||
| // - Isolating time-critical operations to a dedicated core | ||
| // - Improving cache locality for performance-sensitive code | ||
| // - Exclusive access to core-local resources | ||
| // | ||
| // Warning: Pinning goroutines can lead to load imbalance. The goroutine will | ||
| // wait in the specified core's queue even if other cores are idle. If a | ||
| // long-running goroutine occupies the target core, LockCore may appear to | ||
| // block indefinitely (until the next scheduling point on the target core). | ||
| // | ||
| // Valid core values are 0 and 1. Panics if core is out of range. | ||
| // | ||
| // Only available on RP2040 and RP2350 with the "cores" scheduler. | ||
amken3d marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func LockCore(core int) { | ||
| if core < 0 || core >= numCPU { | ||
| panic("machine: core out of range") | ||
| } | ||
| machineLockCore(core) | ||
| } | ||
|
|
||
| // UnlockCore implementation for the cores scheduler. | ||
| // UnlockCore unpins the calling goroutine, allowing it to run on any available core. | ||
| // This undoes a previous call to LockCore. | ||
| // | ||
| // After calling UnlockCore, the scheduler is free to schedule the goroutine on | ||
| // any core for automatic load balancing. | ||
| // | ||
| // Only available on RP2040 and RP2350 with the "cores" scheduler. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Superfluous comment. |
||
| func UnlockCore() { | ||
| machineUnlockCore() | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,9 @@ func os_sigpipe() { | |
| // LockOSThread wires the calling goroutine to its current operating system thread. | ||
| // On microcontrollers with multiple cores (e.g., RP2040/RP2350), this pins the | ||
| // goroutine to the core it's currently running on. | ||
| // With the "cores" scheduler on RP2040/RP2350, this pins the goroutine to the | ||
| // core it's currently running on. The pinning takes effect at the next | ||
| // scheduling point (e.g., channel operation, time.Sleep, or Gosched). | ||
| // Called by go1.18 standard library on windows, see https://github.com/golang/go/issues/49320 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While here, remove this now irrelevant comment. |
||
| func LockOSThread() { | ||
| lockOSThreadImpl() | ||
|
|
@@ -108,6 +111,8 @@ func LockOSThread() { | |
| // UnlockOSThread undoes an earlier call to LockOSThread. | ||
| // On microcontrollers with multiple cores, this unpins the goroutine, allowing | ||
| // it to run on any available core. | ||
| // With the "cores" scheduler, this unpins the goroutine, allowing it to run on | ||
| // any available core. | ||
| func UnlockOSThread() { | ||
| unlockOSThreadImpl() | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.