我已经使用 Oracle VM VirtualBox Manager 在虚拟环境中安装了 Ubuntu,并且运行良好。
不过我想启用多个核心 - 如下图所示,我的 Windows 机器有 2 个核心:
现在,当我在 Ubuntu 中运行时,lscpu
我收到以下信息,显示仅使用了 1 个核心:
我尝试更改虚拟框的设置以启用多个处理器,但它不允许我这样做,如下图所示:
如何在我的虚拟环境中启用多个核心(处理器)?
答案1
要在您的虚拟机中启用超过 1 个 CPU,还需要进行以下设置:
常规设置选项卡中的 64 位虚拟客户机硬件(适用于 64 位客户机)
在主板设置选项卡中启用 I/O APIC
在“加速”选项卡中启用硬件虚拟化(VT-x / AMD-V)
请注意,如果您在所有可用核心上运行虚拟机,则在为 CPU 分配执行上限时您可能会体验到更好的主机性能。
如果您尚未启用热插拔 CPU虚拟机更改核心数的功能只能在关闭。
只有使用多核的软件才能在客户机上实现性能提升。只有这样,为所有核心提供执行上限才有意义。
答案2
如果您的虚拟机当前正在运行(或“已保存”),则您无法更改该值。请先关闭虚拟机。
答案3
我正在开发一个在虚拟机上按顺序执行的多线程程序。我遇到了和你类似的问题。我通过以下方式修复了它:
- 停止虚拟机或者退出。
- 启动 VMWare --> 选择机器 --> 单击系统 --> 单击处理器 --> 更改处理器数量。
- 启动虚拟机。
以前,标准线程中的 std::thread::hardware_concurrency() 返回 1,但是,经过更改后,它会返回虚拟机设置上的 CPU 数量。
现在,我能够在 VM 上成功运行我的多线程程序。
答案4
其他答案对我来说不起作用,所以我分享了我的解决方案,该解决方案基于VirtualBox 手册 §2.4 CPU 热插拔。
GUI 允许我添加 32 个 vCPU,但 Linux 客户机启动时nproc
只会cat /proc/cpuinfo
显示一个 CPU/核心/线程。我可以看到dmesg
检测到了 32 个 vCPU:
# dmesg | grep -E 'smp:|smpboot:'
[ 0.000000] smpboot: Allowing 32 CPUs, 31 hotplug CPUs
[ 0.192151] smpboot: CPU0: Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz (family: 0x6, model: 0x55, stepping: 0x4)
[ 0.193848] smp: Bringing up secondary CPUs ...
[ 0.193850] smp: Brought up 1 node, 1 CPU
[ 0.193852] smpboot: Max logical packages: 32
[ 0.193854] smpboot: Total of 1 processors activated (4788.97 BogoMIPS)
似乎热插拔已启用,但由于某种原因 CPU 未在线。
在主机上运行以下命令:
for i in {1..31}; do VBoxManage controlvm $VM plugcpu $i; done
从客户机内部我可以看到 CPU 已经热插拔,但它们不会自动上线:
# dmesg | grep 'has been hot-added'
[ 317.236099] CPU1 has been hot-added
[ 317.289383] CPU2 has been hot-added
[ 317.341704] CPU3 has been hot-added
[ 317.393434] CPU4 has been hot-added
[ 317.602969] CPU5 has been hot-added
[ 317.603570] CPU6 has been hot-added
[ 317.603810] CPU7 has been hot-added
[ 317.604031] CPU8 has been hot-added
[ 317.780191] CPU9 has been hot-added
[ 317.780498] CPU10 has been hot-added
[ 317.780745] CPU11 has been hot-added
[ 317.817875] CPU12 has been hot-added
[ 317.870629] CPU13 has been hot-added
[ 317.911276] CPU14 has been hot-added
[ 317.962533] CPU15 has been hot-added
[ 318.012838] CPU16 has been hot-added
[ 318.063693] CPU17 has been hot-added
[ 318.116346] CPU18 has been hot-added
[ 318.167847] CPU19 has been hot-added
[ 318.232230] CPU20 has been hot-added
[ 318.284266] CPU21 has been hot-added
[ 318.344941] CPU22 has been hot-added
[ 318.373422] CPU23 has been hot-added
[ 318.501419] CPU24 has been hot-added
[ 318.501714] CPU25 has been hot-added
[ 318.535717] CPU26 has been hot-added
[ 318.652636] CPU27 has been hot-added
[ 318.653046] CPU28 has been hot-added
[ 318.689386] CPU29 has been hot-added
[ 318.739736] CPU30 has been hot-added
[ 318.790743] CPU31 has been hot-added
# nproc
6
但是您可以通过运行以下命令在客户机中手动使 CPU 联机:
# for CPU in /sys/devices/system/cpu/cpu*/online; do echo 1 | sudo tee $CPU >/dev/null; done
健康:
# nproc
32
当客户机重新启动时,这种情况似乎仍然存在:
# dmesg | grep -E 'smp:|smpboot:'
[ 0.000000] smpboot: Allowing 32 CPUs, 0 hotplug CPUs
[ 0.189487] smpboot: CPU0: Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz (family: 0x6, model: 0x55, stepping: 0x4)
[ 0.191502] smp: Bringing up secondary CPUs ...
[ 0.278723] smp: Brought up 1 node, 32 CPUs
[ 0.278723] smpboot: Max logical packages: 1
[ 0.278723] smpboot: Total of 32 processors activated (153236.67 BogoMIPS)