Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa65de0

Browse files
committed
When trace_lwlocks is used, identify individual lwlocks by name.
Naming the individual lwlocks seems like something that may be useful for other types of debugging, monitoring, or instrumentation output, but this commit just implements it for the specific case of trace_lwlocks. Patch by me, reviewed by Amit Kapila and Kyotaro Horiguchi
1 parent a1b2888 commit aa65de0

File tree

9 files changed

+201
-69
lines changed

9 files changed

+201
-69
lines changed

src/backend/Makefile

+13-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ endif
106106
endif # aix
107107

108108
# Update the commonly used headers before building the subdirectories
109-
$(SUBDIRS:%=%-recursive): $(top_builddir)/src/include/parser/gram.h $(top_builddir)/src/include/catalog/schemapg.h $(top_builddir)/src/include/utils/fmgroids.h $(top_builddir)/src/include/utils/errcodes.h $(top_builddir)/src/include/utils/probes.h
109+
$(SUBDIRS:%=%-recursive): $(top_builddir)/src/include/parser/gram.h $(top_builddir)/src/include/catalog/schemapg.h $(top_builddir)/src/include/storage/lwlocknames.h $(top_builddir)/src/include/utils/fmgroids.h $(top_builddir)/src/include/utils/errcodes.h $(top_builddir)/src/include/utils/probes.h
110110

111111
# run this unconditionally to avoid needing to know its dependencies here:
112112
submake-schemapg:
@@ -135,6 +135,9 @@ postgres.o: $(OBJS)
135135
parser/gram.h: parser/gram.y
136136
$(MAKE) -C parser gram.h
137137

138+
storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lwlocknames.txt
139+
$(MAKE) -C storage/lmgr lwlocknames.h
140+
138141
utils/fmgroids.h: utils/Gen_fmgrtab.pl catalog/Catalog.pm $(top_srcdir)/src/include/catalog/pg_proc.h
139142
$(MAKE) -C utils fmgroids.h
140143

@@ -165,6 +168,11 @@ $(top_builddir)/src/include/catalog/schemapg.h: catalog/schemapg.h
165168
cd '$(dir $@)' && rm -f $(notdir $@) && \
166169
$(LN_S) "$$prereqdir/$(notdir $<)" .
167170

171+
$(top_builddir)/src/include/storage/lwlocknames.h: storage/lmgr/lwlocknames.h
172+
prereqdir=`cd '$(dir $<)' >/dev/null && pwd` && \
173+
cd '$(dir $@)' && rm -f $(notdir $@) && \
174+
$(LN_S) "$$prereqdir/$(notdir $<)" .
175+
168176
$(top_builddir)/src/include/utils/errcodes.h: utils/errcodes.h
169177
prereqdir=`cd '$(dir $<)' >/dev/null && pwd` && \
170178
cd '$(dir $@)' && rm -f $(notdir $@) && \
@@ -192,6 +200,7 @@ distprep:
192200
$(MAKE) -C bootstrap bootparse.c bootscanner.c
193201
$(MAKE) -C catalog schemapg.h postgres.bki postgres.description postgres.shdescription
194202
$(MAKE) -C replication repl_gram.c repl_scanner.c
203+
$(MAKE) -C storage lwlocknames.h
195204
$(MAKE) -C utils fmgrtab.c fmgroids.h errcodes.h
196205
$(MAKE) -C utils/misc guc-file.c
197206
$(MAKE) -C utils/sort qsort_tuple.c
@@ -282,6 +291,7 @@ clean:
282291
rm -f $(LOCALOBJS) postgres$(X) $(POSTGRES_IMP) \
283292
$(top_builddir)/src/include/parser/gram.h \
284293
$(top_builddir)/src/include/catalog/schemapg.h \
294+
$(top_builddir)/src/include/storage/lwlocknames.h \
285295
$(top_builddir)/src/include/utils/fmgroids.h \
286296
$(top_builddir)/src/include/utils/probes.h
287297
ifeq ($(PORTNAME), cygwin)
@@ -307,6 +317,8 @@ maintainer-clean: distclean
307317
catalog/postgres.shdescription \
308318
replication/repl_gram.c \
309319
replication/repl_scanner.c \
320+
storage/lmgr/lwlocknames.c \
321+
storage/lmgr/lwlocknames.h \
310322
utils/fmgroids.h \
311323
utils/fmgrtab.c \
312324
utils/errcodes.h \

src/backend/storage/lmgr/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/lwlocknames.c
2+
/lwlocknames.h

src/backend/storage/lmgr/Makefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ s_lock_test: s_lock.c $(top_builddir)/src/port/libpgport.a
2424
$(CC) $(CPPFLAGS) $(CFLAGS) -DS_LOCK_TEST=1 $(srcdir)/s_lock.c \
2525
$(TASPATH) -L $(top_builddir)/src/port -lpgport -o s_lock_test
2626

