Skip to content

Commit da952b4

Browse files
committed
Rework lwlocknames.txt to become lwlocklist.h
This way, we can fold the list of lock names to occur in BuiltinTrancheNames instead of having its own separate array. This saves two lines of code in GetLWTrancheName and some space in BuiltinTrancheNames, as foreseen in commit 74a7306, as well as removing the need for a separate lwlocknames.c file. We still have to build lwlocknames.h using Perl code, which initially I wanted to avoid, but it gives us the chance to cross-check wait_event_names.txt. Discussion: https://postgr.es/m/202401231025.gbv4nnte5fmm@alvherre.pgsql
1 parent e5da0fe commit da952b4

File tree

12 files changed

+136
-125
lines changed

12 files changed

+136
-125
lines changed

src/backend/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ $(top_builddir)/src/port/libpgport_srv.a: | submake-libpgport
110110
parser/gram.h: parser/gram.y
111111
$(MAKE) -C parser gram.h
112112

113-
storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lwlocknames.txt utils/activity/wait_event_names.txt
114-
$(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c
113+
storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl ../include/storage/lwlocklist.h utils/activity/wait_event_names.txt
114+
$(MAKE) -C storage/lmgr lwlocknames.h
115115

116116
utils/activity/wait_event_types.h: utils/activity/generate-wait_event_types.pl utils/activity/wait_event_names.txt
117117
$(MAKE) -C utils/activity wait_event_types.h pgstat_wait_event.c wait_event_funcs_data.c

src/backend/storage/lmgr/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
/lwlocknames.c
21
/lwlocknames.h
32
/s_lock_test

src/backend/storage/lmgr/Makefile

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ OBJS = \
1818
lmgr.o \
1919
lock.o \
2020
lwlock.o \
21-
lwlocknames.o \
2221
predicate.o \
2322
proc.o \
2423
s_lock.o \
@@ -35,16 +34,12 @@ s_lock_test: s_lock.c $(top_builddir)/src/common/libpgcommon.a $(top_builddir)/s
3534
$(TASPATH) -L $(top_builddir)/src/common -lpgcommon \
3635
-L $(top_builddir)/src/port -lpgport -lm -o s_lock_test
3736

38-
# see notes in src/backend/parser/Makefile
39-
lwlocknames.c: lwlocknames.h
40-
touch $@
41-
42-
lwlocknames.h: $(top_srcdir)/src/backend/storage/lmgr/lwlocknames.txt $(top_srcdir)/src/backend/utils/activity/wait_event_names.txt generate-lwlocknames.pl
37+
lwlocknames.h: ../../../include/storage/lwlocklist.h ../../utils/activity/wait_event_names.txt generate-lwlocknames.pl
4338
$(PERL) $(srcdir)/generate-lwlocknames.pl $^
4439

4540
check: s_lock_test
4641
./s_lock_test
4742

4843
clean:
4944
rm -f s_lock_test
50-
rm -f lwlocknames.h lwlocknames.c
45+
rm -f lwlocknames.h
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/perl
22
#
3-
# Generate lwlocknames.h and lwlocknames.c from lwlocknames.txt
3+
# Generate lwlocknames.h from lwlocklist.h
44
# Copyright (c) 2000-2024, PostgreSQL Global Development Group
55

66
use strict;
@@ -14,26 +14,22 @@
1414

1515
GetOptions('outdir:s' => \$output_path);
1616

17-
open my $lwlocknames, '<', $ARGV[0] or die;
17+
open my $lwlocklist, '<', $ARGV[0] or die;
1818
open my $wait_event_names, '<', $ARGV[1] or die;
1919

2020
# Include PID in suffix in case parallel make runs this multiple times.
2121
my $htmp = "$output_path/lwlocknames.h.tmp$$";
22-
my $ctmp = "$output_path/lwlocknames.c.tmp$$";
2322
open my $h, '>', $htmp or die "Could not open $htmp: $!";
24-
open my $c, '>', $ctmp or die "Could not open $ctmp: $!";
2523

2624
my $autogen =
27-
"/* autogenerated from src/backend/storage/lmgr/lwlocknames.txt, do not edit */\n";
25+
"/* autogenerated from src/include/storage/lwlocklist.h, do not edit */\n";
2826
print $h $autogen;
2927
print $h "/* there is deliberately not an #ifndef LWLOCKNAMES_H here */\n\n";
30-
print $c $autogen, "\n";
3128

32-
print $c "const char *const IndividualLWLockNames[] = {";
3329

3430
#
3531
# First, record the predefined LWLocks listed in wait_event_names.txt. We'll
36-
# cross-check those with the ones in lwlocknames.txt.
32+
# cross-check those with the ones in lwlocklist.h.
3733
#
3834
my @wait_event_lwlocks;
3935
my $record_lwlocks = 0;
@@ -61,66 +57,70 @@
6157

6258
# Record the LWLock.
6359
(my $waiteventname, my $waitevendocsentence) = split(/\t/, $_);
64-
push(@wait_event_lwlocks, $waiteventname . "Lock");
60+
push(@wait_event_lwlocks, $waiteventname);
6561
}
6662

