|
| 1 | +#!/bin/bash |
| 2 | +# ------------------------------------------------------------ |
| 3 | +# Move all IRQs (except NICs) to non-isolated CPUs dynamically |
| 4 | +# ------------------------------------------------------------ |
| 5 | + |
| 6 | +# List of NIC interfaces to protect (keep IRQs on isolated cores) |
| 7 | +NIC_IFACES=("eth0" "eth1") # modify as needed |
| 8 | + |
| 9 | +# Determine total CPU count |
| 10 | +CPU_COUNT=$(nproc) |
| 11 | + |
| 12 | +# Set first isolated CPU index (adjust if your isolated CPUs differ) |
| 13 | +ISOLATED_END=1 # e.g., CPUs 0-1 are isolated |
| 14 | + |
| 15 | +# Build CPU mask for CPUs >= ISOLATED_END + 1 |
| 16 | +# Example: if CPU_COUNT=8, ISOLATED_END=1 -> mask = 11111100 = FC |
| 17 | +# The mask is binary, then converted to hex |
| 18 | + |
| 19 | +CPU_MASK=$(printf "%X" $(( (1 << (CPU_COUNT)) - 1 - ((1 << (ISOLATED_END + 1)) - 1) ))) |
| 20 | + |
| 21 | +echo "Detected CPU mask for IRQs: $CPU_MASK (CPUs >= $((ISOLATED_END + 1)))" |
| 22 | + |
| 23 | +# Helper function to check if IRQ belongs to a NIC |
| 24 | +is_nic_irq() { |
| 25 | + local irq_file="$1" |
| 26 | + for nic in "${NIC_IFACES[@]}"; do |
| 27 | + if grep -q "$nic" "$irq_file"; then |
| 28 | + return 0 # It's a NIC IRQ |
| 29 | + fi |
| 30 | + done |
| 31 | + return 1 # Not a NIC IRQ |
| 32 | +} |
| 33 | + |
| 34 | +# Iterate through all IRQ directories |
| 35 | +for irq_dir in /proc/irq/*; do |
| 36 | + [ -d "$irq_dir" ] || continue |
| 37 | + irq_num=$(basename "$irq_dir") |
| 38 | + smp_file="$irq_dir/smp_affinity" |
| 39 | + |
| 40 | + [ -f "$smp_file" ] || continue |
| 41 | + |
| 42 | + if is_nic_irq "$irq_dir/affinity_hint"; then |
| 43 | + echo "Skipping NIC IRQ $irq_num" |
| 44 | + continue |
| 45 | + fi |
| 46 | + |
| 47 | + # Move IRQ to CPUs >= ISOLATED_END + 1 |
| 48 | + echo "$CPU_MASK" > "$smp_file" |
| 49 | + echo "Moved IRQ $irq_num to CPUs >= $((ISOLATED_END + 1))" |
| 50 | +done |
0 commit comments