Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c319991

Browse filesBrowse files
committed
Use separate lwlock tranches for buffer, lock, and predicate lock managers.
This finishes the work - spread across many commits over the last several months - of putting each type of lock other than the named individual locks into a separate tranche. Amit Kapila
1 parent b11d07b commit c319991
Copy full SHA for c319991

File tree

Expand file treeCollapse file tree

2 files changed

+106
-43
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+106
-43
lines changed

‎src/backend/storage/lmgr/lwlock.c

Copy file name to clipboardExpand all lines: src/backend/storage/lmgr/lwlock.c
+103-43Lines changed: 103 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ static int LWLockTranchesAllocated = 0;
125125
*/
126126
LWLockPadded *MainLWLockArray = NULL;
127127
static LWLockTranche MainLWLockTranche;
128+
static LWLockTranche BufMappingLWLockTranche;
129+
static LWLockTranche LockManagerLWLockTranche;
130+
static LWLockTranche PredicateLockManagerLWLockTranche;
128131

129132
/*
130133
* We use this structure to keep track of locked LWLocks for release
@@ -159,6 +162,9 @@ NamedLWLockTranche *NamedLWLockTrancheArray = NULL;
159162

160163
static bool lock_named_request_allowed = true;
161164

165+
static void InitializeLWLocks(void);
166+
static void RegisterLWLockTranches(void);
167+
162168
#ifdef LWLOCK_STATS
163169
typedef struct lwlock_stats_key
164170
{
@@ -395,14 +401,12 @@ LWLockShmemSize(void)
395401
}
396402

397403
/*
398-
* Allocate shmem space for the main LWLock array and named tranches and
399-
* initialize it. We also register the main and named tranche here.
404+
* Allocate shmem space for the main LWLock array and all tranches and
405+
* initialize it. We also register all the LWLock tranches here.
400406
*/
401407
void
402408
CreateLWLocks(void)
403409
{
404-
int i;
405-
406410
StaticAssertExpr(LW_VAL_EXCLUSIVE > (uint32) MAX_BACKENDS,
407411
"MAX_BACKENDS too big for lwlock.c");
408412

@@ -412,14 +416,9 @@ CreateLWLocks(void)
412416

413417
if (!IsUnderPostmaster)
414418
{
415-
int numLocks = NUM_FIXED_LWLOCKS;
416-
int numNamedLocks = NumLWLocksByNamedTranches();
417419
Size spaceLocks = LWLockShmemSize();
418-
LWLockPadded *lock;
419420
int *LWLockCounter;
420421
char *ptr;
421-
int id;
422-
int j;
423422

424423
/* Allocate space */
425424
ptr = (char *) ShmemAlloc(spaceLocks);
@@ -432,55 +431,99 @@ CreateLWLocks(void)
432431

433432
MainLWLockArray = (LWLockPadded *) ptr;
434433

435-
/* Initialize all fixed LWLocks in main array */
436-
for (id = 0, lock = MainLWLockArray; id < numLocks; id++, lock++)
437-
LWLockInitialize(&lock->lock, LWTRANCHE_MAIN);
438-
439434
/*
440435
* Initialize the dynamic-allocation counter for tranches, which is
441436
* stored just before the first LWLock.
442437
*/
443438
LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
444439
*LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
445440

446-
/* Initialize named tranches. */
447-
if (NamedLWLockTrancheRequests > 0)
448-
{
449-
char *trancheNames;
441+
/* Initialize all LWLocks */
442+
InitializeLWLocks();
443+
}
444+
445+
/* Register all LWLock tranches */
446+
RegisterLWLockTranches();
447+
}
448+
449+
/*
450+
* Initialize LWLocks that are fixed and those belonging to named tranches.
451+
*/
452+
static void
453+
InitializeLWLocks(void)
454+
{
455+
int numNamedLocks = NumLWLocksByNamedTranches();
456+
int id;
457+
int i;
458+
int j;
459+
LWLockPadded *lock;
460+
461+
/* Initialize all individual LWLocks in main array */
462+
for (id = 0, lock = MainLWLockArray; id < NUM_INDIVIDUAL_LWLOCKS; id++, lock++)
463+
LWLockInitialize(&lock->lock, LWTRANCHE_MAIN);
464+
465+
/* Initialize buffer mapping LWLocks in main array */
466+
lock = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS;
467+
for (id = 0; id < NUM_BUFFER_PARTITIONS; id++, lock++)
468+
LWLockInitialize(&lock->lock, LWTRANCHE_BUFFER_MAPPING);
469+
470+
/* Initialize lmgrs' LWLocks in main array */
471+
lock = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS + NUM_BUFFER_PARTITIONS;
472+
for (id = 0; id < NUM_LOCK_PARTITIONS; id++, lock++)
473+
LWLockInitialize(&lock->lock, LWTRANCHE_LOCK_MANAGER);
474+
475+
/* Initialize predicate lmgrs' LWLocks in main array */
476+
lock = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS +
477+
NUM_BUFFER_PARTITIONS + NUM_LOCK_PARTITIONS;
478+
for (id = 0; id < NUM_PREDICATELOCK_PARTITIONS; id++, lock++)
479+
LWLockInitialize(&lock->lock, LWTRANCHE_PREDICATE_LOCK_MANAGER);
480+
481+
/* Initialize named tranches. */
482+
if (NamedLWLockTrancheRequests > 0)
483+
{
484+
char *trancheNames;
450485

451-
NamedLWLockTrancheArray = (NamedLWLockTranche *)
452-
&MainLWLockArray[numLocks + numNamedLocks];
486+
NamedLWLockTrancheArray = (NamedLWLockTranche *)
487+
&MainLWLockArray[NUM_FIXED_LWLOCKS + numNamedLocks];
453488

454-
trancheNames = (char *) NamedLWLockTrancheArray +
455-
(NamedLWLockTrancheRequests * sizeof(NamedLWLockTranche));
456-
lock = &MainLWLockArray[numLocks];
489+
trancheNames = (char *) NamedLWLockTrancheArray +
490+
(NamedLWLockTrancheRequests * sizeof(NamedLWLockTranche));
491+
lock = &MainLWLockArray[NUM_FIXED_LWLOCKS];
457492

458-
for (i = 0; i < NamedLWLockTrancheRequests; i++)
459-
{
460-
NamedLWLockTrancheRequest *request;
461-
NamedLWLockTranche *tranche;
462-
char *name;
463-
464-
request = &NamedLWLockTrancheRequestArray[i];
465-
tranche = &NamedLWLockTrancheArray[i];
466-
467-
name = trancheNames;
468-
trancheNames += strlen(request->tranche_name) + 1;
469-
strcpy(name, request->tranche_name);
470-
tranche->lwLockTranche.name = name;
471-
tranche->trancheId = LWLockNewTrancheId();
472-
tranche->lwLockTranche.array_base = lock;
473-
tranche->lwLockTranche.array_stride = sizeof(LWLockPadded);
474-
475-
for (j = 0; j < request->num_lwlocks; j++, lock++)
476-
LWLockInitialize(&lock->lock, tranche->trancheId);
477-
}
493+
for (i = 0; i < NamedLWLockTrancheRequests; i++)
494+
{
495+
NamedLWLockTrancheRequest *request;
496+
NamedLWLockTranche *tranche;
497+
char *name;
498+
499+
request = &NamedLWLockTrancheRequestArray[i];
500+
tranche = &NamedLWLockTrancheArray[i];
501+
502+
name = trancheNames;
503+
trancheNames += strlen(request->tranche_name) + 1;
504+
strcpy(name, request->tranche_name);
505+
tranche->lwLockTranche.name = name;
506+
tranche->trancheId = LWLockNewTrancheId();
507+
tranche->lwLockTranche.array_base = lock;
508+
tranche->lwLockTranche.array_stride = sizeof(LWLockPadded);
509+
510+
for (j = 0; j < request->num_lwlocks; j++, lock++)
511+
LWLockInitialize(&lock->lock, tranche->trancheId);
478512
}
479513
}
514+
}
515+
516+
/*
517+
* Register named tranches and tranches for fixed LWLocks.
518+
*/
519+
static void
520+
RegisterLWLockTranches(void)
521+
{
522+
int i;
480523

481524
if (LWLockTrancheArray == NULL)
482525
{
483-
LWLockTranchesAllocated = 16;
526+
LWLockTranchesAllocated = 32;
484527
LWLockTrancheArray = (LWLockTranche **)
485528
MemoryContextAlloc(TopMemoryContext,
486529
LWLockTranchesAllocated * sizeof(LWLockTranche *));
@@ -492,6 +535,23 @@ CreateLWLocks(void)
492535
MainLWLockTranche.array_stride = sizeof(LWLockPadded);
493536
LWLockRegisterTranche(LWTRANCHE_MAIN, &MainLWLockTranche);
494537

538+
BufMappingLWLockTranche.name = "buffer_mapping";
539+
BufMappingLWLockTranche.array_base = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS;
540+
BufMappingLWLockTranche.array_stride = sizeof(LWLockPadded);
541+
LWLockRegisterTranche(LWTRANCHE_BUFFER_MAPPING, &BufMappingLWLockTranche);
542+
543+
LockManagerLWLockTranche.name = "lock_manager";
544+
LockManagerLWLockTranche.array_base = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS +
545+
NUM_BUFFER_PARTITIONS;
546+
LockManagerLWLockTranche.array_stride = sizeof(LWLockPadded);
547+
LWLockRegisterTranche(LWTRANCHE_LOCK_MANAGER, &LockManagerLWLockTranche);
548+
549+
PredicateLockManagerLWLockTranche.name = "predicate_lock_manager";
550+
PredicateLockManagerLWLockTranche.array_base = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS +
551+
NUM_BUFFER_PARTITIONS + NUM_LOCK_PARTITIONS;
552+
PredicateLockManagerLWLockTranche.array_stride = sizeof(LWLockPadded);
553+
LWLockRegisterTranche(LWTRANCHE_PREDICATE_LOCK_MANAGER, &PredicateLockManagerLWLockTranche);
554+
495555
/* Register named tranches. */
496556
for (i = 0; i < NamedLWLockTrancheRequests; i++)
497557
LWLockRegisterTranche(NamedLWLockTrancheArray[i].trancheId,

‎src/include/storage/lwlock.h

Copy file name to clipboardExpand all lines: src/include/storage/lwlock.h
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ typedef enum BuiltinTrancheIds
233233
LWTRANCHE_REPLICATION_ORIGIN,
234234
LWTRANCHE_REPLICATION_SLOT_IO_IN_PROGRESS,
235235
LWTRANCHE_PROC,
236+
LWTRANCHE_BUFFER_MAPPING,
237+
LWTRANCHE_LOCK_MANAGER,
238+
LWTRANCHE_PREDICATE_LOCK_MANAGER,
236239
LWTRANCHE_FIRST_USER_DEFINED
237240
} BuiltinTrancheIds;
238241

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.