PostgreSQL Source Code git master
Loading...
Searching...
No Matches
wait_event.c File Reference
#include "postgres.h"
#include "storage/lmgr.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "storage/subsystems.h"
#include "storage/spin.h"
#include "utils/wait_event.h"
#include "utils/pgstat_wait_event.c"
Include dependency graph for wait_event.c:

Go to the source code of this file.

Data Structures

struct  WaitEventCustomEntryByInfo
 
struct  WaitEventCustomEntryByName
 
struct  WaitEventCustomCounterData
 

Macros

#define WAIT_EVENT_CLASS_MASK   0xFF000000
 
#define WAIT_EVENT_ID_MASK   0x0000FFFF
 
#define WAIT_EVENT_CUSTOM_HASH_SIZE   128
 
#define WAIT_EVENT_CUSTOM_INITIAL_ID   1
 

Typedefs

typedef struct WaitEventCustomEntryByInfo WaitEventCustomEntryByInfo
 
typedef struct WaitEventCustomEntryByName WaitEventCustomEntryByName
 
typedef struct WaitEventCustomCounterData WaitEventCustomCounterData
 

Functions

static const charpgstat_get_wait_activity (WaitEventActivity w)
 
static const charpgstat_get_wait_buffer (WaitEventBuffer w)
 
static const charpgstat_get_wait_client (WaitEventClient w)
 
static const charpgstat_get_wait_ipc (WaitEventIPC w)
 
static const charpgstat_get_wait_timeout (WaitEventTimeout w)
 
static const charpgstat_get_wait_io (WaitEventIO w)
 
static uint32 WaitEventCustomNew (uint32 classId, const char *wait_event_name)
 
static const charGetWaitEventCustomIdentifier (uint32 wait_event_info)
 
static void WaitEventCustomShmemRequest (void *arg)
 
static void WaitEventCustomShmemInit (void *arg)
 
uint32 WaitEventExtensionNew (const char *wait_event_name)
 
uint32 WaitEventInjectionPointNew (const char *wait_event_name)
 
char ** GetWaitEventCustomNames (uint32 classId, int *nwaitevents)
 
void pgstat_set_wait_event_storage (uint32 *wait_event_info)
 
void pgstat_reset_wait_event_storage (void)
 
const charpgstat_get_wait_event_type (uint32 wait_event_info)
 
const charpgstat_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 HTABWaitEventCustomHashByInfo
 
static HTABWaitEventCustomHashByName
 
static WaitEventCustomCounterDataWaitEventCustomCounter
 
const ShmemCallbacks WaitEventCustomShmemCallbacks
 

Macro Definition Documentation

◆ WAIT_EVENT_CLASS_MASK

#define WAIT_EVENT_CLASS_MASK   0xFF000000

Definition at line 44 of file wait_event.c.

◆ WAIT_EVENT_CUSTOM_HASH_SIZE

#define WAIT_EVENT_CUSTOM_HASH_SIZE   128

Definition at line 67 of file wait_event.c.

◆ WAIT_EVENT_CUSTOM_INITIAL_ID

#define WAIT_EVENT_CUSTOM_INITIAL_ID   1

Definition at line 94 of file wait_event.c.

◆ WAIT_EVENT_ID_MASK

#define WAIT_EVENT_ID_MASK   0x0000FFFF

Definition at line 45 of file wait_event.c.

Typedef Documentation

◆ WaitEventCustomCounterData

◆ WaitEventCustomEntryByInfo

◆ WaitEventCustomEntryByName

Function Documentation

◆ GetWaitEventCustomIdentifier()

static const char * GetWaitEventCustomIdentifier ( uint32  wait_event_info)
static

Definition at line 262 of file wait_event.c.

263{
264 bool found;
266
267 /* Built-in event? */
268 if (wait_event_info == PG_WAIT_EXTENSION)
269 return "Extension";
270
271 /* It is a user-defined wait event, so lookup hash table. */
274 hash_search(WaitEventCustomHashByInfo, &wait_event_info,
275 HASH_FIND, &found);
277
278 if (!entry)
279 elog(ERROR,
280 "could not find custom name for wait event information %u",
281 wait_event_info);
282
283 return entry->wait_event_name;
284}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:889
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
@ HASH_FIND
Definition hsearch.h:108
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_SHARED
Definition lwlock.h:105
static int fb(int x)
char wait_event_name[NAMEDATALEN]
Definition wait_event.c:73
#define PG_WAIT_EXTENSION
static HTAB * WaitEventCustomHashByInfo
Definition wait_event.c:64

References elog, ERROR, fb(), HASH_FIND, hash_search(), LW_SHARED, LWLockAcquire(), LWLockRelease(), PG_WAIT_EXTENSION, WaitEventCustomEntryByInfo::wait_event_name, and WaitEventCustomHashByInfo.

Referenced by pgstat_get_wait_event().

◆ GetWaitEventCustomNames()

char ** GetWaitEventCustomNames ( uint32  classId,
int nwaitevents 
)

Definition at line 292 of file wait_event.c.

293{
294 char **waiteventnames;
297 int index;
298 int els;
299
301
302 /* Now we can safely count the number of entries */
304
305 /* Allocate enough space for all entries */
307
308 /* Now scan the hash table to copy the data */
310
311 index = 0;
313 {
314 if ((hentry->wait_event_info & WAIT_EVENT_CLASS_MASK) != classId)
315 continue;
316 waiteventnames[index] = pstrdup(hentry->wait_event_name);
317 index++;
318 }
319
321
323 return waiteventnames;
324}
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1352
int64 hash_get_num_entries(HTAB *hashp)
Definition dynahash.c:1273
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1317
#define palloc_array(type, count)
Definition fe_memutils.h:76
char * pstrdup(const char *in)
Definition mcxt.c:1781
Definition type.h:96
static HTAB * WaitEventCustomHashByName
Definition wait_event.c:65
#define WAIT_EVENT_CLASS_MASK
Definition wait_event.c:44

References fb(), hash_get_num_entries(), hash_seq_init(), hash_seq_search(), LW_SHARED, LWLockAcquire(), LWLockRelease(), palloc_array, pstrdup(), WAIT_EVENT_CLASS_MASK, and WaitEventCustomHashByName.

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

