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-2022, 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 statistics 28 : */ 29 : void 30 10478 : pgstat_report_bgwriter(void) 31 : { 32 10478 : 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 10478 : if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(all_zeroes)) == 0) 43 3970 : return; 44 : 45 6508 : pgstat_begin_changecount_write(&stats_shmem->changecount); 46 : 47 : #define BGWRITER_ACC(fld) stats_shmem->stats.fld += PendingBgWriterStats.fld 48 6508 : BGWRITER_ACC(buf_written_clean); 49 6508 : BGWRITER_ACC(maxwritten_clean); 50 6508 : BGWRITER_ACC(buf_alloc); 51 : #undef BGWRITER_ACC 52 : 53 6508 : pgstat_end_changecount_write(&stats_shmem->changecount); 54 : 55 : /* 56 : * Clear out the statistics buffer, so it can be re-used. 57 : */ 58 32540 : MemSet(&PendingBgWriterStats, 0, sizeof(PendingBgWriterStats)); 59 : } 60 : 61 : /* 62 : * Support function for the SQL-callable pgstat* functions. Returns 63 : * a pointer to the bgwriter statistics struct. 64 : */ 65 : PgStat_BgWriterStats * 66 32 : pgstat_fetch_stat_bgwriter(void) 67 : { 68 32 : pgstat_snapshot_fixed(PGSTAT_KIND_BGWRITER); 69 : 70 32 : return &pgStatLocal.snapshot.bgwriter; 71 : } 72 : 73 : void 74 818 : pgstat_bgwriter_reset_all_cb(TimestampTz ts) 75 : { 76 818 : PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter; 77 : 78 : /* see explanation above PgStatShared_BgWriter for the reset protocol */ 79 818 : LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE); 80 818 : pgstat_copy_changecounted_stats(&stats_shmem->reset_offset, 81 818 : &stats_shmem->stats, 82 : sizeof(stats_shmem->stats), 83 : &stats_shmem->changecount); 84 818 : stats_shmem->stats.stat_reset_timestamp = ts; 85 818 : LWLockRelease(&stats_shmem->lock); 86 818 : } 87 : 88 : void 89 1842 : pgstat_bgwriter_snapshot_cb(void) 90 : { 91 1842 : PgStatShared_BgWriter *stats_shmem = &pgStatLocal.shmem->bgwriter; 92 1842 : PgStat_BgWriterStats *reset_offset = &stats_shmem->reset_offset; 93 : PgStat_BgWriterStats reset; 94 : 95 1842 : pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.bgwriter, 96 1842 : &stats_shmem->stats, 97 : sizeof(stats_shmem->stats), 98 : &stats_shmem->changecount); 99 : 100 1842 : LWLockAcquire(&stats_shmem->lock, LW_SHARED); 101 1842 : memcpy(&reset, reset_offset, sizeof(stats_shmem->stats)); 102 1842 : LWLockRelease(&stats_shmem->lock); 103 : 104 : /* compensate by reset offsets */ 105 : #define BGWRITER_COMP(fld) pgStatLocal.snapshot.bgwriter.fld -= reset.fld; 106 1842 : BGWRITER_COMP(buf_written_clean); 107 1842 : BGWRITER_COMP(maxwritten_clean); 108 1842 : BGWRITER_COMP(buf_alloc); 109 : #undef BGWRITER_COMP 110 1842 : }