2014-07-29

KSM(Kernel Samepage Merging)

 

KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,added to the Linux kernel in 2.6.32.When KSM enabled, a kernel thread kswapd runs periodically to reclaim memory. Kswapd traverses each process and reclaims pages until free memory size > some threshold. After reclaiming 100 pages(It can be set by change /sys/kernel/mm/ksm/pages_to_scan value), kswaps yields the CPU and calls the scheduler. Reclaiming means kswapd 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).

As you can see, if KSM enabled(kswapd) will do periodic reclaiming, which is positive for reducing out_of_memory freq.

How to measure KSM

The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/:

pages_shared - how many shared pages are being used

pages_sharing - how many more sites are sharing them i.e. how much saved

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

A high ratio of pages_sharing to pages_shared indicates good sharing, but a high ratio of pages_unshared to pages_sharing indicates wasted effort.pages_volatile embraces several different kinds of activity, but a high proportion there would also indicate poor use of madvise MADV_MERGEABLE.

To measure the effectiveness of KSM, we made a utility(see attached ksm.txt) that can collect system KSM information(like pages_shared, pages_sharing, etc) and generate a csv file. Here's a example.

How to enable KSM on Android 4.1-4.3
------------------------------------------------------
1. Add CONFIG_KSM=y in kernel config. For example kernel/arch/arm/configs/xxx _defconfig.
Enabling KSM in kernel will introduce a new kernel thread,kswapd, to run periodically for reclaimming memory. Kswapd traverses each process and reclaims pages until free memory size > some threshold. After reclaiming pages_to_scan pages, kswapd yields the CPU and calls the scheduler, to let other processes run. Its parameters can be set by next setp.

2. Set KSM parameters in init.rc.

File: system/core/rootdir/init.rc
on post-fs
...
# Configure and enable KSM
write /sys/kernel/mm/ksm/pages_to_scan 100
write /sys/kernel/mm/ksm/sleep_millisecs 500
write /sys/kernel/mm/ksm/run 1
...

pages_to_scan - Number of pages ksmd should scan in one batch
sleep_millisecs - Milliseconds ksmd should sleep between batches
run - If KSM is running

3. To measure/debug KSM, I bring ksminfo utility to Android 4.1(No time to port procrank and librank yet).You can get that by applying ksminfo.patch.

4. To test KSM, google recommend looking at long running devices (several hours) and seeing whether KSM makes any noticeable improvement on launch times and rendering times. So we made a utility to show KSM infor on screen that can be enable/disable by develop menu. Please apply showksm.diff to get that.

5. After done, /sys/kernel/mm/ksm/run can present if KSM running
$root@aosp:/ # cat /sys/kernel/mm/ksm/run
1

No comments: