PostgreSQL Source Code  git master
pgstat_bgwriter.c
Go to the documentation of this file.
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 
24 
25 
26 /*
27  * Report bgwriter and IO statistics
28  */
29 void
31 {
33  static const PgStat_BgWriterStats all_zeroes;
34 
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  if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(all_zeroes)) == 0)
43  return;
44 
46 
47 #define BGWRITER_ACC(fld) stats_shmem->stats.fld += PendingBgWriterStats.fld
48  BGWRITER_ACC(buf_written_clean);
49  BGWRITER_ACC(maxwritten_clean);
50  BGWRITER_ACC(buf_alloc);
51 #undef BGWRITER_ACC
52 
54 
55  /*
56  * Clear out the statistics buffer, so it can be re-used.
57  */
59 
60  /*
61  * Report IO statistics
62  */
63  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  */
72 {
74 
76 }
77 
78 void
80 {
82 
83  /* see explanation above PgStatShared_BgWriter for the reset protocol */
84  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
86  &stats_shmem->stats,
87  sizeof(stats_shmem->stats),
88  &stats_shmem->changecount);
89  stats_shmem->stats.stat_reset_timestamp = ts;
90  LWLockRelease(&stats_shmem->lock);
91 }
92 
93 void
95 {
97  PgStat_BgWriterStats *reset_offset = &stats_shmem->reset_offset;
99 
101  &stats_shmem->stats,
102  sizeof(stats_shmem->stats),
103  &stats_shmem->changecount);
104 
105  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
106  memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
107  LWLockRelease(&stats_shmem->lock);
108 
109  /* compensate by reset offsets */
110 #define BGWRITER_COMP(fld) pgStatLocal.snapshot.bgwriter.fld -= reset.fld;
111  BGWRITER_COMP(buf_written_clean);
112  BGWRITER_COMP(maxwritten_clean);
113  BGWRITER_COMP(buf_alloc);
114 #undef BGWRITER_COMP
115 }
#define MemSet(start, val, len)
Definition: c.h:1007
int64 TimestampTz
Definition: timestamp.h:39
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1169
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1782
@ 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_BGWRITER
Definition: pgstat.h:49
PgStat_BgWriterStats * pgstat_fetch_stat_bgwriter(void)
#define BGWRITER_ACC(fld)
PgStat_BgWriterStats PendingBgWriterStats
void pgstat_bgwriter_reset_all_cb(TimestampTz ts)
#define BGWRITER_COMP(fld)
void pgstat_bgwriter_snapshot_cb(void)
void pgstat_report_bgwriter(void)
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_BgWriterStats reset_offset
PgStat_BgWriterStats stats
TimestampTz stat_reset_timestamp
Definition: pgstat.h:258
PgStat_Snapshot snapshot
PgStat_ShmemControl * shmem
PgStatShared_BgWriter bgwriter
PgStat_BgWriterStats bgwriter