Skip to content

Commit 8b47933

Browse files
Stefan Roeschakpm00
authored andcommitted
proc/ksm: add ksm stats to /proc/pid/smaps
With madvise and prctl KSM can be enabled for different VMA's. Once it is enabled we can query how effective KSM is overall. However we cannot easily query if an individual VMA benefits from KSM. This commit adds a KSM section to the /prod/<pid>/smaps file. It reports how many of the pages are KSM pages. Note that KSM-placed zeropages are not included, only actual KSM pages. Here is a typical output: 7f420a000000-7f421a000000 rw-p 00000000 00:00 0 Size: 262144 kB KernelPageSize: 4 kB MMUPageSize: 4 kB Rss: 51212 kB Pss: 8276 kB Shared_Clean: 172 kB Shared_Dirty: 42996 kB Private_Clean: 196 kB Private_Dirty: 7848 kB Referenced: 15388 kB Anonymous: 51212 kB KSM: 41376 kB LazyFree: 0 kB AnonHugePages: 0 kB ShmemPmdMapped: 0 kB FilePmdMapped: 0 kB Shared_Hugetlb: 0 kB Private_Hugetlb: 0 kB Swap: 202016 kB SwapPss: 3882 kB Locked: 0 kB THPeligible: 0 ProtectionKey: 0 ksm_state: 0 ksm_skip_base: 0 ksm_skip_count: 0 VmFlags: rd wr mr mw me nr mg anon This information also helps with the following workflow: - First enable KSM for all the VMA's of a process with prctl. - Then analyze with the above smaps report which VMA's benefit the most - Change the application (if possible) to add the corresponding madvise calls for the VMA's that benefit the most [[email protected]: v5] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Stefan Roesch <[email protected]> Reviewed-by: David Hildenbrand <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Rik van Riel <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 6885938 commit 8b47933

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Documentation/filesystems/proc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ Memory Area, or VMA) there is a series of lines such as the following::
461461
Private_Dirty: 0 kB
462462
Referenced: 892 kB
463463
Anonymous: 0 kB
464+
KSM: 0 kB
464465
LazyFree: 0 kB
465466
AnonHugePages: 0 kB
466467
ShmemPmdMapped: 0 kB
@@ -501,6 +502,9 @@ accessed.
501502
a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE
502503
and a page is modified, the file page is replaced by a private anonymous copy.
503504

505+
"KSM" reports how many of the pages are KSM pages. Note that KSM-placed zeropages
506+
are not included, only actual KSM pages.
507+
504508
"LazyFree" shows the amount of memory which is marked by madvise(MADV_FREE).
505509
The memory isn't freed immediately with madvise(). It's freed in memory
506510
pressure if the memory is clean. Please note that the printed value might

fs/proc/task_mmu.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/hugetlb.h>
55
#include <linux/huge_mm.h>
66
#include <linux/mount.h>
7+
#include <linux/ksm.h>
78
#include <linux/seq_file.h>
89
#include <linux/highmem.h>
910
#include <linux/ptrace.h>
@@ -396,6 +397,7 @@ struct mem_size_stats {
396397
unsigned long swap;
397398
unsigned long shared_hugetlb;
398399
unsigned long private_hugetlb;
400+
unsigned long ksm;
399401
u64 pss;
400402
u64 pss_anon;
401403
u64 pss_file;
@@ -452,6 +454,9 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page,
452454
mss->lazyfree += size;
453455
}
454456

457+
if (PageKsm(page))
458+
mss->ksm += size;
459+
455460
mss->resident += size;
456461
/* Accumulate the size in pages that have been accessed. */
457462
if (young || page_is_young(page) || PageReferenced(page))
@@ -822,6 +827,7 @@ static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss,
822827
SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty);
823828
SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced);
824829
SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous);
830+
SEQ_PUT_DEC(" kB\nKSM: ", mss->ksm);
825831
SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree);
826832
SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp);
827833
SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);

0 commit comments

Comments
 (0)