89 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.0+3#4# Create an awk script that takes as input numbers of CPUs and outputs5# lists of CPUs, one per line in both cases.6#7# Usage: kvm-get-cpus-script.sh /path/to/cpu/arrays /path/to/put/script [ /path/to/state ]8#9# The CPU arrays are output by kvm-assign-cpus.sh, and are valid awk10# statements initializing the variables describing the system's topology.11#12# The optional state is input by this script (if the file exists and is13# non-empty), and can also be output by this script.14 15cpuarrays="${1-/sys/devices/system/node}"16scriptfile="${2}"17statefile="${3}"18 19if ! test -f "$cpuarrays"20then21 echo "File not found: $cpuarrays" 1>&222 exit 123fi24scriptdir="`dirname "$scriptfile"`"25if ! test -d "$scriptdir" || ! test -x "$scriptdir" || ! test -w "$scriptdir"26then27 echo "Directory not usable for script output: $scriptdir"28 exit 129fi30 31cat << '___EOF___' > "$scriptfile"32BEGIN {33___EOF___34cat "$cpuarrays" >> "$scriptfile"35if test -r "$statefile"36then37 cat "$statefile" >> "$scriptfile"38fi39cat << '___EOF___' >> "$scriptfile"40}41 42# Do we have the system architecture to guide CPU affinity?43function gotcpus()44{45 return numnodes != "";46}47 48# Return a comma-separated list of the next n CPUs.49function nextcpus(n, i, s)50{51 for (i = 0; i < n; i++) {52 if (nodecpus[curnode] == "")53 curnode = 0;54 if (cpu[curnode][curcpu[curnode]] == "")55 curcpu[curnode] = 0;56 if (s != "")57 s = s ",";58 s = s cpu[curnode][curcpu[curnode]];59 curcpu[curnode]++;60 curnode++61 }62 return s;63}64 65# Dump out the current node/CPU state so that a later invocation of this66# script can continue where this one left off. Of course, this only works67# when a state file was specified and where there was valid sysfs state.68# Returns 1 if the state was dumped, 0 otherwise.69#70# Dumping the state for one system configuration and loading it into71# another isn't likely to do what you want, whatever that might be.72function dumpcpustate( i, fn)73{74___EOF___75echo ' fn = "'"$statefile"'";' >> $scriptfile76cat << '___EOF___' >> "$scriptfile"77 if (fn != "" && gotcpus()) {78 print "curnode = " curnode ";" > fn;79 for (i = 0; i < numnodes; i++)80 if (curcpu[i] != "")81 print "curcpu[" i "] = " curcpu[i] ";" >> fn;82 return 1;83 }84 if (fn != "")85 print "# No CPU state to dump." > fn;86 return 0;87}88___EOF___89