diff mbox series

[v9,2/3] mm: add new KSM process and sysfs knobs

Message ID 20230418051342.1919757-3-shr@devkernel.io (mailing list archive)
State New
Headers show
Series mm: process/cgroup ksm support | expand

Commit Message

Stefan Roesch April 18, 2023, 5:13 a.m. UTC
This adds the general_profit KSM sysfs knob and the process profit metric
knobs to ksm_stat.

1) expose general_profit metric

   The documentation mentions a general profit metric, however this
   metric is not calculated.  In addition the formula depends on the size
   of internal structures, which makes it more difficult for an
   administrator to make the calculation.  Adding the metric for a better
   user experience.

2) document general_profit sysfs knob

3) calculate ksm process profit metric

   The ksm documentation mentions the process profit metric and how to
   calculate it.  This adds the calculation of the metric.

4) mm: expose ksm process profit metric in ksm_stat

   This exposes the ksm process profit metric in /proc/<pid>/ksm_stat.
   The documentation mentions the formula for the ksm process profit
   metric, however it does not calculate it.  In addition the formula
   depends on the size of internal structures.  So it makes sense to
   expose it.

5) document new procfs ksm knobs

Signed-off-by: Stefan Roesch <shr@devkernel.io>
Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
Acked-by: David Hildenbrand <david@redhat.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Rik van Riel <riel@surriel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---
 Documentation/ABI/testing/sysfs-kernel-mm-ksm |  8 +++++++
 Documentation/admin-guide/mm/ksm.rst          |  5 ++++-
 fs/proc/base.c                                |  3 +++
 include/linux/ksm.h                           |  4 ++++
 mm/ksm.c                                      | 21 +++++++++++++++++++
 5 files changed, 40 insertions(+), 1 deletion(-)

Comments

xu xin April 28, 2023, 9:36 a.m. UTC | #1
> This adds the general_profit KSM sysfs knob and the process profit metric
> knobs to ksm_stat.
> 
> 1) expose general_profit metric
> 
>    The documentation mentions a general profit metric, however this
>    metric is not calculated.  In addition the formula depends on the size
>    of internal structures, which makes it more difficult for an
>    administrator to make the calculation.  Adding the metric for a better
>    user experience.
> 
> 2) document general_profit sysfs knob
> 
> 3) calculate ksm process profit metric
> 
>    The ksm documentation mentions the process profit metric and how to
>    calculate it.  This adds the calculation of the metric.
> 
> 4) mm: expose ksm process profit metric in ksm_stat
> 
>    This exposes the ksm process profit metric in /proc/<pid>/ksm_stat.
>    The documentation mentions the formula for the ksm process profit
>    metric, however it does not calculate it.  In addition the formula
>    depends on the size of internal structures.  So it makes sense to
>    expose it.
>

Hi, Stefan, I think you should give some credits to me about my contributions on
the concept and formula of ksm profit (process wide and system wide),  it's kind
of idea stealing.


Besides, the idea of Process control KSM was proposed by me last year although you use
prctl instead of /proc fs. you even didn't CC my email. I think you should CC my email
(xu.xin16@zte.com.cn) as least.
 
> 5) document new procfs ksm knobs
> 
> Signed-off-by: Stefan Roesch <shr@devkernel.io>
> Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
> Acked-by: David Hildenbrand <david@redhat.com>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Michal Hocko <mhocko@suse.com>
> Cc: Rik van Riel <riel@surriel.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> ---
>  Documentation/ABI/testing/sysfs-kernel-mm-ksm |  8 +++++++
>  Documentation/admin-guide/mm/ksm.rst          |  5 ++++-
>  fs/proc/base.c                                |  3 +++
>  include/linux/ksm.h                           |  4 ++++
>  mm/ksm.c                                      | 21 +++++++++++++++++++
>  5 files changed, 40 insertions(+), 1 deletion(-)
> 
> diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-ksm b/Documentation/ABI/testing/sysfs-kernel-mm-ksm
> index d244674a9480..6041a025b65a 100644
> --- a/Documentation/ABI/testing/sysfs-kernel-mm-ksm
> +++ b/Documentation/ABI/testing/sysfs-kernel-mm-ksm
> @@ -51,3 +51,11 @@ Description:	Control merging pages across different NUMA nodes.
>  
>  		When it is set to 0 only pages from the same node are merged,
>  		otherwise pages from all nodes can be merged together (default).
> +
> +What:		/sys/kernel/mm/ksm/general_profit
> +Date:		April 2023
> +KernelVersion:  6.4
> +Contact:	Linux memory management mailing list <linux-mm@kvack.org>
> +Description:	Measure how effective KSM is.
> +		general_profit: how effective is KSM. The formula for the
> +		calculation is in Documentation/admin-guide/mm/ksm.rst.
> diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
Stefan Roesch April 28, 2023, 4:27 p.m. UTC | #2
xu xin <xu.xin.sc@gmail.com> writes:

