PostgreSQL Source Code  git master
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 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)
 
void pgstat_count_slru_page_zeroed (int slru_idx)
 
void pgstat_count_slru_page_hit (int slru_idx)
 
void pgstat_count_slru_page_exists (int slru_idx)
 
void pgstat_count_slru_page_read (int slru_idx)
 
void pgstat_count_slru_page_written (int slru_idx)
 
void pgstat_count_slru_flush (int slru_idx)
 
void pgstat_count_slru_truncate (int slru_idx)
 
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 (bool nowait)
 
void pgstat_slru_reset_all_cb (TimestampTz ts)
 
void pgstat_slru_snapshot_cb (void)
 

Variables

static PgStat_SLRUStats pending_SLRUStats [SLRU_NUM_ELEMENTS]
 
bool have_slrustats = false
 

Macro Definition Documentation

◆ 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 222 of file pgstat_slru.c.

223 {
225 
226  /*
227  * The postmaster should never register any SLRU statistics counts; if it
228  * did, the counts would be duplicated into child processes via fork().
229  */
231 
232  Assert((slru_idx >= 0) && (slru_idx < SLRU_NUM_ELEMENTS));
233 
234  have_slrustats = true;
235 
236  return &pending_SLRUStats[slru_idx];
237 }
bool IsUnderPostmaster
Definition: globals.c:113
bool IsPostmasterEnvironment
Definition: globals.c:112
Assert(fmt[strlen(fmt) - 1] !='\n')
#define pgstat_assert_is_up()
#define SLRU_NUM_ELEMENTS
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, and SLRU_NUM_ELEMENTS.

Referenced by pgstat_count_slru_flush(), pgstat_count_slru_page_exists(), pgstat_count_slru_page_hit(), pgstat_count_slru_page_read(), pgstat_count_slru_page_written(), pgstat_count_slru_page_zeroed(), and pgstat_count_slru_truncate().

◆ pgstat_count_slru_flush()

void pgstat_count_slru_flush ( int  slru_idx)

Definition at line 89 of file pgstat_slru.c.

90 {
91  get_slru_entry(slru_idx)->flush += 1;
92 }
static PgStat_SLRUStats * get_slru_entry(int slru_idx)
Definition: pgstat_slru.c:222
PgStat_Counter flush
Definition: pgstat.h:343

References PgStat_SLRUStats::flush, and get_slru_entry().

Referenced by SimpleLruWriteAll().

◆ pgstat_count_slru_page_exists()

void pgstat_count_slru_page_exists ( int  slru_idx)

Definition at line 71 of file pgstat_slru.c.

72 {
73  get_slru_entry(slru_idx)->blocks_exists += 1;
74 }
PgStat_Counter blocks_exists
Definition: pgstat.h:342

References PgStat_SLRUStats::blocks_exists, and get_slru_entry().

Referenced by SimpleLruDoesPhysicalPageExist().

◆ pgstat_count_slru_page_hit()

void pgstat_count_slru_page_hit ( int  slru_idx)

Definition at line 65 of file pgstat_slru.c.

66 {
67  get_slru_entry(slru_idx)->blocks_hit += 1;
68 }
PgStat_Counter blocks_hit
Definition: pgstat.h:339

References PgStat_SLRUStats::blocks_hit, and get_slru_entry().

Referenced by SimpleLruReadPage(), and SimpleLruReadPage_ReadOnly().

◆ pgstat_count_slru_page_read()

void pgstat_count_slru_page_read ( int  slru_idx)

Definition at line 77 of file pgstat_slru.c.

78 {
79  get_slru_entry(slru_idx)->blocks_read += 1;
80 }
PgStat_Counter blocks_read
Definition: pgstat.h:340

References PgStat_SLRUStats::blocks_read, and get_slru_entry().

Referenced by SimpleLruReadPage().

◆ pgstat_count_slru_page_written()

void pgstat_count_slru_page_written ( int  slru_idx)

Definition at line 83 of file pgstat_slru.c.

84 {
85  get_slru_entry(slru_idx)->blocks_written += 1;
86 }
PgStat_Counter blocks_written
Definition: pgstat.h:341

References PgStat_SLRUStats::blocks_written, and get_slru_entry().

Referenced by SlruPhysicalWritePage().

◆ pgstat_count_slru_page_zeroed()

void pgstat_count_slru_page_zeroed ( int  slru_idx)

Definition at line 59 of file pgstat_slru.c.

60 {
61  get_slru_entry(slru_idx)->blocks_zeroed += 1;
62 }
PgStat_Counter blocks_zeroed
Definition: pgstat.h:338

References PgStat_SLRUStats::blocks_zeroed, and get_slru_entry().

Referenced by SimpleLruZeroPage().

