diff mbox series

mm: Simplify shrink_inactive_list()

Message ID 155490878845.17489.11907324308110282086.stgit@localhost.localdomain (mailing list archive)
State New, archived
Headers show
Series mm: Simplify shrink_inactive_list() | expand

Commit Message

Kirill Tkhai April 10, 2019, 3:07 p.m. UTC
This merges together duplicating patterns of code.
Changes in enum vm_event_item is made to underline
that *_DIRECT and *_KSWAPD must differ by 1.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 include/linux/vm_event_item.h |    4 ++--
 mm/vmscan.c                   |   31 +++++++++----------------------
 2 files changed, 11 insertions(+), 24 deletions(-)

Comments

Daniel Jordan April 11, 2019, 10:13 p.m. UTC | #1
On Wed, Apr 10, 2019 at 06:07:04PM +0300, Kirill Tkhai wrote:
> @@ -1934,17 +1935,10 @@ shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
>  	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
>  	reclaim_stat->recent_scanned[file] += nr_taken;
>  
> -	if (current_is_kswapd()) {
> -		if (global_reclaim(sc))
> -			__count_vm_events(PGSCAN_KSWAPD, nr_scanned);
> -		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
> -				   nr_scanned);
> -	} else {
> -		if (global_reclaim(sc))
> -			__count_vm_events(PGSCAN_DIRECT, nr_scanned);
> -		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
> -				   nr_scanned);
> -	}
> +	if (global_reclaim(sc))
> +		__count_vm_events(PGSCAN_KSWAPD + is_direct, nr_scanned);
> +	__count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD + is_direct,
> +			     nr_scanned);

Nice to avoid duplication like this, but now it takes looking at
vm_event_item.h to understand that (PGSCAN_KSWAPD + is_direct) might mean
PGSCAN_DIRECT.

What about this pattern for each block instead, which makes the stat used
explicit and avoids the header change?

       stat = current_is_kswapd() ? PG*_KSWAPD : PG*_DIRECT;
Baoquan He April 12, 2019, 12:05 a.m. UTC | #2
On 04/11/19 at 06:13pm, Daniel Jordan wrote:
> On Wed, Apr 10, 2019 at 06:07:04PM +0300, Kirill Tkhai wrote:
> > @@ -1934,17 +1935,10 @@ shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
> >  	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
> >  	reclaim_stat->recent_scanned[file] += nr_taken;
> >  
> > -	if (current_is_kswapd()) {
> > -		if (global_reclaim(sc))
> > -			__count_vm_events(PGSCAN_KSWAPD, nr_scanned);
> > -		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
> > -				   nr_scanned);
> > -	} else {
> > -		if (global_reclaim(sc))
> > -			__count_vm_events(PGSCAN_DIRECT, nr_scanned);
> > -		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
> > -				   nr_scanned);
> > -	}
> > +	if (global_reclaim(sc))
> > +		__count_vm_events(PGSCAN_KSWAPD + is_direct, nr_scanned);
> > +	__count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD + is_direct,
> > +			     nr_scanned);
> 
> Nice to avoid duplication like this, but now it takes looking at
> vm_event_item.h to understand that (PGSCAN_KSWAPD + is_direct) might mean
> PGSCAN_DIRECT.
> 
> What about this pattern for each block instead, which makes the stat used
> explicit and avoids the header change?
> 
>        stat = current_is_kswapd() ? PG*_KSWAPD : PG*_DIRECT;

Yeah, looks nice. Maybe name it as item or event since we have had stat
locally defined as "struct reclaim_stat stat".

	enum vm_event_item item;
	...
        item = current_is_kswapd() ? PG*_KSWAPD : PG*_DIRECT;
