PostgreSQL Source Code  git master
pgstat_checkpointer.c
Go to the documentation of this file.
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 
24 
25 
26 /*
27  * Report checkpointer and IO statistics
28  */
29 void
31 {
32  /* We assume this initializes to zeroes */
33  static const PgStat_CheckpointerStats all_zeroes;
35 
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  if (memcmp(&PendingCheckpointerStats, &all_zeroes,
44  sizeof(all_zeroes)) == 0)
45  return;
46 
48 
49 #define CHECKPOINTER_ACC(fld) stats_shmem->stats.fld += PendingCheckpointerStats.fld
50  CHECKPOINTER_ACC(num_timed);
51  CHECKPOINTER_ACC(num_requested);
52  CHECKPOINTER_ACC(restartpoints_timed);
53  CHECKPOINTER_ACC(restartpoints_requested);
54  CHECKPOINTER_ACC(restartpoints_performed);
55  CHECKPOINTER_ACC(write_time);
56  CHECKPOINTER_ACC(sync_time);
57  CHECKPOINTER_ACC(buffers_written);
58 #undef CHECKPOINTER_ACC
59 
61 
62  /*
63  * Clear out the statistics buffer, so it can be re-used.
64  */
66 
67  /*
68  * Report IO statistics
69  */
70  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  */
81 {
83 
85 }
86 
87 void
89 {
91 
92  /* see explanation above PgStatShared_Checkpointer for the reset protocol */
93  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
95  &stats_shmem->stats,
96  sizeof(stats_shmem->stats),
97  &stats_shmem->changecount);
98  stats_shmem->stats.stat_reset_timestamp = ts;
99  LWLockRelease(&stats_shmem->lock);
100 }
101 
102 void
104 {
106  PgStat_CheckpointerStats *reset_offset = &stats_shmem->reset_offset;
108 
110  &stats_shmem->stats,
111  sizeof(stats_shmem->stats),
112  &stats_shmem->changecount);
113 
114  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
115  memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
116  LWLockRelease(&stats_shmem->lock);
117 
118  /* compensate by reset offsets */
119 #define CHECKPOINTER_COMP(fld) pgStatLocal.snapshot.checkpointer.fld -= reset.fld;
120  CHECKPOINTER_COMP(num_timed);
121  CHECKPOINTER_COMP(num_requested);
122  CHECKPOINTER_COMP(restartpoints_timed);
123  CHECKPOINTER_COMP(restartpoints_requested);
124  CHECKPOINTER_COMP(restartpoints_performed);
125  CHECKPOINTER_COMP(write_time);
126  CHECKPOINTER_COMP(sync_time);
127  CHECKPOINTER_COMP(buffers_written);
128 #undef CHECKPOINTER_COMP
129 }
#define Assert(condition)
Definition: c.h:858
#define MemSet(start, val, len)
Definition: c.h:1020
int64 TimestampTz
Definition: timestamp.h:39
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:938
PgStat_LocalState pgStatLocal
Definition: pgstat.c:193
@ PGSTAT_KIND_CHECKPOINTER
Definition: pgstat.h:50
void pgstat_checkpointer_snapshot_cb(void)
#define CHECKPOINTER_COMP(fld)
void pgstat_report_checkpointer(void)
void pgstat_checkpointer_reset_all_cb(TimestampTz ts)
PgStat_CheckpointerStats PendingCheckpointerStats
PgStat_CheckpointerStats * pgstat_fetch_stat_checkpointer(void)
#define CHECKPOINTER_ACC(fld)
static void pgstat_end_changecount_write(uint32 *cc)
#define pgstat_assert_is_up()
static void pgstat_begin_changecount_write(uint32 *cc)
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
bool pgstat_flush_io(bool nowait)
Definition: pgstat_io.c:173
void reset(void)
Definition: sql-declare.c:600
PgStat_CheckpointerStats reset_offset
PgStat_CheckpointerStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:271
PgStat_Snapshot snapshot
PgStat_ShmemControl * shmem
PgStatShared_Checkpointer checkpointer
PgStat_CheckpointerStats checkpointer