Startup Script

I'm not sure if the below makes any difference so may be worth further investigation.
This includes IRQ Balance and tuna to move things away from the isolated processors that are to be used by linuxcnc.
In this case for a Pi4 CPU's 2 and 3.

Installing Tools

First to install the tools we need

apt install tuna irqbalance

Startup Script

Create a new file /var/lib/dietpi/postboot.d/cpu.opt.sh

#!/bin/sh

# To isolate cores specified in isolcpus using irqbalance
irqbalance --foreground --oneshot

# Use tuna move all kernel threads away from cores 2-3
tuna --cpus=2-3 --isolate

# kernel workqueues needs to be moved away from isolated cores. To move all work queues to core 0 (cpumask 0x1):
find /sys/devices/virtual/workqueue -name cpumask  -exec sh -c 'echo 1 > {}' ';'

Make the file executable

chmod +x /var/lib/dietpi/postboot.d/cpu.opt.sh

Reporting on Status

IRQ Balance

For irqbalance we can check that it's done it's job using the below commands

# List CPU affinity for all IRQs
find /proc/irq/ -name smp_affinity_list -print -exec cat '{}' ';'

# verify that isolated cores are not receiving interrupts by monitoring /proc/interrupts
watch cat /proc/interrupts

Tuna

To get some feedback from the tuna setup

# Use the tuna command to show CPU affinities for all threads
tuna -P

# List current workqueue affinities:
find /sys/devices/virtual/workqueue -name cpumask -print -exec cat '{}' ';'

Perf

We can use perf to check the status as well

# Install linux perf
apt install linux-perf

# Verify if cores were successfully isolated by checking how many thread context switches are occurring per core
# The isolated cores should show a very low context switch count.
perf stat -e 'sched:sched_switch' -a -A --timeout 10000