Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / rcutorture / bin / kvm-get-cpus-script.sh
blob20c7c53c5795e275276e17cd41a5da0393236de5
1 #!/bin/sh
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}"
16 scriptfile="${2}"
17 statefile="${3}"
19 if ! test -f "$cpuarrays"
20 then
21 echo "File not found: $cpuarrays" 1>&2
22 exit 1
24 scriptdir="`dirname "$scriptfile"`"
25 if ! test -d "$scriptdir" || ! test -x "$scriptdir" || ! test -w "$scriptdir"
26 then
27 echo "Directory not usable for script output: $scriptdir"
28 exit 1
31 cat << '___EOF___' > "$scriptfile"
32 BEGIN {
33 ___EOF___
34 cat "$cpuarrays" >> "$scriptfile"
35 if test -r "$statefile"
36 then
37 cat "$statefile" >> "$scriptfile"
39 cat << '___EOF___' >> "$scriptfile"
42 # Do we have the system architecture to guide CPU affinity?
43 function gotcpus()
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] == "")
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++
62 return s;
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)
74 ___EOF___
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++)
80 if (curcpu[i] != "")
81 print "curcpu[" i "] = " curcpu[i] ";" >> fn;
82 return 1;
84 if (fn != "")
85 print "# No CPU state to dump." > fn;
86 return 0;
88 ___EOF___