Skip to content

Commit 81353d4

Browse files
committed
Port to 9.4 (v1)
1 parent a30e8bb commit 81353d4

File tree

28 files changed

+527
-191
lines changed

28 files changed

+527
-191
lines changed

src/backend/access/transam/clog.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ CLOGShmemInit(void)
454454
{
455455
ClogCtl->PagePrecedes = CLOGPagePrecedes;
456456
SimpleLruInit(ClogCtl, "CLOG Ctl", CLOGShmemBuffers(), CLOG_LSNS_PER_PAGE,
457-
CLogControlLock, "pg_clog");
457+
CLogControlLock, "pg_clog",
458+
"CLogBufferLocks");
458459
}
459460

460461
/*

src/backend/access/transam/multixact.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1861,10 +1861,12 @@ MultiXactShmemInit(void)
18611861

18621862
SimpleLruInit(MultiXactOffsetCtl,
18631863
"MultiXactOffset Ctl", NUM_MXACTOFFSET_BUFFERS, 0,
1864-
MultiXactOffsetControlLock, "pg_multixact/offsets");
1864+
MultiXactOffsetControlLock, "pg_multixact/offsets",
1865+
"MultiXactOffsetBufferLocks");
18651866
SimpleLruInit(MultiXactMemberCtl,
18661867
"MultiXactMember Ctl", NUM_MXACTMEMBER_BUFFERS, 0,
1867-
MultiXactMemberControlLock, "pg_multixact/members");
1868+
MultiXactMemberControlLock, "pg_multixact/members",
1869+
"MultiXactMemberBufferLocks");
18681870

18691871
/* Initialize our shared state struct */
18701872
MultiXactState = ShmemInitStruct("Shared MultiXact State",

src/backend/access/transam/slru.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "access/xlog.h"
5757
#include "storage/fd.h"
5858
#include "storage/shmem.h"
59+
#include "storage/wait.h"
5960
#include "miscadmin.h"
6061

6162

@@ -156,15 +157,20 @@ SimpleLruShmemSize(int nslots, int nlsns)
156157
if (nlsns > 0)
157158
sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr)); /* group_lsn[] */
158159

160+
/* size of lwlocks */
161+
sz = add_size(sz, LWLockTrancheShmemSize(nslots));
162+
159163
return BUFFERALIGN(sz) + BLCKSZ * nslots;
160164
}
161165

162166
void
163167
SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
164-
LWLock *ctllock, const char *subdir)
168+
LWLock *ctllock, const char *subdir,
169+
const char *lwlocks_tranche)
165170
{
166-
SlruShared shared;
167-
bool found;
171+
SlruShared shared;
172+
bool found;
173+
LWLockPadded *lwlock_array;
168174

169175
shared = (SlruShared) ShmemInitStruct(name,
170176
SimpleLruShmemSize(nslots, nlsns),
@@ -212,13 +218,18 @@ SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
212218
}
213219

214220
ptr += BUFFERALIGN(offset);
221+
222+
/* Create tranche and lwlocks required for slots */
223+
LWLockCreateTranche(lwlocks_tranche, nslots, &lwlock_array);
224+
225+
/* Initialize slots */
215226
for (slotno = 0; slotno < nslots; slotno++)
216227
{
217228
shared->page_buffer[slotno] = ptr;
218229
shared->page_status[slotno] = SLRU_PAGE_EMPTY;
219230
shared->page_dirty[slotno] = false;
220231
shared->page_lru_count[slotno] = 0;
221-
shared->buffer_locks[slotno] = LWLockAssign();
232+
shared->buffer_locks[slotno] = &lwlock_array[slotno].lock;
222233
ptr += BLCKSZ;
223234
}
224235
}
@@ -663,13 +674,16 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
663674
}
664675

665676
errno = 0;
677+
WAIT_START(WAIT_IO, WAIT_SLRU_READ, pageno,
678+
shared->buffer_locks[0]->tranche, 0, 0, 0);
666679
if (read(fd, shared->page_buffer[slotno], BLCKSZ) != BLCKSZ)
667680
{
668681
slru_errcause = SLRU_READ_FAILED;
669682
slru_errno = errno;
670683
CloseTransientFile(fd);
671684
return false;
672685
}
686+
WAIT_STOP();
673687

674688
if (CloseTransientFile(fd))
675689
{
@@ -822,6 +836,8 @@ SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno, SlruFlush fdata)
822836
}
823837

824838
errno = 0;
839+
WAIT_START(WAIT_IO, WAIT_SLRU_WRITE, pageno,
840+
shared->buffer_locks[0]->tranche, 0, 0, 0);
825841
if (write(fd, shared->page_buffer[slotno], BLCKSZ) != BLCKSZ)
826842
{
827843
/* if write didn't set errno, assume problem is no disk space */
@@ -833,20 +849,24 @@ SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno, SlruFlush fdata)
833849
CloseTransientFile(fd);
834850
return false;
835851
}
852+
WAIT_STOP();
836853

837854
/*
838855
* If not part of Flush, need to fsync now. We assume this happens
839856
* infrequently enough that it's not a performance issue.
840857
*/
841858
if (!fdata)
842859
{
860+
WAIT_START(WAIT_IO, WAIT_SLRU_FSYNC, pageno,
861+
shared->buffer_locks[0]->tranche, 0, 0, 0);
843862
if (ctl->do_fsync && pg_fsync(fd))
844863
{
845864
slru_errcause = SLRU_FSYNC_FAILED;
846865
slru_errno = errno;
847866
CloseTransientFile(fd);
848867
return false;
849868
}
869+
WAIT_STOP();
850870

851871
if (CloseTransientFile(fd))
852872
{

src/backend/access/transam/subtrans.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ SUBTRANSShmemInit(void)
179179
{
180180
SubTransCtl->PagePrecedes = SubTransPagePrecedes;
181181
SimpleLruInit(SubTransCtl, "SUBTRANS Ctl", NUM_SUBTRANS_BUFFERS, 0,
182-
SubtransControlLock, "pg_subtrans");
182+
SubtransControlLock, "pg_subtrans",
183+
"SubtransBufferLocks");
183184
/* Override default assumption that writes should be fsync'd */
184185
SubTransCtl->do_fsync = false;
185186
}

src/backend/access/transam/xlog.c

+19-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "storage/reinit.h"
5959
#include "storage/smgr.h"
6060
#include "storage/spin.h"
61+
#include "storage/wait.h"
6162
#include "utils/builtins.h"
6263
#include "utils/guc.h"
6364
#include "utils/ps_status.h"
@@ -2446,6 +2447,8 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
24462447
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
24472448
nbytes = npages * (Size) XLOG_BLCKSZ;
24482449
nleft = nbytes;
2450+
2451+
WAIT_START(WAIT_IO, WAIT_XLOG_WRITE, 0, 0, 0, 0, 0);
24492452
do
24502453
{
24512454
errno = 0;
@@ -2465,6 +2468,8 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible)
24652468
from += written;
24662469
} while (nleft > 0);
24672470

2471+
WAIT_STOP();
2472+
24682473
/* Update state for write */
24692474
openLogOff += nbytes;
24702475
npages = 0;
@@ -3177,6 +3182,8 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
31773182
*/
31783183
zbuffer = (char *) MAXALIGN(zbuffer_raw);
31793184
memset(zbuffer, 0, XLOG_BLCKSZ);
3185+
3186+
WAIT_START(WAIT_IO, WAIT_XLOG_WRITE, 0, 0, 0, 0, 0);
31803187
for (nbytes = 0; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
31813188
{
31823189
errno = 0;
@@ -3199,7 +3206,9 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
31993206
errmsg("could not write to file \"%s\": %m", tmppath)));
32003207
}
32013208
}
3209+
WAIT_STOP();
32023210

3211+
WAIT_START(WAIT_IO, WAIT_XLOG_FSYNC, 0, 0, 0, 0, 0);
32033212
if (pg_fsync(fd) != 0)
32043213
{
32053214
close(fd);
@@ -3208,6 +3217,8 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
32083217
errmsg("could not fsync file \"%s\": %m", tmppath)));
32093218
}
32103219

3220+
WAIT_STOP();
3221+
32113222
if (close(fd))
32123223
ereport(ERROR,
32133224
(errcode_for_file_access(),
@@ -4877,11 +4888,12 @@ XLOGShmemInit(void)
48774888

48784889
XLogCtl->Insert.WALInsertLockTrancheId = LWLockNewTrancheId();
48794890

4880-
XLogCtl->Insert.WALInsertLockTranche.name = "WALInsertLocks";
4891+
strcpy(XLogCtl->Insert.WALInsertLockTranche.name, "WALInsertLocks");
48814892
XLogCtl->Insert.WALInsertLockTranche.array_base = WALInsertLocks;
48824893
XLogCtl->Insert.WALInsertLockTranche.array_stride = sizeof(WALInsertLockPadded);
48834894

4884-
LWLockRegisterTranche(XLogCtl->Insert.WALInsertLockTrancheId, &XLogCtl->Insert.WALInsertLockTranche);
4895+
LWLockRegisterTranche(XLogCtl->Insert.WALInsertLockTrancheId,
4896+
&XLogCtl->Insert.WALInsertLockTranche);
48854897
for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
48864898
{
48874899
LWLockInitialize(&WALInsertLocks[i].l.lock,
@@ -9718,6 +9730,7 @@ assign_xlog_sync_method(int new_sync_method, void *extra)
97189730
void
97199731
issue_xlog_fsync(int fd, XLogSegNo segno)
97209732
{
9733+
WAIT_START(WAIT_IO, WAIT_XLOG_FSYNC, 0, 0, 0, 0, 0);
97219734
switch (sync_method)
97229735
{
97239736
case SYNC_METHOD_FSYNC:
@@ -9753,6 +9766,7 @@ issue_xlog_fsync(int fd, XLogSegNo segno)
97539766
elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
97549767
break;
97559768
}
9769+
WAIT_STOP();
97569770
}
97579771

97589772
/*
@@ -10838,8 +10852,10 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
1083810852
goto next_record_is_invalid;
1083910853
}
1084010854

10855+
WAIT_START(WAIT_IO, WAIT_XLOG_READ, 0, 0, 0, 0, 0);
1084110856
if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1084210857
{
10858+
WAIT_STOP();
1084310859
char fname[MAXFNAMELEN];
1084410860

1084510861
XLogFileName(fname, curFileTLI, readSegNo);
@@ -10849,6 +10865,7 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
1084910865
fname, readOff)));
1085010866
goto next_record_is_invalid;
1085110867
}
10868+
WAIT_STOP();
1085210869

1085310870
Assert(targetSegNo == readSegNo);
1085410871
Assert(targetPageOff == readOff);

src/backend/commands/async.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ AsyncShmemInit(void)
480480
*/
481481
AsyncCtl->PagePrecedes = asyncQueuePagePrecedes;
482482
SimpleLruInit(AsyncCtl, "Async Ctl", NUM_ASYNC_BUFFERS, 0,
483-
AsyncCtlLock, "pg_notify");
483+
AsyncCtlLock, "pg_notify", "AsyncBufferLocks");
484484
/* Override default assumption that writes should be fsync'd */
485485
AsyncCtl->do_fsync = false;
486486

src/backend/libpq/be-secure.c

+9
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "libpq/libpq.h"
7878
#include "tcop/tcopprot.h"
7979
#include "utils/memutils.h"
80+
#include "storage/wait.h"
8081

8182

8283
#ifdef USE_SSL
@@ -253,6 +254,8 @@ secure_read(Port *port, void *ptr, size_t len)
253254
{
254255
ssize_t n;
255256

257+
WAIT_START(WAIT_NETWORK, WAIT_NETWORK_READ, 0, 0, 0, 0, 0);
258+
256259
#ifdef USE_SSL
257260
if (port->ssl)
258261
{
@@ -319,6 +322,8 @@ secure_read(Port *port, void *ptr, size_t len)
319322
client_read_ended();
320323
}
321324

325+
WAIT_STOP();
326+
322327
return n;
323328
}
324329

@@ -330,6 +335,8 @@ secure_write(Port *port, void *ptr, size_t len)
330335
{
331336
ssize_t n;
332337

338+
WAIT_START(WAIT_NETWORK, WAIT_NETWORK_WRITE, 0, 0, 0, 0, 0);
339+
333340
#ifdef USE_SSL
334341
if (port->ssl)
335342
{
@@ -457,6 +464,8 @@ secure_write(Port *port, void *ptr, size_t len)
457464
#endif
458465
n = send(port->sock, ptr, len, 0);
459466

467+
WAIT_STOP();
468+
460469
return n;
461470
}
462471

src/backend/port/unix_latch.c

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "storage/latch.h"
5555
#include "storage/pmsignal.h"
5656
#include "storage/shmem.h"
57+
#include "storage/wait.h"
5758

5859
/* Are we currently in WaitLatch? The signal handler would like to know. */
5960
static volatile sig_atomic_t waiting = false;
@@ -263,6 +264,8 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
263264
#endif
264265
}
265266

267+
WAIT_START(WAIT_LATCH, 0, 0, 0, 0, 0, 0);
268+
266269
waiting = true;
267270
do
268271
{
@@ -482,6 +485,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
482485
}
483486
} while (result == 0);
484487
waiting = false;
488+
WAIT_STOP();
485489

486490
return result;
487491
}

src/backend/port/win32_latch.c

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "storage/latch.h"
3131
#include "storage/pmsignal.h"
3232
#include "storage/shmem.h"
33+
#include "storage/wait.h"
3334

3435

3536
void
@@ -178,6 +179,8 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
178179
/* Ensure that signals are serviced even if latch is already set */
179180
pgwin32_dispatch_queued_signals();
180181

182+
WAIT_START(WAIT_LATCH, 0, 0, 0, 0, 0, 0);
183+
181184
do
182185
{
183186
/*
@@ -272,6 +275,8 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
272275
}
273276
} while (result == 0);
274277

278+
WAIT_STOP();
279+
275280
/* Clean up the event object we created for the socket */
276281
if (sockevent != WSA_INVALID_EVENT)
277282
{

src/backend/storage/buffer/buf_init.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
BufferDesc *BufferDescriptors;
2222
char *BufferBlocks;
2323
int32 *PrivateRefCount;
24+
LWLockPadded *BufferLWLockArray;
2425

2526

2627
/*
@@ -72,8 +73,9 @@ int32 *PrivateRefCount;
7273
void
7374
InitBufferPool(void)
7475
{
75-
bool foundBufs,
76-
foundDescs;
76+
bool foundBufs;
77+
bool foundDescs;
78+
LWLockPadded *lwlocks_array;
7779

7880
BufferDescriptors = (BufferDesc *)
7981
ShmemInitStruct("Buffer Descriptors",
@@ -83,6 +85,10 @@ InitBufferPool(void)
8385
ShmemInitStruct("Buffer Blocks",
8486
NBuffers * (Size) BLCKSZ, &foundBufs);
8587

88+
/* Init LWLocks for buffer headers */
89+
LWLockCreateTranche("BufferMgrLocks", 2 * NBuffers,
90+
&lwlocks_array);
91+
8692
if (foundDescs || foundBufs)
8793
{
8894
/* both should be present or neither */
@@ -117,8 +123,8 @@ InitBufferPool(void)
117123
*/
118124
buf->freeNext = i + 1;
119125

120-
buf->io_in_progress_lock = LWLockAssign();
121-
buf->content_lock = LWLockAssign();
126+
buf->io_in_progress_lock = &lwlocks_array[i * 2].lock;
127+
buf->content_lock = &lwlocks_array[i * 2 + 1].lock;
122128
}
123129

124130
/* Correct last entry of linked list */
@@ -174,5 +180,9 @@ BufferShmemSize(void)
174180
/* size of stuff controlled by freelist.c */
175181
size = add_size(size, StrategyShmemSize());
176182

183+
/* size of LWLock structures required for buffers */
184+
size = add_size(size, LWLockTrancheShmemSize(NUM_BUFFER_PARTITIONS));
185+
size = add_size(size, LWLockTrancheShmemSize(2 * NBuffers));
186+
177187
return size;
178188
}

0 commit comments

Comments
 (0)