mm: show proportional swap share of the mapping
Subsystems (5)
PATCH 1/1 mm: show proportional swap share of the mapping
For system uses swap heavily and has lots of shared anonymous page, it's very trouble to find swap set size per process because currently smaps doesn't report proportional set size of swap.
It ends up that sum of the number of swap for all processes is greater than swap device size.
This patch introduces SwapPss field on /proc/<pid>/smaps.
Bongkyu tested it. Result is below.
1. 50M used swap
SwapTotal: 461976 kB
SwapFree: 411192 kB
$ adb shell cat /proc/*/smaps | grep "SwapPss:" | \
awk '{sum += $2} END {print sum}';
48236
$ adb shell cat /proc/*/smaps | grep "Swap:" | \
awk '{sum += $2} END {print sum}';
1411842. 240M used swap
SwapTotal: 461976 kB
SwapFree: 216808 kB
$ adb shell cat /proc/*/smaps | grep "SwapPss:" | \
awk '{sum += $2} END {print sum}';
230315
$ adb shell cat /proc/*/smaps | grep "Swap:" | \
awk '{sum += $2} END {print sum}';
1387744--- Documentation/filesystems/proc.txt | 3 ++- fs/proc/task_mmu.c | 18 ++++++++++++++-- include/linux/swap.h | 1 + mm/swapfile.c | 42 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-)
Documentation/filesystems/proc.txt +2 -1
@@ -424,6 +424,7 @@ Private_Dirty: 0 kB Referenced: 892 kB Anonymous: 0 kB Swap: 0 kB +SwapPss: 0 kB KernelPageSize: 4 kB MMUPageSize: 4 kB Locked: 374 kB
@@ -441,7 +442,7 @@ indicates the amount of memory currently marked as referenced or accessed. a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE and a page is modified, the file page is replaced by a private anonymous copy. "Swap" shows how much would-be-anonymous memory is also used, but out on -swap. +swap. "SwapPss" shows process' proportional swap share of this mapping. "VmFlags" field deserves a separate description. This member represents the kernel flags associated with the particular virtual memory area in two letter encoded
fs/proc/task_mmu.c +16 -2
@@ -446,6 +446,7 @@ struct mem_size_stats {unsigned long anonymous_thp;unsigned long swap;u64 pss; +u64 swap_pss; }; static void smaps_account(struct mem_size_stats *mss, struct page *page,
@@ -492,9 +493,20 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr, }else if (is_swap_pte (*pte)) {swp_entry_t swpent =pte_to_swp_entry (*pte); -if (!non_swap_entry (swpent)) +if (!non_swap_entry (swpent)) { +int mapcount; + mss->swap +=PAGE_SIZE ; -else if (is_migration_entry (swpent)) + mapcount =swp_swapcount (swpent); +if (mapcount >=2 ) { +u64 pss_delta = (u64 )PAGE_SIZE <<PSS_SHIFT ; + +do_div (pss_delta, mapcount); + mss->swap_pss += pss_delta; + }else { + mss->swap_pss += (u64 )PAGE_SIZE <<PSS_SHIFT ; + } + }else if (is_migration_entry (swpent)) page =migration_entry_to_page (swpent); }
@@ -640,6 +652,7 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)"Anonymous: %8lu kB\n" "AnonHugePages: %8lu kB\n" "Swap: %8lu kB\n" +"SwapPss: %8lu kB\n" "KernelPageSize: %8lu kB\n" "MMUPageSize: %8lu kB\n" "Locked: %8lu kB\n",
@@ -654,6 +667,7 @@ static int show_smap(struct seq_file *m, void *v, int is_pid) mss.anonymous >>10 , mss.anonymous_thp >>10 , mss.swap >>10 , + (unsigned long )(mss.swap_pss >> (10 +PSS_SHIFT )),vma_kernel_pagesize (vma) >>10 ,vma_mmu_pagesize (vma) >>10 , (vma->vm_flags & VM_LOCKED) ?
include/linux/swap.h +1 -0
@@ -432,6 +432,7 @@ extern unsigned int count_swap_pages(int, int);extern sector_t map_swap_page (struct page *,struct block_device **);extern sector_t swapdev_block (int ,pgoff_t );extern int page_swapcount (struct page *); +extern int swp_swapcount (swp_entry_t entry);extern struct swap_info_struct *page_swap_info (struct page *);extern int reuse_swap_page (struct page *);extern int try_to_free_swap (struct page *);
mm/swapfile.c +42 -0
48 lines not on this page · raw message
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 8 lines · On Tue, 9 Jun 2015 23:43:51 +0900 Minchan Kim <[email protected]> wrote:
For system uses swap heavily and has lots of shared anonymous page, it's very trouble to find swap set size per process because currently smaps doesn't report proportional set size of swap.
It ends up that sum of the number of swap for all processes is greater than swap device size.This patch introduces SwapPss field on /proc/<pid>/smaps.
We should be told quite a bit more about the value of this change, please. Use cases, what problems it solves, etc. Enough to justify adding new code to the kernel, enough to justify adding yet another userspace interface which must be maintained for ever.
Quoted 6 lines
Documentation/filesystems/proc.txt +0
-0
Documentation/filesystems/proc.txt indicates the amount of memory currently marked as referenced or accessed. · line 448
Quoted 8 lines
Documentation/filesystems/proc.txt +1
-1@@ -441,7 +442,7 @@ indicates the amount of memory currently marked as referenced or accessed. a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE and a page is modified, the file page is replaced by a private anonymous copy. "Swap" shows how much would-be-anonymous memory is also used, but out on -swap. +swap. "SwapPss" shows process' proportional swap share of this mapping. "VmFlags" field deserves a separate description. This member represents the kernel flags associated with the particular virtual memory area in two letter encoded
Documentation/filesystems/proc.txt doesn't actually explain what "proportional share" means. A patient reader will hopefully find the comment over PSS_SHIFT in fs/proc/task_mmu.c, but that isn't very user-friendly.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hello Andrew,
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Tue, 9 Jun 2015 23:43:51 +0900 Minchan Kim <[email protected]> wrote:
For system uses swap heavily and has lots of shared anonymous page, it's very trouble to find swap set size per process because currently smaps doesn't report proportional set size of swap.
It ends up that sum of the number of swap for all processes is greater than swap device size.
Quoted 7 lines
Quoted 2 lines
This patch introduces SwapPss field on /proc/<pid>/smaps.
We should be told quite a bit more about the value of this change, please. Use cases, what problems it solves, etc. Enough to justify adding new code to the kernel, enough to justify adding yet another userspace interface which must be maintained for ever.
The goal is same with present pages's PSS.
We want to know per-process workingset size for smart memory management by userland platform memory manager. Our products use swap(ex, zram) heavily to maximize memory efficiency. IOW, workingset includes rss + swap.
However, without this feature, it's really hard to figure out how many each process consumes availabe memory(rss + zram-swap) if the system has lots of shared anonymous memory(e.g, android).
Quoted 13 lines
Quoted 13 lines
Documentation/filesystems/proc.txt +1
-1@@ -441,7 +442,7 @@ indicates the amount of memory currently marked as referenced or accessed. a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE and a page is modified, the file page is replaced by a private anonymous copy. "Swap" shows how much would-be-anonymous memory is also used, but out on -swap. +swap. "SwapPss" shows process' proportional swap share of this mapping.
Quoted 7 lines
Documentation/filesystems/proc.txt indicates the amount of memory currently marked as referenced or accessed. · line 448
Quoted 2 lines
Documentation/filesystems/proc.txt +0
-0"VmFlags" field deserves a separate description. This member represents the kernel flags associated with the particular virtual memory area in two letter encoded
Documentation/filesystems/proc.txtdoesn't actually explain what "proportional share" means. A patient reader will hopefully find the comment overPSS_SHIFTinfs/proc/task_mmu.c, but that isn't very user-friendly.
Okay, I will try to add more comment about PSS in proc.txt on next spin if you are not against this feature.
Thanks.
Quoted 2 lines
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hello,
On (06/09/15 23:43), Minchan Kim wrote:
[..]
Quoted 9 lines
fs/proc/task_mmu.c +1
-0@@ -446,6 +446,7 @@ struct mem_size_stats {unsigned long anonymous_thp;unsigned long swap;u64 pss; +u64 swap_pss; }; static void smaps_account(struct mem_size_stats *mss, struct page *page,
fs/proc/task_mmu.c static void smaps_pte_entry(pte_t *pte, unsigned long addr, · line 500
Quoted 10 lines
fs/proc/task_mmu.c +4
-2@@ -492,9 +493,20 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr, }else if (is_swap_pte (*pte)) {swp_entry_t swpent =pte_to_swp_entry (*pte); -if (!non_swap_entry (swpent)) +if (!non_swap_entry (swpent)) { +int mapcount; + mss->swap +=PAGE_SIZE ; -else if (is_migration_entry (swpent)) + mapcount =swp_swapcount (swpent);
I think this will break swapless builds (CONFIG_SWAP=n builds).
-ss
Quoted 83 lines
fs/proc/task_mmu.c +11
-0+if (mapcount >=2 ) { +u64 pss_delta = (u64 )PAGE_SIZE <<PSS_SHIFT ; + +do_div (pss_delta, mapcount); + mss->swap_pss += pss_delta; + }else { + mss->swap_pss += (u64 )PAGE_SIZE <<PSS_SHIFT ; + } + }else if (is_migration_entry (swpent)) page =migration_entry_to_page (swpent); }@@ -640,6 +652,7 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)"Anonymous: %8lu kB\n" "AnonHugePages: %8lu kB\n" "Swap: %8lu kB\n" +"SwapPss: %8lu kB\n" "KernelPageSize: %8lu kB\n" "MMUPageSize: %8lu kB\n" "Locked: %8lu kB\n",@@ -654,6 +667,7 @@ static int show_smap(struct seq_file *m, void *v, int is_pid) mss.anonymous >>10 , mss.anonymous_thp >>10 , mss.swap >>10 , + (unsigned long )(mss.swap_pss >> (10 +PSS_SHIFT )),vma_kernel_pagesize (vma) >>10 ,vma_mmu_pagesize (vma) >>10 , (vma->vm_flags & VM_LOCKED) ?include/linux/swap.h +1
-0@@ -432,6 +432,7 @@ extern unsigned int count_swap_pages(int, int);extern sector_t map_swap_page (struct page *,struct block_device **);extern sector_t swapdev_block (int ,pgoff_t );extern int page_swapcount (struct page *); +extern int swp_swapcount (swp_entry_t entry);extern struct swap_info_struct *page_swap_info (struct page *);extern int reuse_swap_page (struct page *);extern int try_to_free_swap (struct page *);mm/swapfile.c +35
-0@@ -875,6 +875,48 @@ int page_swapcount(struct page *page) }/* +* How many references to @entry are currently swapped out? +* This considers COUNT_CONTINUED so it returns exact answer. +*/ +int swp_swapcount (swp_entry_t entry) +{ +int count, tmp_count, n; +struct swap_info_struct *p; +struct page *page; +pgoff_t offset; +unsigned char *map; + + p =swap_info_get (entry); +if (!p) +return 0 ; + + count =swap_count (p->swap_map[swp_offset (entry)]); +if (!(count &COUNT_CONTINUED )) + goto out; + + count &= ~COUNT_CONTINUED ; + n =SWAP_MAP_MAX +1 ; + + offset =swp_offset (entry); + page =vmalloc_to_page (p->swap_map + offset); + offset &= ~PAGE_MASK ; +VM_BUG_ON (page_private (page) !=SWP_CONTINUED ); + + do { + page = list_entry(page->lru.next, struct page, lru); + map = kmap_atomic(page) + offset; + tmp_count = *map; + kunmap_atomic(map); + + count += (tmp_count & ~COUNT_CONTINUED) * n; + n *= (SWAP_CONT_MAX + 1);
mm/swapfile.c int page_swapcount(struct page *page) · line 922
Quoted 10 lines
mm/swapfile.c +7
-0+ }while (tmp_count &COUNT_CONTINUED ); +out: +spin_unlock (&p->lock); +return count; +} + +/* * We can write to an anon page without COW if there are no other references * to it. And as a side-effect, free up its swap: because the old content * on disk will never be read, and seeking back there to write new content
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 16 lines · Sergey Senozhatsky
Hello,
On (06/09/15 23:43), Minchan Kim wrote:
[..]Quoted 12 lines
fs/proc/task_mmu.c +1
-0@@ -446,6 +446,7 @@ struct mem_size_stats {unsigned long anonymous_thp;unsigned long swap;u64 pss; +u64 swap_pss; }; static void smaps_account(struct mem_size_stats *mss, struct page *page,@@ -492,9 +493,20 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr, }else if (is_swap_pte (*pte)) {swp_entry_t swpent =pte_to_swp_entry (*pte);
Quoted 9 lines
fs/proc/task_mmu.c static void smaps_pte_entry(pte_t *pte, unsigned long addr, · line 500
Quoted 7 lines
fs/proc/task_mmu.c +4
-2-if (!non_swap_entry (swpent)) +if (!non_swap_entry (swpent)) { +int mapcount; + mss->swap +=PAGE_SIZE ; -else if (is_migration_entry (swpent)) + mapcount =swp_swapcount (swpent);I think this will break swapless builds (
CONFIG_SWAP=nbuilds).
Thanks for the catching.
Will fix!
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>