PostgreSQL Source Code  git master
wait_event.h File Reference
#include "utils/wait_event_types.h"
Include dependency graph for wait_event.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PG_WAIT_LWLOCK   0x01000000U
 
#define PG_WAIT_LOCK   0x03000000U
 
#define PG_WAIT_BUFFERPIN   0x04000000U
 
#define PG_WAIT_ACTIVITY   0x05000000U
 
#define PG_WAIT_CLIENT   0x06000000U
 
#define PG_WAIT_EXTENSION   0x07000000U
 
#define PG_WAIT_IPC   0x08000000U
 
#define PG_WAIT_TIMEOUT   0x09000000U
 
#define PG_WAIT_IO   0x0A000000U
 

Functions

const char * pgstat_get_wait_event (uint32 wait_event_info)
 
const char * pgstat_get_wait_event_type (uint32 wait_event_info)
 
static void pgstat_report_wait_start (uint32 wait_event_info)
 
static void pgstat_report_wait_end (void)
 
void pgstat_set_wait_event_storage (uint32 *wait_event_info)
 
void pgstat_reset_wait_event_storage (void)
 
void WaitEventExtensionShmemInit (void)
 
Size WaitEventExtensionShmemSize (void)
 
uint32 WaitEventExtensionNew (const char *wait_event_name)
 
char ** GetWaitEventExtensionNames (int *nwaitevents)
 

Variables

PGDLLIMPORT uint32my_wait_event_info
 

Macro Definition Documentation

◆ PG_WAIT_ACTIVITY

#define PG_WAIT_ACTIVITY   0x05000000U

Definition at line 21 of file wait_event.h.

◆ PG_WAIT_BUFFERPIN

#define PG_WAIT_BUFFERPIN   0x04000000U

Definition at line 20 of file wait_event.h.

◆ PG_WAIT_CLIENT

#define PG_WAIT_CLIENT   0x06000000U

Definition at line 22 of file wait_event.h.

◆ PG_WAIT_EXTENSION

#define PG_WAIT_EXTENSION   0x07000000U

Definition at line 23 of file wait_event.h.

◆ PG_WAIT_IO

#define PG_WAIT_IO   0x0A000000U

Definition at line 26 of file wait_event.h.

◆ PG_WAIT_IPC

#define PG_WAIT_IPC   0x08000000U

Definition at line 24 of file wait_event.h.

◆ PG_WAIT_LOCK

#define PG_WAIT_LOCK   0x03000000U

Definition at line 19 of file wait_event.h.

◆ PG_WAIT_LWLOCK

#define PG_WAIT_LWLOCK   0x01000000U

Definition at line 18 of file wait_event.h.

◆ PG_WAIT_TIMEOUT

#define PG_WAIT_TIMEOUT   0x09000000U

Definition at line 25 of file wait_event.h.

Function Documentation

◆ GetWaitEventExtensionNames()

char** GetWaitEventExtensionNames ( int *  nwaitevents)

Definition at line 270 of file wait_event.c.

271 {
272  char **waiteventnames;
274  HASH_SEQ_STATUS hash_seq;
275  int index;
276  int els;
277 
278  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
279 
280  /* Now we can safely count the number of entries */
282 
283  /* Allocate enough space for all entries */
284  waiteventnames = palloc(els * sizeof(char *));
285 
286  /* Now scan the hash table to copy the data */
288 
289  index = 0;
290  while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL)
291  {
292  waiteventnames[index] = pstrdup(hentry->wait_event_name);
293  index++;
294  }
295 
296  LWLockRelease(WaitEventExtensionLock);
297 
298  Assert(index == els);
299 
300  *nwaitevents = index;
301  return waiteventnames;
302 }
#define Assert(condition)
Definition: c.h:858
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1341
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1395
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
@ LW_SHARED
Definition: lwlock.h:115
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void * palloc(Size size)
Definition: mcxt.c:1316
char wait_event_name[NAMEDATALEN]
Definition: wait_event.c:76
Definition: type.h:95
static HTAB * WaitEventExtensionHashByName
Definition: wait_event.c:62

