2 # SPDX-License-Identifier: GPL-2.0+
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.
8 # Usage: kvm-assign-cpus.sh /path/to/sysfs
10 T
="`mktemp -d ${TMPDIR-/tmp}/kvm-assign-cpus.sh.XXXXXX`"
13 sysfsdir
=${1-/sys/devices/system/node}
14 if ! cd "$sysfsdir" > $T/msg
2>&1
16 sed -e 's/^/# /' < $T/msg
19 nodelist
="`ls -d node*`"
24 echo "# Not a directory: $sysfsdir/node*"
27 for j
in $i/cpu
*/cache
/index
*
31 echo "# Not a directory: $sysfsdir/$j"
37 indexlist
="`ls -d $i/cpu* | grep 'cpu[0-9][0-9]*' | head -1 | sed -e 's,^.*$,ls -d &/cache/index*,' | sh | sed -e 's,^.*/,,'`"
40 for i
in node
*/cpu
*/cache
/index
*/shared_cpu_list
44 echo "# Not a file: $sysfsdir/$i"
56 f
="$n/cpu*/cache/$i/shared_cpu_list"
57 if ! cat $f > $T/msg
2>&1
59 sed -e 's/^/# /' < $T/msg
64 if grep -q '[-,]' $T/cpulist
66 if test -z "$firstshared"
72 if test -z "$firstshared"
74 splitindex
="`echo $indexlist | sed -e 's/ .*$//'`"
76 splitindex
="$firstshared"
81 cat $n/cpu
*/cache
/$splitindex/shared_cpu_list |
sort -u -k1n |
82 awk -v nodenum
="$nodenum" '
88 nlists = split($0, cpulists, ",");
89 for (i = 1; i <= nlists; i++) {
90 listsize = split(cpulists[i], cpus, "-");
93 for (j = cpus[1]; j <= cpus[2]; j++) {
94 print "cpu[" nodenum "][" idx "] = " j ";";
101 print "nodecpus[" nodenum "] = " idx ";";
103 nodenum
=`expr $nodenum + 1`
105 echo "numnodes = $nodenum;"