LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_checkpointer.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 40 40 100.0 %
Date: 2022-08-17 03:10:30 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -------------------------------------------------------------------------
       2             :  *
       3             :  * pgstat_checkpointer.c
       4             :  *    Implementation of checkpoint statistics.
       5             :  *
       6             :  * This file contains the implementation of checkpoint statistics. It is kept
       7             :  * separate from pgstat.c to enforce the line between the statistics access /
       8             :  * storage implementation and the details about individual types of
       9             :  * statistics.
      10             :  *
      11             :  * Copyright (c) 2001-2022, PostgreSQL Global Development Group
      12             :  *
      13             :  * IDENTIFICATION
      14             :  *    src/backend/utils/activity/pgstat_checkpointer.c
      15             :  * -------------------------------------------------------------------------
      16             :  */
      17             : 
      18             : #include "postgres.h"
      19             : 
      20             : #include "utils/pgstat_internal.h"
      21             : 
      22             : 
      23             : PgStat_CheckpointerStats PendingCheckpointerStats = {0};
      24             : 
      25             : 
      26             : /*
      27             :  * Report checkpointer statistics
      28             :  */
      29             : void
      30       12596 : pgstat_report_checkpointer(void)
      31             : {
      32             :     /* We assume this initializes to zeroes */
      33             :     static const PgStat_CheckpointerStats all_zeroes;
      34       12596 :     PgStatShared_Checkpointer *stats_shmem = &pgStatLocal.shmem->checkpointer;
      35             : 
      36             :     Assert(!pgStatLocal.shmem->is_shutdown);
      37             :     pgstat_assert_is_up();
      38             : 
      39             :     /*
      40             :      * This function can be called even if nothing at all has happened. In
      41             :      * this case, avoid unnecessarily modifying the stats entry.
      42             :      */
      43       12596 :     if (memcmp(&PendingCheckpointerStats, &all_zeroes,
      44             :                sizeof(all_zeroes)) == 0)
      45         988 :         return;
      46             : 
      47       11608 :     pgstat_begin_changecount_write(&stats_shmem->changecount);
      48             : 
      49             : #define CHECKPOINTER_ACC(fld) stats_shmem->stats.fld += PendingCheckpointerStats.fld
      50       11608 :     CHECKPOINTER_ACC(timed_checkpoints);
      51       11608 :     CHECKPOINTER_ACC(requested_checkpoints);
      52       11608 :     CHECKPOINTER_ACC(checkpoint_write_time);
      53       11608 :     CHECKPOINTER_ACC(checkpoint_sync_time);
      54       11608 :     CHECKPOINTER_ACC(buf_written_checkpoints);
      55       11608 :     CHECKPOINTER_ACC(buf_written_backend);
      56       11608 :     CHECKPOINTER_ACC(buf_fsync_backend);
      57             : #undef CHECKPOINTER_ACC
      58             : 
      59       11608 :     pgstat_end_changecount_write(&stats_shmem->changecount);
      60             : 
      61             :     /*
      62             :      * Clear out the statistics buffer, so it can be re-used.
      63             :      */
      64       92864 :     MemSet(&PendingCheckpointerStats, 0, sizeof(PendingCheckpointerStats));
      65             : }
      66             : 
      67             : /*
      68             :  * pgstat_fetch_stat_checkpointer() -
      69             :  *
      70             :  * Support function for the SQL-callable pgstat* functions. Returns
      71             :  * a pointer to the checkpointer statistics struct.
      72             :  */
      73             : PgStat_CheckpointerStats *
      74          28 : pgstat_fetch_stat_checkpointer(void)
      75             : {
      76          28 :     pgstat_snapshot_fixed(PGSTAT_KIND_CHECKPOINTER);
      77             : 
      78          28 :     return &pgStatLocal.snapshot.checkpointer;
      79             : }
      80             : 
      81             : void
      82         818 : pgstat_checkpointer_reset_all_cb(TimestampTz ts)
      83             : {
      84         818 :     PgStatShared_Checkpointer *stats_shmem = &pgStatLocal.shmem->checkpointer;
      85             : 
      86             :     /* see explanation above PgStatShared_Checkpointer for the reset protocol */
      87         818 :     LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
      88         818 :     pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
      89         818 :                                     &stats_shmem->stats,
      90             :                                     sizeof(stats_shmem->stats),
      91             :                                     &stats_shmem->changecount);
      92         818 :     LWLockRelease(&stats_shmem->lock);
      93         818 : }
      94             : 
      95             : void
      96        1830 : pgstat_checkpointer_snapshot_cb(void)
      97             : {
      98        1830 :     PgStatShared_Checkpointer *stats_shmem = &pgStatLocal.shmem->checkpointer;
      99        1830 :     PgStat_CheckpointerStats *reset_offset = &stats_shmem->reset_offset;
     100             :     PgStat_CheckpointerStats reset;
     101             : 
     102        1830 :     pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.checkpointer,
     103        1830 :                                     &stats_shmem->stats,
     104             :                                     sizeof(stats_shmem->stats),
     105             :                                     &stats_shmem->changecount);
     106             : 
     107        1830 :     LWLockAcquire(&stats_shmem->lock, LW_SHARED);
     108        1830 :     memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
     109        1830 :     LWLockRelease(&stats_shmem->lock);
     110             : 
     111             :     /* compensate by reset offsets */
     112             : #define CHECKPOINTER_COMP(fld) pgStatLocal.snapshot.checkpointer.fld -= reset.fld;
     113        1830 :     CHECKPOINTER_COMP(timed_checkpoints);
     114        1830 :     CHECKPOINTER_COMP(requested_checkpoints);
     115        1830 :     CHECKPOINTER_COMP(checkpoint_write_time);
     116        1830 :     CHECKPOINTER_COMP(checkpoint_sync_time);
     117        1830 :     CHECKPOINTER_COMP(buf_written_checkpoints);
     118        1830 :     CHECKPOINTER_COMP(buf_written_backend);
     119        1830 :     CHECKPOINTER_COMP(buf_fsync_backend);
     120             : #undef CHECKPOINTER_COMP
     121        1830 : }

Generated by: LCOV version 1.14