20-smp-tune 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. [ "$ACTION" = add ] || exit
  3. NPROCS="$(grep -c "^processor.*:" /proc/cpuinfo)"
  4. [ "$NPROCS" -gt 1 ] || exit
  5. PROC_MASK="$(( (1 << $NPROCS) - 1 ))"
  6. find_irq_cpu() {
  7. local dev="$1"
  8. local match="$(grep -m 1 "$dev\$" /proc/interrupts)"
  9. local cpu=0
  10. [ -n "$match" ] && {
  11. set -- $match
  12. shift
  13. for cur in `seq 1 $NPROCS`; do
  14. [ "$1" -gt 0 ] && {
  15. cpu=$(($cur - 1))
  16. break
  17. }
  18. shift
  19. done
  20. }
  21. echo "$cpu"
  22. }
  23. set_hex_val() {
  24. local file="$1"
  25. local val="$2"
  26. val="$(printf %x "$val")"
  27. [ -n "$DEBUG" ] && echo "$file = $val"
  28. echo "$val" > "$file"
  29. }
  30. default_ps="$(uci get "network.@globals[0].default_ps")"
  31. [ -n "$default_ps" -a "$default_ps" != 1 ] && exit 0
  32. exec 512>/var/lock/smp_tune.lock
  33. flock 512 || exit 1
  34. for dev in /sys/class/net/*; do
  35. [ -d "$dev" ] || continue
  36. # ignore virtual interfaces
  37. [ -n "$(ls "${dev}/" | grep '^lower_')" ] && continue
  38. [ -d "${dev}/device" ] || continue
  39. device="$(readlink "${dev}/device")"
  40. device="$(basename "$device")"
  41. irq_cpu="$(find_irq_cpu "$device")"
  42. irq_cpu_mask="$((1 << $irq_cpu))"
  43. for q in ${dev}/queues/rx-*; do
  44. set_hex_val "$q/rps_cpus" "$(($PROC_MASK & ~$irq_cpu_mask))"
  45. done
  46. ntxq="$(ls -d ${dev}/queues/tx-* | wc -l)"
  47. idx=$(($irq_cpu + 1))
  48. for q in ${dev}/queues/tx-*; do
  49. set_hex_val "$q/xps_cpus" "$((1 << $idx))"
  50. let "idx = idx + 1"
  51. [ "$idx" -ge "$NPROCS" ] && idx=0
  52. done
  53. done