kvm-get-cpus-script.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Create an awk script that takes as input numbers of CPUs and outputs
  5. # 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 awk
  10. # statements initializing the variables describing the system's topology.
  11. #
  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.
  14. cpuarrays="${1-/sys/devices/system/node}"
  15. scriptfile="${2}"
  16. statefile="${3}"
  17. if ! test -f "$cpuarrays"
  18. then
  19. echo "File not found: $cpuarrays" 1>&2
  20. exit 1
  21. fi
  22. scriptdir="`dirname "$scriptfile"`"
  23. if ! test -d "$scriptdir" || ! test -x "$scriptdir" || ! test -w "$scriptdir"
  24. then
  25. echo "Directory not usable for script output: $scriptdir"
  26. exit 1
  27. fi
  28. cat << '___EOF___' > "$scriptfile"
  29. BEGIN {
  30. ___EOF___
  31. cat "$cpuarrays" >> "$scriptfile"
  32. if test -r "$statefile"
  33. then
  34. cat "$statefile" >> "$scriptfile"
  35. fi
  36. cat << '___EOF___' >> "$scriptfile"
  37. }
  38. # Do we have the system architecture to guide CPU affinity?
  39. function gotcpus()
  40. {
  41. return numnodes != "";
  42. }
  43. # Return a comma-separated list of the next n CPUs.
  44. function nextcpus(n, i, s)
  45. {
  46. for (i = 0; i < n; i++) {
  47. if (nodecpus[curnode] == "")
  48. curnode = 0;
  49. if (cpu[curnode][curcpu[curnode]] == "")
  50. curcpu[curnode] = 0;
  51. if (s != "")
  52. s = s ",";
  53. s = s cpu[curnode][curcpu[curnode]];
  54. curcpu[curnode]++;
  55. curnode++
  56. }
  57. return s;
  58. }
  59. # Dump out the current node/CPU state so that a later invocation of this
  60. # script can continue where this one left off. Of course, this only works
  61. # when a state file was specified and where there was valid sysfs state.
  62. # Returns 1 if the state was dumped, 0 otherwise.
  63. #
  64. # Dumping the state for one system configuration and loading it into
  65. # another isn't likely to do what you want, whatever that might be.
  66. function dumpcpustate( i, fn)
  67. {
  68. ___EOF___
  69. echo ' fn = "'"$statefile"'";' >> $scriptfile
  70. cat << '___EOF___' >> "$scriptfile"
  71. if (fn != "" && gotcpus()) {
  72. print "curnode = " curnode ";" > fn;
  73. for (i = 0; i < numnodes; i++)
  74. if (curcpu[i] != "")
  75. print "curcpu[" i "] = " curcpu[i] ";" >> fn;
  76. return 1;
  77. }
  78. if (fn != "")
  79. print "# No CPU state to dump." > fn;
  80. return 0;
  81. }
  82. ___EOF___