kvm-assign-cpus.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Produce awk statements roughly depicting the system's CPU and cache
  5. # layout. If the required information is not available, produce
  6. # error messages as awk comments. Successful exit regardless.
  7. #
  8. # Usage: kvm-assign-cpus.sh /path/to/sysfs
  9. T="`mktemp -d ${TMPDIR-/tmp}/kvm-assign-cpus.sh.XXXXXX`"
  10. trap 'rm -rf $T' 0 2
  11. sysfsdir=${1-/sys/devices/system/node}
  12. if ! cd "$sysfsdir" > $T/msg 2>&1
  13. then
  14. sed -e 's/^/# /' < $T/msg
  15. exit 0
  16. fi
  17. nodelist="`ls -d node*`"
  18. for i in node*
  19. do
  20. if ! test -d $i/
  21. then
  22. echo "# Not a directory: $sysfsdir/node*"
  23. exit 0
  24. fi
  25. for j in $i/cpu*/cache/index*
  26. do
  27. if ! test -d $j/
  28. then
  29. echo "# Not a directory: $sysfsdir/$j"
  30. exit 0
  31. else
  32. break
  33. fi
  34. done
  35. indexlist="`ls -d $i/cpu* | grep 'cpu[0-9][0-9]*' | head -1 | sed -e 's,^.*$,ls -d &/cache/index*,' | sh | sed -e 's,^.*/,,'`"
  36. break
  37. done
  38. for i in node*/cpu*/cache/index*/shared_cpu_list
  39. do
  40. if ! test -f $i
  41. then
  42. echo "# Not a file: $sysfsdir/$i"
  43. exit 0
  44. else
  45. break
  46. fi
  47. done
  48. firstshared=
  49. for i in $indexlist
  50. do
  51. rm -f $T/cpulist
  52. for n in node*
  53. do
  54. f="$n/cpu*/cache/$i/shared_cpu_list"
  55. if ! cat $f > $T/msg 2>&1
  56. then
  57. sed -e 's/^/# /' < $T/msg
  58. exit 0
  59. fi
  60. cat $f >> $T/cpulist
  61. done
  62. if grep -q '[-,]' $T/cpulist
  63. then
  64. if test -z "$firstshared"
  65. then
  66. firstshared="$i"
  67. fi
  68. fi
  69. done
  70. if test -z "$firstshared"
  71. then
  72. splitindex="`echo $indexlist | sed -e 's/ .*$//'`"
  73. else
  74. splitindex="$firstshared"
  75. fi
  76. nodenum=0
  77. for n in node*
  78. do
  79. cat $n/cpu*/cache/$splitindex/shared_cpu_list | sort -u -k1n |
  80. awk -v nodenum="$nodenum" '
  81. BEGIN {
  82. idx = 0;
  83. }
  84. {
  85. nlists = split($0, cpulists, ",");
  86. for (i = 1; i <= nlists; i++) {
  87. listsize = split(cpulists[i], cpus, "-");
  88. if (listsize == 1)
  89. cpus[2] = cpus[1];
  90. for (j = cpus[1]; j <= cpus[2]; j++) {
  91. print "cpu[" nodenum "][" idx "] = " j ";";
  92. idx++;
  93. }
  94. }
  95. }
  96. END {
  97. print "nodecpus[" nodenum "] = " idx ";";
  98. }'
  99. nodenum=`expr $nodenum + 1`
  100. done
  101. echo "numnodes = $nodenum;"