Use pgstat_kind_infos to write fixed shared statistics
authorMichael Paquier <[email protected]>
Tue, 9 Jul 2024 01:27:12 +0000 (10:27 +0900)
committerMichael Paquier <[email protected]>
Tue, 9 Jul 2024 01:27:12 +0000 (10:27 +0900)
This is similar to 9004abf6206e, but this time for the write part of the
stats file.  The code is changed so as, rather than referring to
individual members of PgStat_Snapshot in an order based on their
PgStat_Kind value, a loop based on pgstat_kind_infos is used to retrieve
the contents to write from the snapshot structure, for a size of
PgStat_KindInfo's shared_data_len.

This requires the addition to PgStat_KindInfo of an offset to track the
location of each fixed-numbered stats in PgStat_Snapshot.  This change
is useful to make this area of the code more easily pluggable, and
reduces the knowledge of specific fixed-numbered kinds in pgstat.c.

Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/[email protected]

src/backend/utils/activity/pgstat.c
src/include/utils/pgstat_internal.h

index c37c11b2ecb0b1d2ca7e62fde1f1d8d05a1ace2e..65b937a85f4cfb935e63f4e0f4f6a158f2ea5d06 100644 (file)
@@ -349,6 +349,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
 
        .fixed_amount = true,
 
+       .snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
        .shared_data_off = offsetof(PgStatShared_Archiver, stats),
        .shared_data_len = sizeof(((PgStatShared_Archiver *) 0)->stats),
@@ -362,6 +363,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
 
        .fixed_amount = true,
 
+       .snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
        .shared_data_off = offsetof(PgStatShared_BgWriter, stats),
        .shared_data_len = sizeof(((PgStatShared_BgWriter *) 0)->stats),
@@ -375,6 +377,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
 
        .fixed_amount = true,
 
+       .snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
        .shared_data_off = offsetof(PgStatShared_Checkpointer, stats),
        .shared_data_len = sizeof(((PgStatShared_Checkpointer *) 0)->stats),
@@ -388,6 +391,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
 
        .fixed_amount = true,
 
+       .snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, io),
        .shared_data_off = offsetof(PgStatShared_IO, stats),
        .shared_data_len = sizeof(((PgStatShared_IO *) 0)->stats),
@@ -401,6 +405,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
 
        .fixed_amount = true,
 
+       .snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
        .shared_data_off = offsetof(PgStatShared_SLRU, stats),
        .shared_data_len = sizeof(((PgStatShared_SLRU *) 0)->stats),
@@ -414,6 +419,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
 
        .fixed_amount = true,
 
+       .snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
        .shared_data_off = offsetof(PgStatShared_Wal, stats),
        .shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
@@ -1371,47 +1377,21 @@ pgstat_write_statsfile(void)
    format_id = PGSTAT_FILE_FORMAT_ID;
    write_chunk_s(fpout, &format_id);
 
-   /*
-    * XXX: The following could now be generalized to just iterate over
-    * pgstat_kind_infos instead of knowing about the different kinds of
-    * stats.
-    */
-
-   /*
-    * Write archiver stats struct
-    */
-   pgstat_build_snapshot_fixed(PGSTAT_KIND_ARCHIVER);
-   write_chunk_s(fpout, &pgStatLocal.snapshot.archiver);
-
-   /*
-    * Write bgwriter stats struct
-    */
-   pgstat_build_snapshot_fixed(PGSTAT_KIND_BGWRITER);
-   write_chunk_s(fpout, &pgStatLocal.snapshot.bgwriter);
-
-   /*
-    * Write checkpointer stats struct
-    */
-   pgstat_build_snapshot_fixed(PGSTAT_KIND_CHECKPOINTER);
-   write_chunk_s(fpout, &pgStatLocal.snapshot.checkpointer);
+   /* Write various stats structs for fixed number of objects */
+   for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
+   {
+       char       *ptr;
+       const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
 
-   /*
-    * Write IO stats struct
-    */
-   pgstat_build_snapshot_fixed(PGSTAT_KIND_IO);
-   write_chunk_s(fpout, &pgStatLocal.snapshot.io);
+       if (!info->fixed_amount)
+           continue;
 
-   /*
-    * Write SLRU stats struct
-    */
-   pgstat_build_snapshot_fixed(PGSTAT_KIND_SLRU);
-   write_chunk_s(fpout, &pgStatLocal.snapshot.slru);
+       Assert(info->snapshot_ctl_off != 0);
 
-   /*
-    * Write WAL stats struct
-    */
-   pgstat_build_snapshot_fixed(PGSTAT_KIND_WAL);
-   write_chunk_s(fpout, &pgStatLocal.snapshot.wal);
+       pgstat_build_snapshot_fixed(kind);
+       ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
+       write_chunk(fpout, ptr, info->shared_data_len);
+   }
 
    /*
     * Walk through the stats entries
index e21ef4e2c98173c350f8c3c6a1d6260077d794b4..43d6deb03cf1c5c9874e7c68f081f101e8aad9eb 100644 (file)
@@ -199,6 +199,12 @@ typedef struct PgStat_KindInfo
     */
    uint32      shared_size;
 
+   /*
+    * The offset of the statistics struct in the cached statistics snapshot
+    * PgStat_Snapshot, for fixed-numbered statistics.
+    */
+   uint32      snapshot_ctl_off;
+
    /*
     * The offset of the statistics struct in the containing shared memory
     * control structure PgStat_ShmemControl, for fixed-numbered statistics.