Dynamic Memory Management 动态内存管理

Introduction

Optimized and effective memory management is a key factor in virtualization environments. KSM and Auto-Ballooning enables sophisticated and economic configurations for physical RAM utilization.

优化和有效的内存管理是虚拟环境的关键因素,KSM和Auto-Ballooning为物理内存管理提供了复杂且实用的配置。

KSM

KSM (Kernel Samepage Merging) is running in the Linux kernel and scans the memory of all the virtual machines running on a single host, looking for duplication and consolidating. With KSM we’re able to improve virtual machine density by as much as 300% without impacting performance. One of the great benefits of using Linux as the hypervisor means KSM is not limited to KVM and virtual machines, but can also reduce memory pressure with normal Linux applications.

KSM (Kernel Samepage Merging)运行在Linux内核中,它扫描所有在单主机上运行的虚拟机的内存,寻找重复的部分并进行整合。通过KSM,我们可以在不影响性能的情况下将虚拟机的密度提高300%。使用Linux作为管理程序的一大好处是KSM不局限于KVM和虚拟机,还能减轻普通Linux应用程序的内存压力。

It was integrated in PVE since version 1.5, and is implemented with the package “ksm-control-daemon” (check your version with the cli command “pveversion -v”).

它自1.5版本开始被集成进PVE,且通过包"ksm-control-daemon"实现(通过cli命令"pveversion -v"检查你的版本)。

KSM in action

Just install several KVM virtual machines with the same OS (using at least 80% of your physical memory on the host) and wait a few minutes. You will notice higher CPU activities on the host (ksm daemon) and the used memory on the host will be lowered significantly (see start page showing the overall memory usage).

仅需安装若干同一系统(使用主机至少80%物理内存)的KVM虚拟机并等待几分钟。你可以注意到主机上更高的CPU活动(KSM进程)和主机的内存使用量显著地下降(看开始页面展示的内存使用总览)。

Howto verify that KSM is working (how many pages are being shared between your KVM guests):

如何验证KSM是否有效(在你的KVM客户机之间有多少页被共享):

watch cat /sys/kernel/mm/ksm/pages_sharing

Note: a page is 4096 bytes

注意:一页是4096字节

The file /etc/ksmtuned.conf allows for some customization of its behaviour.

文件/etc/ksmtuned.conf允许对它的行为进行定制。

See also

Disable KSM

If you don’t care about memory optimization but care about save CPU overhead produced by KSM, in Proxmox >= 4.x you can disable it with:

如果你不关心内存优化但在意节省KSM产生的CPU开销,在>=4.x的Proxmox版本中,你可以用以下命令禁用它:

systemctl disable ksmtuned

and reboot

Ballooning

Memory ballooning (KVM only) allows you to have your guest dynamically change it’s memory usage by evicting unused memory during run time. It reduces the impact your guest can have on memory usage of your host by giving up unused memory back to the host.

内存膨胀(仅KVM)允许你通过驱逐未使用的内存来动态地改变你的客户机的内存使用量。它通过将未使用的内存返还给主机,以减少你的客户机对主机内存使用的影响。

The Proxmox VE host can loan ballooned memory to a busy VM. The VM decides which processes or cache pages to swap out to free up memory for the balloon. The VM (Windows or Linux) knows best which memory regions it can give up without impacting performance of the VM.

PVE主机可以将膨胀的内存给一台繁忙的虚拟机。由虚拟机来决定为了膨胀释放内存而替换掉哪些进程或者页缓存。虚拟机(Windows或Linux)知道放弃哪些内存区域对虚拟机的性能影响是最小的。

Requirements for Windows VM

Installation

Download

Download the latest drivers (ISO) as suggested by the page Windows_VirtIO_Drivers to your desktop. Then upload the ISO to your Proxmox VE server:

  • logon to the Proxmox VE web interface
  • select a ISO-enabled storage (see Storage_Model#Storage_type_Content)
  • switch to “content” tab
  • just use the “upload” button on the menu bar.

Choose the right driver

Enable Auto-Ballooning on Windows Server 2012 / Windows 8.1 or newer

Starting with virtio-win-0.1.173-2 the driver ISO provides an installer located at “<cddrive>:\virtio-win-gt-x64.msi” that can, besides other things, install the ballooning service. If it does not work you can follow the manual way which is described below for Windows 2008r2.

Enable Auto-Ballooning on Windows 2008r2

  1. Set the VM memory to “Automatically allocate memory within this range” - I choose 4096/2048 as example (see screenshot)
  2. Start the VM and install all virtio drivers, including the balloon driver (see screenshot)
  3. Copy and rename as Administrator the WIN7\AMD64 directory from the virtio.iso to “c:/Program files/Balloon”
  4. Open a CMD as Administrator and cd into “c:/Program Files/Balloon”
  5. Install the BLNSVR with “BLNSVR.exe -i”

As soon as the service is started, also the memory information displayed on the Proxmox VE GUI is identical to the value shown in the windows task manager (see screenshot). If you need details about ballooning stats for this VM, go to the KVM monitor and enter ‘info balloon’ VirtIO drivers: KVM project wiki downloads

Enable Auto-Ballooning on Windows 2003 / Windows Xp

You need to install the balloon service with BLNSVR, like for win2008. You also need to install the driver manually Download the “devcon” software on microsoft website http://support.microsoft.com/kb/311272 Then install the balloon driver with

devcon install BALLOON.inf "PCI\VEN_1AF4&DEV_1002&SUBSYS_00051AF4&REV_00"

See also

Requirements for Linux VM

Modern Linux Kernels does include the Balloon drivers by default. It works out of the box, and you only need to set the VM to “Automatically allocate memory within this range”

现代Linux内核默认包含了Balloon驱动程序。它可以开箱即用,你只需要将虚拟机设置为"在此范围内自动分配内存"即可

tbd.