Kirill Tkhai April 12, 2019, 10:52 a.m. UTC | #3
On 12.04.2019 03:05, Baoquan He wrote:
> On 04/11/19 at 06:13pm, Daniel Jordan wrote:
>> On Wed, Apr 10, 2019 at 06:07:04PM +0300, Kirill Tkhai wrote:
>>> @@ -1934,17 +1935,10 @@ shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
>>>  	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
>>>  	reclaim_stat->recent_scanned[file] += nr_taken;
>>>  
>>> -	if (current_is_kswapd()) {
>>> -		if (global_reclaim(sc))
>>> -			__count_vm_events(PGSCAN_KSWAPD, nr_scanned);
>>> -		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
>>> -				   nr_scanned);
>>> -	} else {
>>> -		if (global_reclaim(sc))
>>> -			__count_vm_events(PGSCAN_DIRECT, nr_scanned);
>>> -		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
>>> -				   nr_scanned);
>>> -	}
>>> +	if (global_reclaim(sc))
>>> +		__count_vm_events(PGSCAN_KSWAPD + is_direct, nr_scanned);
>>> +	__count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD + is_direct,
>>> +			     nr_scanned);
>>
>> Nice to avoid duplication like this, but now it takes looking at
>> vm_event_item.h to understand that (PGSCAN_KSWAPD + is_direct) might mean
>> PGSCAN_DIRECT.
>>
>> What about this pattern for each block instead, which makes the stat used
>> explicit and avoids the header change?
>>
>>        stat = current_is_kswapd() ? PG*_KSWAPD : PG*_DIRECT;
> 
> Yeah, looks nice. Maybe name it as item or event since we have had stat
> locally defined as "struct reclaim_stat stat".
> 
> 	enum vm_event_item item;
> 	...
>         item = current_is_kswapd() ? PG*_KSWAPD : PG*_DIRECT;

Sounds good.
diff mbox series

Patch

diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
index 47a3441cf4c4..8f1403e692a2 100644
--- a/include/linux/vm_event_item.h
+++ b/include/linux/vm_event_item.h
@@ -31,9 +31,9 @@  enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
 		PGLAZYFREED,
 		PGREFILL,
 		PGSTEAL_KSWAPD,
-		PGSTEAL_DIRECT,
+		PGSTEAL_DIRECT = PGSTEAL_KSWAPD + 1,
 		PGSCAN_KSWAPD,
-		PGSCAN_DIRECT,
+		PGSCAN_DIRECT = PGSCAN_KSWAPD + 1,
 		PGSCAN_DIRECT_THROTTLE,
 #ifdef CONFIG_NUMA
 		PGSCAN_ZONE_RECLAIM_FAILED,
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 836b28913bd7..f8ac0825d1c7 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1907,6 +1907,7 @@  shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
 	unsigned long nr_taken;
 	struct reclaim_stat stat;
 	int file = is_file_lru(lru);
+	int is_direct = !current_is_kswapd();
 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
 	bool stalled = false;
@@ -1934,17 +1935,10 @@  shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
 	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
 	reclaim_stat->recent_scanned[file] += nr_taken;
 
-	if (current_is_kswapd()) {
-		if (global_reclaim(sc))
-			__count_vm_events(PGSCAN_KSWAPD, nr_scanned);
-		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
-				   nr_scanned);
-	} else {
-		if (global_reclaim(sc))
-			__count_vm_events(PGSCAN_DIRECT, nr_scanned);
-		count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
-				   nr_scanned);
-	}
+	if (global_reclaim(sc))
+		__count_vm_events(PGSCAN_KSWAPD + is_direct, nr_scanned);
+	__count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD + is_direct,
+			     nr_scanned);
 	spin_unlock_irq(&pgdat->lru_lock);
 
 	if (nr_taken == 0)
@@ -1955,17 +1949,10 @@  shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
 
 	spin_lock_irq(&pgdat->lru_lock);
 
-	if (current_is_kswapd()) {
-		if (global_reclaim(sc))
-			__count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed);
-		count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD,
-				   nr_reclaimed);
-	} else {
-		if (global_reclaim(sc))
-			__count_vm_events(PGSTEAL_DIRECT, nr_reclaimed);
-		count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT,
-				   nr_reclaimed);
-	}
+	if (global_reclaim(sc))
+		__count_vm_events(PGSTEAL_KSWAPD + is_direct, nr_reclaimed);
+	__count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD + is_direct,
+			     nr_reclaimed);
 	reclaim_stat->recent_rotated[0] = stat.nr_activate[0];
 	reclaim_stat->recent_rotated[1] = stat.nr_activate[1];