References Assert, hash_get_num_entries(), hash_seq_init(), hash_seq_search(), LW_SHARED, LWLockAcquire(), LWLockRelease(), palloc(), pstrdup(), WaitEventExtensionEntryByName::wait_event_name, and WaitEventExtensionHashByName.

Referenced by pg_get_wait_events().

◆ pgstat_get_wait_event()

const char* pgstat_get_wait_event ( uint32  wait_event_info)

Definition at line 392 of file wait_event.c.

393 {
394  uint32 classId;
395  uint16 eventId;
396  const char *event_name;
397 
398  /* report process as not waiting. */
399  if (wait_event_info == 0)
400  return NULL;
401 
402  classId = wait_event_info & WAIT_EVENT_CLASS_MASK;
403  eventId = wait_event_info & WAIT_EVENT_ID_MASK;
404 
405  switch (classId)
406  {
407  case PG_WAIT_LWLOCK:
408  event_name = GetLWLockIdentifier(classId, eventId);
409  break;
410  case PG_WAIT_LOCK:
411  event_name = GetLockNameFromTagType(eventId);
412  break;
413  case PG_WAIT_EXTENSION:
414  event_name = GetWaitEventExtensionIdentifier(eventId);
415  break;
416  case PG_WAIT_BUFFERPIN:
417  {
418  WaitEventBufferPin w = (WaitEventBufferPin) wait_event_info;
419 
420  event_name = pgstat_get_wait_bufferpin(w);
421  break;
422  }
423  case PG_WAIT_ACTIVITY:
424  {
425  WaitEventActivity w = (WaitEventActivity) wait_event_info;
426 
427  event_name = pgstat_get_wait_activity(w);
428  break;
429  }
430  case PG_WAIT_CLIENT:
431  {
432  WaitEventClient w = (WaitEventClient) wait_event_info;
433 
434  event_name = pgstat_get_wait_client(w);
435  break;
436  }
437  case PG_WAIT_IPC:
438  {
439  WaitEventIPC w = (WaitEventIPC) wait_event_info;
440 
441  event_name = pgstat_get_wait_ipc(w);
442  break;
443  }
444  case PG_WAIT_TIMEOUT:
445  {
446  WaitEventTimeout w = (WaitEventTimeout) wait_event_info;
447 
448  event_name = pgstat_get_wait_timeout(w);
449  break;
450  }
451  case PG_WAIT_IO:
452  {
453  WaitEventIO w = (WaitEventIO) wait_event_info;
454 
455  event_name = pgstat_get_wait_io(w);
456  break;
457  }
458  default:
459  event_name = "unknown wait event";
460  break;
461  }
462 
463  return event_name;
464 }
unsigned short uint16
Definition: c.h:505
unsigned int uint32
Definition: c.h:506
const char * GetLockNameFromTagType(uint16 locktag_type)
Definition: lmgr.c:1340
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:769
static const char * pgstat_get_wait_ipc(WaitEventIPC w)
static const char * pgstat_get_wait_io(WaitEventIO w)
static const char * pgstat_get_wait_bufferpin(WaitEventBufferPin w)
static const char * pgstat_get_wait_timeout(WaitEventTimeout w)
static const char * pgstat_get_wait_client(WaitEventClient w)
#define WAIT_EVENT_ID_MASK
Definition: wait_event.c:44
#define WAIT_EVENT_CLASS_MASK
Definition: wait_event.c:43
static const char * GetWaitEventExtensionIdentifier(uint16 eventId)
Definition: wait_event.c:240
static const char * pgstat_get_wait_activity(WaitEventActivity w)
#define PG_WAIT_TIMEOUT
Definition: wait_event.h:25
#define PG_WAIT_LWLOCK
Definition: wait_event.h:18
#define PG_WAIT_BUFFERPIN
Definition: wait_event.h:20
#define PG_WAIT_IPC
Definition: wait_event.h:24
#define PG_WAIT_CLIENT
Definition: wait_event.h:22
#define PG_WAIT_EXTENSION
Definition: wait_event.h:23
#define PG_WAIT_ACTIVITY
Definition: wait_event.h:21
#define PG_WAIT_LOCK
Definition: wait_event.h:19
#define PG_WAIT_IO
Definition: wait_event.h:26