27+
# see explanation in ../../parser/Makefile
28+
lwlocknames.c: lwlocknames.h ;
29+
30+
lwlocknames.h: $(top_srcdir)/src/backend/storage/lmgr/lwlocknames.txt generate-lwlocknames.pl
31+
$(PERL) $(srcdir)/generate-lwlocknames.pl $<
32+
2733
check: s_lock_test
2834
./s_lock_test
2935

30-
clean distclean maintainer-clean:
36+
clean distclean:
3137
rm -f s_lock_test
38+
39+
maintainer-clean: clean
40+
rm -f lwlocknames.h lwlocknames.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/perl
2+
#
3+
# Generate lwlocknames.h and lwlocknames.c from lwlocknames.txt
4+
# Copyright (c) 2000-2015, PostgreSQL Global Development Group
5+
6+
use warnings;
7+
use strict;
8+
9+
my $lastlockidx = -1;
10+
my $continue = "\n";
11+
12+
open my $lwlocknames, $ARGV[0] or die;
13+
14+
# Include PID in suffix in case parallel make runs this multiple times.
15+
my $htmp = "lwlocknames.h.tmp$$";
16+
my $ctmp = "lwlocknames.c.tmp$$";
17+
open H, '>', $htmp or die "Could not open $htmp: $!";
18+
open C, '>', $ctmp or die "Could not open $ctmp: $!";
19+
20+
my $autogen =
21+
"/* autogenerated from src/backend/storage/lmgr/lwlocknames.txt, do not edit */\n";
22+
print H $autogen;
23+
print H "/* there is deliberately not an #ifndef LWLOCKNAMES_H here */\n\n";
24+
print C $autogen, "\n";
25+
26+
print C "static char *MainLWLockNames[] = {";
27+
28+
while (<$lwlocknames>)
29+
{
30+
chomp;
31+
32+
# Skip comments
33+
next if /^#/;
34+
next if /^\s*$/;
35+
36+
die "unable to parse lwlocknames.txt"
37+
unless /^(\w+)\s+(\d+)$/;
38+
39+
(my $lockname, my $lockidx) = ($1, $2);
40+
41+
die "lwlocknames.txt not in order" if $lockidx < $lastlockidx;
42+
die "lwlocknames.txt has duplicates" if $lockidx == $lastlockidx;
43+
44+
while ($lastlockidx < $lockidx - 1)
45+
{
46+
++$lastlockidx;
47+
printf C "%s \"<unassigned:%d>\"", $continue, $lastlockidx;
48+
$continue = ",\n";
49+
}
50+
printf C "%s \"%s\"", $continue, $lockname;
51+
$lastlockidx = $lockidx;
52+
$continue = ",\n";
53+
54+
print H "#define $lockname (&MainLWLockArray[$lockidx].lock)\n";
55+
}
56+
57+
printf C "\n};\n";
58+
print H "\n";
59+
printf H "#define NUM_INDIVIDUAL_LWLOCKS %s\n", $lastlockidx + 1;
60+
61+
close H;
62+
close C;
63+
64+
rename($htmp, 'lwlocknames.h') || die "rename: $htmp: $!";
65+
rename($ctmp, 'lwlocknames.c') || die "rename: $ctmp: $!";
66+
67+
close $lwlocknames;

src/backend/storage/lmgr/lwlock.c

+43-17
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
#include "utils/hsearch.h"
9696
#endif
9797

98+
/* Constants for lwlock names */
99+
#include "lwlocknames.c"
100+
98101

