2 # SPDX-License-Identifier: GPL-2.0+
4 # Create an awk script that takes as input numbers of CPUs and outputs
5 # lists of CPUs, one per line in both cases.
7 # Usage: kvm-get-cpus-script.sh /path/to/cpu/arrays /path/to/put/script [ /path/to/state ]
9 # The CPU arrays are output by kvm-assign-cpus.sh, and are valid awk
10 # statements initializing the variables describing the system's topology.
12 # The optional state is input by this script (if the file exists and is
13 # non-empty), and can also be output by this script.
15 cpuarrays
="${1-/sys/devices/system/node}"
19 if ! test -f "$cpuarrays"
21 echo "File not found: $cpuarrays" 1>&2
24 scriptdir
="`dirname "$scriptfile"`"
25 if ! test -d "$scriptdir" ||
! test -x "$scriptdir" ||
! test -w "$scriptdir"
27 echo "Directory not usable for script output: $scriptdir"
31 cat << '___EOF___' > "$scriptfile"
34 cat "$cpuarrays" >> "$scriptfile"
35 if test -r "$statefile"
37 cat "$statefile" >> "$scriptfile"
39 cat << '___EOF___' >> "$scriptfile"
42 # Do we have the system architecture to guide CPU affinity?
45 return numnodes
!= "";
48 # Return a comma-separated list of the next n CPUs.
49 function nextcpus
(n
, i
, s
)
51 for (i
= 0; i
< n
; i
++) {
52 if (nodecpus
[curnode
] == "")
54 if (cpu
[curnode
][curcpu
[curnode
]] == "")
58 s
= s cpu
[curnode
][curcpu
[curnode
]];
65 # Dump out the current node/CPU state so that a later invocation of this
66 # script can continue where this one left off. Of course, this only works
67 # when a state file was specified and where there was valid sysfs state.
68 # Returns 1 if the state was dumped, 0 otherwise.
70 # Dumping the state for one system configuration and loading it into
71 # another isn't likely to do what you want, whatever that might be.
72 function dumpcpustate
( i
, fn
)
75 echo ' fn = "'"$statefile"'";' >> $scriptfile
76 cat << '___EOF___' >> "$scriptfile"
77 if (fn
!= "" && gotcpus
()) {
78 print
"curnode = " curnode
";" > fn
;
79 for (i
= 0; i
< numnodes
; i
++)
81 print
"curcpu[" i
"] = " curcpu
[i
] ";" >> fn
;
85 print
"# No CPU state to dump." > fn
;