diff mbox series

[v5] mm/vmstat: add events for ksm cow

Message ID 20220331035616.2390805-1-yang.yang29@zte.com.cn (mailing list archive)
State New
Headers show
Series [v5] mm/vmstat: add events for ksm cow | expand

Commit Message

CGEL March 31, 2022, 3:56 a.m. UTC
From: Yang Yang <yang.yang29@zte.com.cn>

Users may use ksm by calling madvise(, , MADV_MERGEABLE) when they want
to save memory, it's a tradeoff by suffering delay on ksm cow. Users can
get to know how much memory ksm saved by reading
/sys/kernel/mm/ksm/pages_sharing, but they don't know what's the costs
of ksm cow, and this is important of some delay sensitive tasks.

So add ksm cow events to help users evaluate whether or how to use ksm.
Also update Documentation/admin-guide/mm/ksm.rst with new added events.

Signed-off-by: Yang Yang <yang.yang29@zte.com.cn>
Reviewed-by: David Hildenbrand <david@redhat.com>
Reviewed-by: xu xin <xu.xin16@zte.com.cn>
Reviewed-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
---
v2:
- fix compile error when CONFIG_KSM is not set
v3:
- delete KSM_COW_FAIL event
v4:
- modify /Documentation/admin-guide/mm/ksm.rst. And let cow_ksm before
- ksm_swpin_copy, so if new cow_* event could add before cow_ksm.
v5:
- fix compile error when CONFIG_SWAP is not set
---
 Documentation/admin-guide/mm/ksm.rst | 18 ++++++++++++++++++
 include/linux/vm_event_item.h        |  3 +++
 mm/memory.c                          |  4 ++++
 mm/vmstat.c                          |  3 +++
 4 files changed, 28 insertions(+)

Comments

Bagas Sanjaya March 31, 2022, 11:44 a.m. UTC | #1
On 31/03/22 10.56, cgel.zte@gmail.com wrote:
> +Monitoring KSM events
> +=====================
> +
> +There are some counters in /proc/vmstat that may be used to monitor KSM events.
> +KSM might help save memory, it's a tradeoff by may suffering delay on KSM COW
> +or on swapping in copy. Those events could help users evaluate whether or how
> +to use KSM. For example, if cow_ksm increases too fast, user may decrease the
> +range of madvise(, , MADV_MERGEABLE).
> +

Did you mean "tradeoff between possible delay on KSM COW and swapping
in copy"?
CGEL April 1, 2022, 1:35 a.m. UTC | #2
On Thu, Mar 31, 2022 at 06:44:31PM +0700, Bagas Sanjaya wrote:
> On 31/03/22 10.56, cgel.zte@gmail.com wrote:
> > +Monitoring KSM events
> > +=====================
> > +
> > +There are some counters in /proc/vmstat that may be used to monitor KSM events.
> > +KSM might help save memory, it's a tradeoff by may suffering delay on KSM COW
> > +or on swapping in copy. Those events could help users evaluate whether or how
> > +to use KSM. For example, if cow_ksm increases too fast, user may decrease the
> > +range of madvise(, , MADV_MERGEABLE).
> > +
> 
> Did you mean "tradeoff between possible delay on KSM COW and swapping
> in copy"?
>
I mean both are tradeoff, or in other words KSM sacrifices time to gain space.
KSM COW please see do_wp_page(), KSM swapping in copy see ksm_might_need_to_copy().

Thanks.
> -- 
> An old man doll... just what I always wanted! - Clara
diff mbox series

Patch

diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
index 97d816791aca..b244f0202a03 100644
--- a/Documentation/admin-guide/mm/ksm.rst
+++ b/Documentation/admin-guide/mm/ksm.rst
@@ -184,6 +184,24 @@  The maximum possible ``pages_sharing/pages_shared`` ratio is limited by the
 ``max_page_sharing`` tunable. To increase the ratio ``max_page_sharing`` must
 be increased accordingly.
 
+Monitoring KSM events
+=====================
+
+There are some counters in /proc/vmstat that may be used to monitor KSM events.
+KSM might help save memory, it's a tradeoff by may suffering delay on KSM COW
+or on swapping in copy. Those events could help users evaluate whether or how
+to use KSM. For example, if cow_ksm increases too fast, user may decrease the
+range of madvise(, , MADV_MERGEABLE).
+
+cow_ksm
+	is incremented every time a KSM page triggers copy on write (COW)
+	when users try to write to a KSM page, we have to make a copy.
+
+ksm_swpin_copy
+	is incremented every time a KSM page is copied when swapping in
+	note that KSM page might be copied when swapping in because do_swap_page()
+	cannot do all the locking needed to reconstitute a cross-anon_vma KSM page.
+
 --
 Izik Eidus,
 Hugh Dickins, 17 Nov 2009
diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
index 16a0a4fd000b..e83967e4c20e 100644
--- a/include/linux/vm_event_item.h
+++ b/include/linux/vm_event_item.h
@@ -133,6 +133,9 @@  enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
 		KSM_SWPIN_COPY,
 #endif
 #endif
+#ifdef CONFIG_KSM
+		COW_KSM,
+#endif
 #ifdef CONFIG_X86
 		DIRECT_MAP_LEVEL2_SPLIT,
 		DIRECT_MAP_LEVEL3_SPLIT,
diff --git a/mm/memory.c b/mm/memory.c
index 4111f97c91a0..12925ceaf745 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3339,6 +3339,10 @@  static vm_fault_t do_wp_page(struct vm_fault *vmf)
 	get_page(vmf->page);
 
 	pte_unmap_unlock(vmf->pte, vmf->ptl);
+#ifdef CONFIG_KSM
+	if (PageKsm(vmf->page))
+		count_vm_event(COW_KSM);
+#endif
 	return wp_page_copy(vmf);
 }
 
diff --git a/mm/vmstat.c b/mm/vmstat.c
index d5cc8d739fac..79015f144b15 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1392,6 +1392,9 @@  const char * const vmstat_text[] = {
 	"ksm_swpin_copy",
 #endif
 #endif
+#ifdef CONFIG_KSM
+	"cow_ksm",
+#endif
 #ifdef CONFIG_X86
 	"direct_map_level2_splits",
 	"direct_map_level3_splits",