cpu-hotplug.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ===================
  2. Virtual CPU hotplug
  3. ===================
  4. A complete example of vCPU hotplug (and hot-unplug) using QMP
  5. ``device_add`` and ``device_del``.
  6. vCPU hotplug
  7. ------------
  8. (1) Launch QEMU as follows (note that the "maxcpus" is mandatory to
  9. allow vCPU hotplug)::
  10. $ qemu-system-x86_64 -display none -no-user-config -m 2048 \
  11. -nodefaults -monitor stdio -machine pc,accel=kvm,usb=off \
  12. -smp 1,maxcpus=2 -cpu IvyBridge-IBRS \
  13. -qmp unix:/tmp/qmp-sock,server=on,wait=off
  14. (2) Run 'qmp-shell' (located in the source tree, under: "scripts/qmp/)
  15. to connect to the just-launched QEMU::
  16. $> ./qmp-shell -p -v /tmp/qmp-sock
  17. [...]
  18. (QEMU)
  19. (3) Find out which CPU types could be plugged, and into which sockets::
  20. (QEMU) query-hotpluggable-cpus
  21. {
  22. "execute": "query-hotpluggable-cpus",
  23. "arguments": {}
  24. }
  25. {
  26. "return": [
  27. {
  28. "props": {
  29. "core-id": 1,
  30. "socket-id": 0,
  31. "thread-id": 0
  32. },
  33. "type": "IvyBridge-IBRS-x86_64-cpu",
  34. "vcpus-count": 1
  35. },
  36. {
  37. "props": {
  38. "core-id": 0,
  39. "socket-id": 0,
  40. "thread-id": 0
  41. },
  42. "qom-path": "/machine/unattached/device[0]",
  43. "type": "IvyBridge-IBRS-x86_64-cpu",
  44. "vcpus-count": 1
  45. }
  46. ]
  47. }
  48. (QEMU)
  49. (4) The ``query-hotpluggable-cpus`` command returns an object for CPUs
  50. that are present (containing a "qom-path" member) or which may be
  51. hot-plugged (no "qom-path" member). From its output in step (3), we
  52. can see that ``IvyBridge-IBRS-x86_64-cpu`` is present in socket 0 core 0,
  53. while hot-plugging a CPU into socket 0 core 1 requires passing the listed
  54. properties to QMP ``device_add``::
  55. (QEMU) device_add id=cpu-2 driver=IvyBridge-IBRS-x86_64-cpu socket-id=0 core-id=1 thread-id=0
  56. {
  57. "execute": "device_add",
  58. "arguments": {
  59. "core-id": 1,
  60. "driver": "IvyBridge-IBRS-x86_64-cpu",
  61. "id": "cpu-2",
  62. "socket-id": 0,
  63. "thread-id": 0
  64. }
  65. }
  66. {
  67. "return": {}
  68. }
  69. (QEMU)
  70. (5) Optionally, run QMP ``query-cpus-fast`` for some details about the
  71. vCPUs::
  72. (QEMU) query-cpus-fast
  73. {
  74. "arguments": {}
  75. "execute": "query-cpus-fast",
  76. }
  77. {
  78. "return": [
  79. {
  80. "cpu-index": 0,
  81. "props": {
  82. "core-id": 0,
  83. "socket-id": 0,
  84. "thread-id": 0
  85. },
  86. "qom-path": "/machine/unattached/device[0]",
  87. "target": "x86_64",
  88. "thread-id": 28957
  89. },
  90. {
  91. "cpu-index": 1,
  92. "props": {
  93. "core-id": 1,
  94. "socket-id": 0,
  95. "thread-id": 0
  96. },
  97. "qom-path": "/machine/peripheral/cpu-2",
  98. "target": "x86_64",
  99. "thread-id": 29095
  100. }
  101. ]
  102. }
  103. (QEMU)
  104. vCPU hot-unplug
  105. ---------------
  106. From the 'qmp-shell', invoke the QMP ``device_del`` command::
  107. (QEMU) device_del id=cpu-2
  108. {
  109. "arguments": {
  110. "id": "cpu-2"
  111. }
  112. "execute": "device_del",
  113. }
  114. {
  115. "return": {}
  116. }
  117. (QEMU)
  118. .. note::
  119. vCPU hot-unplug requires guest cooperation; so the ``device_del``
  120. command above does not guarantee vCPU removal -- it's a "request to
  121. unplug". At this point, the guest will get a System Control
  122. Interrupt (SCI) and calls the ACPI handler for the affected vCPU
  123. device. Then the guest kernel will bring the vCPU offline and tell
  124. QEMU to unplug it.