99102
/* We use the ShmemLock spinlock to protect LWLockAssign */
100103
extern slock_t *ShmemLock;
@@ -183,18 +186,32 @@ PRINT_LWDEBUG(const char *where, LWLock *lock, LWLockMode mode)
183186
if (Trace_lwlocks)
184187
{
185188
uint32 state = pg_atomic_read_u32(&lock->state);
186-
187-
ereport(LOG,
188-
(errhidestmt(true),
189-
errhidecontext(true),
190-
errmsg("%d: %s(%s %d): excl %u shared %u haswaiters %u waiters %u rOK %d",
191-
MyProcPid,
192-
where, T_NAME(lock), T_ID(lock),
193-
!!(state & LW_VAL_EXCLUSIVE),
194-
state & LW_SHARED_MASK,
195-
!!(state & LW_FLAG_HAS_WAITERS),
196-
pg_atomic_read_u32(&lock->nwaiters),
197-
!!(state & LW_FLAG_RELEASE_OK))));
189+
int id = T_ID(lock);
190+
191+
if (lock->tranche == 0 && id < NUM_INDIVIDUAL_LWLOCKS)
192+
ereport(LOG,
193+
(errhidestmt(true),
194+
errhidecontext(true),
195+
errmsg("%d: %s(%s): excl %u shared %u haswaiters %u waiters %u rOK %d",
196+
MyProcPid,
197+
where, MainLWLockNames[id],
198+
!!(state & LW_VAL_EXCLUSIVE),
199+
state & LW_SHARED_MASK,
200+
!!(state & LW_FLAG_HAS_WAITERS),
201+
pg_atomic_read_u32(&lock->nwaiters),
202+
!!(state & LW_FLAG_RELEASE_OK))));
203+
else
204+
ereport(LOG,
205+
(errhidestmt(true),
206+
errhidecontext(true),
207+
errmsg("%d: %s(%s %d): excl %u shared %u haswaiters %u waiters %u rOK %d",
208+
MyProcPid,
209+
where, T_NAME(lock), id,
210+
!!(state & LW_VAL_EXCLUSIVE),
211+
state & LW_SHARED_MASK,
212+
!!(state & LW_FLAG_HAS_WAITERS),
213+
pg_atomic_read_u32(&lock->nwaiters),
214+
!!(state & LW_FLAG_RELEASE_OK))));
198215
}
199216
}
200217

@@ -204,11 +221,20 @@ LOG_LWDEBUG(const char *where, LWLock *lock, const char *msg)
204221
/* hide statement & context here, otherwise the log is just too verbose */
205222
if (Trace_lwlocks)
206223
{
207-
ereport(LOG,
208-
(errhidestmt(true),
209-
errhidecontext(true),
210-
errmsg("%s(%s %d): %s", where,
211-
T_NAME(lock), T_ID(lock), msg)));
224+
int id = T_ID(lock);
225+
226+
if (lock->tranche == 0 && id < NUM_INDIVIDUAL_LWLOCKS)
227+
ereport(LOG,
228+
(errhidestmt(true),
229+
errhidecontext(true),
230+
errmsg("%s(%s): %s", where,
231+
MainLWLockNames[id], msg)));
232+
else
233+
ereport(LOG,
234+
(errhidestmt(true),
235+
errhidecontext(true),
236+
errmsg("%s(%s %d): %s", where,
237+
T_NAME(lock), id, msg)));
212238
}
213239
}
214240

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Some commonly-used locks have predefined positions within MainLWLockArray;
2+
# these are defined here. If you add a lock, add it to the end to avoid
3+
# renumbering the existing locks; if you remove a lock, consider leaving a gap
4+
# in the numbering sequence for the benefit of DTrace and other external
5+
# debugging scripts.
6+
7+
# 0 is available; was formerly BufFreelistLock
8+
ShmemIndexLock 1
9+
OidGenLock 2
10+
XidGenLock 3
11+
ProcArrayLock 4
12+
SInvalReadLock 5
13+
SInvalWriteLock 6
14+
WALBufMappingLock 7
15+
WALWriteLock 8
16+
ControlFileLock 9
17+
CheckpointLock 10
18+
CLogControlLock 11
19+
SubtransControlLock 12
20+
MultiXactGenLock 13
21+
MultiXactOffsetControlLock 14
22+
MultiXactMemberControlLock 15
23+
RelCacheInitLock 16
24+
CheckpointerCommLock 17
25+
TwoPhaseStateLock 18
26+
TablespaceCreateLock 19
27+
BtreeVacuumLock 20
28+
AddinShmemInitLock 21
29+
AutovacuumLock 22
30+
AutovacuumScheduleLock 23
31+
SyncScanLock 24
32+
RelationMappingLock 25
33+
AsyncCtlLock 26
34+
AsyncQueueLock 27
35+
SerializableXactHashLock 28
36+
SerializableFinishedListLock 29
37+
SerializablePredicateLockListLock 30
38+
OldSerXidLock 31
39+
SyncRepLock 32
40+
BackgroundWorkerLock 33
41+
DynamicSharedMemoryControlLock 34
42+
AutoFileLock 35
43+
ReplicationSlotAllocationLock 36
44+
ReplicationSlotControlLock 37
45+
CommitTsControlLock 38
46+
CommitTsLock 39
47+
ReplicationOriginLock 40

src/include/storage/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/lwlocknames.h

src/include/storage/lwlock.h

+2-50
Original file line numberDiff line numberDiff line change
@@ -91,56 +91,8 @@ typedef union LWLockPadded
9191
} LWLockPadded;
9292
extern PGDLLIMPORT LWLockPadded *MainLWLockArray;
9393

