Skip to content

Commit 1a8e843

Browse files
xu xinakpm00
authored andcommitted
ksm: consider KSM-placed zeropages when calculating KSM profit
When use_zero_pages is enabled, the calculation of ksm profit is not correct because ksm zero pages is not counted in. So update the calculation of KSM profit including the documentation. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: xu xin <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Xiaokai Ran <[email protected]> Cc: Yang Yang <[email protected]> Cc: Jiang Xuexin <[email protected]> Cc: Claudio Imbrenda <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 6080d19 commit 1a8e843

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

Documentation/admin-guide/mm/ksm.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,25 @@ several times, which are unprofitable memory consumed.
203203
1) How to determine whether KSM save memory or consume memory in system-wide
204204
range? Here is a simple approximate calculation for reference::
205205

206-
general_profit =~ pages_sharing * sizeof(page) - (all_rmap_items) *
206+
general_profit =~ ksm_saved_pages * sizeof(page) - (all_rmap_items) *
207207
sizeof(rmap_item);
208208

209-
where all_rmap_items can be easily obtained by summing ``pages_sharing``,
210-
``pages_shared``, ``pages_unshared`` and ``pages_volatile``.
209+
where ksm_saved_pages equals to the sum of ``pages_sharing`` +
210+
``ksm_zero_pages`` of the system, and all_rmap_items can be easily
211+
obtained by summing ``pages_sharing``, ``pages_shared``, ``pages_unshared``
212+
and ``pages_volatile``.
211213

212214
2) The KSM profit inner a single process can be similarly obtained by the
213215
following approximate calculation::
214216

215-
process_profit =~ ksm_merging_pages * sizeof(page) -
217+
process_profit =~ ksm_saved_pages * sizeof(page) -
216218
ksm_rmap_items * sizeof(rmap_item).
217219

218-
where ksm_merging_pages is shown under the directory ``/proc/<pid>/``,
219-
and ksm_rmap_items is shown in ``/proc/<pid>/ksm_stat``. The process profit
220-
is also shown in ``/proc/<pid>/ksm_stat`` as ksm_process_profit.
220+
where ksm_saved_pages equals to the sum of ``ksm_merging_pages`` and
221+
``ksm_zero_pages``, both of which are shown under the directory
222+
``/proc/<pid>/ksm_stat``, and ksm_rmap_items is also shown in
223+
``/proc/<pid>/ksm_stat``. The process profit is also shown in
224+
``/proc/<pid>/ksm_stat`` as ksm_process_profit.
221225

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

mm/ksm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,7 +3092,7 @@ static void wait_while_offlining(void)
30923092
#ifdef CONFIG_PROC_FS
30933093
long ksm_process_profit(struct mm_struct *mm)
30943094
{
3095-
return mm->ksm_merging_pages * PAGE_SIZE -
3095+
return (long)(mm->ksm_merging_pages + mm->ksm_zero_pages) * PAGE_SIZE -
30963096
mm->ksm_rmap_items * sizeof(struct ksm_rmap_item);
30973097
}
30983098
#endif /* CONFIG_PROC_FS */
@@ -3373,7 +3373,7 @@ static ssize_t general_profit_show(struct kobject *kobj,
33733373
{
33743374
long general_profit;
33753375

3376-
general_profit = ksm_pages_sharing * PAGE_SIZE -
3376+
general_profit = (ksm_pages_sharing + ksm_zero_pages) * PAGE_SIZE -
33773377
ksm_rmap_items * sizeof(struct ksm_rmap_item);
33783378

33793379
return sysfs_emit(buf, "%ld\n", general_profit);

0 commit comments

Comments
 (0)