PostgreSQL Source Code  git master
wait_event.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "storage/lmgr.h"
#include "storage/lwlock.h"
#include "storage/spin.h"
#include "utils/memutils.h"
#include "utils/wait_event.h"
#include "pgstat_wait_event.c"
Include dependency graph for wait_event.c:

Go to the source code of this file.

Data Structures

struct  WaitEventExtensionEntryById
 
struct  WaitEventExtensionEntryByName
 
struct  WaitEventExtensionCounterData
 

Macros

#define WAIT_EVENT_CLASS_MASK   0xFF000000
 
#define WAIT_EVENT_ID_MASK   0x0000FFFF
 
#define WAIT_EVENT_EXTENSION_HASH_INIT_SIZE   16
 
#define WAIT_EVENT_EXTENSION_HASH_MAX_SIZE   128
 
#define NUM_BUILTIN_WAIT_EVENT_EXTENSION    (WAIT_EVENT_EXTENSION_FIRST_USER_DEFINED - WAIT_EVENT_EXTENSION)
 
#define WAIT_EVENT_EXTENSION_INFO(eventId)   (PG_WAIT_EXTENSION | eventId)
 

Typedefs

typedef struct WaitEventExtensionEntryById WaitEventExtensionEntryById
 
typedef struct WaitEventExtensionEntryByName WaitEventExtensionEntryByName
 
typedef struct WaitEventExtensionCounterData WaitEventExtensionCounterData
 

Functions

static const char * pgstat_get_wait_activity (WaitEventActivity w)
 
static const char * pgstat_get_wait_bufferpin (WaitEventBufferPin w)
 
static const char * pgstat_get_wait_client (WaitEventClient w)
 
static const char * pgstat_get_wait_ipc (WaitEventIPC w)
 
static const char * pgstat_get_wait_timeout (WaitEventTimeout w)
 
static const char * pgstat_get_wait_io (WaitEventIO w)
 
static const char * GetWaitEventExtensionIdentifier (uint16 eventId)
 
Size WaitEventExtensionShmemSize (void)
 
void WaitEventExtensionShmemInit (void)
 
uint32 WaitEventExtensionNew (const char *wait_event_name)
 
char ** GetWaitEventExtensionNames (int *nwaitevents)
 
void pgstat_set_wait_event_storage (uint32 *wait_event_info)
 
void pgstat_reset_wait_event_storage (void)
 
const char * pgstat_get_wait_event_type (uint32 wait_event_info)
 
const char * pgstat_get_wait_event (uint32 wait_event_info)
 

Variables

static uint32 local_my_wait_event_info
 
uint32my_wait_event_info = &local_my_wait_event_info
 
static HTABWaitEventExtensionHashById
 
static HTABWaitEventExtensionHashByName
 
static WaitEventExtensionCounterDataWaitEventExtensionCounter
 

Macro Definition Documentation

◆ NUM_BUILTIN_WAIT_EVENT_EXTENSION

#define NUM_BUILTIN_WAIT_EVENT_EXTENSION    (WAIT_EVENT_EXTENSION_FIRST_USER_DEFINED - WAIT_EVENT_EXTENSION)

Definition at line 94 of file wait_event.c.

◆ WAIT_EVENT_CLASS_MASK

#define WAIT_EVENT_CLASS_MASK   0xFF000000

Definition at line 45 of file wait_event.c.

◆ WAIT_EVENT_EXTENSION_HASH_INIT_SIZE

#define WAIT_EVENT_EXTENSION_HASH_INIT_SIZE   16

Definition at line 66 of file wait_event.c.

◆ WAIT_EVENT_EXTENSION_HASH_MAX_SIZE

#define WAIT_EVENT_EXTENSION_HASH_MAX_SIZE   128

Definition at line 67 of file wait_event.c.

◆ WAIT_EVENT_EXTENSION_INFO

#define WAIT_EVENT_EXTENSION_INFO (   eventId)    (PG_WAIT_EXTENSION | eventId)

Definition at line 98 of file wait_event.c.

◆ WAIT_EVENT_ID_MASK

#define WAIT_EVENT_ID_MASK   0x0000FFFF

Definition at line 46 of file wait_event.c.

Typedef Documentation

◆ WaitEventExtensionCounterData

◆ WaitEventExtensionEntryById

