106 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.0+3#4# Produce awk statements roughly depicting the system's CPU and cache5# layout. If the required information is not available, produce6# error messages as awk comments. Successful exit regardless.7#8# Usage: kvm-assign-cpus.sh /path/to/sysfs9 10T="`mktemp -d ${TMPDIR-/tmp}/kvm-assign-cpus.sh.XXXXXX`"11trap 'rm -rf $T' 0 212 13sysfsdir=${1-/sys/devices/system/node}14if ! cd "$sysfsdir" > $T/msg 2>&115then16 sed -e 's/^/# /' < $T/msg17 exit 018fi19nodelist="`ls -d node*`"20for i in node*21do22 if ! test -d $i/23 then24 echo "# Not a directory: $sysfsdir/node*"25 exit 026 fi27 for j in $i/cpu*/cache/index*28 do29 if ! test -d $j/30 then31 echo "# Not a directory: $sysfsdir/$j"32 exit 033 else34 break35 fi36 done37 indexlist="`ls -d $i/cpu* | grep 'cpu[0-9][0-9]*' | head -1 | sed -e 's,^.*$,ls -d &/cache/index*,' | sh | sed -e 's,^.*/,,'`"38 break39done40for i in node*/cpu*/cache/index*/shared_cpu_list41do42 if ! test -f $i43 then44 echo "# Not a file: $sysfsdir/$i"45 exit 046 else47 break48 fi49done50firstshared=51for i in $indexlist52do53 rm -f $T/cpulist54 for n in node*55 do56 f="$n/cpu*/cache/$i/shared_cpu_list"57 if ! cat $f > $T/msg 2>&158 then59 sed -e 's/^/# /' < $T/msg60 exit 061 fi62 cat $f >> $T/cpulist63 done64 if grep -q '[-,]' $T/cpulist65 then66 if test -z "$firstshared"67 then68 firstshared="$i"69 fi70 fi71done72if test -z "$firstshared"73then74 splitindex="`echo $indexlist | sed -e 's/ .*$//'`"75else76 splitindex="$firstshared"77fi78nodenum=079for n in node*80do81 cat $n/cpu*/cache/$splitindex/shared_cpu_list | sort -u -k1n |82 awk -v nodenum="$nodenum" '83 BEGIN {84 idx = 0;85 }86 87 {88 nlists = split($0, cpulists, ",");89 for (i = 1; i <= nlists; i++) {90 listsize = split(cpulists[i], cpus, "-");91 if (listsize == 1)92 cpus[2] = cpus[1];93 for (j = cpus[1]; j <= cpus[2]; j++) {94 print "cpu[" nodenum "][" idx "] = " j ";";95 idx++;96 }97 }98 }99 100 END {101 print "nodecpus[" nodenum "] = " idx ";";102 }'103 nodenum=`expr $nodenum + 1`104done105echo "numnodes = $nodenum;"106