ksmtuned
About
All machines idle? Trade in some CPU and SAVE A WHOLE BUNCH OF MEM!
Installation
Install ksmtuned without treating recommendations as dependencies
1 aptitude install --without-recommends ksmtuned
Configure
Information can be acquired below
/sys/kernel/mm/ksm
Run ksm earlier by lowering KSM_THRES_COEF in
/etc/ksmtuned.conf
1 # Configuration file for ksmtuned.
2
3 # How long ksmtuned should sleep between tuning adjustments
4 # KSM_MONITOR_INTERVAL=60
5
6 # Millisecond sleep between ksm scans for 16Gb server.
7 # Smaller servers sleep more, bigger sleep less.
8 # KSM_SLEEP_MSEC=10
9
10 # KSM_NPAGES_BOOST=300
11 # KSM_NPAGES_DECAY=-50
12 # KSM_NPAGES_MIN=64
13 # KSM_NPAGES_MAX=1250
14
15 #KSM_THRES_COEF=20
16 KSM_THRES_COEF=50
17 # KSM_THRES_CONST=2048
18
19 # uncomment the following if you want ksmtuned debug info
20
21 # LOGFILE=/var/log/ksmtuned
22 # DEBUG=1
The shell script only sources the configuration when starting up, so the systemd unit must be restarted on changes of the configuration.
1 systemctl restart ksmtuned.service
Monitoring and adjustment
To watch ksm in action use the following little commands.
Example - Debian Buster monoculture in production:
1 Every 1,0s: grep -rH "" /sys/kernel/mm/ksm*;echo "Saved mem: $(($(cat /sys/kernel/mm/ksm/pages_sharing)*4/(2^1024))) MiB" kvm2: Fri Jan 31 14:03:57 2020
2
3 /sys/kernel/mm/ksm/stable_node_dups:5938
4 /sys/kernel/mm/ksm/max_page_sharing:256
5 /sys/kernel/mm/ksm/pages_volatile:271474
6 /sys/kernel/mm/ksm/stable_node_chains_prune_millisecs:2000
7 /sys/kernel/mm/ksm/merge_across_nodes:1
8 /sys/kernel/mm/ksm/pages_unshared:5277423
9 /sys/kernel/mm/ksm/stable_node_chains:497
10 /sys/kernel/mm/ksm/pages_shared:1049263
11 /sys/kernel/mm/ksm/use_zero_pages:0
12 /sys/kernel/mm/ksm/pages_to_scan:1250
13 /sys/kernel/mm/ksm/sleep_millisecs:10
14 /sys/kernel/mm/ksm/run:1
15 /sys/kernel/mm/ksm/full_scans:153
16 /sys/kernel/mm/ksm/pages_sharing:5635769
17 Saved mem: 21971 MiB
Well, ~22GiB of 64GiB saved! WTF?!1!! I dare to claim, this is a must have!
Assumptions
I assume KSM doesn't search already swapped pages to save expensive disk io. Maybe it's a good idea to gain some memory in a first run and to move content of the swap space back to the main memory in a second step by umounting and mounting swap-space.
1 swapoff -a; swapon -a
In addition it may be a good idea to reduce vm.swappiness to once more profit from KSM.