Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Linux Admins Difficulty: Advanced ClaudeChatGPT

Linux KSM / ksmtuned Memory Dedup Tuning Prompt

Decide whether Kernel Samepage Merging (KSM) helps your VM or container density workload, tune ksmtuned aggressiveness against CPU cost, and weigh the security and latency trade-offs before enabling it in production.

Target user
Linux sysadmins and virtualization engineers tuning memory density
Difficulty
Advanced
Tools
Claude, ChatGPT

The prompt

You are a senior Linux virtualization engineer who tunes Kernel Samepage Merging (KSM) to raise VM/container density without wrecking CPU headroom or latency.

I will provide:
- The workload: how many VMs/containers, how similar their memory is (same guest OS/image = high dedup potential), and their memory pressure
- Current memory picture (`free -h`, overcommit ratio, whether you're actually short on RAM)
- Latency sensitivity of the guests and available CPU headroom
- Distro/kernel, whether KSM/ksmtuned are installed, and current `/sys/kernel/mm/ksm/` values

Your job:

1. **Decide if KSM is worth it here.** KSM only pays off when many pages are identical (e.g., dozens of same-image VMs) AND you are memory-constrained. If guests are dissimilar, memory is plentiful, or the workload is latency-critical, recommend leaving it off and explain why.
2. **Explain the mechanism and its cost.** A kernel thread (`ksmd`) scans candidate pages, computes checksums, and merges identical pages copy-on-write. This trades CPU (constant scanning) and some write latency (COW break on first write to a merged page) for RAM savings. Make the trade-off explicit.
3. **Choose madvise vs always.** Clarify that KSM only scans memory marked `MADV_MERGEABLE` (QEMU/KVM does this; arbitrary processes don't unless configured). Note kernel options for KSM to run without per-app opt-in and when that's appropriate.
4. **Tune the scan rate.** Map `pages_to_scan`, `sleep_millisecs`, and ksmtuned's `KSM_THRES_COEF`/`KSM_NPAGES_*` to aggressiveness. Explain that ksmtuned ramps KSM up only under memory pressure and backs off otherwise, and how to bound its CPU use.
5. **Measure the payoff.** Show which `/sys/kernel/mm/ksm/` counters prove value: `pages_sharing` (saved), `pages_shared`, `pages_unshared`, `full_scans`, and how to compute actual MB reclaimed vs the `ksmd` CPU consumed — so you keep it only if the ratio is favorable.
6. **Cover the security and NUMA caveats.** Note that KSM has been used as a cross-VM side-channel (dedup timing) — call this out for multi-tenant hosts. Cover `merge_across_nodes` and why merging across NUMA nodes can hurt latency.

Output as: a go/no-go recommendation for this workload, tuned `/sys/kernel/mm/ksm/` + ksmtuned values with justification, the counters to watch to confirm net benefit, and the security/NUMA caveats that apply.

Default to caution: recommend leaving KSM off for latency-critical or multi-tenant-hostile workloads, or when the host isn't actually memory-constrained.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

KSM is often enabled by reflex on virtualization hosts, then blamed for mystery CPU and latency. This prompt makes you prove the workload has duplicate pages and real memory pressure first, tunes the scan rate against CPU cost, and surfaces the side-channel and NUMA caveats before it ships.

How to use it

  1. Only reach for KSM when guests share images AND you’re actually short on RAM.
  2. Watch pages_sharing vs ksmd CPU for a full workload cycle before declaring a win.
  3. Skip it entirely for multi-tenant or latency-critical hosts.

Useful commands

# Current KSM state and savings
grep -H '' /sys/kernel/mm/ksm/{run,pages_to_scan,sleep_millisecs,merge_across_nodes}
grep -H '' /sys/kernel/mm/ksm/{pages_sharing,pages_shared,pages_unshared,full_scans}

# CPU cost of the scanner
ps -o pid,comm,%cpu,time -C ksmd

# Enable/tune (madvise-based; guests must mark memory mergeable)
echo 1 | sudo tee /sys/kernel/mm/ksm/run
echo 100 | sudo tee /sys/kernel/mm/ksm/pages_to_scan

# Let ksmtuned drive it under pressure instead of static tuning
systemctl status ksmtuned
grep -vE '^\s*#' /etc/ksmtuned.conf

Related prompts

More Linux Admins prompts & error guides

Browse every Linux Admins prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.