Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b68b29b

Browse files
committed
Use pgstat_kind_infos to write fixed shared statistics
This is similar to 9004abf, 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/Zot5bxoPYdS7yaoy@paquier.xyz
1 parent c048cd9 commit b68b29b

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

src/backend/utils/activity/pgstat.c

+18-38
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
349349

350350
.fixed_amount = true,
351351

352+
.snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
352353
.shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
353354
.shared_data_off = offsetof(PgStatShared_Archiver, stats),
354355
.shared_data_len = sizeof(((PgStatShared_Archiver *) 0)->stats),
@@ -362,6 +363,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
362363

363364
.fixed_amount = true,
364365

366+
.snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
365367
.shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
366368
.shared_data_off = offsetof(PgStatShared_BgWriter, stats),
367369
.shared_data_len = sizeof(((PgStatShared_BgWriter *) 0)->stats),
@@ -375,6 +377,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
375377

376378
.fixed_amount = true,
377379

380+
.snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
378381
.shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
379382
.shared_data_off = offsetof(PgStatShared_Checkpointer, stats),
380383
.shared_data_len = sizeof(((PgStatShared_Checkpointer *) 0)->stats),
@@ -388,6 +391,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
388391

389392
.fixed_amount = true,
390393

394+
.snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
391395
.shared_ctl_off = offsetof(PgStat_ShmemControl, io),
392396
.shared_data_off = offsetof(PgStatShared_IO, stats),
393397
.shared_data_len = sizeof(((PgStatShared_IO *) 0)->stats),
@@ -401,6 +405,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
401405

402406
.fixed_amount = true,
403407

408+
.snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
404409
.shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
405410
.shared_data_off = offsetof(PgStatShared_SLRU, stats),
406411
.shared_data_len = sizeof(((PgStatShared_SLRU *) 0)->stats),
@@ -414,6 +419,7 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
414419

415420
.fixed_amount = true,
416421

422+
.snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
417423
.shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
418424
.shared_data_off = offsetof(PgStatShared_Wal, stats),
419425
.shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
@@ -1371,47 +1377,21 @@ pgstat_write_statsfile(void)
13711377
format_id = PGSTAT_FILE_FORMAT_ID;
13721378
write_chunk_s(fpout, &format_id);
13731379

1374-
/*
1375-
* XXX: The following could now be generalized to just iterate over
1376-
* pgstat_kind_infos instead of knowing about the different kinds of
1377-
* stats.
1378-
*/
1379-
1380-
/*
1381-
* Write archiver stats struct
1382-
*/
1383-
pgstat_build_snapshot_fixed(PGSTAT_KIND_ARCHIVER);
1384-
write_chunk_s(fpout, &pgStatLocal.snapshot.archiver);
1385-
1386-
/*
1387-
* Write bgwriter stats struct
1388-
*/
1389-
pgstat_build_snapshot_fixed(PGSTAT_KIND_BGWRITER);
1390-
write_chunk_s(fpout, &pgStatLocal.snapshot.bgwriter);
1391-
1392-
/*
1393-
* Write checkpointer stats struct
1394-
*/
1395-
pgstat_build_snapshot_fixed(PGSTAT_KIND_CHECKPOINTER);
1396-
write_chunk_s(fpout, &pgStatLocal.snapshot.checkpointer);
1380+
/* Write various stats structs for fixed number of objects */
1381+
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1382+
{
1383+
char *ptr;
1384+
const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
13971385

1398-
/*
1399-
* Write IO stats struct
1400-
*/
1401-
pgstat_build_snapshot_fixed(PGSTAT_KIND_IO);
1402-
write_chunk_s(fpout, &pgStatLocal.snapshot.io);
1386+
if (!info->fixed_amount)
1387+
continue;
14031388

1404-
/*
1405-
* Write SLRU stats struct
1406-
*/
1407-
pgstat_build_snapshot_fixed(PGSTAT_KIND_SLRU);
1408-
write_chunk_s(fpout, &pgStatLocal.snapshot.slru);
1389+
Assert(info->snapshot_ctl_off != 0);
14091390

1410-
/*
1411-
* Write WAL stats struct
1412-
*/
1413-
pgstat_build_snapshot_fixed(PGSTAT_KIND_WAL);
1414-
write_chunk_s(fpout, &pgStatLocal.snapshot.wal);
1391+
pgstat_build_snapshot_fixed(kind);
1392+
ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
1393+
write_chunk(fpout, ptr, info->shared_data_len);
1394+
}
14151395

14161396
/*
14171397
* Walk through the stats entries

src/include/utils/pgstat_internal.h

+6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ typedef struct PgStat_KindInfo
199199
*/
200200
uint32 shared_size;
201201

202+
/*
203+
* The offset of the statistics struct in the cached statistics snapshot
204+
* PgStat_Snapshot, for fixed-numbered statistics.
205+
*/
206+
uint32 snapshot_ctl_off;
207+
202208
/*
203209
* The offset of the statistics struct in the containing shared memory
204210
* control structure PgStat_ShmemControl, for fixed-numbered statistics.

0 commit comments

Comments
 (0)