Skip to content

Instantly share code, notes, and snippets.

@ihipop
Last active November 14, 2024 01:45
Show Gist options
  • Save ihipop/d97b731a252f49f69b8a0a7dd8f0aceb to your computer and use it in GitHub Desktop.
Save ihipop/d97b731a252f49f69b8a0a7dd8f0aceb to your computer and use it in GitHub Desktop.
Ethernet IRQ RPS CPU SMP Affinity
#!/bin/bash
match=eth0 # Network interface name
num_big_cores=2 # Number of high-performance cores to select
# Retrieve cpu_capacity for each core and sort in descending order
declare -A cpu_capacities
for cpu in /sys/devices/system/cpu/cpu[0-9]*; do
capacity_file="$cpu/cpu_capacity"
if [ -f "$capacity_file" ]; then
capacity=$(cat "$capacity_file")
cpu_number=$(basename "$cpu" | grep -o '[0-9]*')
cpu_capacities["$cpu_number"]=$capacity
fi
done
# Sort and select the highest performance cores
sorted_cores=($(for cpu in "${!cpu_capacities[@]}"; do echo "$cpu ${cpu_capacities[$cpu]}"; done | sort -k2 -nr | head -n "$num_big_cores" | awk '{print $1}'))
# Exit if no high-performance cores are found
if [ ${#sorted_cores[@]} -eq 0 ]; then
echo "No high-performance cores found. Please check system configuration."
exit 1
fi
# Generate smp_affinity_list in comma-separated format, and rps_cpus in hex bitmask format
smp_affinity_list=$(IFS=,; echo "${sorted_cores[*]}")
cpu_bitmask=0
for core in "${sorted_cores[@]}"; do
cpu_bitmask=$((cpu_bitmask | (1 << core)))
done
hex_bitmask=$(printf '%x' "$cpu_bitmask")
# Iterate through all possible IRQ paths, configure smp_affinity_list and rps_cpus
queue_index=0
while [ -f "/sys/class/net/${match}/queues/rx-${queue_index}/rps_cpus" ]; do
# Set RPS CPU bitmask for each RX queue
echo "$hex_bitmask" > "/sys/class/net/${match}/queues/rx-${queue_index}/rps_cpus"
echo "Set /sys/class/net/${match}/queues/rx-${queue_index}/rps_cpus to $hex_bitmask"
# Traverse the /proc/irq directory to locate corresponding IRQ files
for irq_path in /proc/irq/*/${match}-rx-${queue_index} /proc/irq/*/${match}; do
if [ -e "$irq_path" ]; then
irq=$(basename "$(dirname "$irq_path")")
if [ -f /proc/irq/${irq}/smp_affinity_list ]; then
echo "$smp_affinity_list" > /proc/irq/${irq}/smp_affinity_list
echo "Set /proc/irq/${irq}/smp_affinity_list to $smp_affinity_list"
fi
fi
done
((queue_index++))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment