From 2b28c29d68eb2c37137aef66c7634b65aa640520 Mon Sep 17 00:00:00 2001 From: Kyotaro Horiguchi Date: Wed, 13 Oct 2021 10:31:26 +0900 Subject: [PATCH v2 1/5] procfreelist in ascending order --- src/backend/storage/lmgr/proc.c | 62 +++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index b7d9da0aa9..78e05976a4 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -163,6 +163,9 @@ InitProcGlobal(void) j; bool found; uint32 TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts; + PGPROC **freelist; + int switchpoint; + int arraykind; /* Create the ProcGlobal shared structure */ ProcGlobal = (PROC_HDR *) @@ -214,6 +217,8 @@ InitProcGlobal(void) ProcGlobal->statusFlags = (uint8 *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->statusFlags)); MemSet(ProcGlobal->statusFlags, 0, TotalProcs * sizeof(*ProcGlobal->statusFlags)); + switchpoint = 0; + arraykind = 0; for (i = 0; i < TotalProcs; i++) { /* Common initialization for all PGPROCs, regardless of type. */ @@ -239,33 +244,38 @@ InitProcGlobal(void) * linear search. PGPROCs for prepared transactions are added to a * free list by TwoPhaseShmemInit(). */ - if (i < MaxConnections) + if (i < MaxBackends) { - /* PGPROC for normal backend, add to freeProcs list */ - procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs; - ProcGlobal->freeProcs = &procs[i]; - procs[i].procgloballist = &ProcGlobal->freeProcs; - } - else if (i < MaxConnections + autovacuum_max_workers + 1) - { - /* PGPROC for AV launcher/worker, add to autovacFreeProcs list */ - procs[i].links.next = (SHM_QUEUE *) ProcGlobal->autovacFreeProcs; - ProcGlobal->autovacFreeProcs = &procs[i]; - procs[i].procgloballist = &ProcGlobal->autovacFreeProcs; - } - else if (i < MaxConnections + autovacuum_max_workers + 1 + max_worker_processes) - { - /* PGPROC for bgworker, add to bgworkerFreeProcs list */ - procs[i].links.next = (SHM_QUEUE *) ProcGlobal->bgworkerFreeProcs; - ProcGlobal->bgworkerFreeProcs = &procs[i]; - procs[i].procgloballist = &ProcGlobal->bgworkerFreeProcs; - } - else if (i < MaxBackends) - { - /* PGPROC for walsender, add to walsenderFreeProcs list */ - procs[i].links.next = (SHM_QUEUE *) ProcGlobal->walsenderFreeProcs; - ProcGlobal->walsenderFreeProcs = &procs[i]; - procs[i].procgloballist = &ProcGlobal->walsenderFreeProcs; + if (i == switchpoint) + { + switch (arraykind++) + { + case 0: + freelist = &ProcGlobal->freeProcs; + switchpoint += MaxConnections; + break; + + case 1: + freelist = &ProcGlobal->autovacFreeProcs; + switchpoint += autovacuum_max_workers + 1; + break; + + case 2: + freelist = &ProcGlobal->bgworkerFreeProcs; + switchpoint += max_worker_processes; + break; + + case 3: + freelist = &ProcGlobal->walsenderFreeProcs; + } + + /* link the element to the just-switched freelist */ + *freelist = &procs[i]; + } + else + procs[i - 1].links.next = (SHM_QUEUE *) &procs[i]; + + procs[i].procgloballist = freelist; } /* Initialize myProcLocks[] shared memory queues. */ -- 2.27.0