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");
130 #define PG_LWLOCK(id, lockname) [id] = CppAsString(lockname) "Lock",
176 "missing entries in BuiltinTrancheNames[]");
199 #define MAX_SIMUL_LWLOCKS 200
237 #define T_NAME(lock) \
238 GetLWTrancheName((lock)->tranche)
241 typedef struct lwlock_stats_key
247 typedef struct lwlock_stats
249 lwlock_stats_key
key;
250 int sh_acquire_count;
251 int ex_acquire_count;
253 int dequeue_self_count;
254 int spin_delay_count;
257 static HTAB *lwlock_stats_htab;
258 static lwlock_stats lwlock_stats_dummy;
262 bool Trace_lwlocks =
false;
275 errmsg_internal(
"%d: %s(%s %p): excl %u shared %u haswaiters %u waiters %u rOK %d",
277 where,
T_NAME(lock), lock,
296 T_NAME(lock), lock, msg)));
301 #define PRINT_LWDEBUG(a,b,c) ((void)0)
302 #define LOG_LWDEBUG(a,b,c) ((void)0)
307 static void init_lwlock_stats(
void);
308 static void print_lwlock_stats(
int code,
Datum arg);
309 static lwlock_stats * get_lwlock_stats_entry(
LWLock *lock);
312 init_lwlock_stats(
void)
316 static bool exit_registered =
false;
318 if (lwlock_stats_cxt != NULL)
334 ctl.keysize =
sizeof(lwlock_stats_key);
335 ctl.entrysize =
sizeof(lwlock_stats);
336 ctl.hcxt = lwlock_stats_cxt;
339 if (!exit_registered)
342 exit_registered =
true;
347 print_lwlock_stats(
int code,
Datum arg)
350 lwlock_stats *lwstats;
360 "PID %d lwlock %s %p: shacq %u exacq %u blk %u spindelay %u dequeue self %u\n",
362 lwstats->key.instance, lwstats->sh_acquire_count,
363 lwstats->ex_acquire_count, lwstats->block_count,
364 lwstats->spin_delay_count, lwstats->dequeue_self_count);
370 static lwlock_stats *
371 get_lwlock_stats_entry(
LWLock *lock)
373 lwlock_stats_key
key;
374 lwlock_stats *lwstats;
382 if (lwlock_stats_htab == NULL)
383 return &lwlock_stats_dummy;
392 lwstats->sh_acquire_count = 0;
393 lwstats->ex_acquire_count = 0;
394 lwstats->block_count = 0;
395 lwstats->dequeue_self_count = 0;
396 lwstats->spin_delay_count = 0;
502 for (
id = 0, lock =
MainLWLockArray;
id < NUM_INDIVIDUAL_LWLOCKS;
id++, lock++)
596 elog(
ERROR,
"requested tranche is not registered");
613 result = (*LWLockCounter)++;
649 newalloc *
sizeof(
char *));
677 elog(
FATAL,
"cannot request additional LWLocks outside shmem_request_hook");
804 desired_state = old_state;
830 &old_state, desired_state))
861 lwlock_stats *lwstats;
864 lwstats = get_lwlock_stats_entry(lock);
886 delays += delayStatus.
delays;
898 lwstats->spin_delay_count += delays;
925 bool wokeup_somebody =
false;
931 new_release_ok =
true;
953 new_release_ok =
false;
958 wokeup_somebody =
true;
988 desired_state = old_state;
1013 LOG_LWDEBUG(
"LWLockRelease", lock,
"release waiter");
1046 elog(
PANIC,
"cannot wait without a PGPROC structure");
1049 elog(
PANIC,
"queueing for lock while waiting on another one");
1086 lwlock_stats *lwstats;
1088 lwstats = get_lwlock_stats_entry(lock);
1090 lwstats->dequeue_self_count++;
1147 while (extraWaits-- > 0)
1176 lwlock_stats *lwstats;
1178 lwstats = get_lwlock_stats_entry(lock);
1188 lwstats->ex_acquire_count++;
1190 lwstats->sh_acquire_count++;
1239 LOG_LWDEBUG(
"LWLockAcquire", lock,
"immediately acquired lock");
1263 LOG_LWDEBUG(
"LWLockAcquire", lock,
"acquired, undoing queue");
1280 lwstats->block_count++;
1284 if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1285 TRACE_POSTGRESQL_LWLOCK_WAIT_START(
T_NAME(lock),
mode);
1307 if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1308 TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(
T_NAME(lock),
mode);
1317 if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_ENABLED())
1318 TRACE_POSTGRESQL_LWLOCK_ACQUIRE(
T_NAME(lock),
mode);
1327 while (extraWaits-- > 0)
1368 LOG_LWDEBUG(
"LWLockConditionalAcquire", lock,
"failed");
1369 if (TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_FAIL_ENABLED())
1370 TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_FAIL(
T_NAME(lock),
mode);
1377 if (TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_ENABLED())
1378 TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE(
T_NAME(lock),
mode);
1404 lwlock_stats *lwstats;
1406 lwstats = get_lwlock_stats_entry(lock);
1442 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"waiting");
1445 lwstats->block_count++;
1449 if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1450 TRACE_POSTGRESQL_LWLOCK_WAIT_START(
T_NAME(lock),
mode);
1468 if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1469 TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(
T_NAME(lock),
mode);
1472 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"awakened");
1476 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"acquired, undoing queue");
1491 while (extraWaits-- > 0)
1498 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"failed");
1499 if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_FAIL_ENABLED())
1500 TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_FAIL(
T_NAME(lock),
mode);
1504 LOG_LWDEBUG(
"LWLockAcquireOrWait", lock,
"succeeded");
1508 if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_ENABLED())
1509 TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT(
T_NAME(lock),
mode);
1526 uint64 *
newval,
bool *result)
1555 if (
value != oldval)
1591 bool result =
false;
1593 lwlock_stats *lwstats;
1595 lwstats = get_lwlock_stats_entry(lock);
1646 LOG_LWDEBUG(
"LWLockWaitForVar", lock,
"free, undoing queue");
1663 lwstats->block_count++;
1667 if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1687 if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1691 LOG_LWDEBUG(
"LWLockWaitForVar", lock,
"awakened");
1699 while (extraWaits-- > 0)
1821 if (TRACE_POSTGRESQL_LWLOCK_RELEASE_ENABLED())
1822 TRACE_POSTGRESQL_LWLOCK_RELEASE(
T_NAME(lock));
1831 check_waiters =
true;
1833 check_waiters =
false;
1842 LOG_LWDEBUG(
"LWLockRelease", lock,
"releasing waiters");
1915 char *held_lock_addr;
1920 begin = (
char *) lock;
1921 end = begin + nlocks * stride;
1925 if (held_lock_addr >= begin &&
1926 held_lock_addr < end &&
1927 (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 Assert(condition)
#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)
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)
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_PARALLEL_VACUUM_DSA
@ 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]