static const char * pgstat_get_wait_buffer ( WaitEventBuffer  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 417 of file wait_event.c.

418{
419 uint32 classId;
421 const char *event_name;
422
423 /* report process as not waiting. */
424 if (wait_event_info == 0)
425 return NULL;
426
427 classId = wait_event_info & WAIT_EVENT_CLASS_MASK;
428 eventId = wait_event_info & WAIT_EVENT_ID_MASK;
429
430 switch (classId)
431 {
432 case PG_WAIT_LWLOCK:
434 break;
435 case PG_WAIT_LOCK:
437 break;
440 event_name = GetWaitEventCustomIdentifier(wait_event_info);
441 break;
442 case PG_WAIT_BUFFER:
443 {
444 WaitEventBuffer w = (WaitEventBuffer) wait_event_info;
445
447 break;
448 }
449 case PG_WAIT_ACTIVITY:
450 {
451 WaitEventActivity w = (WaitEventActivity) wait_event_info;
452
454 break;
455 }
456 case PG_WAIT_CLIENT:
457 {
458 WaitEventClient w = (WaitEventClient) wait_event_info;
459
461 break;
462 }
463 case PG_WAIT_IPC:
464 {
465 WaitEventIPC w = (WaitEventIPC) wait_event_info;
466
468 break;
469 }
470 case PG_WAIT_TIMEOUT:
471 {
472 WaitEventTimeout w = (WaitEventTimeout) wait_event_info;
473
475 break;
476 }
477 case PG_WAIT_IO:
478 {
479 WaitEventIO w = (WaitEventIO) wait_event_info;
480
482 break;
483 }
484 default:
485 event_name = "unknown wait event";
486 break;
487 }
488
489 return event_name;
490}
uint16_t uint16
Definition c.h:623
uint32_t uint32
Definition c.h:624
const char * GetLockNameFromTagType(uint16 locktag_type)
Definition lmgr.c:1346
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition lwlock.c:747
#define PG_WAIT_TIMEOUT
#define PG_WAIT_INJECTIONPOINT
#define PG_WAIT_LWLOCK
#define PG_WAIT_BUFFER
#define PG_WAIT_IPC
#define PG_WAIT_CLIENT
#define PG_WAIT_ACTIVITY
#define PG_WAIT_LOCK
#define PG_WAIT_IO
static const char * pgstat_get_wait_timeout(WaitEventTimeout w)
static const char * pgstat_get_wait_activity(WaitEventActivity w)
static const char * pgstat_get_wait_io(WaitEventIO w)
static const char * GetWaitEventCustomIdentifier(uint32 wait_event_info)
Definition wait_event.c:262
static const char * pgstat_get_wait_client(WaitEventClient w)
static const char * pgstat_get_wait_ipc(WaitEventIPC w)
#define WAIT_EVENT_ID_MASK
Definition wait_event.c:45
static const char * pgstat_get_wait_buffer(WaitEventBuffer w)

References fb(), GetLockNameFromTagType(), GetLWLockIdentifier(), GetWaitEventCustomIdentifier(), PG_WAIT_ACTIVITY, PG_WAIT_BUFFER, PG_WAIT_CLIENT, PG_WAIT_EXTENSION, PG_WAIT_INJECTIONPOINT, PG_WAIT_IO, PG_WAIT_IPC, PG_WAIT_LOCK, PG_WAIT_LWLOCK, PG_WAIT_TIMEOUT, pgstat_get_wait_activity(), pgstat_get_wait_buffer(), 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 359 of file wait_event.c.

360{
361 uint32 classId;
362 const char *event_type;
363
364 /* report process as not waiting. */
365 if (wait_event_info == 0)
366 return NULL;
367
368 classId = wait_event_info & WAIT_EVENT_CLASS_MASK;
369
370 switch (classId)
371 {
372 case PG_WAIT_LWLOCK:
373 event_type = "LWLock";
374 break;
375 case PG_WAIT_LOCK:
376 event_type = "Lock";
377 break;
378 case PG_WAIT_BUFFER:
379 event_type = "Buffer";
380 break;
381 case PG_WAIT_ACTIVITY:
382 event_type = "Activity";
383 break;
384 case PG_WAIT_CLIENT:
385 event_type = "Client";
386 break;
388 event_type = "Extension";
389 break;
390 case PG_WAIT_IPC:
391 event_type = "IPC";
392 break;
393 case PG_WAIT_TIMEOUT:
394 event_type = "Timeout";
395 break;
396 case PG_WAIT_IO:
397 event_type = "IO";
398 break;
400 event_type = "InjectionPoint";
401 break;
402 default:
403 event_type = "???";
404 break;
405 }
406
407 return event_type;
408}

References fb(), PG_WAIT_ACTIVITY, PG_WAIT_BUFFER, PG_WAIT_CLIENT, PG_WAIT_EXTENSION, PG_WAIT_INJECTIONPOINT, PG_WAIT_IO, PG_WAIT_IPC, PG_WAIT_LOCK, PG_WAIT_LWLOCK, PG_WAIT_TIMEOUT, and WAIT_EVENT_CLASS_MASK.

Referenced by pg_isolation_test_session_is_blocked(), pg_stat_get_activity(), pg_stat_get_backend_wait_event_type(), and WaitEventCustomNew().

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

348{
350}
static uint32 local_my_wait_event_info
Definition wait_event.c:41
uint32 * my_wait_event_info
Definition wait_event.c:42

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

336{
337 my_wait_event_info = wait_event_info;
338}

References my_wait_event_info.

Referenced by InitAuxiliaryProcess(), and InitProcess().

◆ WaitEventCustomNew()

static uint32 WaitEventCustomNew ( uint32  classId,
const char wait_event_name 
)
static

Definition at line 161 of file wait_event.c.

162{
164 bool found;
167 uint32 wait_event_info;
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 */
181 hash_search(WaitEventCustomHashByName, wait_event_name,
182 HASH_FIND, &found);
184 if (found)
185 {
187
189 if (oldClassId != classId)
192 errmsg("wait event \"%s\" already exists in type \"%s\"",
193 wait_event_name,
194 pgstat_get_wait_event_type(entry_by_name->wait_event_info))));
195 return entry_by_name->wait_event_info;
196 }
197
198 /*
199 * Allocate and register a new wait event. Recheck if the event name
200 * exists, as it could be possible that a concurrent process has inserted
201 * one with the same name since the LWLock acquired again here was
202 * previously released.
203 */
206 hash_search(WaitEventCustomHashByName, wait_event_name,
207 HASH_FIND, &found);
208 if (found)
209 {
211
214 if (oldClassId != classId)
217 errmsg("wait event \"%s\" already exists in type \"%s\"",
218 wait_event_name,
219 pgstat_get_wait_event_type(entry_by_name->wait_event_info))));
220 return entry_by_name->wait_event_info;
221 }
222
223 /* Allocate a new event Id */
225
227 {
231 errmsg("too many custom wait events"));
232 }
233
235
237
238 /* Register the new wait event */
239 wait_event_info = classId | eventId;
241 hash_search(WaitEventCustomHashByInfo, &wait_event_info,
242 HASH_ENTER, &found);
243 Assert(!found);
244 strlcpy(entry_by_info->wait_event_name, wait_event_name,
245 sizeof(entry_by_info->wait_event_name));
246
248 hash_search(WaitEventCustomHashByName, wait_event_name,
249 HASH_ENTER, &found);
250 Assert(!found);
251 entry_by_name->wait_event_info = wait_event_info;
252
254
255 return wait_event_info;
256}
#define Assert(condition)
Definition c.h:943
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereport(elevel,...)
Definition elog.h:152
@ HASH_ENTER
Definition hsearch.h:109
@ LW_EXCLUSIVE
Definition lwlock.h:104
static char * errmsg
#define NAMEDATALEN
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
#define ERRCODE_DUPLICATE_OBJECT
Definition streamutil.c:30
static WaitEventCustomCounterData * WaitEventCustomCounter
Definition wait_event.c:91
const char * pgstat_get_wait_event_type(uint32 wait_event_info)
Definition wait_event.c:359
#define WAIT_EVENT_CUSTOM_HASH_SIZE
Definition wait_event.c:67

