How to keep important VMs in memory without disabling swap

centos6kvm-virtualizationswapvirtual-machinesvirtual-memory

VM Host -> Xenon E5-2440 w/ 48G mem… Everything runs CentOS6.5 (2.6.32-431)

I have 4 guest VMs running each w/ 2G of memory. Their disk images are local files.
The host machine is also running a lightly used NFS server but not much else. (swappiness=0) After a day or so as the buffer cache grows to nearly 40G some of these VMs wind up nearly fully swapped to disk. (viewed via: grep VmSwap /proc/PID/status)

The problem I'm having is while these VMs may not be regularly used they have to be at the ready. However in practice they are being swapped out causing serious issues with their response time.

I'm certainly all for having my guest VMs use a reasonable sized swap file and let the OS decide the balance between buffer cache & swap but this doesn't seem to be working for the host machine in my use case.

Any option to keep the VMs responsiveness from being degraded besides disabling swap on the host machine? Attempt with cgroups or just pull the plug on the swap file for this use case?

Best Answer

You can lock the pages into memory in later versions of libvirt:-

http://libvirt.org/formatdomain.html#elementsMemoryBacking

Careful: This doesn't appear when using Fedora 19 as a hypervisor, nevertheless according to the changelog for the latest RPM (I can find) for EL6.5 libvirt this exists;

  • Thu Jul 18 2013 Jiri Denemark - 0.10.2-21
    • conf: Avoid NULL deref for pmsuspended domain state (rhbz#822306)
    • libvirt: Define domain crash event types (rhbz#822306)
    • qemu: Refactor processWatchdogEvent (rhbz#822306)
    • qemu: Expose qemuProcessShutdownOrReboot() (rhbz#822306)
    • qemu: Implement 'oncrash' events when guest panicked (rhbz#822306)
    • qemu: Implement 'oncrash' coredump events when guest panicked (rhbz#822306)
    • conf: Fix a memory leak when parsing nat port XML nodes (rhbz#851455)
    • security_manager: Fix comparison (rhbz#984793)
    • qemu: Prevent crash of libvirtd without guest agent configuration (rhbz#984821)
    • qemu: Fix double free of returned JSON array in qemuAgentGetVCPUs() (rhbz#984821)
    • qemu_agent: Add support for appending arrays to commands (rhbz#924400)
    • Add support for locking domain's memory pages (rhbz#947118)
    • qemu: Implement support for locking domain's memory pages (rhbz#947118)
    • qemu: Check for -realtime mlock=on|off support (rhbz#947118)
    • qemu: Move memory limit computation to a reusable function (rhbz#947118)
    • util: New virCommandSetMax(MemLock|Processes|Files) (rhbz#947118)
    • qemu: Set RLIMIT_MEMLOCK when memoryBacking/locked is used (rhbz#947118)
    • Add Gluster protocol as supported network disk backend (rhbz#849796)
    • qemu: Add support for gluster protocol based network storage backend. (rhbz#849796)
    • tests: Add tests for gluster protocol based network disks support (rhbz#849796)