Ubuntu 20.04 KSM not working

I want to use KSM in Ubuntu 20.04 to achieve memory sharing between to qemu VMs. And I installed ksmtuned as follows:

$ sudo apt install ksmtuned

But I found KSM not working, and systemctl show as follows:

$ systemctl status ksm.service
● ksm.service - Kernel Samepage Merging
     Loaded: loaded (/lib/systemd/system/ksm.service; enabled; vendor preset: enabled)
     Active: active (exited) since Mon 2021-07-12 22:44:16 CST; 2min 39s ago
    Process: 661 ExecStart=/usr/sbin/ksmctl start (code=exited, status=0/SUCCESS)
   Main PID: 661 (code=exited, status=0/SUCCESS)

7月 12 22:44:16 qhx-u20 systemd[1]: Starting Kernel Samepage Merging...
7月 12 22:44:16 qhx-u20 systemd[1]: Finished Kernel Samepage Merging.
$ systemctl status ksmtuned.service
● ksmtuned.service - Kernel Samepage Merging (KSM) Tuning Daemon
     Loaded: loaded (/lib/systemd/system/ksmtuned.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-07-12 22:44:16 CST; 4min 54s ago
    Process: 701 ExecStart=/usr/sbin/ksmtuned (code=exited, status=0/SUCCESS)
   Main PID: 711 (ksmtuned)
      Tasks: 2 (limit: 14194)
     Memory: 2.9M
     CGroup: /system.slice/ksmtuned.service
             ├─ 711 /bin/bash /usr/sbin/ksmtuned
             └─2394 sleep 60

7月 12 22:44:16 qhx-u20 systemd[1]: Starting Kernel Samepage Merging (KSM) Tuning Daemon...
7月 12 22:44:16 qhx-u20 systemd[1]: Started Kernel Samepage Merging (KSM) Tuning Daemon.

And values in /sys/kernel/mm/ksm are:

$ grep . /sys/kernel/mm/ksm/*
/sys/kernel/mm/ksm/full_scans:0
/sys/kernel/mm/ksm/max_page_sharing:256
/sys/kernel/mm/ksm/merge_across_nodes:1
/sys/kernel/mm/ksm/pages_shared:0
/sys/kernel/mm/ksm/pages_sharing:0
/sys/kernel/mm/ksm/pages_to_scan:100
/sys/kernel/mm/ksm/pages_unshared:0
/sys/kernel/mm/ksm/pages_volatile:0
/sys/kernel/mm/ksm/run:0
/sys/kernel/mm/ksm/sleep_millisecs:200
/sys/kernel/mm/ksm/stable_node_chains:0
/sys/kernel/mm/ksm/stable_node_chains_prune_millisecs:2000
/sys/kernel/mm/ksm/stable_node_dups:0
/sys/kernel/mm/ksm/use_zero_pages:0

I've tried to use both ksmctl start and echo 1 > /sys/kernel/mm/ksm/run to start KSM but failed.

Can anyone tell me why or how to use KSM correctly in Ubuntu 20.04?


Solution 1:

Finally, I found the answer.

According to Redhat doc for KSM, KSM is managed by ksmtuned service, whose configuration file is /etc/ksmtuned.conf. You need to change the values in this file to activate the KSM, especifically KSM_THRES_COEF and KSM_THRES_CONST.