LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_bgwriter.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 100.0 % 36 36 36
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 4 4 4
Baseline: 16@8cea358b128 Branches: 64.3 % 14 9 5 9
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 100.0 % 36 36 36
Function coverage date bins:
(240..) days: 100.0 % 4 4 4
Branch coverage date bins:
(240..) days: 64.3 % 14 9 5 9

 Age         Owner                    Branch data    TLA  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
  739 andres@anarazel.de         30                 :CBC       15299 : pgstat_report_bgwriter(void)
                                 31                 :                : {
                                 32                 :          15299 :     PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
                                 33                 :                :     static const PgStat_BgWriterStats all_zeroes;
                                 34                 :                : 
                                 35         [ -  + ]:          15299 :     Assert(!pgStatLocal.shmem->is_shutdown);
  755                            36                 :          15299 :     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                 :                :      */
  739                            42         [ +  + ]:          15299 :     if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(all_zeroes)) == 0)
  755                            43                 :           6717 :         return;
                                 44                 :                : 
  739                            45                 :           8582 :     pgstat_begin_changecount_write(&stats_shmem->changecount);
                                 46                 :                : 
                                 47                 :                : #define BGWRITER_ACC(fld) stats_shmem->stats.fld += PendingBgWriterStats.fld
                                 48                 :           8582 :     BGWRITER_ACC(buf_written_clean);
                                 49                 :           8582 :     BGWRITER_ACC(maxwritten_clean);
                                 50                 :           8582 :     BGWRITER_ACC(buf_alloc);
                                 51                 :                : #undef BGWRITER_ACC
                                 52                 :                : 
                                 53                 :           8582 :     pgstat_end_changecount_write(&stats_shmem->changecount);
                                 54                 :                : 
                                 55                 :                :     /*
                                 56                 :                :      * Clear out the statistics buffer, so it can be re-used.
                                 57                 :                :      */
  755                            58   [ +  -  +  -  :          42910 :     MemSet(&PendingBgWriterStats, 0, sizeof(PendingBgWriterStats));
                                     +  -  +  -  +  
                                                 + ]
                                 59                 :                : 
                                 60                 :                :     /*
                                 61                 :                :      * Report IO statistics
                                 62                 :                :      */
  431                            63                 :           8582 :     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 *
  739                            71                 :              6 : pgstat_fetch_stat_bgwriter(void)
                                 72                 :                : {
                                 73                 :              6 :     pgstat_snapshot_fixed(PGSTAT_KIND_BGWRITER);
                                 74                 :                : 
                                 75                 :              6 :     return &pgStatLocal.snapshot.bgwriter;
                                 76                 :                : }
                                 77                 :                : 
                                 78                 :                : void
                                 79                 :            246 : pgstat_bgwriter_reset_all_cb(TimestampTz ts)
                                 80                 :                : {
                                 81                 :            246 :     PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
                                 82                 :                : 
                                 83                 :                :     /* see explanation above PgStatShared_BgWriter for the reset protocol */
                                 84                 :            246 :     LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
                                 85                 :            246 :     pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
                                 86                 :            246 :                                     &stats_shmem->stats,
                                 87                 :                :                                     sizeof(stats_shmem->stats),
                                 88                 :                :                                     &stats_shmem->changecount);
                                 89                 :            246 :     stats_shmem->stats.stat_reset_timestamp = ts;
                                 90                 :            246 :     LWLockRelease(&stats_shmem->lock);
                                 91                 :            246 : }
                                 92                 :                : 
                                 93                 :                : void
                                 94                 :            543 : pgstat_bgwriter_snapshot_cb(void)
                                 95                 :                : {
                                 96                 :            543 :     PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter;
                                 97                 :            543 :     PgStat_BgWriterStats *reset_offset = &stats_shmem->reset_offset;
                                 98                 :                :     PgStat_BgWriterStats reset;
                                 99                 :                : 
                                100                 :            543 :     pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.bgwriter,
                                101                 :            543 :                                     &stats_shmem->stats,
                                102                 :                :                                     sizeof(stats_shmem->stats),
                                103                 :                :                                     &stats_shmem->changecount);
                                104                 :                : 
                                105                 :            543 :     LWLockAcquire(&stats_shmem->lock, LW_SHARED);
                                106                 :            543 :     memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
                                107                 :            543 :     LWLockRelease(&stats_shmem->lock);
                                108                 :                : 
                                109                 :                :     /* compensate by reset offsets */
                                110                 :                : #define BGWRITER_COMP(fld) pgStatLocal.snapshot.bgwriter.fld -= reset.fld;
                                111                 :            543 :     BGWRITER_COMP(buf_written_clean);
                                112                 :            543 :     BGWRITER_COMP(maxwritten_clean);
                                113                 :            543 :     BGWRITER_COMP(buf_alloc);
                                114                 :                : #undef BGWRITER_COMP
                                115                 :            543 : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622