97 #define LW_FLAG_HAS_WAITERS ((uint32) 1 << 30)
98 #define LW_FLAG_RELEASE_OK ((uint32) 1 << 29)
99 #define LW_FLAG_LOCKED ((uint32) 1 << 28)
101 #define LW_VAL_EXCLUSIVE ((uint32) 1 << 24)
102 #define LW_VAL_SHARED 1
104 #define LW_LOCK_MASK ((uint32) ((1 << 25)-1))
106 #define LW_SHARED_MASK ((uint32) ((1 << 24)-1))
109 "MAX_BACKENDS too big for lwlock.c");
174 "missing entries in BuiltinTrancheNames[]");
197 #define MAX_SIMUL_LWLOCKS 200
235 #define T_NAME(lock) \
236 GetLWTrancheName((lock)->tranche)
239 typedef struct lwlock_stats_key
245 typedef struct lwlock_stats
247 lwlock_stats_key
key;
248 int sh_acquire_count;
249 int ex_acquire_count;
251 int dequeue_self_count;
252 int spin_delay_count;
255 static HTAB *lwlock_stats_htab;
256 static lwlock_stats lwlock_stats_dummy;
260 bool Trace_lwlocks =
false;
273 errmsg_internal(
"%d: %s(%s %p): excl %u shared %u haswaiters %u waiters %u rOK %d",
275 where,
T_NAME(lock), lock,
294 T_NAME(lock), lock, msg)));
299 #define PRINT_LWDEBUG(a,b,c) ((void)0)
300 #define LOG_LWDEBUG(a,b,c) ((void)0)
305 static void init_lwlock_stats(
void);
306 static void print_lwlock_stats(
int code,
Datum arg);
307 static lwlock_stats * get_lwlock_stats_entry(
LWLock *lock);
310 init_lwlock_stats(
void)
314 static bool exit_registered =
false;
316 if (lwlock_stats_cxt != NULL)
332 ctl.
keysize =
sizeof(lwlock_stats_key);
334 ctl.
hcxt = lwlock_stats_cxt;
335 lwlock_stats_htab =
hash_create(
"lwlock stats", 16384, &ctl,
337 if (!exit_registered)
340 exit_registered =
true;
345 print_lwlock_stats(
int code,
Datum arg)
348 lwlock_stats *lwstats;
358 "PID %d lwlock %s %p: shacq %u exacq %u blk %u spindelay %u dequeue self %u\n",
360 lwstats->key.instance, lwstats->sh_acquire_count,
361 lwstats->ex_acquire_count, lwstats->block_count,
362 lwstats->spin_delay_count, lwstats->dequeue_self_count);
368 static lwlock_stats *
369 get_lwlock_stats_entry(
LWLock *lock)
371 lwlock_stats_key
key;
372 lwlock_stats *lwstats;
380 if (lwlock_stats_htab == NULL)
381 return &lwlock_stats_dummy;
390 lwstats->sh_acquire_count = 0;
391 lwstats->ex_acquire_count = 0;
392 lwstats->block_count = 0;
393 lwstats->dequeue_self_count = 0;
394 lwstats->spin_delay_count = 0;
500 for (
id = 0, lock =
MainLWLockArray;
id < NUM_INDIVIDUAL_LWLOCKS;
id++, lock++)
594 elog(
ERROR,
"requested tranche is not registered");
611 result = (*LWLockCounter)++;
647 newalloc *
sizeof(
char *));
675 elog(
FATAL,
"cannot request additional LWLocks outside shmem_request_hook");
746 if (trancheId < NUM_INDIVIDUAL_LWLOCKS)
806 desired_state = old_state;
832 &old_state, desired_state))
863 lwlock_stats *lwstats;
866 lwstats = get_lwlock_stats_entry(lock);
888 delays += delayStatus.
delays;
900 lwstats->spin_delay_count += delays;
927 bool wokeup_somebody =
false;
933 new_release_ok =
true;
955 new_release_ok =
false;
960 wokeup_somebody =
true;
990 desired_state = old_state;
1015 LOG_LWDEBUG(
"LWLockRelease", lock,
"release waiter");
1048 elog(
PANIC,
"cannot wait without a PGPROC structure");
1051 elog(
PANIC,
"queueing for lock while waiting on another one");
1088 lwlock_stats *lwstats;
1090 lwstats = get_lwlock_stats_entry(lock);
1092 lwstats->dequeue_self_count++;
1149 while (extraWaits-- > 0)
1178 lwlock_stats *lwstats;
1180 lwstats = get_lwlock_stats_entry(lock);
1190 lwstats->ex_acquire_count++;
1192 lwstats->sh_acquire_count++;
1241 LOG_LWDEBUG(
"LWLockAcquire", lock,
"immediately acquired lock");
1265 LOG_LWDEBUG(
"LWLockAcquire", lock,
"acquired, undoing queue");
1282 lwstats->block_count++;
1286 if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1287 TRACE_POSTGRESQL_LWLOCK_WAIT_START(
T_NAME(lock),
mode);
1309 if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1310 TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(
T_NAME(lock),
mode);
1319 if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_ENABLED())
1320 TRACE_POSTGRESQL_LWLOCK_ACQUIRE(
T_NAME(lock),
mode);
1329 while (extraWaits-- > 0)
1370 LOG_LWDEBUG(
"LWLockConditionalAcquire", lock,
"failed");
1371 if (TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_FAIL_ENABLED())
1372 TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_FAIL(
T_NAME(lock),
mode);
1379 if (TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_ENABLED())
1380 TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE(
T_NAME(lock),
mode);
1406 lwlock_stats *lwstats;
1408 lwstats = get_lwlock_stats_entry(lock);
1444 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"waiting");
1447 lwstats->block_count++;
1451 if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1452 TRACE_POSTGRESQL_LWLOCK_WAIT_START(
T_NAME(lock),
mode);
1470 if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1471 TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(
T_NAME(lock),
mode);
1474 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"awakened");
1478 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"acquired, undoing queue");
1493 while (extraWaits-- > 0)
1500 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"failed");
1501 if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_FAIL_ENABLED())
1502 TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_FAIL(
T_NAME(lock),
mode);
1506 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"succeeded");
1510 if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_ENABLED())
1511 TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT(
T_NAME(lock),
mode);
1528 uint64 *
newval,
bool *result)
1557 if (
value != oldval)
1593 bool result =
false;
1595 lwlock_stats *lwstats;
1597 lwstats = get_lwlock_stats_entry(lock);
1648 LOG_LWDEBUG(
"LWLockWaitForVar", lock,
"free, undoing queue");
1665 lwstats->block_count++;
1669 if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1689 if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1693 LOG_LWDEBUG(
"LWLockWaitForVar", lock,
"awakened");
1701 while (extraWaits-- > 0)
1823 if (TRACE_POSTGRESQL_LWLOCK_RELEASE_ENABLED())
1824 TRACE_POSTGRESQL_LWLOCK_RELEASE(
T_NAME(lock));
1833 check_waiters =
true;
1835 check_waiters =
false;
1844 LOG_LWDEBUG(
"LWLockRelease", lock,
"releasing waiters");
1917 char *held_lock_addr;
1922 begin = (
char *) lock;
1923 end = begin + nlocks * stride;
1927 if (held_lock_addr >= begin &&
1928 held_lock_addr < end &&
1929 (held_lock_addr - begin) % stride == 0)
static uint32 pg_atomic_fetch_and_u32(volatile pg_atomic_uint32 *ptr, uint32 and_)
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
#define pg_write_barrier()
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
static uint64 pg_atomic_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 newval)
#define PG_USED_FOR_ASSERTS_ONLY
#define MemSet(start, val, len)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
void * hash_seq_search(HASH_SEQ_STATUS *status)
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
int errmsg_internal(const char *fmt,...)
int errhidestmt(bool hide_stmt)
int errhidecontext(bool hide_ctx)
#define ereport(elevel,...)
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Assert(fmt[strlen(fmt) - 1] !='\n')
void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
StaticAssertDecl(LW_VAL_EXCLUSIVE >(uint32) MAX_BACKENDS, "MAX_BACKENDS too big for lwlock.c")
static void LWLockWakeup(LWLock *lock)
bool LWLockHeldByMe(LWLock *lock)
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
static int LWLockTrancheNamesAllocated
void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
const char *const IndividualLWLockNames[]
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
NamedLWLockTranche * NamedLWLockTrancheArray
static bool LWLockAttemptLock(LWLock *lock, LWLockMode mode)
static void LWLockWaitListLock(LWLock *lock)
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
static void LWLockReportWaitEnd(void)
struct LWLockHandle LWLockHandle
bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval)
int LWLockNewTrancheId(void)
static const char * GetLWTrancheName(uint16 trancheId)
int NamedLWLockTrancheRequests
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
#define LW_FLAG_RELEASE_OK
#define LW_FLAG_HAS_WAITERS
#define MAX_SIMUL_LWLOCKS
struct NamedLWLockTrancheRequest NamedLWLockTrancheRequest
static int NumLWLocksForNamedTranches(void)
void LWLockRelease(LWLock *lock)
static int num_held_lwlocks
void LWLockReleaseAll(void)
static void InitializeLWLocks(void)
void LWLockInitialize(LWLock *lock, int tranche_id)
static int NamedLWLockTrancheRequestsAllocated
static const char *const BuiltinTrancheNames[]
static NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
static void LWLockWaitListUnlock(LWLock *lock)
static const char ** LWLockTrancheNames
#define LOG_LWDEBUG(a, b, c)
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
static void LWLockQueueSelf(LWLock *lock, LWLockMode mode)
#define PRINT_LWDEBUG(a, b, c)
static void LWLockReportWaitStart(LWLock *lock)
LWLockPadded * MainLWLockArray
static void LWLockDequeueSelf(LWLock *lock)
Size LWLockShmemSize(void)
bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
static bool LWLockConflictsWithVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval, bool *result)
void InitLWLockAccess(void)
#define LWLOCK_PADDED_SIZE
#define BUFFER_MAPPING_LWLOCK_OFFSET
#define NUM_LOCK_PARTITIONS
@ LWTRANCHE_FIRST_USER_DEFINED
@ LWTRANCHE_SHARED_TIDBITMAP
@ LWTRANCHE_PER_SESSION_DSA
@ LWTRANCHE_PARALLEL_QUERY_DSA
@ LWTRANCHE_COMMITTS_BUFFER
@ LWTRANCHE_SUBTRANS_BUFFER
@ LWTRANCHE_PER_SESSION_RECORD_TYPMOD
@ LWTRANCHE_LAUNCHER_HASH
@ LWTRANCHE_DSM_REGISTRY_DSA
@ LWTRANCHE_DSM_REGISTRY_HASH
@ LWTRANCHE_REPLICATION_ORIGIN_STATE
@ LWTRANCHE_MULTIXACTOFFSET_SLRU
@ LWTRANCHE_PARALLEL_APPEND
@ LWTRANCHE_REPLICATION_SLOT_IO
@ LWTRANCHE_SUBTRANS_SLRU
@ LWTRANCHE_MULTIXACTMEMBER_SLRU
@ LWTRANCHE_BUFFER_CONTENT
@ LWTRANCHE_MULTIXACTMEMBER_BUFFER
@ LWTRANCHE_NOTIFY_BUFFER
@ LWTRANCHE_PER_SESSION_RECORD_TYPE
@ LWTRANCHE_PREDICATE_LOCK_MANAGER
@ LWTRANCHE_BUFFER_MAPPING
@ LWTRANCHE_SERIAL_BUFFER
@ LWTRANCHE_PARALLEL_HASH_JOIN
@ LWTRANCHE_COMMITTS_SLRU
@ LWTRANCHE_PER_XACT_PREDICATE_LIST
@ LWTRANCHE_MULTIXACTOFFSET_BUFFER
@ LWTRANCHE_SHARED_TUPLESTORE
@ LWTRANCHE_LOCK_FASTPATH
#define LOCK_MANAGER_LWLOCK_OFFSET
#define NUM_BUFFER_PARTITIONS
#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET
#define NUM_FIXED_LWLOCKS
#define NUM_PREDICATELOCK_PARTITIONS
MemoryContext TopMemoryContext
void * MemoryContextAllocZero(MemoryContext context, Size size)
void * repalloc(void *pointer, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
void MemoryContextDelete(MemoryContext context)
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define RESUME_INTERRUPTS()
#define HOLD_INTERRUPTS()
bool process_shmem_requests_in_progress
#define repalloc0_array(pointer, type, oldcount, count)
static uint32 pg_nextpower2_32(uint32 num)
static PgChecksumMode mode
size_t strlcpy(char *dst, const char *src, size_t siz)
void PGSemaphoreUnlock(PGSemaphore sema)
void PGSemaphoreLock(PGSemaphore sema)
#define GetPGProcByNumber(n)
#define proclist_delete(list, procno, link_member)
static void proclist_init(proclist_head *list)
#define proclist_push_tail(list, procno, link_member)
#define proclist_push_head(list, procno, link_member)
#define proclist_foreach_modify(iter, lhead, link_member)
static bool proclist_is_empty(const proclist_head *list)
void perform_spin_delay(SpinDelayStatus *status)
void finish_spin_delay(SpinDelayStatus *status)
#define init_local_spin_delay(status)
void * ShmemAlloc(Size size)
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
static pg_noinline void Size size
#define SpinLockRelease(lock)
#define SpinLockAcquire(lock)
char tranche_name[NAMEDATALEN]
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]