Hello,
In pursuit of saving memory on Android, I started experimenting with Kernel Same Page Merging(KSM). Number of pages shared because of KSM is reported by /sys/kernel/mm/pages_sharing. Documentation/vm/ksm.txt explains this as:
"pages_sharing - how many more sites are sharing them i.e. how much saved"
After enabling KSM on Android device, this number was reported as 19666 pages. Obvious optimization is to find out source of sharing and see if we can avoid duplicate pages at first place. In order to collect the data needed, It needed few modifications(trace_printk) statement in mm/ksm.c. Data should be collected from second cycle because that's when ksm starts merging pages. First KSM cycle is only used to calculate the checksum, pages are added to unstable tree and eventually moved to stable tree after this.
After analyzing data from second KSM cycle, few things which stood out: 1. In the same cycle, KSM can scan same page multiple times. Scanning a page involves comparing page with pages in stable tree, if no match is found checksum is calculated. From the look of it, it seems to be cpu intensive operation and impacts dcache as well.
2. Same page which is already shared by multiple process can be replaced by KSM page. In this case, let say a particular page is mapped 24 times and is replaced by KSM page then eventually all 24 entries will point to KSM page. pages_sharing will account for all 24 pages. so pages _sharing does not actually report amount of memory saved.From the above example actual savings is one page.
Both cases happen very often with Android because of its architecture - Zygote spawning(fork) multipleapplications. To calculate actual savings, we should account for same page(pfn)replaced by same KSM page only once. In the case 2 example, page_sharing should account only one page.After recalculating memory saving comes out to be 8602 pages (~34MB).
I am trying to find out right solution to fix pages_sharing and eventually optimize KSM to scan pageonce even if it is mapped multiple times.
Comments? Has anyone tried this before?
Thanks, Pradeep
Hi, can you send this mail to linux mm and cc me? (I think it is better to discuss it there)
Thanks On 15/02/14 04:54, Pradeep Sawlani wrote:
Hello,
In pursuit of saving memory on Android, I started experimenting with Kernel Same Page Merging(KSM). Number of pages shared because of KSM is reported by /sys/kernel/mm/pages_sharing. Documentation/vm/ksm.txt explains this as:
"pages_sharing - how many more sites are sharing them i.e. how much saved"
After enabling KSM on Android device, this number was reported as 19666 pages. Obvious optimization is to find out source of sharing and see if we can avoid duplicate pages at first place. In order to collect the data needed, It needed few modifications(trace_printk) statement in mm/ksm.c. Data should be collected from second cycle because that's when ksm starts merging pages. First KSM cycle is only used to calculate the checksum, pages are added to unstable tree and eventually moved to stable tree after this.
After analyzing data from second KSM cycle, few things which stood out:
- In the same cycle, KSM can scan same page multiple times. Scanning
a page involves comparing page with pages in stable tree, if no match is found checksum is calculated. From the look of it, it seems to be cpu intensive operation and impacts dcache as well.
- Same page which is already shared by multiple process can be
replaced by KSM page. In this case, let say a particular page is mapped 24 times and is replaced by KSM page then eventually all 24 entries will point to KSM page. pages_sharing will account for all 24 pages. so pages _sharing does not actually report amount of memory saved.From the above example actual savings is one page.
Both cases happen very often with Android because of its architecture
- Zygote spawning(fork) multipleapplications. To calculate actual
savings, we should account for same page(pfn)replaced by same KSM page only once. In the case 2 example, page_sharing should account only one page.After recalculating memory saving comes out to be 8602 pages (~34MB).
I am trying to find out right solution to fix pages_sharing and eventually optimize KSM to scan pageonce even if it is mapped multiple times.
Comments? Has anyone tried this before?
Thanks, Pradeep
Thanks Izik for your reply.
I had posted the same on linux-mm yesterday but had your email address incorrect.
http://www.spinics.net/lists/linux-mm/msg69518.html
Thanks
On Fri, Feb 14, 2014 at 10:09 PM, Izik Eidus izik.eidus@ravellosystems.com wrote:
Hi, can you send this mail to linux mm and cc me? (I think it is better to discuss it there)
Thanks
On 15/02/14 04:54, Pradeep Sawlani wrote:
Hello,
In pursuit of saving memory on Android, I started experimenting with Kernel Same Page Merging(KSM). Number of pages shared because of KSM is reported by /sys/kernel/mm/pages_sharing. Documentation/vm/ksm.txt explains this as:
"pages_sharing - how many more sites are sharing them i.e. how much saved"
After enabling KSM on Android device, this number was reported as 19666 pages. Obvious optimization is to find out source of sharing and see if we can avoid duplicate pages at first place. In order to collect the data needed, It needed few modifications(trace_printk) statement in mm/ksm.c. Data should be collected from second cycle because that's when ksm starts merging pages. First KSM cycle is only used to calculate the checksum, pages are added to unstable tree and eventually moved to stable tree after this.
After analyzing data from second KSM cycle, few things which stood out:
- In the same cycle, KSM can scan same page multiple times. Scanning
a page involves comparing page with pages in stable tree, if no match is found checksum is calculated. From the look of it, it seems to be cpu intensive operation and impacts dcache as well.
- Same page which is already shared by multiple process can be
replaced by KSM page. In this case, let say a particular page is mapped 24 times and is replaced by KSM page then eventually all 24 entries will point to KSM page. pages_sharing will account for all 24 pages. so pages _sharing does not actually report amount of memory saved.From the above example actual savings is one page.
Both cases happen very often with Android because of its architecture
- Zygote spawning(fork) multipleapplications. To calculate actual
savings, we should account for same page(pfn)replaced by same KSM page only once. In the case 2 example, page_sharing should account only one page.After recalculating memory saving comes out to be 8602 pages (~34MB).
I am trying to find out right solution to fix pages_sharing and eventually optimize KSM to scan pageonce even if it is mapped multiple times.
Comments? Has anyone tried this before?
Thanks, Pradeep
linaro-mm-sig@lists.linaro.org