63+
my $in_comment = 0;
6764
my $i = 0;
68-
while (<$lwlocknames>)
65+
while (<$lwlocklist>)
6966
{
7067
chomp;
7168

72-
# Skip comments
73-
next if /^#/;
69+
# Skip single-line C comments and empty lines
70+
next if m{^\s*/\*.*\*/$};
7471
next if /^\s*$/;
7572

76-
die "unable to parse lwlocknames.txt"
77-
unless /^(\w+)\s+(\d+)$/;
73+
# skip multiline C comments
74+
if ($in_comment == 1)
75+
{
76+
$in_comment = 0 if m{\*/};
77+
next;
78+
}
79+
elsif (m{^\s*/\*})
80+
{
81+
$in_comment = 1;
82+
next;
83+
}
7884

79-
(my $lockname, my $lockidx) = ($1, $2);
85+
die "unable to parse lwlocklist.h line \"$_\""
86+
unless /^PG_LWLOCK\((\d+),\s+(\w+)\)$/;
8087

81-
my $trimmedlockname = $lockname;
82-
$trimmedlockname =~ s/Lock$//;
83-
die "lock names must end with 'Lock'" if $trimmedlockname eq $lockname;
88+
(my $lockidx, my $lockname) = ($1, $2);
8489

85-
die "lwlocknames.txt not in order" if $lockidx < $lastlockidx;
86-
die "lwlocknames.txt has duplicates" if $lockidx == $lastlockidx;
90+
die "lwlocklist.h not in order" if $lockidx < $lastlockidx;
91+
die "lwlocklist.h has duplicates" if $lockidx == $lastlockidx;
8792

88-
die "$lockname defined in lwlocknames.txt but missing from "
93+
die "$lockname defined in lwlocklist.h but missing from "
8994
. "wait_event_names.txt"
9095
if $i >= scalar @wait_event_lwlocks;
9196
die "lists of predefined LWLocks do not match (first mismatch at "
9297
. "$wait_event_lwlocks[$i] in wait_event_names.txt and $lockname in "
93-
. "lwlocknames.txt)"
98+
. "lwlocklist.h)"
9499
if $wait_event_lwlocks[$i] ne $lockname;
95100
$i++;
96101

97102
while ($lastlockidx < $lockidx - 1)
98103
{
99104
++$lastlockidx;
100-
printf $c "%s \"<unassigned:%d>\"", $continue, $lastlockidx;
101105
$continue = ",\n";
102106
}
103-
printf $c "%s \"%s\"", $continue, $trimmedlockname;
104107
$lastlockidx = $lockidx;
105108
$continue = ",\n";
106109

107-
print $h "#define $lockname (&MainLWLockArray[$lockidx].lock)\n";
110+
print $h "#define ${lockname}Lock (&MainLWLockArray[$lockidx].lock)\n";
108111
}
109112

110113
die
111114
"$wait_event_lwlocks[$i] defined in wait_event_names.txt but missing from "
112-
. "lwlocknames.txt"
115+
. "lwlocklist.h"
113116
if $i < scalar @wait_event_lwlocks;
114117

115-
printf $c "\n};\n";
116118
print $h "\n";
117119
printf $h "#define NUM_INDIVIDUAL_LWLOCKS %s\n", $lastlockidx + 1;
118120

119121
close $h;
120-
close $c;
121122

122123
rename($htmp, "$output_path/lwlocknames.h")
123124
|| die "rename: $htmp to $output_path/lwlocknames.h: $!";
124-
rename($ctmp, "$output_path/lwlocknames.c") || die "rename: $ctmp: $!";
125125

126-
close $lwlocknames;
126+
close $lwlocklist;

