Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook

  • gene_weber
  • gene_weber's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
16 Jan 2025 12:38 #319119 by gene_weber
Replied by gene_weber on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
The good news is switching to X11 is trivial.

You can check the session type:
echo $XDG_SESSION_TYPE
    wayland

Launch Rasp-config command line tool in a terminal:
sudo raspi-config

Arrow down to "Advanced Options", and enter.
Arrow down to "Wayland                 Switch between X and Wayland backends", and enter.
If not on the line that says "W1 X11     Openbox window manager with X11 backend", arrow to it and enter.
The popup should say "Openbox on X11 is active" and have "Ok" highlighted. Enter.
It will return to the main screen. Use left-righ arrows to select "Finish". Enter.
Popup asks if you would like to reboot now, with "Yes" highlighted. Enter.

After reboot:
echo $XDG_SESSION_TYPE
    x11

Now here's the interesting apples to apples comparison. The only difference between these two pre-jitter-optimization baseline latency runs is Wayland vs X11.

Wayland:
 

X11:
 

 
Attachments:

Please Log in or Create an account to join the conversation.

  • cornholio
  • cornholio's Avatar
  • Away
  • Platinum Member
  • Platinum Member
More
16 Jan 2025 13:10 #319121 by cornholio
Replied by cornholio on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
Looks a lot better.

One thing that was strange when running a basic config I got a real time error. hmmmmm

ATM the best image I've come across is Joco's rpi5 image rod has on his googledrive, that has a 6.6.54 kernel. It's my choice of the pre-built images.

I installed mate desktop, with 2 cores isolated, watching youtube latency is about 17-18us on my pi.

Please Log in or Create an account to join the conversation.

  • gene_weber
  • gene_weber's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
16 Jan 2025 14:42 #319126 by gene_weber
Replied by gene_weber on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
I edited my original post to include changing the display protocol from Wayland to X11. Comments by rodw and cornholio were based on the terrible jitter that Wayland induces as shown above.

Note that Kunena has the worst forum editor I've ever used, and it was quite a job to edit those posts and restore the formatting. I wonder if forum posting would increase with forum software that was more user friendly.

Please Log in or Create an account to join the conversation.

  • cornholio
  • cornholio's Avatar
  • Away
  • Platinum Member
  • Platinum Member
More
16 Jan 2025 15:46 #319128 by cornholio
Replied by cornholio on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
Ah yes the editor. Truly you have come of age and walked the path of darkness ;)

Please Log in or Create an account to join the conversation.

  • gene_weber
  • gene_weber's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
17 Jan 2025 17:00 #319192 by gene_weber
Replied by gene_weber on topic Raspberry Pi OS PREEMPT RT 6.13 Kernel Cookbook
Rather than starting a new thread about jitter mitigation, I'm just going to add it here.

First, I'm keeping in mind that Peter Wallace said " The jitter is basically unimportant with a 7C81 unless its so bad you
get real time errors at the normal servo thread rate
."

I'm also treating the Raspberry Pi + 7C81 as a dedicated controller, not a PC. By that I mean I'm not going to be doing anything on the Pi except LinuxCNC. I may turn on WIFI to update packages, or use email to transfer a file. But I plan to even turn off WIFI before I launch LinuxCNC.


I ran several tests to see which combination of settings offered the most optimized jitter. I ran two Glxgears for all tests as I though that was reasonable, and ran the tests for long periods of time unless they looked bad early on.

I thought that turning off all unused services would decrease Jitter. So I turned off 9 of the 23 services, such as CUPS, Bluetooth, etc. To my surprise this did not decrease jitter.

Screen blank increased jitter a bit, so I turned that off. It doesn't make much sense anyway on a CNC controller.


These are the settings I added to the line already in /boot/firmware/cmdline.txt:
skew_tick=1 kthread_cpus=0-2 irqaffinity=0-2 rcu_nocb_poll rcu_nocbs=3 nohz=on nohz_full=3 isolcpus=3

I tried a few others that some people recommended on various blogs. Nothing else I tried had any noticeable positive impact on jitter, and some noticeably slowed down the responsiveness of the Pi.


Turning off timer_migration did reduce jitter. What I read said that you could make this setting permanent by adding it to the boot command line, or setting it in either of two different system files. Oddly none of that worked. So I setup a cron job to disable it at boot, and that works.
crontab -e
Add the following line:
@reboot sudo sysctl kernel.timer_migration=0


The other thing that helped reduce jitter was changing the scheduling policy to real-time and increasing the priority. I decided to create a shell script to launch latency-histogram, and eventually linuxcnc, which changes these things on the process.
#!/bin/bash
#
# Run with the SCHED_RR real-time policy at the highest priority level (99)
# Uses chrt to switch the process policy and priority immediately after launch.
#
# Gene Weber - January 2025
#
# "The difference between SCHED_FIFO and SCHED_RR is that among tasks with the same priority,
# SCHED_RR performs a round-robin with a certain timeslice; SCHED_FIFO, instead, needs the task
# to explicitly yield the processor." -Claudio

priority=99

# Truncate $1 (the command) to 15 characters starting at 0.
command=${1:0:15}

# Execute the command line provided to rrt as a background task.
`$@` &

# Get the process ID of the command.
PID=`pgrep $command`

# Change the real-time policy to SCHED_FIFO with a priority of $priority.
sudo chrt -r -p $priority $PID

I named it rrt (run real-time). So to launch latency-histogram it's simply:
rrt latency-histogram --nobase --sbins 1000

All spawned sub-processes inherit the policy and priority. So if linuxcnc is launched with rrt:
rrt linuxcnc
all of the processes that are part of LinuxCNC have the policy of SCHED_RR and priority of 99. I used Round Robin scheduling to make sure no LinuxCNC process "starves" another.

I don't know if this will work in the end, but I'm going to give it a try. Nothing ventured, nothing gained.

Implementing all of these, here is the jitter histogram. Good enough.

 
Attachments:

Please Log in or Create an account to join the conversation.

Time to create page: 0.091 seconds
Powered by Kunena Forum