PostgreSQL Source Code  git master
wait_event.c
Go to the documentation of this file.
1 /* ----------
2  * wait_event.c
3  * Wait event reporting infrastructure.
4  *
5  * Copyright (c) 2001-2024, PostgreSQL Global Development Group
6  *
7  *
8  * IDENTIFICATION
9  * src/backend/utils/activity/wait_event.c
10  *
11  * NOTES
12  *
13  * To make pgstat_report_wait_start() and pgstat_report_wait_end() as
14  * lightweight as possible, they do not check if shared memory (MyProc
15  * specifically, where the wait event is stored) is already available. Instead
16  * we initially set my_wait_event_info to a process local variable, which then
17  * is redirected to shared memory using pgstat_set_wait_event_storage(). For
18  * the same reason pgstat_track_activities is not checked - the check adds
19  * more work than it saves.
20  *
21  * ----------
22  */
23 #include "postgres.h"
24 
25 #include "port/pg_bitutils.h"
26 #include "storage/lmgr.h" /* for GetLockNameFromTagType */
27 #include "storage/lwlock.h" /* for GetLWLockIdentifier */
28 #include "storage/spin.h"
29 #include "utils/wait_event.h"
30 
31 
32 static const char *pgstat_get_wait_activity(WaitEventActivity w);
33 static const char *pgstat_get_wait_bufferpin(WaitEventBufferPin w);
34 static const char *pgstat_get_wait_client(WaitEventClient w);
35 static const char *pgstat_get_wait_ipc(WaitEventIPC w);
36 static const char *pgstat_get_wait_timeout(WaitEventTimeout w);
37 static const char *pgstat_get_wait_io(WaitEventIO w);
38 
39 
42 
43 #define WAIT_EVENT_CLASS_MASK 0xFF000000
44 #define WAIT_EVENT_ID_MASK 0x0000FFFF
45 
46 /*
47  * Hash tables for storing custom wait event ids and their names in
48  * shared memory.
49  *
50  * WaitEventExtensionHashById is used to find the name from an event id.
51  * Any backend can search it to find custom wait events.
52  *
53  * WaitEventExtensionHashByName is used to find the event ID from a name.
54  * It is used to ensure that no duplicated entries are registered.
55  *
56  * The size of the hash table is based on the assumption that
57  * WAIT_EVENT_EXTENSION_HASH_INIT_SIZE is enough for most cases, and it seems
58  * unlikely that the number of entries will reach
59  * WAIT_EVENT_EXTENSION_HASH_MAX_SIZE.
60  */
61 static HTAB *WaitEventExtensionHashById; /* find names from IDs */
62 static HTAB *WaitEventExtensionHashByName; /* find IDs from names */
63 
64 #define WAIT_EVENT_EXTENSION_HASH_INIT_SIZE 16
65 #define WAIT_EVENT_EXTENSION_HASH_MAX_SIZE 128
66 
67 /* hash table entries */
69 {
70  uint16 event_id; /* hash key */
71  char wait_event_name[NAMEDATALEN]; /* custom wait event name */
73 
75 {
76  char wait_event_name[NAMEDATALEN]; /* hash key */
77  uint16 event_id; /* wait event ID */
79 
80 
81 /* dynamic allocation counter for custom wait events in extensions */
83 {
84  int nextId; /* next ID to assign */
85  slock_t mutex; /* protects the counter */
87 
88 /* pointer to the shared memory */
90 
91 /* first event ID of custom wait events for extensions */
92 #define NUM_BUILTIN_WAIT_EVENT_EXTENSION \
93  (WAIT_EVENT_EXTENSION_FIRST_USER_DEFINED - WAIT_EVENT_EXTENSION)
94 
95 /* wait event info for extensions */
96 #define WAIT_EVENT_EXTENSION_INFO(eventId) (PG_WAIT_EXTENSION | eventId)
97 
98 static const char *GetWaitEventExtensionIdentifier(uint16 eventId);
99 
100 /*
101  * Return the space for dynamic shared hash tables and dynamic allocation counter.
102  */
103 Size
105 {
106  Size sz;
107 
110  sizeof(WaitEventExtensionEntryById)));
113  return sz;
114 }
115 
116 /*
117  * Allocate shmem space for dynamic shared hash and dynamic allocation counter.
118  */
119 void
121 {
122  bool found;
123  HASHCTL info;
124 
126  ShmemInitStruct("WaitEventExtensionCounterData",
127  sizeof(WaitEventExtensionCounterData), &found);
128 
129  if (!found)
130  {
131  /* initialize the allocation counter and its spinlock. */
134  }
135 
136  /* initialize or attach the hash tables to store custom wait events */
137  info.keysize = sizeof(uint16);
138  info.entrysize = sizeof(WaitEventExtensionEntryById);
139  WaitEventExtensionHashById = ShmemInitHash("WaitEventExtension hash by id",
142  &info,
144 
145  /* key is a NULL-terminated string */
146  info.keysize = sizeof(char[NAMEDATALEN]);
148  WaitEventExtensionHashByName = ShmemInitHash("WaitEventExtension hash by name",
151  &info,
153 }
154 
155 /*
156  * Allocate a new event ID and return the wait event info.
157  *
158  * If the wait event name is already defined, this does not allocate a new
159  * entry; it returns the wait event information associated to the name.
160  */
161 uint32
162 WaitEventExtensionNew(const char *wait_event_name)
163 {
164  uint16 eventId;
165  bool found;
166  WaitEventExtensionEntryByName *entry_by_name;
167  WaitEventExtensionEntryById *entry_by_id;
168 
169  /* Check the limit of the length of the event name */
170  if (strlen(wait_event_name) >= NAMEDATALEN)
171  elog(ERROR,
172  "cannot use custom wait event string longer than %u characters",
173  NAMEDATALEN - 1);
174 
175  /*
176  * Check if the wait event info associated to the name is already defined,
177  * and return it if so.
178  */
179  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
180  entry_by_name = (WaitEventExtensionEntryByName *)
181  hash_search(WaitEventExtensionHashByName, wait_event_name,
182  HASH_FIND, &found);
183  LWLockRelease(WaitEventExtensionLock);
184  if (found)
185  return WAIT_EVENT_EXTENSION_INFO(entry_by_name->event_id);
186 
187  /*
188  * Allocate and register a new wait event. Recheck if the event name
189  * exists, as it could be possible that a concurrent process has inserted
190  * one with the same name since the LWLock acquired again here was
191  * previously released.
192  */
193  LWLockAcquire(WaitEventExtensionLock, LW_EXCLUSIVE);
194  entry_by_name = (WaitEventExtensionEntryByName *)
195  hash_search(WaitEventExtensionHashByName, wait_event_name,
196  HASH_FIND, &found);
197  if (found)
198  {
199  LWLockRelease(WaitEventExtensionLock);
200  return WAIT_EVENT_EXTENSION_INFO(entry_by_name->event_id);
201  }
202 
203  /* Allocate a new event Id */
205 
207  {
209  ereport(ERROR,
210  errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
211  errmsg("too many wait events for extensions"));
212  }
213 
214  eventId = WaitEventExtensionCounter->nextId++;
215 
217 
218  /* Register the new wait event */
219  entry_by_id = (WaitEventExtensionEntryById *)
221  HASH_ENTER, &found);
222  Assert(!found);
223  strlcpy(entry_by_id->wait_event_name, wait_event_name,
224  sizeof(entry_by_id->wait_event_name));
225 
226  entry_by_name = (WaitEventExtensionEntryByName *)
227  hash_search(WaitEventExtensionHashByName, wait_event_name,
228  HASH_ENTER, &found);
229  Assert(!found);
230  entry_by_name->event_id = eventId;
231 
232  LWLockRelease(WaitEventExtensionLock);
233 
234  return WAIT_EVENT_EXTENSION_INFO(eventId);
235 }
236 
237 /*
238  * Return the name of an wait event ID for extension.
239  */
240 static const char *
242 {
243  bool found;
245 
246  /* Built-in event? */
247  if (eventId < NUM_BUILTIN_WAIT_EVENT_EXTENSION)
248  return "Extension";
249 
250  /* It is a user-defined wait event, so lookup hash table. */
251  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
252  entry = (WaitEventExtensionEntryById *)
254  HASH_FIND, &found);
255  LWLockRelease(WaitEventExtensionLock);
256 
257  if (!entry)
258  elog(ERROR, "could not find custom wait event name for ID %u",
259  eventId);
260 
261  return entry->wait_event_name;
262 }
263 
264 
265 /*
266  * Returns a list of currently defined custom wait event names for extensions.
267  * The result is a palloc'd array, with the number of elements saved in
268  * *nwaitevents.
269  */
270 char **
271 GetWaitEventExtensionNames(int *nwaitevents)
272 {
273  char **waiteventnames;
275  HASH_SEQ_STATUS hash_seq;
276  int index;
277  int els;
278 
279  LWLockAcquire(WaitEventExtensionLock, LW_SHARED);
280 
281  /* Now we can safely count the number of entries */
283 
284  /* Allocate enough space for all entries */
285  waiteventnames = palloc(els * sizeof(char *));
286 
287  /* Now scan the hash table to copy the data */
289 
290  index = 0;
291  while ((hentry = (WaitEventExtensionEntryByName *) hash_seq_search(&hash_seq)) != NULL)
292  {
293  waiteventnames[index] = pstrdup(hentry->wait_event_name);
294  index++;
295  }
296 
297  LWLockRelease(WaitEventExtensionLock);
298 
299  Assert(index == els);
300 
301  *nwaitevents = index;
302  return waiteventnames;
303 }
304 
305 /*
306  * Configure wait event reporting to report wait events to *wait_event_info.
307  * *wait_event_info needs to be valid until pgstat_reset_wait_event_storage()
308  * is called.
309  *
310  * Expected to be called during backend startup, to point my_wait_event_info
311  * into shared memory.
312  */
313 void
315 {
316  my_wait_event_info = wait_event_info;
317 }
318 
319 /*
320  * Reset wait event storage location.
321  *
322  * Expected to be called during backend shutdown, before the location set up
323  * pgstat_set_wait_event_storage() becomes invalid.
324  */
325 void
327 {
329 }
330 
331 /* ----------
332  * pgstat_get_wait_event_type() -
333  *
334  * Return a string representing the current wait event type, backend is
335  * waiting on.
336  */
337 const char *
339 {
340  uint32 classId;
341  const char *event_type;
342 
343  /* report process as not waiting. */
344  if (wait_event_info == 0)
345  return NULL;
346 
347  classId = wait_event_info & WAIT_EVENT_CLASS_MASK;
348 
349  switch (classId)
350  {
351  case PG_WAIT_LWLOCK:
352  event_type = "LWLock";
353  break;
354  case PG_WAIT_LOCK:
355  event_type = "Lock";
356  break;
357  case PG_WAIT_BUFFERPIN:
358  event_type = "BufferPin";
359  break;
360  case PG_WAIT_ACTIVITY:
361  event_type = "Activity";
362  break;
363  case PG_WAIT_CLIENT:
364  event_type = "Client";
365  break;
366  case PG_WAIT_EXTENSION:
367  event_type = "Extension";
368  break;
369  case PG_WAIT_IPC:
370  event_type = "IPC";
371  break;
372  case PG_WAIT_TIMEOUT:
373  event_type = "Timeout";
374  break;
375  case PG_WAIT_IO:
376  event_type = "IO";
377  break;
378  default:
379  event_type = "???";
380  break;
381  }
382 
383  return event_type;
384 }
385 
386 /* ----------
387  * pgstat_get_wait_event() -
388  *
389  * Return a string representing the current wait event, backend is
390  * waiting on.
391  */
392 const char *
394 {
395  uint32 classId;
396  uint16 eventId;
397  const char *event_name;
398 
399  /* report process as not waiting. */
400  if (wait_event_info == 0)
401  return NULL;
402 
403  classId = wait_event_info & WAIT_EVENT_CLASS_MASK;
404  eventId = wait_event_info & WAIT_EVENT_ID_MASK;
405 
406  switch (classId)
407  {
408  case PG_WAIT_LWLOCK:
409  event_name = GetLWLockIdentifier(classId, eventId);
410  break;
411  case PG_WAIT_LOCK:
412  event_name = GetLockNameFromTagType(eventId);
413  break;
414  case PG_WAIT_EXTENSION:
415  event_name = GetWaitEventExtensionIdentifier(eventId);
416  break;
417  case PG_WAIT_BUFFERPIN:
418  {
419  WaitEventBufferPin w = (WaitEventBufferPin) wait_event_info;
420 
421  event_name = pgstat_get_wait_bufferpin(w);
422  break;
423  }
424  case PG_WAIT_ACTIVITY:
425  {
426  WaitEventActivity w = (WaitEventActivity) wait_event_info;
427 
428  event_name = pgstat_get_wait_activity(w);
429  break;
430  }
431  case PG_WAIT_CLIENT:
432  {
433  WaitEventClient w = (WaitEventClient) wait_event_info;
434 
435  event_name = pgstat_get_wait_client(w);
436  break;
437  }
438  case PG_WAIT_IPC:
439  {
440  WaitEventIPC w = (WaitEventIPC) wait_event_info;
441 
442  event_name = pgstat_get_wait_ipc(w);
443  break;
444  }
445  case PG_WAIT_TIMEOUT:
446  {
447  WaitEventTimeout w = (WaitEventTimeout) wait_event_info;
448 
449  event_name = pgstat_get_wait_timeout(w);
450  break;
451  }
452  case PG_WAIT_IO:
453  {
454  WaitEventIO w = (WaitEventIO) wait_event_info;
455 
456  event_name = pgstat_get_wait_io(w);
457  break;
458  }
459  default:
460  event_name = "unknown wait event";
461  break;
462  }
463 
464  return event_name;
465 }
466 
467 #include "pgstat_wait_event.c"
unsigned short uint16
Definition: c.h:505
unsigned int uint32
Definition: c.h:506
#define MAXALIGN(LEN)
Definition: c.h:811
#define Assert(condition)
Definition: c.h:858
size_t Size
Definition: c.h:605
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1341
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:783
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
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
#define HASH_STRINGS
Definition: hsearch.h:96
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
const char * GetLockNameFromTagType(uint16 locktag_type)
Definition: lmgr.c:1340
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:769
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void * palloc(Size size)
Definition: mcxt.c:1316
#define NAMEDATALEN
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int slock_t
Definition: s_lock.h:735
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
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
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
Definition: dynahash.c:220
char wait_event_name[NAMEDATALEN]
Definition: wait_event.c:71
char wait_event_name[NAMEDATALEN]
Definition: wait_event.c:76
Definition: type.h:95
static HTAB * WaitEventExtensionHashByName
Definition: wait_event.c:62
#define NUM_BUILTIN_WAIT_EVENT_EXTENSION
Definition: wait_event.c:92
static const char * pgstat_get_wait_ipc(WaitEventIPC w)
static const char * pgstat_get_wait_io(WaitEventIO w)
const char * pgstat_get_wait_event(uint32 wait_event_info)
Definition: wait_event.c:393
void pgstat_set_wait_event_storage(uint32 *wait_event_info)
Definition: wait_event.c:314
static const char * pgstat_get_wait_bufferpin(WaitEventBufferPin w)
struct WaitEventExtensionCounterData WaitEventExtensionCounterData
Size WaitEventExtensionShmemSize(void)
Definition: wait_event.c:104
struct WaitEventExtensionEntryByName WaitEventExtensionEntryByName
static WaitEventExtensionCounterData * WaitEventExtensionCounter
Definition: wait_event.c:89
#define WAIT_EVENT_EXTENSION_INFO(eventId)
Definition: wait_event.c:96
static const char * pgstat_get_wait_timeout(WaitEventTimeout w)
static uint32 local_my_wait_event_info
Definition: wait_event.c:40
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition: wait_event.c:338
static HTAB * WaitEventExtensionHashById
Definition: wait_event.c:61
void pgstat_reset_wait_event_storage(void)
Definition: wait_event.c:326
#define WAIT_EVENT_EXTENSION_HASH_MAX_SIZE
Definition: wait_event.c:65
static const char * pgstat_get_wait_client(WaitEventClient w)
#define WAIT_EVENT_ID_MASK
Definition: wait_event.c:44
char ** GetWaitEventExtensionNames(int *nwaitevents)
Definition: wait_event.c:271
#define WAIT_EVENT_CLASS_MASK
Definition: wait_event.c:43
uint32 WaitEventExtensionNew(const char *wait_event_name)
Definition: wait_event.c:162
void WaitEventExtensionShmemInit(void)
Definition: wait_event.c:120
#define WAIT_EVENT_EXTENSION_HASH_INIT_SIZE
Definition: wait_event.c:64
struct WaitEventExtensionEntryById WaitEventExtensionEntryById
uint32 * my_wait_event_info
Definition: wait_event.c:41
static const char * GetWaitEventExtensionIdentifier(uint16 eventId)
Definition: wait_event.c:241
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