2023-01-30 - 21:12

Scripts to set the SMP affinity mask of kernel threads

IRQ threads - Non-IRQ kernel threads

#!/bin/bash
 
defaultmask=7
 
cpucores=`grep ^processor /proc/cpuinfo | wc -l`
if test $cpucores -gt 16
then
  coremask=4294967295
elif test $cpucores -gt 8
then
  coremask=65535
elif test $cpucores -gt 4
then
  coremask=255
else
  coremask=15
fi
 
eval `cat /proc/cmdline | tr ' ' '\n' | grep isolcpus`
if test "$isolcpus"
then
  OLDIFS="$IFS"
  IFS=,
  isolmask=0
  for i in $isolcpus
  do
    thismask=$((2 ** $i))
    isolmask=$(($isolmask + $thismask))
  done
  IFS="$OLDIFS"
fi
if test "$isolmask"
then
  decidefaultmask=$(($coremask ^ $isolmask))
  defaultmask=`printf %x $decidefaultmask`
fi
 
if test "$1" -a "$1" = --help
then
  echo "Usage: `basename $0` <mask>"
  echo "Function: Set SMP affinity mask of kernel threads to <mask>"
  echo "          <mask> defaults to $defaultmask" 
  echo "Options: none"
  exit 1
fi
 
if test "$1"
then
  if echo $1 | grep -iq [^0-9a-f]
  then
    echo "Argument is expected to be a lower-case hexadecimal number, not $1"
    exit 1
  fi
  mask=$1
else
  mask=$defaultmask
fi
mask10=`printf %d 0x$mask`
 
redcolor='\033[0;31m'
yellowcolor='\033[0;33m'
greencolor='\033[0;32m'
nocolor='\033[0m'
 
red() {
  printf $redcolor$@$nocolor
}
 
yellow() {
  printf $yellowcolor$@$nocolor
}
 
green() {
  printf $greencolor$@$nocolor
}
 
NOT() {
  echo `red NOT`
}
 
is() {
  echo `green is`
}
 
can() {
  echo `yellow can`
}
 
could() {
  echo `yellow could`
}
 
cd /proc
 
for i in `ls -1d [0-9]* | sort -n`
do
  if grep -q "PPid:\W2$" $i/status 2>/dev/null
  then
    if grep -q -e "/[0-9]*$" -e "/[0-9]:" -e "^irq/[0-9]" $i/comm 2>/dev/null
    then
      # echo Affinity mask of kernel IRQ thread `cat $i/comm` will not be modified
      continue
    fi
    realcore10=`ps -o psr $i | sed 's/^ *//' | grep ^[0-9]`
    realcoremask10=$((2 ** $realcore10))
    affinity=`grep Cpus_allowed: $i/status | cut -f2 | sed s/^0*//`
    flags=`cat $i/stat | sed 's/(.*)/comm/' | cut -d" " -f 9`
    if test $(($flags & 0x04000000)) != 0
    then
      echo -n Affinity of `cat $i/comm` "($i)" can `NOT` be modified, since it has the PF_NO_SETAFFINITY flag set
      if test $((0x$affinity - 0x$mask)) = 0
      then
        if test $(($realcoremask10 & 0x$mask)) != 0
        then
          echo ", but the affinity mask 0x$affinity fortunately `is` correct, and the thread is currently running on core $realcore10 which `is` ok"
        else
          echo ", but the affinity mask 0x$affinity fortunately `is` correct; however, the thread is currently running on core $realcore10 which is `NOT` ok"
        fi
      else
        if test $(($realcoremask10 & 0x$mask)) != 0
        then
          echo ", but although the affinity mask is 0x$affinity which is `NOT` ok, the thread is currently running on core $realcore10 which `is` ok"
        else
          echo ", and because the affinity mask unfortunately is 0x$affinity which is `NOT` ok, the thread is running on core $realcore10 which is `NOT` ok"
        fi
      fi
      continue
    fi
 
    if test $((0x$affinity - 0x$mask)) = 0
    then
      echo -n Affinity of `cat $i/comm` "($i)" already is 0x$mask which `is` ok
      if test $(($realcoremask10 & 0x$mask)) != 0
      then
        echo ", and the thread is currently running on core $realcore10 which `is` ok"
      else
        echo ", but the thread is currently running on core $realcore10 which is `NOT` ok"
      fi
      continue
    fi
 
    if taskset -p $mask $i >/dev/null 2>&1
    then
      echo -n Affinity of `cat $i/comm` "($i)" was successfully attempted to be modified to 0x$mask
      realcore10=`ps -o psr $i | sed 's/^ *//' | grep ^[0-9]`
      realcoremask10=$((2 ** $realcore10))
      affinity=`grep Cpus_allowed: $i/status | cut -f2 | sed s/^0*//`
      if test $((0x$affinity - 0x$mask)) = 0
      then
        if test $(($realcoremask10 & 0x$mask)) != 0
        then
          echo " and `is` now 0x$mask, and the thread is currently running on core $realcore10 which `is` ok"
        else
          echo " and `is` now 0x$mask, but the thread is currently running on core $realcore10 which is `NOT` ok"
        fi
      else
        echo -n , but still is $affinity which `is`
        if test $((0x$affinity & 0x$mask)) != 0
        then
          if test $(($realcoremask10 & 0x$mask)) != 0
          then
            echo ok, and the thread is currently running on core $realcore10 which also `is` ok
          else 
            echo numerically ok, but the thread is currently running on core $realcore10 which is `NOT` ok
          fi
        else
          if test $(($realcoremask10 & 0x$mask)) != 0
          then
            echo `NOT` ok, but the thread is currently running on core $realcore10 which `is` ok
          else
            echo `NOT` ok, and the thread is in fact currently running on core $realcore10 which is `NOT` ok
          fi
        fi 
      fi
    else
      echo -n Affinity of `cat $i/comm` "($i)" could not be modified to 0x$mask
      masklen=`echo -n $mask | wc -c`
      affinitylen=`echo -n $affinity | wc -c`
      if test $affinitylen -gt $masklen
      then
        start=`expr $affinitylen - $masklen + 1`
        testaffinity=`echo $affinity | cut -c$start-$affinitylen`
      else
        testaffinity=$affinity
      fi
      if test $((~0x$mask & 0x$testaffinity)) = 0
      then
        if test $(($realcoremask10 & 0x$mask)) != 0
        then
          echo , but is 0x$affinity and `is` ok, and the thread is currently running on core $realcore10 which `is` ok
        else
          echo , but is 0x$affinity and `could` be ok, but the thread is currently running on core $realcore10 which is `NOT` ok
        fi
      else
        if test $(($realcoremask10 & 0x$mask)) != 0
        then
          echo " and still is 0x$affinity which is `NOT` ok, but the thread is currently running on core $realcore10 which `is` ok"
        else
          echo " and still is 0x$affinity which is `NOT` ok, and the thread is in fact currently running on core $realcore10 which is `NOT` ok"
        fi
      fi
    fi
  fi