References GetLockNameFromTagType(), GetLWLockIdentifier(), GetWaitEventExtensionIdentifier(), PG_WAIT_ACTIVITY, PG_WAIT_BUFFERPIN, PG_WAIT_CLIENT, PG_WAIT_EXTENSION, PG_WAIT_IO, PG_WAIT_IPC, PG_WAIT_LOCK, PG_WAIT_LWLOCK, PG_WAIT_TIMEOUT, pgstat_get_wait_activity(), pgstat_get_wait_bufferpin(), pgstat_get_wait_client(), pgstat_get_wait_io(), pgstat_get_wait_ipc(), pgstat_get_wait_timeout(), WAIT_EVENT_CLASS_MASK, and WAIT_EVENT_ID_MASK.

Referenced by pg_stat_get_activity(), and pg_stat_get_backend_wait_event().

◆ pgstat_get_wait_event_type()

const char* pgstat_get_wait_event_type ( uint32  wait_event_info)

Definition at line 337 of file wait_event.c.

338 {
339  uint32 classId;
340  const char *event_type;
341 
342  /* report process as not waiting. */
343  if (wait_event_info == 0)
344  return NULL;
345 
346  classId = wait_event_info & WAIT_EVENT_CLASS_MASK;
347 
348  switch (classId)
349  {
350  case PG_WAIT_LWLOCK:
351  event_type = "LWLock";
352  break;
353  case PG_WAIT_LOCK:
354  event_type = "Lock";
355  break;
356  case PG_WAIT_BUFFERPIN:
357  event_type = "BufferPin";
358  break;
359  case PG_WAIT_ACTIVITY:
360  event_type = "Activity";
361  break;
362  case PG_WAIT_CLIENT:
363  event_type = "Client";
364  break;
365  case PG_WAIT_EXTENSION:
366  event_type = "Extension";
367  break;
368  case PG_WAIT_IPC:
369  event_type = "IPC";
370  break;
371  case PG_WAIT_TIMEOUT:
372  event_type = "Timeout";
373  break;
374  case PG_WAIT_IO:
375  event_type = "IO";
376  break;
377  default:
378  event_type = "???";
379  break;
380  }
381 
382  return event_type;
383 }

References PG_WAIT_ACTIVITY, PG_WAIT_BUFFERPIN, PG_WAIT_CLIENT, PG_WAIT_EXTENSION, PG_WAIT_IO, PG_WAIT_IPC, PG_WAIT_LOCK, PG_WAIT_LWLOCK, PG_WAIT_TIMEOUT, and WAIT_EVENT_CLASS_MASK.

Referenced by pg_stat_get_activity(), and pg_stat_get_backend_wait_event_type().

◆ pgstat_report_wait_end()

static void pgstat_report_wait_end ( void  )
inlinestatic

Definition at line 98 of file wait_event.h.

99 {
100  /* see pgstat_report_wait_start() */
101  *(volatile uint32 *) my_wait_event_info = 0;
102 }
PGDLLIMPORT uint32 * my_wait_event_info
Definition: wait_event.c:41

References my_wait_event_info.

