Generate pgstat_count_slru*() functions for slru using macros

Started by Bertrand Drouvot5 months ago2 messages
#1Bertrand Drouvot
bertranddrouvot.pg@gmail.com
1 attachment(s)

Hi hackers,

I was looking at pgstat_slru.c (I've in mind to provide some of those metrics
per backend), and realized that the same code pattern is repeated 7 times.

PFA a patch to $SUBJECT, removing a few lines of originally-duplicated
code patterns. In passing, some functions have to be renamed to match their
associated counter.

This is the same idea as 850f4b4, 8018ffb or 83a1a1b.

Looking forward to your feedback,

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v1-0001-Generate-pgstat_count_slru-functions-for-slru-usi.patchtext/x-diff; charset=us-asciiDownload
From 3b4a59d7a6265c6fa6f75a54e06ad4d783872727 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Fri, 29 Aug 2025 14:11:38 +0000
Subject: [PATCH v1] Generate pgstat_count_slru*() functions for slru using
 macros

This change replaces seven functions definitions by macros.
This is the same idea as 850f4b4, 8018ffb or 83a1a1b.
---
 src/backend/access/transam/slru.c        | 12 +++---
 src/backend/utils/activity/pgstat_slru.c | 54 +++++++++---------------
 src/include/pgstat.h                     | 10 ++---
 3 files changed, 31 insertions(+), 45 deletions(-)
  26.9% src/backend/access/transam/
  50.7% src/backend/utils/activity/
  22.3% src/include/

diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c
index 10ec259f382..ca69ee4d4f4 100644
--- a/src/backend/access/transam/slru.c
+++ b/src/backend/access/transam/slru.c
@@ -408,7 +408,7 @@ SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
 	pg_atomic_write_u64(&shared->latest_page_number, pageno);
 
 	/* update the stats counter of zeroed pages */
-	pgstat_count_slru_page_zeroed(shared->slru_stats_idx);
+	pgstat_count_slru_blocks_zeroed(shared->slru_stats_idx);
 
 	return slotno;
 }
@@ -560,7 +560,7 @@ SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
 			SlruRecentlyUsed(shared, slotno);
 
 			/* update the stats counter of pages found in the SLRU */
-			pgstat_count_slru_page_hit(shared->slru_stats_idx);
+			pgstat_count_slru_blocks_hit(shared->slru_stats_idx);
 
 			return slotno;
 		}
@@ -605,7 +605,7 @@ SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
 		SlruRecentlyUsed(shared, slotno);
 
 		/* update the stats counter of pages not found in SLRU */
-		pgstat_count_slru_page_read(shared->slru_stats_idx);
+		pgstat_count_slru_blocks_read(shared->slru_stats_idx);
 
 		return slotno;
 	}
@@ -648,7 +648,7 @@ SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
 			SlruRecentlyUsed(shared, slotno);
 
 			/* update the stats counter of pages found in the SLRU */
-			pgstat_count_slru_page_hit(shared->slru_stats_idx);
+			pgstat_count_slru_blocks_hit(shared->slru_stats_idx);
 
 			return slotno;
 		}
@@ -778,7 +778,7 @@ SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
 	off_t		endpos;
 
 	/* update the stats counter of checked pages */
-	pgstat_count_slru_page_exists(ctl->shared->slru_stats_idx);
+	pgstat_count_slru_blocks_exists(ctl->shared->slru_stats_idx);
 
 	SlruFileName(ctl, path, segno);
 
@@ -907,7 +907,7 @@ SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno, SlruWriteAll fdata)
 	int			fd = -1;
 
 	/* update the stats counter of written pages */
-	pgstat_count_slru_page_written(shared->slru_stats_idx);
+	pgstat_count_slru_blocks_written(shared->slru_stats_idx);
 
 	/*
 	 * Honor the write-WAL-before-data rule, if appropriate, so that we do not
diff --git a/src/backend/utils/activity/pgstat_slru.c b/src/backend/utils/activity/pgstat_slru.c
index 7bd8744accb..da50f8a0457 100644
--- a/src/backend/utils/activity/pgstat_slru.c
+++ b/src/backend/utils/activity/pgstat_slru.c
@@ -55,47 +55,33 @@ pgstat_reset_slru(const char *name)
  * SLRU statistics count accumulation functions --- called from slru.c
  */
 