>> This adds the general_profit KSM sysfs knob and the process profit metric
>> knobs to ksm_stat.
>>
>> 1) expose general_profit metric
>>
>>    The documentation mentions a general profit metric, however this
>>    metric is not calculated.  In addition the formula depends on the size
>>    of internal structures, which makes it more difficult for an
>>    administrator to make the calculation.  Adding the metric for a better
>>    user experience.
>>
>> 2) document general_profit sysfs knob
>>
>> 3) calculate ksm process profit metric
>>
>>    The ksm documentation mentions the process profit metric and how to
>>    calculate it.  This adds the calculation of the metric.
>>
>> 4) mm: expose ksm process profit metric in ksm_stat
>>
>>    This exposes the ksm process profit metric in /proc/<pid>/ksm_stat.
>>    The documentation mentions the formula for the ksm process profit
>>    metric, however it does not calculate it.  In addition the formula
>>    depends on the size of internal structures.  So it makes sense to
>>    expose it.
>>
>
> Hi, Stefan, I think you should give some credits to me about my contributions on
> the concept and formula of ksm profit (process wide and system wide),  it's kind
> of idea stealing.
>
>

I wasn't aware the formula that is described in the documentation is
from you. I simply implemented the documented metrics. If you came up
with it, happy to give you credit.

> Besides, the idea of Process control KSM was proposed by me last year although you use
> prctl instead of /proc fs. you even didn't CC my email. I think you should CC my email
> (xu.xin16@zte.com.cn) as least.
>
>> 5) document new procfs ksm knobs
>>
>> Signed-off-by: Stefan Roesch <shr@devkernel.io>
>> Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
>> Acked-by: David Hildenbrand <david@redhat.com>
>> Cc: David Hildenbrand <david@redhat.com>
>> Cc: Johannes Weiner <hannes@cmpxchg.org>
>> Cc: Michal Hocko <mhocko@suse.com>
>> Cc: Rik van Riel <riel@surriel.com>
>> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
>> ---
>>  Documentation/ABI/testing/sysfs-kernel-mm-ksm |  8 +++++++
>>  Documentation/admin-guide/mm/ksm.rst          |  5 ++++-
>>  fs/proc/base.c                                |  3 +++
>>  include/linux/ksm.h                           |  4 ++++
>>  mm/ksm.c                                      | 21 +++++++++++++++++++
>>  5 files changed, 40 insertions(+), 1 deletion(-)
>>
>> diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-ksm b/Documentation/ABI/testing/sysfs-kernel-mm-ksm
>> index d244674a9480..6041a025b65a 100644
>> --- a/Documentation/ABI/testing/sysfs-kernel-mm-ksm
>> +++ b/Documentation/ABI/testing/sysfs-kernel-mm-ksm
>> @@ -51,3 +51,11 @@ Description:	Control merging pages across different NUMA nodes.
>>
>>  		When it is set to 0 only pages from the same node are merged,
>>  		otherwise pages from all nodes can be merged together (default).
>> +
>> +What:		/sys/kernel/mm/ksm/general_profit
>> +Date:		April 2023
>> +KernelVersion:  6.4
>> +Contact:	Linux memory management mailing list <linux-mm@kvack.org>
>> +Description:	Measure how effective KSM is.
>> +		general_profit: how effective is KSM. The formula for the
>> +		calculation is in Documentation/admin-guide/mm/ksm.rst.
>> diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-ksm b/Documentation/ABI/testing/sysfs-kernel-mm-ksm
index d244674a9480..6041a025b65a 100644
--- a/Documentation/ABI/testing/sysfs-kernel-mm-ksm
+++ b/Documentation/ABI/testing/sysfs-kernel-mm-ksm
@@ -51,3 +51,11 @@  Description:	Control merging pages across different NUMA nodes.
 
 		When it is set to 0 only pages from the same node are merged,
 		otherwise pages from all nodes can be merged together (default).