◆ WaitEventExtensionEntryByName

Function Documentation

◆ GetWaitEventExtensionIdentifier()

static const char * GetWaitEventExtensionIdentifier ( uint16  eventId)
static

Definition at line 243 of file wait_event.c.

244 {
245  bool found;
247 
248  /* Built-in event? */
249  if (eventId < NUM_BUILTIN_WAIT_EVENT_EXTENSION)
250  return "Extension";
251 
252  /* It is a user-defined wait event, so lookup hash table. */
253  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
254  entry = (WaitEventExtensionEntryById *)
256  HASH_FIND, &found);
257  LWLockRelease(WaitEventExtensionLock);
258 
259  if (!entry)
260  elog(ERROR, "could not find custom wait event name for ID %u",
261  eventId);
262 
263  return entry->wait_event_name;
264 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
#define ERROR
Definition: elog.h:39
@ HASH_FIND
Definition: hsearch.h:113
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1808
@ LW_SHARED
Definition: lwlock.h:117
char wait_event_name[NAMEDATALEN]
Definition: wait_event.c:73
#define NUM_BUILTIN_WAIT_EVENT_EXTENSION
Definition: wait_event.c:94
static HTAB * WaitEventExtensionHashById
Definition: wait_event.c:63

References elog(), ERROR, HASH_FIND, hash_search(), LW_SHARED, LWLockAcquire(), LWLockRelease(), NUM_BUILTIN_WAIT_EVENT_EXTENSION, WaitEventExtensionEntryById::wait_event_name, and WaitEventExtensionHashById.

Referenced by pgstat_get_wait_event().

◆ GetWaitEventExtensionNames()

char** GetWaitEventExtensionNames ( int *  nwaitevents)

Definition at line 273 of file wait_event.c.

274 {
275  char **waiteventnames;
277  HASH_SEQ_STATUS hash_seq;
278  int index;
279  int els;
280 
281  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
282 
283  /* Now we can safely count the number of entries */
285 
286  /* Allocate enough space for all entries */
287  waiteventnames = palloc(els * sizeof(char *));
288 
289  /* Now scan the hash table to copy the data */
291 
292  index = 0;
293  while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL)
294  {
295  waiteventnames[index] = pstrdup(hentry->wait_event_name);
296  index++;
297  }
298 
299  LWLockRelease(WaitEventExtensionLock);
300 
301  Assert(index == els);
302 
303  *nwaitevents = index;
304  return waiteventnames;
305 }
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1377
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1431
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1421
Assert(fmt[strlen(fmt) - 1] !='\n')
char * pstrdup(const char *in)
Definition: mcxt.c:1644
void * palloc(Size size)
Definition: mcxt.c:1226
char wait_event_name[NAMEDATALEN]
Definition: wait_event.c:78
Definition: type.h:95
static HTAB * WaitEventExtensionHashByName
Definition: wait_event.c:64

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

static const char* pgstat_get_wait_activity ( WaitEventActivity  w)
static

Referenced by pgstat_get_wait_event().

◆ pgstat_get_wait_bufferpin()

static const char* pgstat_get_wait_bufferpin ( WaitEventBufferPin  w)
static

Referenced by pgstat_get_wait_event().

◆ pgstat_get_wait_client()

static const char* pgstat_get_wait_client ( WaitEventClient  w)
static

Referenced by pgstat_get_wait_event().

◆ pgstat_get_wait_event()

const char* pgstat_get_wait_event ( uint32  wait_event_info)

Definition at line 395 of file wait_event.c.