-void
-pgstat_count_slru_page_zeroed(int slru_idx)
-{
-	get_slru_entry(slru_idx)->blocks_zeroed += 1;
+#define PGSTAT_COUNT_SLRU(stat)						\
+void												\
+CppConcat(pgstat_count_slru_,stat)(int slru_idx)	\
+{													\
+	get_slru_entry(slru_idx)->stat += 1;			\
 }
 
-void
-pgstat_count_slru_page_hit(int slru_idx)
-{
-	get_slru_entry(slru_idx)->blocks_hit += 1;
-}
+/* pgstat_count_slru_blocks_zeroed */
+PGSTAT_COUNT_SLRU(blocks_zeroed)
 
-void
-pgstat_count_slru_page_exists(int slru_idx)
-{
-	get_slru_entry(slru_idx)->blocks_exists += 1;
-}
+/* pgstat_count_slru_blocks_hit */
+PGSTAT_COUNT_SLRU(blocks_hit)
 
-void
-pgstat_count_slru_page_read(int slru_idx)
-{
-	get_slru_entry(slru_idx)->blocks_read += 1;
-}
+/* pgstat_count_slru_blocks_exists */
+PGSTAT_COUNT_SLRU(blocks_exists)
 
-void
-pgstat_count_slru_page_written(int slru_idx)
-{
-	get_slru_entry(slru_idx)->blocks_written += 1;
-}
+/* pgstat_count_slru_blocks_read */
+PGSTAT_COUNT_SLRU(blocks_read)
 
-void
-pgstat_count_slru_flush(int slru_idx)
-{
-	get_slru_entry(slru_idx)->flush += 1;
-}
+/* pgstat_count_slru_blocks_written */
+PGSTAT_COUNT_SLRU(blocks_written)
 
-void
-pgstat_count_slru_truncate(int slru_idx)
-{
-	get_slru_entry(slru_idx)->truncate += 1;
-}
+/* pgstat_count_slru_flush */
+PGSTAT_COUNT_SLRU(flush)
+
+/* pgstat_count_slru_truncate */
+PGSTAT_COUNT_SLRU(truncate)
 
 /*
  * Support function for the SQL-callable pgstat* functions. Returns
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 202bd2d5ace..f402b17295c 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -747,11 +747,11 @@ extern PgStat_StatReplSlotEntry *pgstat_fetch_replslot(NameData slotname);
  */
 
 extern void pgstat_reset_slru(const char *);
-extern void pgstat_count_slru_page_zeroed(int slru_idx);
-extern void pgstat_count_slru_page_hit(int slru_idx);
-extern void pgstat_count_slru_page_read(int slru_idx);
-extern void pgstat_count_slru_page_written(int slru_idx);
-extern void pgstat_count_slru_page_exists(int slru_idx);
+extern void pgstat_count_slru_blocks_zeroed(int slru_idx);
+extern void pgstat_count_slru_blocks_hit(int slru_idx);
+extern void pgstat_count_slru_blocks_read(int slru_idx);
+extern void pgstat_count_slru_blocks_written(int slru_idx);
+extern void pgstat_count_slru_blocks_exists(int slru_idx);
 extern void pgstat_count_slru_flush(int slru_idx);
 extern void pgstat_count_slru_truncate(int slru_idx);
 extern const char *pgstat_get_slru_name(int slru_idx);
-- 
2.34.1

#2Michael Paquier
michael@paquier.xyz
In reply to: Bertrand Drouvot (#1)
Re: Generate pgstat_count_slru*() functions for slru using macros

On Fri, Aug 29, 2025 at 03:02:23PM +0000, Bertrand Drouvot wrote:

I was looking at pgstat_slru.c (I've in mind to provide some of those metrics
per backend), and realized that the same code pattern is repeated 7 times.

So, the gist of the change is centralized around get_slru_entry(),
renaming the routines to match with the field names. It's a bit
puzzling that we used different names for the fields and the routines,
with "blocks" vs "page".

PFA a patch to $SUBJECT, removing a few lines of originally-duplicated
code patterns. In passing, some functions have to be renamed to match their
associated counter.

It does not hurt, so fine by me on consistency grounds.

This is the same idea as 850f4b4, 8018ffb or 83a1a1b.

That rings a bell. I'm OK with your proposal here, cutting some code.
Let's wait a bit to see if others have any comments.
--
Michael