xref: /cloud-hypervisor/docs/cpu.md (revision 6b0df31e5d4134c9a15b0fb22aedac5d4a5de58b)
1# CPU
2
3Cloud Hypervisor has many options when it comes to the creation of virtual
4CPUs. This document aims to explain what Cloud Hypervisor is capable of and
5how it can be used to meet the needs of very different use cases.
6
7## Options
8
9`CpusConfig` or what is known as `--cpus` from the CLI perspective is the way
10to set vCPUs options for Cloud Hypervisor.
11
12```rust
13struct CpusConfig {
14    boot_vcpus: u8,
15    max_vcpus: u8,
16    topology: Option<CpuTopology>,
17    kvm_hyperv: bool,
18    max_phys_bits: u8,
19    affinity: Option<Vec<CpuAffinity>>,
20    features: CpuFeatures,
21}
22```
23
24```
25--cpus boot=<boot_vcpus>,max=<max_vcpus>,topology=<threads_per_core>:<cores_per_die>:<dies_per_package>:<packages>,kvm_hyperv=on|off,max_phys_bits=<maximum_number_of_physical_bits>,affinity=<list_of_vcpus_with_their_associated_cpuset>,features=<list_of_features_to_enable>
26```
27
28### `boot`
29
30Number of vCPUs present at boot time.
31
32This option allows to define a specific number of vCPUs to be present at the
33time the VM is started. This option is mandatory when using the `--cpus`
34parameter. If `--cpus` is not specified, this option takes the default value
35of `1`, starting the VM with a single vCPU.
36
37Value is an unsigned integer of 8 bits.
38
39_Example_
40
41```
42--cpus boot=2
43```
44
45### `max`
46
47Maximum number of vCPUs.
48
49This option defines the maximum number of vCPUs that can be assigned to the VM.
50In particular, this option is used when looking for CPU hotplug as it lets the
51provide an indication about how many vCPUs might be needed later during the
52runtime of the VM.
53For instance, if booting the VM with 2 vCPUs and a maximum of 6 vCPUs, it means
54up to 4 vCPUs can be added later at runtime by resizing the VM.
55
56The value must be greater than or equal to the number of boot vCPUs.
57The value is an unsigned integer of 8 bits.
58
59By default this option takes the value of `boot`, meaning vCPU hotplug is not
60expected and can't be performed.
61
62_Example_
63
64```
65--cpus max=3
66```
67
68### `topology`
69
70Topology of the guest platform.
71
72This option gives the user a way to describe the exact topology that should be
73exposed to the guest. It can be useful to describe to the guest the same
74topology found on the host as it allows for proper usage of the resources and
75is a way to achieve better performances.
76
77The topology is described through the following structure:
78
79```rust
80struct CpuTopology {
81    threads_per_core: u8,
82    cores_per_die: u8,
83    dies_per_package: u8,
84    packages: u8,
85}
86```
87
88or the following syntax through the CLI:
89
90```
91topology=<threads_per_core>:<cores_per_die>:<dies_per_package>:<packages>
92```
93
94By default the topology will be `1:1:1:1`.
95
96_Example_
97
98```
99--cpus boot=2,topology=1:1:2:1
100```
101
102### `kvm_hyperv`
103
104Enable KVM Hyper-V emulation.
105
106When turned on, this option relies on KVM to emulate the synthetic interrupt
107controller (SynIC) along with synthetic timers expected by a Windows guest.
108A Windows guest usually runs on top of Microsoft Hyper-V, therefore expects
109these synthetic devices to be present. That's why KVM provides a way to emulate
110them and avoids failures running a Windows guest with Cloud Hypervisor.
111
112By default this option is turned off.
113
114_Example_
115
116```
117--cpus kvm_hyperv=on
118```
119
120### `max_phys_bits`
121
122Maximum size for guest's addressable space.
123
124This option defines the maximum number of physical bits for all vCPUs, which
125sets a limit for the size of the guest's addressable space. This is mainly
126useful for debug purpose.
127
128The value is an unsigned integer of 8 bits.
129
130_Example_
131
132```
133--cpus max_phys_bits=40
134```
135
136### `affinity`
137
138Affinity of each vCPU.
139
140This option gives the user a way to provide the host CPU set associated with
141each vCPU. It is useful for achieving CPU pinning, ensuring multiple VMs won't
142affect the performance of each other. It might also be used in the context of
143NUMA as it is way of making sure the VM can run on a specific host NUMA node.
144In general, this option is used to increase the performances of a VM depending
145on the host platform and the type of workload running in the guest.
146
147The affinity is described through the following structure:
148
149```rust
150struct CpuAffinity {
151    vcpu: u8,
152    host_cpus: Vec<u8>,
153}
154```
155
156or the following syntax through the CLI:
157
158```
159affinity=[<vcpu_id1>@[<host_cpu_id1>, <host_cpu_id2>], <vcpu_id2>@[<host_cpu_id3>, <host_cpu_id4>]]
160```
161
162The outer brackets define the list of vCPUs. And for each vCPU, the inner
163brackets attached to `@` define the list of host CPUs the vCPU is allowed to
164run onto.
165
166Multiple values can be provided to define each list. Each value is an unsigned
167integer of 8 bits.
168
169For instance, if one needs to run vCPU 0 on host CPUs from 0 to 4, the syntax
170using `-` will help define a contiguous range with `affinity=0@[0-4]`. The
171same example could also be described with `affinity=0@[0,1,2,3,4]`.
172
173A combination of both `-` and `,` separators is useful when one might need to
174describe a list containing host CPUs from 0 to 99 and the host CPU 255, as it
175could simply be described with `affinity=0@[0-99,255]`.
176
177As soon as one tries to describe a list of values, `[` and `]` must be used to
178demarcate the list.
179
180By default each vCPU runs on the entire host CPU set.
181
182_Example_
183
184```
185--cpus boot=3,affinity=[0@[2,3],1@[0,1]]
186```
187
188In this example, assuming the host has 4 CPUs, vCPU 0 will run exclusively on
189host CPUs 2 and 3, while vCPU 1 will run exclusively on host CPUs 0 and 1.
190Because nothing is defined for vCPU 2, it can run on any of the 4 host CPUs.
191
192### `features`
193
194Set of CPU features to enable.
195
196This option allows the user to enable a set of CPU features that are disabled
197by default otherwise.
198
199The currently available feature set is: `amx`.
200
201The `amx` feature will enable the x86 extension adding hardware units for
202matrix operations (int and float dot products). The goal of the extension is to
203provide performance enhancements for these common operations.
204
205_Example_
206
207```
208--cpus features=amx
209```
210
211In this example the amx CPU feature will be enabled for the VMM.
212