Last active
October 17, 2024 15:18
-
-
Save Bartvz/b7a1a631b943815c5079d675a962baaa to your computer and use it in GitHub Desktop.
A script to balance IRQs for the Xiaomi AX3000T running OpenWRT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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="11 15 16 53 80 81 82 83" | |
# Initialize a counter for non-excluded IRQs | |
non_excluded_count=0 | |
# 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 current IRQ belongs to mt7915e | |
if echo "$device_name" | grep -q "mt7915e"; then | |
set_affinity "$irq" "2" # Set mt7915e IRQ to CPU mask 2 | |
logger "Set IRQ $irq (mt7915e) affinity to CPU mask 2" | |
continue | |
fi | |
# 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 | |
# 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 for non-excluded IRQs." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment