Is there an optimal setting for the number of CPU cores exposed to a VM?

23

4

VirtualBox, and possibly other VM software, allows the user to specify the number of CPUs exposed to the VM. In my particular case, I have a dual core CPU, with Windows 7 64-bit running natively, and Ubuntu 9.04 64-bit as the VM.

The help file suggests not setting this above the number of physical cores available. This makes sense. So, is it best allocating 1 or 2 CPU cores? Does this depend entirely on the native CPU load?

sblair

Posted 2009-08-12T20:30:53.763

Reputation: 12 231

Answers

6

It entirely depends on what the guest OS is doing. In my personal experience a VM is running a single program (build server, trac server, SVN, etc) so there is no reason to set it above one.

A possible case may be if it's an end user on say a University that is using the guest OS as a throwaway (i.e. TAs) then maybe set it to higher than one.

Nate

Posted 2009-08-12T20:30:53.763

Reputation: 434

So, in other words: for a temporary system, you may as well max out the CPU core quota; but for "normal" or typical use, set it to one core (so not to interfere too much with the performance of the host OS)? – sblair – 2009-08-12T21:04:00.187

18

The information in this answer is no longer correct. It was correct back in the days of strict co-scheduling, but is no longer the case.

I am keeping the answer below for prosperity sake, but please do not rely on it to be accurate for modern hypervisors.


You should never assign all your cores as vCPUs in a virtual machine.

If a VM requests any CPU time, VMWare has to allocate either all the vCPUs, or none of the vCPUs.

What this means is, on a 4-core system if you allocate all 4 cores, then only either the host, or the guest, can be allocated processor time, not both at the same time.

This will absolutally ruin performance in both environments.

Feel free to allocate an odd-number of cores (say, 3). It feels odd, but it's a totally legitimate option.

Generally I never allocate more than (n/2) vCPUs (in your case, 2) - but I'm normally allocating cores on servers with between 12 and 32 cores where you can allocate huge numbers of vCPUs without major impact.

Mark Henderson

Posted 2009-08-12T20:30:53.763

Reputation: 5 956

1Just wanted to drop a note here that on both Ubuntu 14.04 and 16.04, there are boot issues as well as crashes if you set the number of CPU's to an odd number. I haven't been able to figure out why because I simply decided to keep it at even numbers to avoid the issues. – Dorian – 2016-09-26T17:10:34.960

this is the only answer thus far written with any degree of authority. thanks for sharing your experiences. – G-Wiz – 2012-04-12T15:57:36.010

5-1: Not consistent with my experience. The VM is a process whose CPU time can be scheduled in the same manner as any other process on the host. You can adjust the priority of the VM process(es) on the host, and the system will balance the loads appropriately. – bwDraco – 2013-05-01T02:10:47.640

2(continued) Let's say you have a 16-core server. An example configuration would be 4 VMs, each configured for 8 cores. While this may seem to be too many virtual cores, it allows individual VMs to scale better on heavier workloads. Should the combined utilization exceed the number of physical cores, the host will balance the loads accordingly. That's how virtualization can increase throughput and reduce costs--by making the most of the host hardware. – bwDraco – 2013-05-01T02:12:00.520

@DragonLord - this answer is over 2 years old on a question that's almost 4 years old, and might be based on outdated information. I am referring to "Strict Co-Scheduling" from ESX but I do not recall how old that information is, and I haven't kept up to date with how CPU scheduling has changed in the last few years. – Mark Henderson – 2013-05-01T02:58:54.773

@MarkHenderson VMware ESX stopped using strict co-scheduling since ESX 3 (2006?). It uses relaxed (or dynamic) co-scheduling, as does Hyper-V. – projectshave – 2014-02-03T17:51:34.277

3

As Nate mentioned, as long as your VM will mostly serve running a single-threaded process, there is little reason to allocate more than one core.

However, if you plan to use it in a way that can benefit from more cores (multi-threaded program, many parallel programs, ..), the story is more complicated. I previously assumed that using half of your CPU's for your VM was a good idea. Turns out this is not always optimal.

The only way to make sure is to run benchmarks.. I don't know why, but in my Core2 Quad system (which has VT-x support, nested paging, ..), actually using 1 out of the 4 cores performs better than using 2 or more cores! I see this simply in start-up times of the VM, but also during compilation benchmarks.

With other hardware results are different. I also have an i7 with 8 cores, and performance seems to scale much better with the number of cores there (using 4 there happily).

My tip: benchmark a new hardware first.

Arnout

Posted 2009-08-12T20:30:53.763

Reputation: 301

3

The number of cores exposed to guest CPUs does not affect performance as much as you would like. It merely adjusts the amount of cores the guest CPU "sees".

Depending on how the emulator is set up, it could literally half the performance of the virtual machine, do nothing, or only impact the way the guest OS/applications split up their thread scheduling.

It's best to leave it to the default. If you encounter performance issues on the host PC, then you can simply adjust the process priority with the Windows Task Manager.

Breakthrough

Posted 2009-08-12T20:30:53.763

Reputation: 32 927

1

I'd trust the OS's scheduler and just set the VM to the # of physical cores, unless you Know that you always want the host to have a potentially idle core while you're busy in the VM.

Andrew Coleson

Posted 2009-08-12T20:30:53.763

Reputation: 1 835

1

This depends on what you want to do on the virtual machine as well as on the physical machine. If you need to use the virtual machine while the host operating system is being used for some other activity, it may be better to use two processor cores for the VM so that the host has two remaining if the VM is under full load. On the other hand, if you are working primarily on the VM, you can use all processor cores.

However, unless the guest can be expected to use all four cores at once, you can safely use all of them while continuing to work on the host OS. Remember that you can always adjust the priority of the VM process while it's running on the host operating system, and the system will balance the loads accordingly.

bwDraco

Posted 2009-08-12T20:30:53.763

Reputation: 41 701

This is based on my experience with VirtualBox. As I am not familiar with VMware products, I may be wrong; if so, I'll delete this answer. – bwDraco – 2011-04-21T13:20:28.057

0

yes, I'd also say that it depends. But I'll say what I prefer to do. At work I have a quad core CPU. So for my virtual image (win7), I've allocated two CPUs. That way, I can use both the host machine and the virtual machine at the same time, and they each have equal CPU resources.

Joel Martinez

Posted 2009-08-12T20:30:53.763

Reputation: 1 227