Skip to content

Commit d21077f

Browse files
Stefan Roeschakpm00
authored andcommitted
mm: add new KSM process and sysfs knobs
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 Link: https://lkml.kernel.org/r/20230418051342.1919757-3-shr@devkernel.io 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>
1 parent d7597f5 commit d21077f

5 files changed

Lines changed: 41 additions & 1 deletion

File tree

Documentation/ABI/testing/sysfs-kernel-mm-ksm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ Description: Control merging pages across different NUMA nodes.
5151

5252
When it is set to 0 only pages from the same node are merged,
5353
otherwise pages from all nodes can be merged together (default).
54+
55+
What: /sys/kernel/mm/ksm/general_profit
56+
Date: April 2023
57+
KernelVersion: 6.4
58+
Contact: Linux memory management mailing list <linux-mm@kvack.org>
59+
Description: Measure how effective KSM is.
60+
general_profit: how effective is KSM. The formula for the
61+
calculation is in Documentation/admin-guide/mm/ksm.rst.

Documentation/admin-guide/mm/ksm.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ stable_node_chains_prune_millisecs
157157

158158
The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``:
159159

160+
general_profit
161+
how effective is KSM. The calculation is explained below.
160162
pages_shared
161163
how many shared pages are being used
162164
pages_sharing
@@ -207,7 +209,8 @@ several times, which are unprofitable memory consumed.
207209
ksm_rmap_items * sizeof(rmap_item).
208210

209211
where ksm_merging_pages is shown under the directory ``/proc/<pid>/``,
210-
and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``.
212+
and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``. The process profit
213+
is also shown in ``/proc/<pid>/ksm_stat`` as ksm_process_profit.
211214

212215
From the perspective of application, a high ratio of ``ksm_rmap_items`` to
213216
``ksm_merging_pages`` means a bad madvise-applied policy, so developers or

fs/proc/base.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#include <linux/time_namespace.h>
9797
#include <linux/resctrl.h>
9898
#include <linux/cn_proc.h>
99+
#include <linux/ksm.h>
99100
#include <trace/events/oom.h>
100101
#include "internal.h"
101102
#include "fd.h"
@@ -3207,6 +3208,8 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
32073208
mm = get_task_mm(task);
32083209
if (mm) {
32093210
seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
3211+
seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
3212+
seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
32103213
mmput(mm);
32113214
}
32123215

include/linux/ksm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio);
6868
void collect_procs_ksm(struct page *page, struct list_head *to_kill,
6969
int force_early);
7070
#endif
71+
72+
#ifdef CONFIG_PROC_FS
73+
long ksm_process_profit(struct mm_struct *);
74+
#endif /* CONFIG_PROC_FS */
75+
7176
#else /* !CONFIG_KSM */
7277

7378
static inline void ksm_add_vma(struct vm_area_struct *vma)

mm/ksm.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,14 @@ static void wait_while_offlining(void)
30073007
}
30083008
#endif /* CONFIG_MEMORY_HOTREMOVE */
30093009

3010+
#ifdef CONFIG_PROC_FS
3011+
long ksm_process_profit(struct mm_struct *mm)
3012+
{
3013+
return mm->ksm_merging_pages * PAGE_SIZE -
3014+
mm->ksm_rmap_items * sizeof(struct ksm_rmap_item);
3015+
}
3016+
#endif /* CONFIG_PROC_FS */
3017+
30103018
#ifdef CONFIG_SYSFS
30113019
/*
30123020
* This all compiles without CONFIG_SYSFS, but is a waste of space.
@@ -3271,6 +3279,18 @@ static ssize_t pages_volatile_show(struct kobject *kobj,
32713279
}
32723280
KSM_ATTR_RO(pages_volatile);
32733281

3282+
static ssize_t general_profit_show(struct kobject *kobj,
3283+
struct kobj_attribute *attr, char *buf)
3284+
{
3285+
long general_profit;
3286+
3287+
general_profit = ksm_pages_sharing * PAGE_SIZE -
3288+
ksm_rmap_items * sizeof(struct ksm_rmap_item);
3289+
3290+
return sysfs_emit(buf, "%ld\n", general_profit);
3291+
}
3292+
KSM_ATTR_RO(general_profit);
3293+
32743294
static ssize_t stable_node_dups_show(struct kobject *kobj,
32753295
struct kobj_attribute *attr, char *buf)
32763296
{
@@ -3335,6 +3355,7 @@ static struct attribute *ksm_attrs[] = {
33353355
&stable_node_dups_attr.attr,
33363356
&stable_node_chains_prune_millisecs_attr.attr,
33373357
&use_zero_pages_attr.attr,
3358+
&general_profit_attr.attr,
33383359
NULL,
33393360
};
33403361

0 commit comments

Comments
 (0)