# Virtual Machine的资源分配

By [ku0023](https://paragraph.com/@ku0023) · 2022-09-29

---

`Virtual machine resources involve many aspects: CPU, memory, network, and disk. The relationship between these resources should be considered when planning virtual machines. Otherwise, the unreasonable allocation of resources will lead to poor performance of applications in virtual machines.`

`CPU`

`Each vCPU of the virtual machine only runs on one physical core. Therefore, the higher the CPU frequency, the higher the running speed of the virtual machine. The more vCPUs are, the better the performance of the application. If the virtual machine needs to consume a lot of CPU time, you can consider allocating a second vCPU to the virtual machine. However, allocating more than two vCPUs to the virtual machine does not necessarily make the application run faster, because only multithreaded applications can effectively use multiple vCPUs.`

`RAM`

`RAM resources are usually limited, so you need to be careful when allocating RAM to virtual machines. After the physical memory is completely used up, you must determine which virtual machines can retain the physical memory and which virtual machines will release the physical memory. This is called "memory reclamation". When the physical memory occupied by the virtual machine is recycled, there is a risk that the performance of the virtual machine will be affected. The more memory the virtual machine is recycled, the greater the risk.`

`It is wisest to allocate only the memory needed to complete the work for the virtual machine. Allocating additional memory increases the risk of recycling. On the other hand, when the virtual machine operating system uses unused memory as disk cache, it will significantly reduce the performance requirements on the disk system, so there is a tradeoff.`

`network bandwidth`

`Network bandwidth includes two aspects: one is the bandwidth between virtual machines and virtual switches, and the other is the bandwidth between virtual switches and external networks. For the connection with the external physical network, make sure that the host has the fastest physical network card. The transmission of virtual machines, virtual machines and data packets that perform a large number of network transfers will consume CPU time. Therefore, the virtual machine running on the server with limited CPU may face low network throughput because the CPU cannot respond quickly to requests.`

`Disk performance`

`Disk performance is often a silent performance killer. The performance of virtual machine disks is limited by the number and type of array disks and the number of virtual machines running on them. Because centralized sharing of the storage architecture will lead to access to all virtual machine disks through the same location, the storage controller and disk overload of the array are easy to occur, leaving only the virtual machine waiting for the storage response.`

`The virtual machine waits for disk IO, and the virtual machine CPU is idle. The virtual machine waiting for IO cannot do other work, so high I/O latency means that the performance will definitely decline. Careful storage design is essential to avoid the above.`

`Select disks reasonably to improve the performance of virtual machines. In virtualization, the hypervisor abstracts the workload from the underlying physical hardware, allowing rapid allocation and sharing of computing resources and workload migration. Although the hypervisor and virtualization compatible processors have little performance overhead, the presence of the virtualization layer has an impact on performance.`

`When disk performance is critical to the workload, some administrators may choose to configure LUNs in pass through mode, allowing the operating system of the virtual machine to bypass the hypervisor to communicate with the direct LUN. For example, the Windows server virtual machine may use the pass through mode to bypass Hyper-V and directly access the disk, which will slightly improve the performance of the SQL Server database. However, since the guest operating system (in pass through mode) and the hypervisor attempt to access the disk at the same time, the hypervisor must be configured to ignore pass through`

`LUN。`

`The problem with the pass through mode is that it is not supported by some important virtualization functions, such as virtual machine snapshots or clusters. Therefore, virtual machines may actually benefit from various functional features provided by virtualization rather than the marginal performance improvement brought by adopting the pass through mode. Administrators need to assess the requirements of virtual machines and determine the suitability of the pass through model.`

`In addition to pass through mode, Hyper-V and other hypervisors provide other disk storage options. For example, when When VHD files are created, all data blocks will be allocated to disks with fixed size. Once created, fixed size disks cannot be adjusted.`

`However, the dynamic expansion disk created a. VHD file without data blocks from the beginning. When the data is written to the. VHD file, the disk space will be allocated.`

`This is similar to thin provisioning. Although a disk is created logically, the actual disk space is only used when data is written.`

`Differential disk is a special type of dynamic expansion disk. The design idea is that the parent disk has a fixed image and the differential disk is associated with the parent disk, so the data written to the disk is stored in the differential disk rather than in the. VHD file.`

`The read request first checks the. VHD file of the differential disk. If there is no change, the parent will be read VHD files. When standardized disk mirroring is required and rollback is important, differential is a good idea, but administrators may face challenges in maintaining the parent-child disk configuration.`

---

*Originally published on [ku0023](https://paragraph.com/@ku0023/virtual-machine)*