Referenced by AbortSubTransaction(), AbortTransaction(), AddToDataDirLockFile(), ApplyLogicalMappingFile(), assign_wal_sync_method(), BackgroundWriterMain(), basebackup_read_file(), BootStrapXLOG(), CheckpointerMain(), CheckPointLogicalRewriteHeap(), copy_file(), CreateCheckPoint(), CreateDirAndVersionFile(), CreateLockFile(), dsm_impl_mmap(), dsm_impl_posix_resize(), ExecuteRecoveryCommand(), FileFallocate(), FilePrefetch(), FileReadV(), FileSync(), FileTruncate(), FileWriteback(), FileWriteV(), FileZero(), heap_xlog_logical_rewrite(), issue_xlog_fsync(), LWLockReportWaitEnd(), perform_spin_delay(), pgarch_archiveXlog(), ProcArrayGroupClearXid(), read_relmap_file(), ReadControlFile(), readTimeLineHistory(), ReadTwoPhaseFile(), RecheckDataDirLockFile(), RecreateTwoPhaseFile(), ReorderBufferSerializeChange(), RestoreArchivedFile(), RestoreSlotFromDisk(), SaveSlotToPath(), SendTimeLineHistory(), shell_archive_file(), ShutdownAuxiliaryProcess(), SlruPhysicalReadPage(), SlruPhysicalWritePage(), SlruSyncFileTag(), SnapBuildRestoreContents(), SnapBuildSerialize(), TransactionGroupUpdateXidStatus(), update_controlfile(), vacuum_delay_point(), WaitEventSetWait(), WaitExceedsMaxStandbyDelay(), WALRead(), WalSndErrorCleanup(), WalSummarizerMain(), WalWriterMain(), write_relmap_file(), WriteControlFile(), writeTimeLineHistory(), writeTimeLineHistoryFile(), XLogFileCopy(), XLogFileInitInternal(), XLogPageRead(), and XLogWrite().

◆ pgstat_report_wait_start()

static void pgstat_report_wait_start ( uint32  wait_event_info)
inlinestatic

Definition at line 82 of file wait_event.h.

83 {
84  /*
85  * Since this is a four-byte field which is always read and written as
86  * four-bytes, updates are atomic.
87  */
88  *(volatile uint32 *) my_wait_event_info = wait_event_info;
89 }

References my_wait_event_info.

Referenced by AddToDataDirLockFile(), ApplyLogicalMappingFile(), assign_wal_sync_method(), basebackup_read_file(), BootStrapXLOG(), CheckPointLogicalRewriteHeap(), copy_file(), CreateCheckPoint(), CreateDirAndVersionFile(), CreateLockFile(), dsm_impl_mmap(), dsm_impl_posix_resize(), ExecuteRecoveryCommand(), FileFallocate(), FilePrefetch(), FileReadV(), FileSync(), FileTruncate(), FileWriteback(), FileWriteV(), FileZero(), heap_xlog_logical_rewrite(), issue_xlog_fsync(), LWLockReportWaitStart(), perform_spin_delay(), ProcArrayGroupClearXid(), read_relmap_file(), ReadControlFile(), readTimeLineHistory(), ReadTwoPhaseFile(), RecheckDataDirLockFile(), RecreateTwoPhaseFile(), ReorderBufferSerializeChange(), RestoreArchivedFile(), RestoreSlotFromDisk(), SaveSlotToPath(), SendTimeLineHistory(), shell_archive_file(), SlruPhysicalReadPage(), SlruPhysicalWritePage(), SlruSyncFileTag(), SnapBuildRestoreContents(), SnapBuildSerialize(), TransactionGroupUpdateXidStatus(), update_controlfile(), vacuum_delay_point(), WaitEventSetWait(), WaitExceedsMaxStandbyDelay(), WALRead(), write_relmap_file(), WriteControlFile(), writeTimeLineHistory(), writeTimeLineHistoryFile(), XLogFileCopy(), XLogFileInitInternal(), XLogPageRead(), and XLogWrite().

◆ pgstat_reset_wait_event_storage()

void pgstat_reset_wait_event_storage ( void  )

Definition at line 325 of file wait_event.c.