94-
/*
95-
* Some commonly-used locks have predefined positions within MainLWLockArray;
96-
* defining macros here makes it much easier to keep track of these. If you
97-
* add a lock, add it to the end to avoid renumbering the existing locks;
98-
* if you remove a lock, consider leaving a gap in the numbering sequence for
99-
* the benefit of DTrace and other external debugging scripts.
100-
*/
101-
/* 0 is available; was formerly BufFreelistLock */
102-
#define ShmemIndexLock (&MainLWLockArray[1].lock)
103-
#define OidGenLock (&MainLWLockArray[2].lock)
104-
#define XidGenLock (&MainLWLockArray[3].lock)
105-
#define ProcArrayLock (&MainLWLockArray[4].lock)
106-
#define SInvalReadLock (&MainLWLockArray[5].lock)
107-
#define SInvalWriteLock (&MainLWLockArray[6].lock)
108-
#define WALBufMappingLock (&MainLWLockArray[7].lock)
109-
#define WALWriteLock (&MainLWLockArray[8].lock)
110-
#define ControlFileLock (&MainLWLockArray[9].lock)
111-
#define CheckpointLock (&MainLWLockArray[10].lock)
112-
#define CLogControlLock (&MainLWLockArray[11].lock)
113-
#define SubtransControlLock (&MainLWLockArray[12].lock)
114-
#define MultiXactGenLock (&MainLWLockArray[13].lock)
115-
#define MultiXactOffsetControlLock (&MainLWLockArray[14].lock)
116-
#define MultiXactMemberControlLock (&MainLWLockArray[15].lock)
117-
#define RelCacheInitLock (&MainLWLockArray[16].lock)
118-
#define CheckpointerCommLock (&MainLWLockArray[17].lock)
119-
#define TwoPhaseStateLock (&MainLWLockArray[18].lock)
120-
#define TablespaceCreateLock (&MainLWLockArray[19].lock)
121-
#define BtreeVacuumLock (&MainLWLockArray[20].lock)
122-
#define AddinShmemInitLock (&MainLWLockArray[21].lock)
123-
#define AutovacuumLock (&MainLWLockArray[22].lock)
124-
#define AutovacuumScheduleLock (&MainLWLockArray[23].lock)
125-
#define SyncScanLock (&MainLWLockArray[24].lock)
126-
#define RelationMappingLock (&MainLWLockArray[25].lock)
127-
#define AsyncCtlLock (&MainLWLockArray[26].lock)
128-
#define AsyncQueueLock (&MainLWLockArray[27].lock)
129-
#define SerializableXactHashLock (&MainLWLockArray[28].lock)
130-
#define SerializableFinishedListLock (&MainLWLockArray[29].lock)
131-
#define SerializablePredicateLockListLock (&MainLWLockArray[30].lock)
132-
#define OldSerXidLock (&MainLWLockArray[31].lock)
133-
#define SyncRepLock (&MainLWLockArray[32].lock)
134-
#define BackgroundWorkerLock (&MainLWLockArray[33].lock)
135-
#define DynamicSharedMemoryControlLock (&MainLWLockArray[34].lock)
136-
#define AutoFileLock (&MainLWLockArray[35].lock)
137-
#define ReplicationSlotAllocationLock (&MainLWLockArray[36].lock)
138-
#define ReplicationSlotControlLock (&MainLWLockArray[37].lock)
139-
#define CommitTsControlLock (&MainLWLockArray[38].lock)
140-
#define CommitTsLock (&MainLWLockArray[39].lock)
141-
#define ReplicationOriginLock (&MainLWLockArray[40].lock)
142-
143-
#define NUM_INDIVIDUAL_LWLOCKS 41
94+
/* Names for fixed lwlocks */
95+
#include "lwlocknames.h"
14496

14597
/*
14698
* It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS

src/tools/msvc/Solution.pm

+16
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,22 @@ s{PG_VERSION_STR "[^"]+"}{__STRINGIFY(x) #x\n#define __STRINGIFY2(z) __STRINGIFY
285285
'src/include/utils/fmgroids.h');
286286
}
287287

288+
if (IsNewer(
289+
'src/include/storage/lwlocknames.h', 'src/backend/storage/lmgr/lwlocknames.txt'))
290+
{
291+
print "Generating lwlocknames.c and lwlocknames.h...\n";
292+
chdir('src/backend/storage/lmgr');
293+
system('perl generate-lwlocknames.pl lwlocknames.txt');
294+
chdir('../../../..');
295+
}
296+
if (IsNewer(
297+
'src/include/storage/lwlocknames.h',
298+
'src/backend/storage/lmgr/lwlocknames.h'))
299+
{
300+
copyFile('src/backend/storage/lmgr/lwlocknames.h',
301+
'src/include/storage/lwlocknames.h');
302+
}
303+
288304
if (IsNewer('src/include/utils/probes.h', 'src/backend/utils/probes.d'))
289305
{
290306
print "Generating probes.h...\n";

0 commit comments

Comments
 (0)