References Assert, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg, ERROR, fb(), HASH_ENTER, HASH_FIND, hash_search(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), WaitEventCustomCounterData::mutex, NAMEDATALEN, WaitEventCustomCounterData::nextId, pgstat_get_wait_event_type(), SpinLockAcquire(), SpinLockRelease(), strlcpy(), WAIT_EVENT_CLASS_MASK, WAIT_EVENT_CUSTOM_HASH_SIZE, WaitEventCustomCounter, WaitEventCustomHashByInfo, and WaitEventCustomHashByName.

Referenced by WaitEventExtensionNew(), and WaitEventInjectionPointNew().

◆ WaitEventCustomShmemInit()

static void WaitEventCustomShmemInit ( void arg)
static

Definition at line 135 of file wait_event.c.

136{
137 /* initialize the allocation counter and its spinlock. */
140}
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
#define WAIT_EVENT_CUSTOM_INITIAL_ID
Definition wait_event.c:94

References WaitEventCustomCounterData::mutex, WaitEventCustomCounterData::nextId, SpinLockInit(), WAIT_EVENT_CUSTOM_INITIAL_ID, and WaitEventCustomCounter.

◆ WaitEventCustomShmemRequest()

static void WaitEventCustomShmemRequest ( void arg)
static

Definition at line 111 of file wait_event.c.