326 {
328 }
static uint32 local_my_wait_event_info
Definition: wait_event.c:40
uint32 * my_wait_event_info
Definition: wait_event.c:41

References local_my_wait_event_info, and my_wait_event_info.

Referenced by AuxiliaryProcKill(), and ProcKill().

◆ pgstat_set_wait_event_storage()

void pgstat_set_wait_event_storage ( uint32 wait_event_info)

Definition at line 313 of file wait_event.c.

314 {
315  my_wait_event_info = wait_event_info;
316 }

References my_wait_event_info.

Referenced by InitAuxiliaryProcess(), and InitProcess().

◆ WaitEventExtensionNew()

uint32 WaitEventExtensionNew ( const char *  wait_event_name)

Definition at line 161 of file wait_event.c.

162 {
163  uint16 eventId;
164  bool found;
165  WaitEventExtensionEntryByName *entry_by_name;
166  WaitEventExtensionEntryById *entry_by_id;
167 
168  /* Check the limit of the length of the event name */
169  if (strlen(wait_event_name) >= NAMEDATALEN)
170  elog(ERROR,
171  "cannot use custom wait event string longer than %u characters",
172  NAMEDATALEN - 1);
173 
174  /*
175  * Check if the wait event info associated to the name is already defined,
176  * and return it if so.
177  */
178  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
179  entry_by_name = (WaitEventExtensionEntryByName *)
180  hash_search(WaitEventExtensionHashByName, wait_event_name,
181  HASH_FIND, &found);
182  LWLockRelease(WaitEventExtensionLock);
183  if (found)
184  return WAIT_EVENT_EXTENSION_INFO(entry_by_name->event_id);
185 
186  /*
187  * Allocate and register a new wait event. Recheck if the event name
188  * exists, as it could be possible that a concurrent process has inserted
189  * one with the same name since the LWLock acquired again here was
190  * previously released.
191  */
192  LWLockAcquire(WaitEventExtensionLock, LW_EXCLUSIVE);
193  entry_by_name = (WaitEventExtensionEntryByName *)
194  hash_search(WaitEventExtensionHashByName, wait_event_name,
195  HASH_FIND, &found);
196  if (found)
197  {
198  LWLockRelease(WaitEventExtensionLock);
199  return WAIT_EVENT_EXTENSION_INFO(entry_by_name->event_id);
200  }
201 
202  /* Allocate a new event Id */
204 
206  {
208  ereport(ERROR,
209  errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
210  errmsg("too many wait events for extensions"));
211  }
212 
213  eventId = WaitEventExtensionCounter->nextId++;
214 
216 
217  /* Register the new wait event */
218  entry_by_id = (WaitEventExtensionEntryById *)
220  HASH_ENTER, &found);
221  Assert(!found);
222  strlcpy(entry_by_id->wait_event_name, wait_event_name,
223  sizeof(entry_by_id->wait_event_name));
224 
225  entry_by_name = (WaitEventExtensionEntryByName *)
226  hash_search(WaitEventExtensionHashByName, wait_event_name,
227  HASH_ENTER, &found);
228  Assert(!found);
229  entry_by_name->event_id = eventId;
230 
231  LWLockRelease(WaitEventExtensionLock);
232 
233  return WAIT_EVENT_EXTENSION_INFO(eventId);
234 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
@ LW_EXCLUSIVE
Definition: lwlock.h:114
#define NAMEDATALEN
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
char wait_event_name[NAMEDATALEN]
Definition: wait_event.c:71
static WaitEventExtensionCounterData * WaitEventExtensionCounter
Definition: wait_event.c:89
#define WAIT_EVENT_EXTENSION_INFO(eventId)
Definition: wait_event.c:95
static HTAB * WaitEventExtensionHashById
Definition: wait_event.c:61
#define WAIT_EVENT_EXTENSION_HASH_MAX_SIZE
Definition: wait_event.c:65