done
 

Output of the above script when running it on the 4-core system at rack #5, slot #4 at shadow position

Affinity of rcu_gp (3) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of rcu_par_gp (4) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of mm_percpu_wq (8) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of rcu_preempt (11) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of kdevtmpfs (40) already is 0x7 which is ok, but the thread is currently running on core 3 which is NOT ok
Affinity of netns (41) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of rcu_tasks_kthre (42) already is 0x7 which is ok, and the thread is currently running on core 1 which is ok
Affinity of kauditd (43) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of khungtaskd (45) already is 0x7 which is ok, and the thread is currently running on core 1 which is ok
Affinity of oom_reaper (46) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of writeback (47) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 2 which is ok
Affinity of kcompactd0 (48) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of ksmd (49) already is 0x7 which is ok, and the thread is currently running on core 1 which is ok
Affinity of cryptd (69) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of kintegrityd (104) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 2 which is ok
Affinity of kblockd (105) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of blkcg_punt_bio (106) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of ata_sff (108) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 2 which is ok
Affinity of md (109) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of edac-poller (110) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of devfreq_wq (111) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of watchdogd (112) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of kswapd0 (114) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of kthrotld (117) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of acpi_thermal_pm (118) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 2 which is ok
Affinity of scsi_eh_0 (120) already is 0x7 which is ok, but the thread is currently running on core 3 which is NOT ok
Affinity of scsi_tmf_0 (121) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of scsi_eh_1 (122) already is 0x7 which is ok, and the thread is currently running on core 1 which is ok
Affinity of scsi_tmf_1 (123) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of scsi_eh_2 (124) already is 0x7 which is ok, and the thread is currently running on core 1 which is ok
Affinity of scsi_tmf_2 (125) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 2 which is ok
Affinity of scsi_eh_3 (126) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of scsi_tmf_3 (127) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of scsi_eh_4 (128) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of scsi_tmf_4 (129) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of scsi_eh_5 (130) already is 0x7 which is ok, and the thread is currently running on core 1 which is ok
Affinity of scsi_tmf_5 (131) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xff which is NOT ok, the thread is currently running on core 2 which is ok
Affinity of raid5wq (140) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of dm_bufio_cache (141) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of ipv6_addrconf (142) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of printk (200) already is 0x7 which is ok, and the thread is currently running on core 0 which is ok
Affinity of e1000e (262) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of e1000e (266) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of jbd2/sda2-8 (315) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of ext4-rsv-conver (316) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of firewire (371) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of rpciod (382) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of xprtiod (383) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of jbd2/sda1-8 (492) already is 0x7 which is ok, and the thread is currently running on core 2 which is ok
Affinity of ext4-rsv-conver (493) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of nfsiod (789) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of NFSv4 callback (6448) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, and because the affinity mask unfortunately is 0xff which is NOT ok, the thread is running on core 3 which is NOT ok
Affinity of kworker/u17:0-xprtiod (10168) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xf7 which is NOT ok, the thread is currently running on core 2 which is ok
Affinity of kworker/u16:1-events_unbound (10894) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xf7 which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of kworker/u16:0-events_unbound (12997) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xf7 which is NOT ok, the thread is currently running on core 0 which is ok
Affinity of kworker/u17:2-xprtiod (19479) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xf7 which is NOT ok, the thread is currently running on core 1 which is ok
Affinity of kworker/u16:2-flush-8:0 (20130) can NOT be modified, since it has the PF_NO_SETAFFINITY flag set, but although the affinity mask is 0xf7 which is NOT ok, the thread is currently running on core 2 which is ok