LCOV - code coverage report
Current view: top level - src/backend/utils/activity - pgstat_bgwriter.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 100.0 % 44 44
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 62.5 % 16 10

             Branch data     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-2026, 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/memutils.h"
      21                 :             : #include "utils/pgstat_internal.h"
      22                 :             : 
      23                 :             : 
      24                 :             : PgStat_BgWriterStats PendingBgWriterStats = {0};
      25                 :             : 
      26                 :             : 
      27                 :             : /*
      28                 :             :  * Report bgwriter and IO statistics
      29                 :             :  */
      30                 :             : void
      31                 :          24 : pgstat_report_bgwriter(void)
      32                 :             : {
      33                 :          24 :         PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
      34                 :             : 
      35         [ +  - ]:          24 :         Assert(!pgStatLocal.shmem->is_shutdown);
      36                 :          24 :         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         [ +  + ]:          24 :         if (pg_memory_is_all_zeros(&PendingBgWriterStats,
      43                 :             :                                                            sizeof(struct PgStat_BgWriterStats)))
      44                 :          11 :                 return;
      45                 :             : 
      46                 :          13 :         pgstat_begin_changecount_write(&stats_shmem->changecount);
      47                 :             : 
      48                 :             : #define BGWRITER_ACC(fld) stats_shmem->stats.fld += PendingBgWriterStats.fld
      49                 :          13 :         BGWRITER_ACC(buf_written_clean);
      50                 :          13 :         BGWRITER_ACC(maxwritten_clean);
      51                 :          13 :         BGWRITER_ACC(buf_alloc);
      52                 :             : #undef BGWRITER_ACC
      53                 :             : 
      54                 :          13 :         pgstat_end_changecount_write(&stats_shmem->changecount);
      55                 :             : 
      56                 :             :         /*
      57                 :             :          * Clear out the statistics buffer, so it can be re-used.
      58                 :             :          */
      59   [ +  -  +  -  :          65 :         MemSet(&PendingBgWriterStats, 0, sizeof(PendingBgWriterStats));
          +  -  -  +  +  
                      + ]
      60                 :             : 
      61                 :             :         /*
      62                 :             :          * Report IO statistics
      63                 :             :          */
      64                 :          13 :         pgstat_flush_io(false);
      65         [ -  + ]:          24 : }
      66                 :             : 
      67                 :             : /*
      68                 :             :  * Support function for the SQL-callable pgstat* functions. Returns
      69                 :             :  * a pointer to the bgwriter statistics struct.
      70                 :             :  */
      71                 :             : PgStat_BgWriterStats *
      72                 :           2 : pgstat_fetch_stat_bgwriter(void)
      73                 :             : {
      74                 :           2 :         pgstat_snapshot_fixed(PGSTAT_KIND_BGWRITER);
      75                 :             : 
      76                 :           2 :         return &pgStatLocal.snapshot.bgwriter;
      77                 :             : }
      78                 :             : 
      79                 :             : void
      80                 :           6 : pgstat_bgwriter_init_shmem_cb(void *stats)
      81                 :             : {
      82                 :           6 :         PgStatShared_BgWriter *stats_shmem = (PgStatShared_BgWriter *) stats;
      83                 :             : 
      84                 :           6 :         LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
      85                 :           6 : }
      86                 :             : 
      87                 :             : void
      88                 :           2 : pgstat_bgwriter_reset_all_cb(TimestampTz ts)
      89                 :             : {
      90                 :           2 :         PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
      91                 :             : 
      92                 :             :         /* see explanation above PgStatShared_BgWriter for the reset protocol */
      93                 :           2 :         LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
      94                 :           4 :         pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
      95                 :           2 :                                                                         &stats_shmem->stats,
      96                 :             :                                                                         sizeof(stats_shmem->stats),
      97                 :           2 :                                                                         &stats_shmem->changecount);
      98                 :           2 :         stats_shmem->stats.stat_reset_timestamp = ts;
      99                 :           2 :         LWLockRelease(&stats_shmem->lock);
     100                 :           2 : }
     101                 :             : 
     102                 :             : void
     103                 :           9 : pgstat_bgwriter_snapshot_cb(void)
     104                 :             : {
     105                 :           9 :         PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
     106                 :           9 :         PgStat_BgWriterStats *reset_offset = &stats_shmem->reset_offset;
     107                 :           9 :         PgStat_BgWriterStats reset;
     108                 :             : 
     109                 :           9 :         pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.bgwriter,
     110                 :           9 :                                                                         &stats_shmem->stats,
     111                 :             :                                                                         sizeof(stats_shmem->stats),
     112                 :           9 :                                                                         &stats_shmem->changecount);
     113                 :             : 
     114                 :           9 :         LWLockAcquire(&stats_shmem->lock, LW_SHARED);
     115                 :           9 :         memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
     116                 :           9 :         LWLockRelease(&stats_shmem->lock);
     117                 :             : 
     118                 :             :         /* compensate by reset offsets */
     119                 :             : #define BGWRITER_COMP(fld) pgStatLocal.snapshot.bgwriter.fld -= reset.fld;
     120                 :           9 :         BGWRITER_COMP(buf_written_clean);
     121                 :           9 :         BGWRITER_COMP(maxwritten_clean);
     122                 :           9 :         BGWRITER_COMP(buf_alloc);
     123                 :             : #undef BGWRITER_COMP
     124                 :           9 : }
        

Generated by: LCOV version 2.3.2-1