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-2023, 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 and IO statistics 28 : */ 29 : void 30 13612 : pgstat_report_checkpointer(void) 31 : { 32 : /* We assume this initializes to zeroes */ 33 : static const PgStat_CheckpointerStats all_zeroes; 34 13612 : 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 13612 : if (memcmp(&PendingCheckpointerStats, &all_zeroes, 44 : sizeof(all_zeroes)) == 0) 45 10974 : return; 46 : 47 2638 : pgstat_begin_changecount_write(&stats_shmem->changecount); 48 : 49 : #define CHECKPOINTER_ACC(fld) stats_shmem->stats.fld += PendingCheckpointerStats.fld 50 2638 : CHECKPOINTER_ACC(num_timed); 51 2638 : CHECKPOINTER_ACC(num_requested); 52 2638 : CHECKPOINTER_ACC(write_time); 53 2638 : CHECKPOINTER_ACC(sync_time); 54 2638 : CHECKPOINTER_ACC(buffers_written); 55 : #undef CHECKPOINTER_ACC 56 : 57 2638 : pgstat_end_changecount_write(&stats_shmem->changecount); 58 : 59 : /* 60 : * Clear out the statistics buffer, so it can be re-used. 61 : */ 62 18466 : MemSet(&PendingCheckpointerStats, 0, sizeof(PendingCheckpointerStats)); 63 : 64 : /* 65 : * Report IO statistics 66 : */ 67 2638 : pgstat_flush_io(false); 68 : } 69 : 70 : /* 71 : * pgstat_fetch_stat_checkpointer() - 72 : * 73 : * Support function for the SQL-callable pgstat* functions. Returns 74 : * a pointer to the checkpointer statistics struct. 75 : */ 76 : PgStat_CheckpointerStats * 77 48 : pgstat_fetch_stat_checkpointer(void) 78 : { 79 48 : pgstat_snapshot_fixed(PGSTAT_KIND_CHECKPOINTER); 80 : 81 48 : return &pgStatLocal.snapshot.checkpointer; 82 : } 83 : 84 : void 85 380 : pgstat_checkpointer_reset_all_cb(TimestampTz ts) 86 : { 87 380 : PgStatShared_Checkpointer *stats_shmem = &pgStatLocal.shmem->checkpointer; 88 : 89 : /* see explanation above PgStatShared_Checkpointer for the reset protocol */ 90 380 : LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE); 91 380 : pgstat_copy_changecounted_stats(&stats_shmem->reset_offset, 92 380 : &stats_shmem->stats, 93 : sizeof(stats_shmem->stats), 94 : &stats_shmem->changecount); 95 380 : stats_shmem->stats.stat_reset_timestamp = ts; 96 380 : LWLockRelease(&stats_shmem->lock); 97 380 : } 98 : 99 : void 100 968 : pgstat_checkpointer_snapshot_cb(void) 101 : { 102 968 : PgStatShared_Checkpointer *stats_shmem = &pgStatLocal.shmem->checkpointer; 103 968 : PgStat_CheckpointerStats *reset_offset = &stats_shmem->reset_offset; 104 : PgStat_CheckpointerStats reset; 105 : 106 968 : pgstat_copy_changecounted_stats(&pgStatLocal.snapshot.checkpointer, 107 968 : &stats_shmem->stats, 108 : sizeof(stats_shmem->stats), 109 : &stats_shmem->changecount); 110 : 111 968 : LWLockAcquire(&stats_shmem->lock, LW_SHARED); 112 968 : memcpy(&reset, reset_offset, sizeof(stats_shmem->stats)); 113 968 : LWLockRelease(&stats_shmem->lock); 114 : 115 : /* compensate by reset offsets */ 116 : #define CHECKPOINTER_COMP(fld) pgStatLocal.snapshot.checkpointer.fld -= reset.fld; 117 968 : CHECKPOINTER_COMP(num_timed); 118 968 : CHECKPOINTER_COMP(num_requested); 119 968 : CHECKPOINTER_COMP(write_time); 120 968 : CHECKPOINTER_COMP(sync_time); 121 968 : CHECKPOINTER_COMP(buffers_written); 122 : #undef CHECKPOINTER_COMP 123 968 : }