396 {
397  uint32 classId;
398  uint16 eventId;
399  const char *event_name;
400 
401  /* report process as not waiting. */
402  if (wait_event_info == 0)
403  return NULL;
404 
405  classId = wait_event_info & WAIT_EVENT_CLASS_MASK;
406  eventId = wait_event_info & WAIT_EVENT_ID_MASK;
407 
408  switch (classId)
409  {
410  case PG_WAIT_LWLOCK:
411  event_name = GetLWLockIdentifier(classId, eventId);
412  break;
413  case PG_WAIT_LOCK:
414  event_name = GetLockNameFromTagType(eventId);
415  break;
416  case PG_WAIT_EXTENSION:
417  event_name = GetWaitEventExtensionIdentifier(eventId);
418  break;
419  case PG_WAIT_BUFFERPIN:
420  {
421  WaitEventBufferPin w = (WaitEventBufferPin) wait_event_info;
422 
423  event_name = pgstat_get_wait_bufferpin(w);
424  break;
425  }
426  case PG_WAIT_ACTIVITY:
427  {
428  WaitEventActivity w = (WaitEventActivity) wait_event_info;
429 
430  event_name = pgstat_get_wait_activity(w);
431  break;
432  }
433  case PG_WAIT_CLIENT:
434  {
435  WaitEventClient w = (WaitEventClient) wait_event_info;
436 
437  event_name = pgstat_get_wait_client(w);
438  break;
439  }
440  case PG_WAIT_IPC:
441  {
442  WaitEventIPC w = (WaitEventIPC) wait_event_info;
443 
444  event_name = pgstat_get_wait_ipc(w);
445  break;
446  }
447  case PG_WAIT_TIMEOUT:
448  {
449  WaitEventTimeout w = (WaitEventTimeout) wait_event_info;
450 
451  event_name = pgstat_get_wait_timeout(w);
452  break;
453  }
454  case PG_WAIT_IO:
455  {
456  WaitEventIO w = (WaitEventIO) wait_event_info;
457 
458  event_name = pgstat_get_wait_io(w);
459  break;
460  }
461  default:
462  event_name = "unknown wait event";
463  break;
464  }
465 
466  return event_name;
467 }
unsigned short uint16
Definition: c.h:494
unsigned int uint32
Definition: c.h:495
const char * GetLockNameFromTagType(uint16 locktag_type)
Definition: lmgr.c:1265
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:794
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:46
#define WAIT_EVENT_CLASS_MASK
Definition: wait_event.c:45
static const char * GetWaitEventExtensionIdentifier(uint16 eventId)
Definition: wait_event.c:243
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 340 of file wait_event.c.

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

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

static const char* pgstat_get_wait_io ( WaitEventIO  w)
static

Referenced by pgstat_get_wait_event().

◆ pgstat_get_wait_ipc()

static const char* pgstat_get_wait_ipc ( WaitEventIPC  w)
static

Referenced by pgstat_get_wait_event().

◆ pgstat_get_wait_timeout()

static const char* pgstat_get_wait_timeout ( WaitEventTimeout  w)
static

Referenced by pgstat_get_wait_event().

◆ pgstat_reset_wait_event_storage()

void pgstat_reset_wait_event_storage ( void  )

Definition at line 328 of file wait_event.c.

329 {
331 }
static uint32 local_my_wait_event_info
Definition: wait_event.c:42
uint32 * my_wait_event_info
Definition: wait_event.c:43

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 316 of file wait_event.c.

317 {
318  my_wait_event_info = wait_event_info;
319 }

References my_wait_event_info.

Referenced by InitAuxiliaryProcess(), and InitProcess().

◆ WaitEventExtensionNew()

uint32 WaitEventExtensionNew ( const char *  wait_event_name)

Definition at line 164 of file wait_event.c.

