PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pgstat_slru.c File Reference
#include "postgres.h"
#include "utils/pgstat_internal.h"
#include "utils/timestamp.h"
Include dependency graph for pgstat_slru.c:

Go to the source code of this file.

Macros

#define PGSTAT_COUNT_SLRU(stat)
 
#define SLRU_ACC(fld)   sharedent->fld += pendingent->fld
 

Functions

static PgStat_SLRUStatsget_slru_entry (int slru_idx)
 
static void pgstat_reset_slru_counter_internal (int index, TimestampTz ts)
 
void pgstat_reset_slru (const char *name)
 
PgStat_SLRUStatspgstat_fetch_slru (void)
 
const char * pgstat_get_slru_name (int slru_idx)
 
int pgstat_get_slru_index (const char *name)
 
bool pgstat_slru_flush_cb (bool nowait)
 
void pgstat_slru_init_shmem_cb (void *stats)
 
void pgstat_slru_reset_all_cb (TimestampTz ts)
 
void pgstat_slru_snapshot_cb (void)
 

Variables

static PgStat_SLRUStats pending_SLRUStats [SLRU_NUM_ELEMENTS]
 
static bool have_slrustats = false
 

Macro Definition Documentation

◆ PGSTAT_COUNT_SLRU

#define PGSTAT_COUNT_SLRU (   stat)
Value:
CppConcat(pgstat_count_slru_,stat)(int slru_idx) \
{ \
get_slru_entry(slru_idx)->stat += 1; \
}
#define CppConcat(x, y)
Definition: c.h:419

Definition at line 58 of file pgstat_slru.c.

◆ SLRU_ACC

#define SLRU_ACC (   fld)    sharedent->fld += pendingent->fld

Function Documentation

◆ get_slru_entry()

static PgStat_SLRUStats * get_slru_entry ( int  slru_idx)
inlinestatic

Definition at line 214 of file pgstat_slru.c.

215{
217
218 /*
219 * The postmaster should never register any SLRU statistics counts; if it
220 * did, the counts would be duplicated into child processes via fork().
221 */
223
224 Assert((slru_idx >= 0) && (slru_idx < SLRU_NUM_ELEMENTS));
225
226 have_slrustats = true;
227 pgstat_report_fixed = true;
228
229 return &pending_SLRUStats[slru_idx];
230}
bool IsUnderPostmaster
Definition: globals.c:120
bool IsPostmasterEnvironment
Definition: globals.c:119
Assert(PointerIsAligned(start, uint64))
bool pgstat_report_fixed
Definition: pgstat.c:218
#define pgstat_assert_is_up()
#define SLRU_NUM_ELEMENTS
static bool have_slrustats
Definition: pgstat_slru.c:35
static PgStat_SLRUStats pending_SLRUStats[SLRU_NUM_ELEMENTS]
Definition: pgstat_slru.c:34

References Assert(), have_slrustats, IsPostmasterEnvironment, IsUnderPostmaster, pending_SLRUStats, pgstat_assert_is_up, pgstat_report_fixed, and SLRU_NUM_ELEMENTS.

◆ pgstat_fetch_slru()

PgStat_SLRUStats * pgstat_fetch_slru ( void  )

Definition at line 91 of file pgstat_slru.c.

92{
94
96}
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1060
PgStat_LocalState pgStatLocal
Definition: pgstat.c:212
#define PGSTAT_KIND_SLRU
Definition: pgstat_kind.h:39
PgStat_Snapshot snapshot
PgStat_SLRUStats slru[SLRU_NUM_ELEMENTS]

References PGSTAT_KIND_SLRU, pgstat_snapshot_fixed(), pgStatLocal, PgStat_Snapshot::slru, and PgStat_LocalState::snapshot.

Referenced by pg_stat_get_slru().

◆ pgstat_get_slru_index()

int pgstat_get_slru_index ( const char *  name)

Definition at line 118 of file pgstat_slru.c.

119{
120 int i;
121
122 for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
123 {
124 if (strcmp(slru_names[i], name) == 0)
125 return i;
126 }
127
128 /* return index of the last entry (which is the "other" one) */
129 return (SLRU_NUM_ELEMENTS - 1);
130}
int i
Definition: isn.c:77
static const char *const slru_names[]
const char * name

References i, name, slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pgstat_reset_slru(), and SimpleLruInit().

◆ pgstat_get_slru_name()

const char * pgstat_get_slru_name ( int  slru_idx)

Definition at line 104 of file pgstat_slru.c.

105{
106 if (slru_idx < 0 || slru_idx >= SLRU_NUM_ELEMENTS)
107 return NULL;
108
109 return slru_names[slru_idx];
110}

References slru_names, and SLRU_NUM_ELEMENTS.

Referenced by pg_stat_get_slru().

◆ pgstat_reset_slru()

void pgstat_reset_slru ( const char *  name)

