linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/vmscan: simplify the calculation of fractions for SCAN_FRACT
@ 2024-02-28  1:55 Byungchul Park
  2024-02-28 21:44 ` Johannes Weiner
  0 siblings, 1 reply; 2+ messages in thread
From: Byungchul Park @ 2024-02-28  1:55 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, linux-mm, kernel_team, hannes

The current way to calculate fractions for SACN_FRACT is little readable
and more complicated than it should be.  It also performs unnecessary
division and adjustment to avoid zero operands.  Prune away by
multiplying the fractions by 'anon_cost * file_cost / (3 * total_cost)':

where:
   total_cost = sc->anon_cost + sc->file_cost
   anon_cost = total_cost + sc->anon_cost
   file_cost = total_cost + sc->file_cost

before:
   fraction[0] = swappiness * 3 * total_cost / anon_cost
   fraction[1] = (200 - swappiness) * 3 * total_cost / file_cost

after:
   fraction[0] = swappiness * file_cost
   fraction[1] = (200 - swappiness) * anon_cost

Worth noting that this patch doesn't change the formula.

Signed-off-by: Byungchul Park <byungchul@sk.com>
---
 mm/vmscan.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 4657440854db..7b33fcc1cbdc 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -2339,7 +2339,6 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 	u64 fraction[ANON_AND_FILE];
 	u64 denominator = 0;	/* gcc */
 	enum scan_balance scan_balance;
-	unsigned long ap, fp;
 	enum lru_list lru;
 
 	/*
@@ -2416,17 +2415,10 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 	total_cost = sc->anon_cost + sc->file_cost;
 	anon_cost = total_cost + sc->anon_cost;
 	file_cost = total_cost + sc->file_cost;
-	total_cost = anon_cost + file_cost;
 
-	ap = swappiness * (total_cost + 1);
-	ap /= anon_cost + 1;
-
-	fp = (200 - swappiness) * (total_cost + 1);
-	fp /= file_cost + 1;
-
-	fraction[0] = ap;
-	fraction[1] = fp;
-	denominator = ap + fp;
+	fraction[0] = swappiness * file_cost;
+	fraction[1] = (200 - swappiness) * anon_cost;
+	denominator = fraction[0] + fraction[1];
 out:
 	for_each_evictable_lru(lru) {
 		int file = is_file_lru(lru);
-- 
2.17.1



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] mm/vmscan: simplify the calculation of fractions for SCAN_FRACT
  2024-02-28  1:55 [PATCH] mm/vmscan: simplify the calculation of fractions for SCAN_FRACT Byungchul Park
@ 2024-02-28 21:44 ` Johannes Weiner
  0 siblings, 0 replies; 2+ messages in thread
From: Johannes Weiner @ 2024-02-28 21:44 UTC (permalink / raw)
  To: Byungchul Park; +Cc: akpm, linux-kernel, linux-mm, kernel_team

On Wed, Feb 28, 2024 at 10:55:00AM +0900, Byungchul Park wrote:
> The current way to calculate fractions for SACN_FRACT is little readable
> and more complicated than it should be.  It also performs unnecessary
> division and adjustment to avoid zero operands.  Prune away by
> multiplying the fractions by 'anon_cost * file_cost / (3 * total_cost)':
> 
> where:
>    total_cost = sc->anon_cost + sc->file_cost
>    anon_cost = total_cost + sc->anon_cost
>    file_cost = total_cost + sc->file_cost
> 
> before:
>    fraction[0] = swappiness * 3 * total_cost / anon_cost
>    fraction[1] = (200 - swappiness) * 3 * total_cost / file_cost
> 
> after:
>    fraction[0] = swappiness * file_cost
>    fraction[1] = (200 - swappiness) * anon_cost
> 
> Worth noting that this patch doesn't change the formula.
> 
> Signed-off-by: Byungchul Park <byungchul@sk.com>
> ---
>  mm/vmscan.c | 14 +++-----------
>  1 file changed, 3 insertions(+), 11 deletions(-)
> 
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 4657440854db..7b33fcc1cbdc 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -2339,7 +2339,6 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
>  	u64 fraction[ANON_AND_FILE];
>  	u64 denominator = 0;	/* gcc */
>  	enum scan_balance scan_balance;
> -	unsigned long ap, fp;
>  	enum lru_list lru;
>  
>  	/*
> @@ -2416,17 +2415,10 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
>  	total_cost = sc->anon_cost + sc->file_cost;
>  	anon_cost = total_cost + sc->anon_cost;
>  	file_cost = total_cost + sc->file_cost;
> -	total_cost = anon_cost + file_cost;
>  
> -	ap = swappiness * (total_cost + 1);
> -	ap /= anon_cost + 1;
> -
> -	fp = (200 - swappiness) * (total_cost + 1);
> -	fp /= file_cost + 1;
> -
> -	fraction[0] = ap;
> -	fraction[1] = fp;
> -	denominator = ap + fp;
> +	fraction[0] = swappiness * file_cost;
> +	fraction[1] = (200 - swappiness) * anon_cost;

Unfortunately, I don't think that

anon = swappiness * file_cost
file = (200 - swappiness) * anon_cost

is more readable. Sure it's the same, but I think it's clearer to
actually see that `anon = total_cost / anon_cost` ratio in the code.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-28 21:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-28  1:55 [PATCH] mm/vmscan: simplify the calculation of fractions for SCAN_FRACT Byungchul Park
2024-02-28 21:44 ` Johannes Weiner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox