LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_bgwriter.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 38 38 100.0 %
Date: 2024-10-10 05:14:53 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -------------------------------------------------------------------------
       2             :  *
       3             :  * pgstat_bgwriter.c
       4             :  *    Implementation of bgwriter statistics.
       5             :  *
       6             :  * This file contains the implementation of bgwriter 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-2024, PostgreSQL Global Development Group
      12             :  *
      13             :  * IDENTIFICATION
      14             :  *    src/backend/utils/activity/pgstat_bgwriter.c
      15             :  * -------------------------------------------------------------------------
      16             :  */
      17             : 
      18             : #include "postgres.h"
      19             : 
      20             : #include "utils/pgstat_internal.h"
      21             : 
      22             : 
      23             : PgStat_BgWriterStats PendingBgWriterStats = {0};
      24             : 
      25             : 
      26             : /*
      27             :  * Report bgwriter and IO statistics
      28             :  */
      29             : void
      30       16992 : pgstat_report_bgwriter(void)
      31             : {
      32       16992 :     PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
      33             :     static const PgStat_BgWriterStats all_zeroes;
      34             : 
      35             :     Assert(!pgStatLocal.shmem->is_shutdown);
      36             :     pgstat_assert_is_up();
      37             : 
      38             :     /*
      39             :      * This function can be called even if nothing at all has happened. In
      40             :      * this case, avoid unnecessarily modifying the stats entry.
      41             :      */
      42       16992 :     if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(all_zeroes)) == 0)
      43        7372 :         return;
      44             : 
      45        9620 :     pgstat_begin_changecount_write(&stats_shmem->changecount);
      46             : 
      47             : #define BGWRITER_ACC(fld) stats_shmem->stats.fld += PendingBgWriterStats.fld
      48        9620 :     BGWRITER_ACC(buf_written_clean);
      49        9620 :     BGWRITER_ACC(maxwritten_clean);
      50        9620 :     BGWRITER_ACC(buf_alloc);
      51             : #undef BGWRITER_ACC
      52             : 
      53        9620 :     pgstat_end_changecount_write(&stats_shmem->changecount);
      54             : 
      55             :     /*
      56             :      * Clear out the statistics buffer, so it can be re-used.
      57             :      */
      58       48100 :     MemSet(&PendingBgWriterStats, 0, sizeof(PendingBgWriterStats));
      59             : 
      60             :     /*
      61             :      * Report IO statistics
      62             :      */
      63        9620 :     pgstat_flush_io(false);
      64             : }
      65             : 
      66             : /*
      67             :  * Support function for the SQL-callable pgstat* functions. Returns
      68             :  * a pointer to the bgwriter statistics struct.
      69             :  */
      70             : PgStat_BgWriterStats *
      71          12 : pgstat_fetch_stat_bgwriter(void)
      72             : {
      73          12 :     pgstat_snapshot_fixed(PGSTAT_KIND_BGWRITER);
      74             : 
      75          12 :     return &pgStatLocal.snapshot.bgwriter;
      76             : }
      77             : 
      78             : void
      79        1828 : pgstat_bgwriter_init_shmem_cb(void *stats)
      80             : {
      81        1828 :     PgStatShared_BgWriter *stats_shmem = (PgStatShared_BgWriter *) stats;
      82             : 
      83        1828 :     LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
      84        1828 : }
      85             : 
      86             : void
      87         448 : pgstat_bgwriter_reset_all_cb(TimestampTz ts)
      88             : {
      89         448 :     PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
      90             : 
      91             :     /* see explanation above PgStatShared_BgWriter for the reset protocol */
      92         448 :     LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
      93         448 :     pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
      94         448 :                                     &stats_shmem->stats,
      95             :                                     sizeof(stats_shmem->stats),
      96             :                                     &stats_shmem->changecount);
      97         448 :     stats_shmem->stats.stat_reset_timestamp = ts;
      98         448 :     LWLockRelease(&stats_shmem->lock);
      99         448 : }
     100             : 
     101             : void
     102        1104 : pgstat_bgwriter_snapshot_cb(void)
     103             : {
     104        1104 :     PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
     105        1104 :     PgStat_BgWriterStats *reset_offset = &stats_shmem->reset_offset;
     106             :     PgStat_BgWriterStats reset;
     107             : 
     108        1104 :     pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.bgwriter,
     109        1104 :                                     &stats_shmem->stats,
     110             :                                     sizeof(stats_shmem->stats),
     111             :                                     &stats_shmem->changecount);
     112             : 
     113        1104 :     LWLockAcquire(&stats_shmem->lock, LW_SHARED);
     114        1104 :     memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
     115        1104 :     LWLockRelease(&stats_shmem->lock);
     116             : 
     117             :     /* compensate by reset offsets */
     118             : #define BGWRITER_COMP(fld) pgStatLocal.snapshot.bgwriter.fld -= reset.fld;
     119        1104 :     BGWRITER_COMP(buf_written_clean);
     120        1104 :     BGWRITER_COMP(maxwritten_clean);
     121        1104 :     BGWRITER_COMP(buf_alloc);
     122             : #undef BGWRITER_COMP
     123        1104 : }

Generated by: LCOV version 1.14