Operations 20 min read

Master Linux VM Performance: Tuning KVM, CPU, Memory, and Disk Settings

This guide walks through essential Linux VM performance tuning techniques, covering tuned profiles for virtual guests and hosts, kernel parameters like vm.dirty_ratio and vm.swappiness, NUMA-aware resource monitoring, CPU pinning, memory limits, KSM configuration, and disk optimization with qcow2 images and I/O throttling.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux VM Performance: Tuning KVM, CPU, Memory, and Disk Settings

Introduction

This article provides notes for exam preparation on common Linux VM management operations and tuning configurations.

Using Tools for Tuning

The tuned package offers two virtualization profiles: virtual-guest for optimizing a virtual machine and virtual-host for optimizing the host running KVM guests. virtual-guest: optimization for a VM client. virtual-host: optimization for the host running KVM guests.

Both profiles inherit the throughput-performance profile, which maximizes disk and network I/O throughput.

- virtual-guest   - Optimize for running inside a virtual guest
- virtual-host   - Optimize for running KVM guests

The virtual-guest configuration ( /usr/lib/tuned/virtual-guest/tuned.conf) sets the following kernel parameters:

vm.dirty_ratio = 30
vm.swappiness = 30
vm.dirty_ratio

defines the percentage of dirty anonymous pages at which the kernel starts writeback. Raising this value delays writeback, improving I/O performance. vm.swappiness controls the tendency to swap memory pages. Lowering it (to 30) makes the system prefer file system I/O over swapping, which is safe on servers with high‑performance storage.

The virtual-host profile ( /usr/lib/tuned/virtual-host/tuned.conf) adds:

vm.dirty_background_ratio = 5
vm.dirty_background_ratio

sets the threshold for starting background writeback threads, allowing earlier writeback and better I/O performance under light load.

It also configures CPU C‑state power savings: force_latency=cstate.id_no_zero:3|70 This forces idle CPUs into C3 deep‑sleep state to reduce power consumption.

Resource Usage Information

Install numactl to manage NUMA (Non‑Uniform Memory Access) systems. yum -y install numactl NUMA divides memory into nodes, each associated with a set of CPU cores. Use numastat -c qemu-kvm to view per‑node memory usage of QEMU/KVM processes.

Per-node process memory usage (in MBs)
PID          Node 0 Total
---------------  ------  -----
1755 (qemu-kvm)   855   855
... (additional rows omitted) ...
Total               6048  6048

Setting VM CPU Pinning

Use virsh vcpupin to view and set VCPU affinity:

virsh vcpupin workstation
VCPU: CPU Affinity
----------------------------------
  0: 0-7
  1: 0-7

Pin the first VCPU of serverc to CPU 0 only:

virsh vcpupin serverc 0 0
virsh vcpupin serverc
VCPU: CPU Affinity
----------------------------------
  0: 0
  1: 0-7

Limiting VM CPU Resources

Adjust CPU shares with virsh schedinfo. A VM with cpu_shares=2048 receives more CPU time than one with cpu_shares=1024.

virsh schedinfo workstation
cpu_shares : 1024
... (other fields omitted) ...
virsh schedinfo workstation cpu_shares=2048
cpu_shares : 2048

Setting VM Memory Limits

Set maximum memory (requires the VM to be shut down): virsh setmaxmem workstation --size 2097152 Set current memory while the VM is running: virsh setmem workstation --size 2097152 View and adjust memory limits with virsh memtune:

virsh memtune workstation
hard_limit : unlimited
soft_limit : unlimited
...
virsh memtune workstation --hard-limit 2G --soft-limit 1G
virsh memtune workstation
hard_limit : 2097152
soft_limit : 1048576

KSM Parameter Configuration

KSM (Kernel Samepage Merging) merges identical memory pages across VMs. The ksmtuned daemon controls scan aggressiveness.

systemctl status ksm.service
systemctl status ksmtuned.service

Adjust KSM parameters via /sys/kernel/mm/ksm/ files, e.g., enable scanning: echo 1 > /sys/kernel/mm/ksm/run Key parameters: run: 1 enables scanning, 0 disables. pages_to_scan: number of pages scanned per cycle (default 100). sleep_millisecs: sleep time between cycles (default 20 ms). pages_shared, pages_sharing, full_scans, merge_across_nodes etc.

View and modify these values with virsh node-memory-tune or by editing the files directly.

virsh node-memory-tune
Shared memory:
  shm_pages_to_scan 100
  shm_sleep_millisecs 30
  ... (other fields omitted) ...

Virtual Disk Configuration Tuning

VM disks can be block devices or image files. Using block devices offers better performance, while image files (qcow2) provide features such as sparse allocation, copy‑on‑write, snapshots, compression, and encryption.

Creating a qcow2 Image

qemu-img create -f qcow2 -o preallocation=metadata /var/lib/libvirt/images/disk.qcow2 1G

Preallocation options: off: no space pre‑allocated (default). metadata: allocate metadata only, fast creation, slower writes. falloc: allocate metadata and data blocks as unallocated, slower creation, faster writes. full: allocate all space upfront, similar performance to falloc.

Creating a Snapshot (ROW)

qemu-img create -f qcow2 -b /var/lib/libvirt/images/disk.qcow2 disk-snap.qcow2 30G

VM Disk Cache Modes

cache=none

: no cache, data written directly, supports live migration. cache=writethrough: writes go to cache and disk, ensures integrity, slower. cache=writeback: writes cached first, then flushed to disk. cache=directsync: similar to writethrough, bypasses cache when needed. cache=unsafe: like writeback but ignores all flushes; higher performance but unsafe for production.

cat *.xml | grep driver
<driver name="qemu" type="qcow2" cache='none'/>

Virtual Disk I/O Tuning

Limit I/O for a VM’s disk device using virsh blkdeviotune:

virsh blkdeviotune workstation vda --total-iops-sec 1000 --total-bytes-sec 10MB

Verify the settings in the domain XML:

<iotune>
  <total_bytes_sec>10000000</total_bytes_sec>
  <total_iops_sec>1000</total_iops_sec>
</iotune>

Virtualization Performance Monitoring

Device I/O statistics:

virsh domblkstat workstation vda --human
Device: vda
  number of read operations:      15012
  number of bytes read:            437122048
  number of write operations:     3193
  number of bytes written:        71808512
  ... (other fields omitted) ...

Network interface list and statistics:

virsh domiflist workstation
Interface  Type   Source   Model   MAC
vnet4      bridge privbr0  virtio  52:54:00:00:fa:09

virsh domifstat workstation vnet4
vnet4 rx_bytes 600038
vnet4 rx_packets 10176
... (other fields omitted) ...

Memory statistics (values in KB):

virsh dommemstat workstation
actual 2097152
swap_in 0
swap_out 0
major_fault 304
minor_fault 147927
unused 1669348
available 1873292
usable 1631560
rss 1246776

References

Content references are credited to their original authors. © 2018‑2024 [email protected]. All rights reserved. CC BY‑NC‑SA 4.0.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxKVM
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.