最後更新: 2022-11-28
介紹
KSM = Kernel Same-page Merging
KSM was originally developed for use with KVM (where it was known as Kernel Shared Memory),
to fit more virtual machines into physical memory, by sharing the data common between them.
But it can be useful to any application which generates many instances of the same data.
MADVISE_MERGEABLE
Page is marked for KSM with MADVISE_MERGEABLE
The KSM daemon ksmd periodically scans those areas of user memory which have been registered with it,
looking for pages of identical content which can be replaced by a single write-protected page
(which is automatically copied if a process later wants to update its content).
KSM only merges anonymous (private) pages, never pagecache (file) pages.
KSM only operates on those areas of address space which an application has advised to be likely candidates for merging,
by using the madvise(2) system call:
int madvise(addr, length, MADV_MERGEABLE)
* high pages_volatile => indicate poor use of madvise MADV_MERGEABLE
Centos 7
The ksm service starts and stops the KSM kernel thread.
The ksmtuned service controls and tunes the ksm service, dynamically managing same-page merging.
Info.
Linux page size = 4k
Config location = /sys/kernel/mm/ksm/
Settings
run
Whether the KSM process is running. (Default 0)
2 to stop ksmd and unmerge all pages currently merged
sleep_millisecs
Sleep milliseconds. (Default: 20)
pages_to_scan
The amount of pages ksmd will evaluate on each run. (100)
Statistic
pages_shared - 一共有幾多 page 參與 share
pages_sharing - 相當於 savings 多小 page
pages_unshared - how many pages unique but repeatedly checked for merging
pages_volatile - how many pages changing too fast to be placed in a tree
full_scans - how many times all mergeable areas have been scanned
Example
Process A P1 P2 P3 P4 P5 Process B P1 P2 P3 P4 X5 Process C P1 P2 P3 X4 P5
- pages_shared = 13
- pages_sharing = B(P1, P2, P3) + C(P1, P2, P3) + B(P4) + C(P5) = 8
- pages_unshared = X4, X5
Summary
- A high ratio of pages_sharing/pages_shared indicates good sharing
- A high ratio of pages_unshared/pages_sharing indicates wasted effort
MyConfig
/etc/rc.d/rc.local
echo 1 > /sys/kernel/mm/ksm/run echo 500 > /sys/kernel/mm/ksm/sleep_millisecs
Kernel Same Page Merging Effectiveness
netdata 內的 mem.ksm_ratios 指標
KSM savings
Savings indicates saved memory.
Offered indicates memory marked as mergeable.
KSM effectiveness
percentage of the mergeable pages that are currently merged.
offered = pages_sharing + pages_shared + pages_unshared + pages_volatile;
saved = pages_sharing - pages_shared;
savings (ksm_ratios) = saved / offered;