112{
113 ShmemRequestStruct(.name = "WaitEventCustomCounterData",
114 .size = sizeof(WaitEventCustomCounterData),
115 .ptr = (void **) &WaitEventCustomCounter,
116 );
117 ShmemRequestHash(.name = "WaitEventCustom hash by wait event information",
120 .hash_info.keysize = sizeof(uint32),
121 .hash_info.entrysize = sizeof(WaitEventCustomEntryByInfo),
122 .hash_flags = HASH_ELEM | HASH_BLOBS,
123 );
124 ShmemRequestHash(.name = "WaitEventCustom hash by name",
127 /* key is a NULL-terminated string */
128 .hash_info.keysize = sizeof(char[NAMEDATALEN]),
129 .hash_info.entrysize = sizeof(WaitEventCustomEntryByName),
130 .hash_flags = HASH_ELEM | HASH_STRINGS,
131 );
132}
#define HASH_STRINGS
Definition hsearch.h:91
#define HASH_ELEM
Definition hsearch.h:90
#define HASH_BLOBS
Definition hsearch.h:92
#define ShmemRequestHash(...)
Definition shmem.h:179
#define ShmemRequestStruct(...)
Definition shmem.h:176
Size keysize
Definition dynahash.c:241
const char * name

References HASH_BLOBS, HASH_ELEM, HASH_STRINGS, HTAB::keysize, name, NAMEDATALEN, ShmemRequestHash, ShmemRequestStruct, WAIT_EVENT_CUSTOM_HASH_SIZE, WaitEventCustomCounter, WaitEventCustomHashByInfo, and WaitEventCustomHashByName.

◆ WaitEventExtensionNew()

uint32 WaitEventExtensionNew ( const char wait_event_name)

Definition at line 149 of file wait_event.c.

150{
151 return WaitEventCustomNew(PG_WAIT_EXTENSION, wait_event_name);
152}
static uint32 WaitEventCustomNew(uint32 classId, const char *wait_event_name)
Definition wait_event.c:161

References PG_WAIT_EXTENSION, and WaitEventCustomNew().

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

◆ WaitEventInjectionPointNew()

uint32 WaitEventInjectionPointNew ( const char wait_event_name)

Definition at line 155 of file wait_event.c.

156{
157 return WaitEventCustomNew(PG_WAIT_INJECTIONPOINT, wait_event_name);
158}

References PG_WAIT_INJECTIONPOINT, and WaitEventCustomNew().

Referenced by injection_wait(), and test_aio_shmem_init().

Variable Documentation

◆ local_my_wait_event_info

uint32 local_my_wait_event_info
static

Definition at line 41 of file wait_event.c.

Referenced by pgstat_reset_wait_event_storage().

◆ my_wait_event_info

◆ WaitEventCustomCounter

WaitEventCustomCounterData* WaitEventCustomCounter
static

◆ WaitEventCustomHashByInfo

HTAB* WaitEventCustomHashByInfo
static

◆ WaitEventCustomHashByName

HTAB* WaitEventCustomHashByName
static

◆ WaitEventCustomShmemCallbacks

const ShmemCallbacks WaitEventCustomShmemCallbacks
Initial value:
= {
}
static void WaitEventCustomShmemRequest(void *arg)
Definition wait_event.c:111
static void WaitEventCustomShmemInit(void *arg)
Definition wait_event.c:135

Definition at line 102 of file wait_event.c.

102 {
103 .request_fn = WaitEventCustomShmemRequest,
104 .init_fn = WaitEventCustomShmemInit,
105};