2024-03-23 08:57:53

by Chen Taotao

[permalink] [raw]
Subject: [RFC] mm: get_mm_counter() get the total memory usage of the process

Currently, the get_mm_counter() function returns only the value of
the process memory counter percpu_counter ->count record, ignoring
the memory usage count maintained by each CPU in the
percpu_counter->counters array, which leads to an error in obtaining
the memory usage count of a process, especially when there are many
CPU cores. counts, especially when there are many CPU cores.

It is now possible to have get_mm_counter() get the memory count of a
process by adding the memory counts maintained by each cpu, thus getting
an accurate memory count of the process.

This patch is an unofficial version that simply fixes the above problem,
as I'm not sure if it makes sense to do so.

Signed-off-by: Chen Taotao <[email protected]>
---
include/linux/mm.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index f5a97dec5..5cf6443aa 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2569,7 +2569,7 @@ static inline bool get_user_page_fast_only(unsigned long addr,
*/
static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
{
- return percpu_counter_read_positive(&mm->rss_stat[member]);
+ return percpu_counter_sum_positive(&mm->rss_stat[member]);
}

void mm_trace_rss_stat(struct mm_struct *mm, int member);
--
2.27.0



2024-04-04 19:31:00

by David Hildenbrand

[permalink] [raw]
Subject: Re: [RFC] mm: get_mm_counter() get the total memory usage of the process

On 22.03.24 16:11, Chen Taotao wrote:
> Currently, the get_mm_counter() function returns only the value of
> the process memory counter percpu_counter ->count record, ignoring
> the memory usage count maintained by each CPU in the
> percpu_counter->counters array, which leads to an error in obtaining
> the memory usage count of a process, especially when there are many
> CPU cores. counts, especially when there are many CPU cores.
>
> It is now possible to have get_mm_counter() get the memory count of a
> process by adding the memory counts maintained by each cpu, thus getting
> an accurate memory count of the process.
>
> This patch is an unofficial version that simply fixes the above problem,
> as I'm not sure if it makes sense to do so.

What is the effective change? Do you have any real examples where we can
observe a real difference, so it's worth the churn?

On the other hand, doing the sum gets more expensive, do we care? How
expensive is it?

Such a discussion should ideally be part of this change log.

"I'm not sure if it makes sense" -- you tell us, how did you come up
with the idea? :)

--
Cheers,

David / dhildenb


2024-04-10 05:22:46

by Tim Chen

[permalink] [raw]
Subject: Re: [RFC] mm: get_mm_counter() get the total memory usage of the process

On Fri, 2024-03-22 at 23:11 +0800, Chen Taotao wrote:
> Currently, the get_mm_counter() function returns only the value of
> the process memory counter percpu_counter ->count record, ignoring
> the memory usage count maintained by each CPU in the
> percpu_counter->counters array, which leads to an error in obtaining
> the memory usage count of a process, especially when there are many
> CPU cores. counts, especially when there are many CPU cores.
>
> It is now possible to have get_mm_counter() get the memory count of a
> process by adding the memory counts maintained by each cpu, thus getting
> an accurate memory count of the process.
>
> This patch is an unofficial version that simply fixes the above problem,
> as I'm not sure if it makes sense to do so.

Summing up the mm counts maintained in every cpu is expensive,
especially if we are doing the read often. More so
when there are a large number of cores on large servers or
newer CPU with high core counts.

For mm counters,the count in fbc->count is good enough we
don't need to correct fbc->count with the the small counts
update (< percpu_counter_batch) cached in per cpu counts.

So do you have a mm count use case where you really need the precise
count with get_mm_counter?

I do not think we should make the change you suggested.

Tim


>
> Signed-off-by: Chen Taotao <[email protected]>
> ---
> include/linux/mm.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index f5a97dec5..5cf6443aa 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -2569,7 +2569,7 @@ static inline bool get_user_page_fast_only(unsigned long addr,
> */
> static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
> {
> - return percpu_counter_read_positive(&mm->rss_stat[member]);
> + return percpu_counter_sum_positive(&mm->rss_stat[member]);
> }
>
> void mm_trace_rss_stat(struct mm_struct *mm, int member);