src/backend/storage/lmgr/lwlock.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ StaticAssertDecl(LW_VAL_EXCLUSIVE > (uint32) MAX_BACKENDS,
112112
* There are three sorts of LWLock "tranches":
113113
*
114114
* 1. The individually-named locks defined in lwlocknames.h each have their
115-
* own tranche. The names of these tranches appear in IndividualLWLockNames[]
116-
* in lwlocknames.c.
115+
* own tranche. We absorb the names of these tranches from there into
116+
* BuiltinTrancheNames here.
117117
*
118118
* 2. There are some predefined tranches for built-in groups of locks.
119119
* These are listed in enum BuiltinTrancheIds in lwlock.h, and their names
@@ -126,9 +126,10 @@ StaticAssertDecl(LW_VAL_EXCLUSIVE > (uint32) MAX_BACKENDS,
126126
* All these names are user-visible as wait event names, so choose with care
127127
* ... and do not forget to update the documentation's list of wait events.
128128
*/
129-
extern const char *const IndividualLWLockNames[]; /* in lwlocknames.c */
130-
131129
static const char *const BuiltinTrancheNames[] = {
130+
#define PG_LWLOCK(id, lockname) [id] = CppAsString(lockname) "Lock",
131+
#include "storage/lwlocklist.h"
132+
#undef PG_LWLOCK
132133
[LWTRANCHE_XACT_BUFFER] = "XactBuffer",
133134
[LWTRANCHE_COMMITTS_BUFFER] = "CommitTsBuffer",
134135
[LWTRANCHE_SUBTRANS_BUFFER] = "SubtransBuffer",
@@ -742,11 +743,7 @@ LWLockReportWaitEnd(void)
742743
static const char *
743744
GetLWTrancheName(uint16 trancheId)
744745
{
745-
/* Individual LWLock? */
746-
if (trancheId < NUM_INDIVIDUAL_LWLOCKS)
747-
return IndividualLWLockNames[trancheId];
748-
749-
/* Built-in tranche? */
746+
/* Built-in tranche or individual LWLock? */
750747
if (trancheId < LWTRANCHE_FIRST_USER_DEFINED)
751748
return BuiltinTrancheNames[trancheId];
752749

src/backend/storage/lmgr/lwlocknames.txt

-60
This file was deleted.

src/backend/storage/lmgr/meson.build

-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ backend_sources += files(
1111
's_lock.c',
1212
'spin.c',
1313
)
14-
15-
generated_backend_sources += lwlocknames[1]

src/backend/utils/activity/wait_event_names.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ Extension "Waiting in an extension."
280280
# This class of wait events has its own set of C structure, so these are
281281
# only used for the documentation.
282282
#
283-
# NB: Predefined LWLocks (i.e., those declared in lwlocknames.txt) must be
283+
# NB: Predefined LWLocks (i.e., those declared in lwlocklist.h) must be
284284
# listed in the top section of locks and must be listed in the same order as in
285-
# lwlocknames.txt.
285+
# lwlocklist.h.
286286
#
287287

288288
Section: ClassName - WaitEventLWLock
@@ -333,9 +333,9 @@ SerialControl "Waiting to read or update shared <filename>pg_serial</filename> s
333333
#
334334
# END OF PREDEFINED LWLOCKS (DO NOT CHANGE THIS LINE)
335335
#
336-
# Predefined LWLocks (i.e., those declared in lwlocknames.txt) must be listed
336+
# Predefined LWLocks (i.e., those declared in lwlocknames.h) must be listed
337337
# in the section above and must be listed in the same order as in
338-
# lwlocknames.txt. Other LWLocks must be listed in the section below.
338+
# lwlocknames.h. Other LWLocks must be listed in the section below.
339339
#
340340

341341
XactBuffer "Waiting for I/O on a transaction status SLRU buffer."

src/include/storage/lwlock.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#endif
2020

2121
#include "port/atomics.h"
22+
#include "storage/lwlocknames.h"
2223
#include "storage/proclist_types.h"
2324

2425
struct PGPROC;
@@ -82,9 +83,6 @@ typedef struct NamedLWLockTranche
8283
extern PGDLLIMPORT NamedLWLockTranche *NamedLWLockTrancheArray;
8384
extern PGDLLIMPORT int NamedLWLockTrancheRequests;
8485

85-
/* Names for fixed lwlocks */
86-
#include "storage/lwlocknames.h"
87-
8886
/*
8987
* It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
9088
* here, but we need them to figure out offsets within MainLWLockArray, and

0 commit comments

Comments
 (0)