165 {
166  uint16 eventId;
167  bool found;
168  WaitEventExtensionEntryByName *entry_by_name;
169  WaitEventExtensionEntryById *entry_by_id;
170 
171  /* Check the limit of the length of the event name */
172  if (strlen(wait_event_name) >= NAMEDATALEN)
173  elog(ERROR,
174  "cannot use custom wait event string longer than %u characters",
175  NAMEDATALEN - 1);
176 
177  /*
178  * Check if the wait event info associated to the name is already defined,
179  * and return it if so.
180  */
181  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
182  entry_by_name = (WaitEventExtensionEntryByName *)
183  hash_search(WaitEventExtensionHashByName, wait_event_name,
184  HASH_FIND, &found);
185  LWLockRelease(WaitEventExtensionLock);
186  if (found)
187  return WAIT_EVENT_EXTENSION_INFO(entry_by_name->event_id);
188 
189  /*
190  * Allocate and register a new wait event. Recheck if the event name
191  * exists, as it could be possible that a concurrent process has inserted
192  * one with the same name since the LWLock acquired again here was
193  * previously released.
194  */
195  LWLockAcquire(WaitEventExtensionLock, LW_EXCLUSIVE);
196  entry_by_name = (WaitEventExtensionEntryByName *)
197  hash_search(WaitEventExtensionHashByName, wait_event_name,
198  HASH_FIND, &found);
199  if (found)
200  {
201  LWLockRelease(WaitEventExtensionLock);
202  return WAIT_EVENT_EXTENSION_INFO(entry_by_name->event_id);
203  }
204 
205  /* Allocate a new event Id */
207 
209  {
211  ereport(ERROR,
212  errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
213  errmsg("too many wait events for extensions"));
214  }
215 
216  eventId = WaitEventExtensionCounter->nextId++;
217 
219 
220  /* Register the new wait event */
221  entry_by_id = (WaitEventExtensionEntryById *)
223  HASH_ENTER, &found);
224  Assert(!found);
225  strlcpy(entry_by_id->wait_event_name, wait_event_name,
226  sizeof(entry_by_id->wait_event_name));
227 
228  entry_by_name = (WaitEventExtensionEntryByName *)
229  hash_search(WaitEventExtensionHashByName, wait_event_name,
230  HASH_ENTER, &found);
231  Assert(!found);
232  entry_by_name->event_id = eventId;
233 
234  LWLockRelease(WaitEventExtensionLock);
235 
236  return WAIT_EVENT_EXTENSION_INFO(eventId);
237 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
@ HASH_ENTER
Definition: hsearch.h:114
@ LW_EXCLUSIVE
Definition: lwlock.h:116
#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
static WaitEventExtensionCounterData * WaitEventExtensionCounter
Definition: wait_event.c:91
#define WAIT_EVENT_EXTENSION_INFO(eventId)
Definition: wait_event.c:98
#define WAIT_EVENT_EXTENSION_HASH_MAX_SIZE
Definition: wait_event.c:67

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

◆ WaitEventExtensionShmemInit()

void WaitEventExtensionShmemInit ( void  )

Definition at line 122 of file wait_event.c.

123 {
124  bool found;
125  HASHCTL info;
126 
128  ShmemInitStruct("WaitEventExtensionCounterData",
129  sizeof(WaitEventExtensionCounterData), &found);
130 
131  if (!found)
132  {
133  /* initialize the allocation counter and its spinlock. */
136  }
137 
138  /* initialize or attach the hash tables to store custom wait events */
139  info.keysize = sizeof(uint16);
140  info.entrysize = sizeof(WaitEventExtensionEntryById);
141  WaitEventExtensionHashById = ShmemInitHash("WaitEventExtension hash by id",
144  &info,
146 
147  /* key is a NULL-terminated string */
148  info.keysize = sizeof(char[NAMEDATALEN]);
150  WaitEventExtensionHashByName = ShmemInitHash("WaitEventExtension hash by name",
153  &info,
155 }
#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:396
HTAB * ShmemInitHash(const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags)
Definition: shmem.c:341
#define SpinLockInit(lock)
Definition: spin.h:60
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
struct WaitEventExtensionEntryByName WaitEventExtensionEntryByName
#define WAIT_EVENT_EXTENSION_HASH_INIT_SIZE
Definition: wait_event.c:66
struct WaitEventExtensionEntryById WaitEventExtensionEntryById

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ WaitEventExtensionShmemSize()

Size WaitEventExtensionShmemSize ( void  )

Definition at line 106 of file wait_event.c.

107 {
108  Size sz;
109 
112  sizeof(WaitEventExtensionEntryById)));
115  return sz;
116 }
#define MAXALIGN(LEN)
Definition: c.h:800
size_t Size
Definition: c.h:594
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:781
Size add_size(Size s1, Size s2)
Definition: shmem.c:502

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

Referenced by CalculateShmemSize().

Variable Documentation

◆ local_my_wait_event_info

uint32 local_my_wait_event_info
static

Definition at line 42 of file wait_event.c.

Referenced by pgstat_reset_wait_event_storage().

◆ my_wait_event_info

◆ WaitEventExtensionCounter

WaitEventExtensionCounterData* WaitEventExtensionCounter
static

Definition at line 91 of file wait_event.c.

Referenced by WaitEventExtensionNew(), and WaitEventExtensionShmemInit().

◆ WaitEventExtensionHashById

HTAB* WaitEventExtensionHashById
static

◆ WaitEventExtensionHashByName

HTAB* WaitEventExtensionHashByName
static