Definition at line 45 of file pgstat_slru.c.

46{
48
49 Assert(name != NULL);
50
52}
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
int64 TimestampTz
Definition: timestamp.h:39
int pgstat_get_slru_index(const char *name)
Definition: pgstat_slru.c:118
static void pgstat_reset_slru_counter_internal(int index, TimestampTz ts)
Definition: pgstat_slru.c:233

References Assert(), GetCurrentTimestamp(), name, pgstat_get_slru_index(), and pgstat_reset_slru_counter_internal().

Referenced by pg_stat_reset_slru().

◆ pgstat_reset_slru_counter_internal()

static void pgstat_reset_slru_counter_internal ( int  index,
TimestampTz  ts 
)
static

Definition at line 233 of file pgstat_slru.c.

234{
235 PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
236
237 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
238
239 memset(&stats_shmem->stats[index], 0, sizeof(PgStat_SLRUStats));
240 stats_shmem->stats[index].stat_reset_timestamp = ts;
241
242 LWLockRelease(&stats_shmem->lock);
243}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_EXCLUSIVE
Definition: lwlock.h:112
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStat_ShmemControl * shmem
TimestampTz stat_reset_timestamp
Definition: pgstat.h:413
PgStatShared_SLRU slru
Definition: type.h:96

References PgStatShared_SLRU::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_ShmemControl::slru, PgStat_SLRUStats::stat_reset_timestamp, and PgStatShared_SLRU::stats.

Referenced by pgstat_reset_slru(), and pgstat_slru_reset_all_cb().

◆ pgstat_slru_flush_cb()

bool pgstat_slru_flush_cb ( bool  nowait)

Definition at line 142 of file pgstat_slru.c.

143{
144 PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
145 int i;
146
147 if (!have_slrustats)
148 return false;
149
150 if (!nowait)
151 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
152 else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
153 return true;
154
155 for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
156 {
157 PgStat_SLRUStats *sharedent = &stats_shmem->stats[i];
158 PgStat_SLRUStats *pendingent = &pending_SLRUStats[i];
159
160#define SLRU_ACC(fld) sharedent->fld += pendingent->fld
161 SLRU_ACC(blocks_zeroed);
162 SLRU_ACC(blocks_hit);
163 SLRU_ACC(blocks_read);
165 SLRU_ACC(blocks_exists);
166 SLRU_ACC(flush);
167 SLRU_ACC(truncate);
168#undef SLRU_ACC
169 }
170
171 /* done, clear the pending entry */
173
174 LWLockRelease(&stats_shmem->lock);
175
176 have_slrustats = false;
177
178 return false;
179}
#define MemSet(start, val, len)
Definition: c.h:1019
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1345
static int64 blocks_written
Definition: pg_checksums.c:38
#define SLRU_ACC(fld)

References blocks_written, have_slrustats, i, PgStatShared_SLRU::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), MemSet, pending_SLRUStats, pgStatLocal, PgStat_LocalState::shmem, PgStat_ShmemControl::slru, SLRU_ACC, SLRU_NUM_ELEMENTS, and PgStatShared_SLRU::stats.

◆ pgstat_slru_init_shmem_cb()

void pgstat_slru_init_shmem_cb ( void *  stats)

Definition at line 182 of file pgstat_slru.c.

183{
184 PgStatShared_SLRU *stats_shmem = (PgStatShared_SLRU *) stats;
185
186 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
187}
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:698

References PgStatShared_SLRU::lock, and LWLockInitialize().

◆ pgstat_slru_reset_all_cb()

void pgstat_slru_reset_all_cb ( TimestampTz  ts)

Definition at line 190 of file pgstat_slru.c.

191{
192 for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
194}

References i, pgstat_reset_slru_counter_internal(), and SLRU_NUM_ELEMENTS.

◆ pgstat_slru_snapshot_cb()

void pgstat_slru_snapshot_cb ( void  )

Definition at line 197 of file pgstat_slru.c.

198{
199 PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
200
201 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
202
203 memcpy(pgStatLocal.snapshot.slru, &stats_shmem->stats,
204 sizeof(stats_shmem->stats));
205
206 LWLockRelease(&stats_shmem->lock);
207}
@ LW_SHARED
Definition: lwlock.h:113

References PgStatShared_SLRU::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_ShmemControl::slru, PgStat_Snapshot::slru, PgStat_LocalState::snapshot, and PgStatShared_SLRU::stats.

Variable Documentation

◆ have_slrustats

bool have_slrustats = false
static

Definition at line 35 of file pgstat_slru.c.

Referenced by get_slru_entry(), and pgstat_slru_flush_cb().

◆ pending_SLRUStats

PgStat_SLRUStats pending_SLRUStats[SLRU_NUM_ELEMENTS]
static

Definition at line 34 of file pgstat_slru.c.

Referenced by get_slru_entry(), and pgstat_slru_flush_cb().