+
+What:		/sys/kernel/mm/ksm/general_profit
+Date:		April 2023
+KernelVersion:  6.4
+Contact:	Linux memory management mailing list <linux-mm@kvack.org>
+Description:	Measure how effective KSM is.
+		general_profit: how effective is KSM. The formula for the
+		calculation is in Documentation/admin-guide/mm/ksm.rst.
diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
index eed51a910c94..551083a396fb 100644
--- a/Documentation/admin-guide/mm/ksm.rst
+++ b/Documentation/admin-guide/mm/ksm.rst
@@ -157,6 +157,8 @@  stable_node_chains_prune_millisecs
 
 The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``:
 
+general_profit
+        how effective is KSM. The calculation is explained below.
 pages_shared
         how many shared pages are being used
 pages_sharing
@@ -207,7 +209,8 @@  several times, which are unprofitable memory consumed.
 			  ksm_rmap_items * sizeof(rmap_item).
 
    where ksm_merging_pages is shown under the directory ``/proc/<pid>/``,
-   and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``.
+   and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``. The process profit
+   is also shown in ``/proc/<pid>/ksm_stat`` as ksm_process_profit.
 
 From the perspective of application, a high ratio of ``ksm_rmap_items`` to
 ``ksm_merging_pages`` means a bad madvise-applied policy, so developers or
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 5e0e0ccd47aa..96a6a08c8235 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -96,6 +96,7 @@ 
 #include <linux/time_namespace.h>
 #include <linux/resctrl.h>
 #include <linux/cn_proc.h>
+#include <linux/ksm.h>
 #include <trace/events/oom.h>
 #include "internal.h"
 #include "fd.h"
@@ -3207,6 +3208,8 @@  static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
 	mm = get_task_mm(task);
 	if (mm) {
 		seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
+		seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
+		seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
 		mmput(mm);
 	}
 
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index fa996373653b..590934bdddcf 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -64,6 +64,10 @@  struct page *ksm_might_need_to_copy(struct page *page,
 void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc);
 void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
 
+#ifdef CONFIG_PROC_FS
+long ksm_process_profit(struct mm_struct *);
+#endif /* CONFIG_PROC_FS */
+
 #else  /* !CONFIG_KSM */
 
 static inline void ksm_add_vma(struct vm_area_struct *vma)
diff --git a/mm/ksm.c b/mm/ksm.c
index f3f76f2ebe89..a959e8925413 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2962,6 +2962,14 @@  static void wait_while_offlining(void)
 }
 #endif /* CONFIG_MEMORY_HOTREMOVE */
 
+#ifdef CONFIG_PROC_FS
+long ksm_process_profit(struct mm_struct *mm)
+{
+	return mm->ksm_merging_pages * PAGE_SIZE -
+		mm->ksm_rmap_items * sizeof(struct ksm_rmap_item);
+}
+#endif /* CONFIG_PROC_FS */
+
 #ifdef CONFIG_SYSFS
 /*
  * This all compiles without CONFIG_SYSFS, but is a waste of space.
@@ -3226,6 +3234,18 @@  static ssize_t pages_volatile_show(struct kobject *kobj,
 }
 KSM_ATTR_RO(pages_volatile);
 
+static ssize_t general_profit_show(struct kobject *kobj,
+				   struct kobj_attribute *attr, char *buf)
+{
+	long general_profit;
+
+	general_profit = ksm_pages_sharing * PAGE_SIZE -
+				ksm_rmap_items * sizeof(struct ksm_rmap_item);
+
+	return sysfs_emit(buf, "%ld\n", general_profit);
+}
+KSM_ATTR_RO(general_profit);
+
 static ssize_t stable_node_dups_show(struct kobject *kobj,
 				     struct kobj_attribute *attr, char *buf)
 {
@@ -3290,6 +3310,7 @@  static struct attribute *ksm_attrs[] = {
 	&stable_node_dups_attr.attr,
 	&stable_node_chains_prune_millisecs_attr.attr,
 	&use_zero_pages_attr.attr,
+	&general_profit_attr.attr,
 	NULL,
 };