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