◆ pgstat_count_slru_truncate()

void pgstat_count_slru_truncate ( int  slru_idx)

Definition at line 95 of file pgstat_slru.c.

96 {
97  get_slru_entry(slru_idx)->truncate += 1;
98 }
PgStat_Counter truncate
Definition: pgstat.h:344

References get_slru_entry(), and PgStat_SLRUStats::truncate.

Referenced by SimpleLruTruncate().

◆ pgstat_fetch_slru()

PgStat_SLRUStats* pgstat_fetch_slru ( void  )

Definition at line 105 of file pgstat_slru.c.

106 {
108 
109  return pgStatLocal.snapshot.slru;
110 }
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:899
PgStat_LocalState pgStatLocal
Definition: pgstat.c:195
@ PGSTAT_KIND_SLRU
Definition: pgstat.h:51
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 132 of file pgstat_slru.c.

133 {
134  int i;
135 
136  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
137  {
138  if (strcmp(slru_names[i], name) == 0)
139  return i;
140  }
141 
142  /* return index of the last entry (which is the "other" one) */
143  return (SLRU_NUM_ELEMENTS - 1);
144 }
const char * name
Definition: encode.c:561
int i
Definition: isn.c:73
static const char *const slru_names[]

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 118 of file pgstat_slru.c.

119 {
120  if (slru_idx < 0 || slru_idx >= SLRU_NUM_ELEMENTS)
121  return NULL;
122 
123  return slru_names[slru_idx];
124 }

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  AssertArg(name != NULL);
50 
52 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
#define AssertArg(condition)
Definition: c.h:806
int64 TimestampTz
Definition: timestamp.h:39
int pgstat_get_slru_index(const char *name)
Definition: pgstat_slru.c:132
static void pgstat_reset_slru_counter_internal(int index, TimestampTz ts)
Definition: pgstat_slru.c:240

References AssertArg, 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 240 of file pgstat_slru.c.

241 {
242  PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
243 
244  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
245 
246  memset(&stats_shmem->stats[index], 0, sizeof(PgStat_SLRUStats));
247  stats_shmem->stats[index].stat_reset_timestamp = ts;
248 
249  LWLockRelease(&stats_shmem->lock);
250 }
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1196
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
@ LW_EXCLUSIVE
Definition: lwlock.h:104
PgStat_SLRUStats stats[SLRU_NUM_ELEMENTS]
PgStat_ShmemControl * shmem
TimestampTz stat_reset_timestamp
Definition: pgstat.h:345
PgStatShared_SLRU slru
Definition: type.h:90

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()

bool pgstat_slru_flush ( bool  nowait)

Definition at line 158 of file pgstat_slru.c.

159 {
160  PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
161  int i;
162 
163  if (!have_slrustats)
164  return false;
165 
166  if (!nowait)
167  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
168  else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
169  return true;
170 
171  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
172  {
173  PgStat_SLRUStats *sharedent = &stats_shmem->stats[i];
174  PgStat_SLRUStats *pendingent = &pending_SLRUStats[i];
175 
176 #define SLRU_ACC(fld) sharedent->fld += pendingent->fld
177  SLRU_ACC(blocks_zeroed);
178  SLRU_ACC(blocks_hit);
179  SLRU_ACC(blocks_read);
181  SLRU_ACC(blocks_exists);
182  SLRU_ACC(flush);
183  SLRU_ACC(truncate);
184 #undef SLRU_ACC
185  }
186 
187  /* done, clear the pending entry */
189 
190  LWLockRelease(&stats_shmem->lock);
191 
192  have_slrustats = false;
193 
194  return false;
195 }
#define MemSet(start, val, len)
Definition: c.h:1008
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1367
static int64 blocks_written
Definition: pg_checksums.c:39
#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.

Referenced by pgstat_report_stat().

◆ pgstat_slru_reset_all_cb()

void pgstat_slru_reset_all_cb ( TimestampTz  ts)

Definition at line 198 of file pgstat_slru.c.

199 {
200  for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
202 }

References i, pgstat_reset_slru_counter_internal(), and SLRU_NUM_ELEMENTS.

◆ pgstat_slru_snapshot_cb()

void pgstat_slru_snapshot_cb ( void  )

Definition at line 205 of file pgstat_slru.c.

206 {
207  PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
208 
209  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
210 
211  memcpy(pgStatLocal.snapshot.slru, &stats_shmem->stats,
212  sizeof(stats_shmem->stats));
213 
214  LWLockRelease(&stats_shmem->lock);
215 }
@ LW_SHARED
Definition: lwlock.h:105

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

Definition at line 35 of file pgstat_slru.c.

Referenced by get_slru_entry(), pgstat_report_stat(), and pgstat_slru_flush().

◆ 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().