References Assert, elog, ereport, errcode(), errmsg(), ERROR, WaitEventExtensionEntryByName::event_id, HASH_ENTER, HASH_FIND, hash_search(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), WaitEventExtensionCounterData::mutex, NAMEDATALEN, WaitEventExtensionCounterData::nextId, SpinLockAcquire, SpinLockRelease, strlcpy(), WAIT_EVENT_EXTENSION_HASH_MAX_SIZE, WAIT_EVENT_EXTENSION_INFO, WaitEventExtensionEntryById::wait_event_name, WaitEventExtensionCounter, WaitEventExtensionHashById, and WaitEventExtensionHashByName.

Referenced by connect_pg_server(), dblink_connect(), dblink_get_conn(), dblink_init(), GetConnection(), injection_wait(), pgfdw_get_cleanup_result(), test_shm_mq_pipelined(), wait_for_workers_to_become_ready(), and worker_spi_main().

◆ WaitEventExtensionShmemInit()

void WaitEventExtensionShmemInit ( void  )

Definition at line 119 of file wait_event.c.

120 {
121  bool found;
122  HASHCTL info;
123 
125  ShmemInitStruct("WaitEventExtensionCounterData",
126  sizeof(WaitEventExtensionCounterData), &found);
127 
128  if (!found)
129  {
130  /* initialize the allocation counter and its spinlock. */
133  }
134 
135  /* initialize or attach the hash tables to store custom wait events */
136  info.keysize = sizeof(uint16);
137  info.entrysize = sizeof(WaitEventExtensionEntryById);
138  WaitEventExtensionHashById = ShmemInitHash("WaitEventExtension hash by id",
141  &info,
143 
144  /* key is a NULL-terminated string */
145  info.keysize = sizeof(char[NAMEDATALEN]);
147  WaitEventExtensionHashByName = ShmemInitHash("WaitEventExtension hash by name",
150  &info,
152 }
#define HASH_STRINGS
Definition: hsearch.h:96
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
HTAB * ShmemInitHash(const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags)
Definition: shmem.c:332
#define SpinLockInit(lock)
Definition: spin.h:60
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
#define WAIT_EVENT_EXTENSION_INITIAL_ID
Definition: wait_event.c:92
struct WaitEventExtensionEntryByName WaitEventExtensionEntryByName
#define WAIT_EVENT_EXTENSION_HASH_INIT_SIZE
Definition: wait_event.c:64
struct WaitEventExtensionEntryById WaitEventExtensionEntryById

References HASHCTL::entrysize, HASH_BLOBS, HASH_ELEM, HASH_STRINGS, HASHCTL::keysize, WaitEventExtensionCounterData::mutex, NAMEDATALEN, WaitEventExtensionCounterData::nextId, ShmemInitHash(), ShmemInitStruct(), SpinLockInit, WAIT_EVENT_EXTENSION_HASH_INIT_SIZE, WAIT_EVENT_EXTENSION_HASH_MAX_SIZE, WAIT_EVENT_EXTENSION_INITIAL_ID, WaitEventExtensionCounter, WaitEventExtensionHashById, and WaitEventExtensionHashByName.

Referenced by CreateOrAttachShmemStructs().

◆ WaitEventExtensionShmemSize()

Size WaitEventExtensionShmemSize ( void  )

Definition at line 103 of file wait_event.c.

104 {
105  Size sz;
106 
109  sizeof(WaitEventExtensionEntryById)));
112  return sz;
113 }
#define MAXALIGN(LEN)
Definition: c.h:811
size_t Size
Definition: c.h:605
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:783
Size add_size(Size s1, Size s2)
Definition: shmem.c:493

References add_size(), hash_estimate_size(), MAXALIGN, and WAIT_EVENT_EXTENSION_HASH_MAX_SIZE.

Referenced by CalculateShmemSize().

Variable Documentation

◆ my_wait_event_info