Skip to content

Instantly share code, notes, and snippets.

@Bartvz
Created October 17, 2024 15:18
Show Gist options
  • Save Bartvz/dc524964543bfe8b4c41f4fafad82ef7 to your computer and use it in GitHub Desktop.
Save Bartvz/dc524964543bfe8b4c41f4fafad82ef7 to your computer and use it in GitHub Desktop.
A script to balance IRQs for the Belkin RT3200 running OpenWRT
#!/bin/sh
# Function to set IRQ affinity
set_affinity() {
irq=$1
cpu_mask=$2
# Check if the CPU mask is valid
if echo "$cpu_mask" | grep -qE '^[0-9A-Fa-f]+$'; then
echo "$cpu_mask" > "/proc/irq/$irq/smp_affinity" 2>/dev/null
if [ $? -eq 0 ]; then
logger "Set IRQ $irq affinity to CPU mask $cpu_mask"
else
logger "Failed to set IRQ $irq affinity: IO error"
fi
else
logger "Invalid CPU mask for IRQ $irq: $cpu_mask"
fi
}
# Count the number of available CPUs by reading from /proc/cpuinfo
num_cpus=$(grep -c '^processor' /proc/cpuinfo)
# List of IRQs to exclude
excluded_irqs="10 14 67 116 130 133 134 135 136 137 140"
# Iterate over /proc/interrupts to determine IRQs
while read -r line; do
# Match lines with IRQ numbers (excluding "IPI" and "Err" lines)
if echo "$line" | grep -E '^[[:space:]]*[0-9]+:' > /dev/null; then
irq=$(echo "$line" | awk -F: '{print $1}' | tr -d ' ')
device_name=$(echo "$line" | awk -F: '{print $2}' | tr -d ' ')
# Check if the IRQ is in the excluded list
if echo "$excluded_irqs" | grep -q "\b$irq\b"; then
logger "Skipping excluded IRQ $irq"
continue
fi
# Check if the current IRQ belongs to mt7615e
if echo "$device_name" | grep -q "mt7615e"; then
set_affinity "$irq" "2" # Set IRQ for mt7615e to CPU mask 2 (cpu1)
continue
fi
# Rotate IRQ affinities between available CPUs
affinity_cpu=$((non_excluded_count % num_cpus))
# Set the CPU mask (convert CPU number to hexadecimal bitmask)
cpu_mask=$(printf "%X" $((1 << affinity_cpu)))
# Apply the affinity setting
set_affinity "$irq" "$cpu_mask"
# Increment the non-excluded IRQ counter
non_excluded_count=$((non_excluded_count + 1))
fi
done < /proc/interrupts
logger "IRQ affinities have been updated."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment