lib/show_mem.c: Use for_each_populated_zone() simplify code

Message ID 20230417035226.4013584-1-yajun.deng@linux.dev
State New
Headers
Series lib/show_mem.c: Use for_each_populated_zone() simplify code |

Commit Message

Yajun Deng April 17, 2023, 3:52 a.m. UTC
  The __show_mem() needs to iterate over all zones that have memory, we can
simplify the code by using for_each_populated_zone().

Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
 lib/show_mem.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
  

Comments

Vlastimil Babka April 17, 2023, 7:27 a.m. UTC | #1
On 4/17/23 05:52, Yajun Deng wrote:
> The __show_mem() needs to iterate over all zones that have memory, we can
> simplify the code by using for_each_populated_zone().
> 
> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  lib/show_mem.c | 19 ++++++-------------
>  1 file changed, 6 insertions(+), 13 deletions(-)
> 
> diff --git a/lib/show_mem.c b/lib/show_mem.c
> index 0d7585cde2a6..1485c87be935 100644
> --- a/lib/show_mem.c
> +++ b/lib/show_mem.c
> @@ -10,26 +10,19 @@
>  
>  void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
>  {
> -	pg_data_t *pgdat;
>  	unsigned long total = 0, reserved = 0, highmem = 0;
> +	struct zone *zone;
>  
>  	printk("Mem-Info:\n");
>  	__show_free_areas(filter, nodemask, max_zone_idx);
>  
> -	for_each_online_pgdat(pgdat) {
> -		int zoneid;
> +	for_each_populated_zone(zone) {
>  
> -		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
> -			struct zone *zone = &pgdat->node_zones[zoneid];
> -			if (!populated_zone(zone))
> -				continue;
> +		total += zone->present_pages;
> +		reserved += zone->present_pages - zone_managed_pages(zone);
>  
> -			total += zone->present_pages;
> -			reserved += zone->present_pages - zone_managed_pages(zone);
> -
> -			if (is_highmem_idx(zoneid))
> -				highmem += zone->present_pages;
> -		}
> +		if (is_highmem(zone))
> +			highmem += zone->present_pages;
>  	}
>  
>  	printk("%lu pages RAM\n", total);
  
Michal Hocko April 17, 2023, 7:52 a.m. UTC | #2
On Mon 17-04-23 11:52:26, Yajun Deng wrote:
> The __show_mem() needs to iterate over all zones that have memory, we can
> simplify the code by using for_each_populated_zone().
> 
> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>

Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!

> ---
>  lib/show_mem.c | 19 ++++++-------------
>  1 file changed, 6 insertions(+), 13 deletions(-)
> 
> diff --git a/lib/show_mem.c b/lib/show_mem.c
> index 0d7585cde2a6..1485c87be935 100644
> --- a/lib/show_mem.c
> +++ b/lib/show_mem.c
> @@ -10,26 +10,19 @@
>  
>  void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
>  {
> -	pg_data_t *pgdat;
>  	unsigned long total = 0, reserved = 0, highmem = 0;
> +	struct zone *zone;
>  
>  	printk("Mem-Info:\n");
>  	__show_free_areas(filter, nodemask, max_zone_idx);
>  
> -	for_each_online_pgdat(pgdat) {
> -		int zoneid;
> +	for_each_populated_zone(zone) {
>  
> -		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
> -			struct zone *zone = &pgdat->node_zones[zoneid];
> -			if (!populated_zone(zone))
> -				continue;
> +		total += zone->present_pages;
> +		reserved += zone->present_pages - zone_managed_pages(zone);
>  
> -			total += zone->present_pages;
> -			reserved += zone->present_pages - zone_managed_pages(zone);
> -
> -			if (is_highmem_idx(zoneid))
> -				highmem += zone->present_pages;
> -		}
> +		if (is_highmem(zone))
> +			highmem += zone->present_pages;
>  	}
>  
>  	printk("%lu pages RAM\n", total);
> -- 
> 2.25.1
  

Patch

diff --git a/lib/show_mem.c b/lib/show_mem.c
index 0d7585cde2a6..1485c87be935 100644
--- a/lib/show_mem.c
+++ b/lib/show_mem.c
@@ -10,26 +10,19 @@ 
 
 void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
 {
-	pg_data_t *pgdat;
 	unsigned long total = 0, reserved = 0, highmem = 0;
+	struct zone *zone;
 
 	printk("Mem-Info:\n");
 	__show_free_areas(filter, nodemask, max_zone_idx);
 
-	for_each_online_pgdat(pgdat) {
-		int zoneid;
+	for_each_populated_zone(zone) {
 
-		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
-			struct zone *zone = &pgdat->node_zones[zoneid];
-			if (!populated_zone(zone))
-				continue;
+		total += zone->present_pages;
+		reserved += zone->present_pages - zone_managed_pages(zone);
 
-			total += zone->present_pages;
-			reserved += zone->present_pages - zone_managed_pages(zone);
-
-			if (is_highmem_idx(zoneid))
-				highmem += zone->present_pages;
-		}
+		if (is_highmem(zone))
+			highmem += zone->present_pages;
 	}
 
 	printk("%lu pages RAM\n", total);