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

Commit 9004abf

Browse files
committed
Use pgstat_kind_infos to read fixed shared statistics
Shared statistics with a fixed number of objects are read from the stats file in pgstat_read_statsfile() using members of PgStat_ShmemControl and following an order based on their PgStat_Kind value. Instead of being explicit, this commit changes the stats read to iterate over the pgstat_kind_infos array to find the memory locations to read into, based on a new shared_ctl_off in PgStat_KindInfo that can be used to define the position of this stats kind in shared memory. This makes the read logic simpler, and eases the introduction of future improvements aimed at making this area more pluggable for external modules. Original idea suggested by Andres Freund. Author: Tristan Partin Reviewed-by: Andres Freund, Michael Paquier Discussion: https://postgr.es/m/D12SQ7OYCD85.20BUVF3DWU5K7@neon.tech
1 parent a1333ec commit 9004abf

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

src/backend/utils/activity/pgstat.c

+36-38
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
341341

342342
.fixed_amount = true,
343343

344+
.shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
345+
.shared_data_off = offsetof(PgStatShared_Archiver, stats),
346+
.shared_data_len = sizeof(((PgStatShared_Archiver *) 0)->stats),
347+
344348
.reset_all_cb = pgstat_archiver_reset_all_cb,
345349
.snapshot_cb = pgstat_archiver_snapshot_cb,
346350
},
@@ -350,6 +354,10 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
350354

351355
.fixed_amount = true,
352356

357+
.shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
358+
.shared_data_off = offsetof(PgStatShared_BgWriter, stats),
359+
.shared_data_len = sizeof(((PgStatShared_BgWriter *) 0)->stats),
360+
353361
.reset_all_cb = pgstat_bgwriter_reset_all_cb,
354362
.snapshot_cb = pgstat_bgwriter_snapshot_cb,
355363
},
@@ -359,6 +367,10 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
359367

360368
.fixed_amount = true,
361369

370+
.shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
371+
.shared_data_off = offsetof(PgStatShared_Checkpointer, stats),
372+
.shared_data_len = sizeof(((PgStatShared_Checkpointer *) 0)->stats),
373+
362374
.reset_all_cb = pgstat_checkpointer_reset_all_cb,
363375
.snapshot_cb = pgstat_checkpointer_snapshot_cb,
364376
},
@@ -368,6 +380,10 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
368380

369381
.fixed_amount = true,
370382

383+
.shared_ctl_off = offsetof(PgStat_ShmemControl, io),
384+
.shared_data_off = offsetof(PgStatShared_IO, stats),
385+
.shared_data_len = sizeof(((PgStatShared_IO *) 0)->stats),
386+
371387
.reset_all_cb = pgstat_io_reset_all_cb,
372388
.snapshot_cb = pgstat_io_snapshot_cb,
373389
},
@@ -377,6 +393,10 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
377393

378394
.fixed_amount = true,
379395

396+
.shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
397+
.shared_data_off = offsetof(PgStatShared_SLRU, stats),
398+
.shared_data_len = sizeof(((PgStatShared_SLRU *) 0)->stats),
399+
380400
.reset_all_cb = pgstat_slru_reset_all_cb,
381401
.snapshot_cb = pgstat_slru_snapshot_cb,
382402
},
@@ -386,6 +406,10 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
386406

387407
.fixed_amount = true,
388408

409+
.shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
410+
.shared_data_off = offsetof(PgStatShared_Wal, stats),
411+
.shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
412+
389413
.reset_all_cb = pgstat_wal_reset_all_cb,
390414
.snapshot_cb = pgstat_wal_snapshot_cb,
391415
},
@@ -1519,47 +1543,21 @@ pgstat_read_statsfile(void)
15191543
format_id != PGSTAT_FILE_FORMAT_ID)
15201544
goto error;
15211545

1522-
/*
1523-
* XXX: The following could now be generalized to just iterate over
1524-
* pgstat_kind_infos instead of knowing about the different kinds of
1525-
* stats.
1526-
*/
1527-
1528-
/*
1529-
* Read archiver stats struct
1530-
*/
1531-
if (!read_chunk_s(fpin, &shmem->archiver.stats))
1532-
goto error;
1533-
1534-
/*
1535-
* Read bgwriter stats struct
1536-
*/
1537-
if (!read_chunk_s(fpin, &shmem->bgwriter.stats))
1538-
goto error;
1539-
1540-
/*
1541-
* Read checkpointer stats struct
1542-
*/
1543-
if (!read_chunk_s(fpin, &shmem->checkpointer.stats))
1544-
goto error;
1546+
/* Read various stats structs with fixed number of objects */
1547+
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1548+
{
1549+
char *ptr;
1550+
const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
15451551

1546-
/*
1547-
* Read IO stats struct
1548-
*/
1549-
if (!read_chunk_s(fpin, &shmem->io.stats))
1550-
goto error;
1552+
if (!info->fixed_amount)
1553+
continue;
15511554

1552-
/*
1553-
* Read SLRU stats struct
1554-
*/
1555-
if (!read_chunk_s(fpin, &shmem->slru.stats))
1556-
goto error;
1555+
Assert(info->shared_ctl_off != 0);
15571556

1558-
/*
1559-
* Read WAL stats struct
1560-
*/
1561-
if (!read_chunk_s(fpin, &shmem->wal.stats))
1562-
goto error;
1557+
ptr = ((char *) shmem) + info->shared_ctl_off + info->shared_data_off;
1558+
if (!read_chunk(fpin, ptr, info->shared_data_len))
1559+
goto error;
1560+
}
15631561

15641562
/*
15651563
* We found an existing statistics file. Read it and put all the hash

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 containing shared memory
204+
* control structure PgStat_ShmemControl, for fixed-numbered statistics.
205+
*/
206+
uint32 shared_ctl_off;
207+
202208
/*
203209
* The offset/size of statistics inside the shared stats entry. Used when
204210
* [de-]serializing statistics to / from disk respectively. Separate from

0 commit comments

Comments
 (0)