Hacker News new | ask | show | jobs
by clarry 2275 days ago
Tldw? What do you mean by timing?

Measuring time (down to nanoseconds) is easy. Hard realtime scheduling with microsecond precision... just forget about it, unless you're building a custom distro & custom kernel and have in-depth knowledge about all the drivers that are going to be running on the system (you almost definitely do not).

EDIT: I found a relatively recent socket benchmark with worst case scheduling latencies ranging from tens to hundreds of usec: https://www.codeblueprint.co.uk/2019/12/23/linux-preemption-...

2 comments

You can't really bang bits like you can with carefully cycle counted code on something like an Arduino, but you can put all your bits into a good sized buffer and use DMA to push them out a stable, hardware clocked rate. You still have to be able ping pong your buffers fast enough and that isn't really guaranteed, but for large enough buffers can be made to work, mostly, probably, well… except when…

This technique doesn't work when you need to respond to inputs rapidly, but is fine for driving steppers. I see he appears to have gone with servos and encoders, so that would be a bit of a problem since there are inputs to be read.

> put all your bits into a good sized buffer and use DMA to push them out

Sure, time-honored technique for audio. Very on the mark.

But it doesn't really fix the "latency is forever" problem. Buffer too much and your control will lag. Too little and buffers can bottom-out. Best is to over-spec the metal and then be the only code on it so you don't need to buffer at all.

(Worst is, you didn't get to spec the metal, and there's a whole OS full of someone else's Bright Ideas between you and that 200us-or-the-reactor-goes-blooie control loop. Good luck, you're gonna need it).

> you can put all your bits into a good sized buffer and use DMA to push them out a stable, hardware clocked rate.

Obviously; without buffering, glitch-free audio playback would be impossible. I made the assumption that when we're talking about timing on Linux, it's not about throwing buffers at a hardware clocked device.

Does libgpio or some other interface on Linux give you access to clocked DMA out of the box? Do the drivers on RPi support it?

All the gpio drivers I've worked with are just directly writing state to registers.

Yes, there are DMA driven gpio libraries.
In Linux you can isolate a cpu from the others, so you can get closer to a realtime solution (though you still get contention on cache lines, memory bus etc.). I haven't tried it though.

https://www.linuxjournal.com/article/6900

Ouch those graphs indicate worst case interrupt latency in the 100ms range... that's one old article!