Line data Source code
1 : /*------------------------------------------------------------------------- 2 : * wait_event.h 3 : * Definitions related to wait event reporting 4 : * 5 : * Copyright (c) 2001-2024, PostgreSQL Global Development Group 6 : * 7 : * src/include/utils/wait_event.h 8 : * ---------- 9 : */ 10 : #ifndef WAIT_EVENT_H 11 : #define WAIT_EVENT_H 12 : 13 : 14 : /* ---------- 15 : * Wait Classes 16 : * ---------- 17 : */ 18 : #define PG_WAIT_LWLOCK 0x01000000U 19 : #define PG_WAIT_LOCK 0x03000000U 20 : #define PG_WAIT_BUFFERPIN 0x04000000U 21 : #define PG_WAIT_ACTIVITY 0x05000000U 22 : #define PG_WAIT_CLIENT 0x06000000U 23 : #define PG_WAIT_EXTENSION 0x07000000U 24 : #define PG_WAIT_IPC 0x08000000U 25 : #define PG_WAIT_TIMEOUT 0x09000000U 26 : #define PG_WAIT_IO 0x0A000000U 27 : 28 : /* enums for wait events */ 29 : #include "utils/wait_event_types.h" 30 : 31 : extern const char *pgstat_get_wait_event(uint32 wait_event_info); 32 : extern const char *pgstat_get_wait_event_type(uint32 wait_event_info); 33 : static inline void pgstat_report_wait_start(uint32 wait_event_info); 34 : static inline void pgstat_report_wait_end(void); 35 : extern void pgstat_set_wait_event_storage(uint32 *wait_event_info); 36 : extern void pgstat_reset_wait_event_storage(void); 37 : 38 : extern PGDLLIMPORT uint32 *my_wait_event_info; 39 : 40 : 41 : /* ---------- 42 : * Wait Events - Extension 43 : * 44 : * Use this category when the server process is waiting for some condition 45 : * defined by an extension module. 46 : * 47 : * Extensions can define their own wait events in this category. They should 48 : * call WaitEventExtensionNew() with a wait event string. If the wait event 49 : * associated to a string is already allocated, it returns the wait event 50 : * information to use. If not, it gets one wait event ID allocated from 51 : * a shared counter, associates the string to the ID in the shared dynamic 52 : * hash and returns the wait event information. 53 : * 54 : * The ID retrieved can be used with pgstat_report_wait_start() or equivalent. 55 : */ 56 : typedef enum 57 : { 58 : WAIT_EVENT_EXTENSION = PG_WAIT_EXTENSION, 59 : WAIT_EVENT_EXTENSION_FIRST_USER_DEFINED, 60 : } WaitEventExtension; 61 : 62 : extern void WaitEventExtensionShmemInit(void); 63 : extern Size WaitEventExtensionShmemSize(void); 64 : 65 : extern uint32 WaitEventExtensionNew(const char *wait_event_name); 66 : extern char **GetWaitEventExtensionNames(int *nwaitevents); 67 : 68 : /* ---------- 69 : * pgstat_report_wait_start() - 70 : * 71 : * Called from places where server process needs to wait. This is called 72 : * to report wait event information. The wait information is stored 73 : * as 4-bytes where first byte represents the wait event class (type of 74 : * wait, for different types of wait, refer WaitClass) and the next 75 : * 3-bytes represent the actual wait event. Currently 2-bytes are used 76 : * for wait event which is sufficient for current usage, 1-byte is 77 : * reserved for future usage. 78 : * 79 : * Historically we used to make this reporting conditional on 80 : * pgstat_track_activities, but the check for that seems to add more cost 81 : * than it saves. 82 : * 83 : * my_wait_event_info initially points to local memory, making it safe to 84 : * call this before MyProc has been initialized. 85 : * ---------- 86 : */ 87 : static inline void 88 11523996 : pgstat_report_wait_start(uint32 wait_event_info) 89 : { 90 : /* 91 : * Since this is a four-byte field which is always read and written as 92 : * four-bytes, updates are atomic. 93 : */ 94 11523996 : *(volatile uint32 *) my_wait_event_info = wait_event_info; 95 11523996 : } 96 : 97 : /* ---------- 98 : * pgstat_report_wait_end() - 99 : * 100 : * Called to report end of a wait. 101 : * ---------- 102 : */ 103 : static inline void 104 11582468 : pgstat_report_wait_end(void) 105 : { 106 : /* see pgstat_report_wait_start() */ 107 11582468 : *(volatile uint32 *) my_wait_event_info = 0; 108 11582468 : } 109 : 110 : 111 : #endif /* WAIT_EVENT_H */