| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Generate lwlocknames.h from lwlocklist.h |
| 4 |
# Copyright (c) 2000-2025, PostgreSQL Global Development Group |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
use Getopt::Long; |
| 9 |
|
| 10 |
my $output_path = '.'; |
| 11 |
|
| 12 |
my $lastlockidx = -1; |
| 13 |
my $continue = "\n"; |
| 14 |
|
| 15 |
GetOptions('outdir:s' => \$output_path); |
| 16 |
|
| 17 |
open my $lwlocklist, '<', $ARGV[0] or die; |
| 18 |
open my $wait_event_names, '<', $ARGV[1] or die; |
| 19 |
|
| 20 |
# Include PID in suffix in case parallel make runs this multiple times. |
| 21 |
my $htmp = "$output_path/lwlocknames.h.tmp$$"; |
| 22 |
open my $h, '>', $htmp or die "Could not open $htmp: $!"; |
| 23 |
|
| 24 |
my $autogen = |
| 25 |
"/* autogenerated from src/include/storage/lwlocklist.h, do not edit */\n"; |
| 26 |
print $h $autogen; |
| 27 |
print $h "/* there is deliberately not an #ifndef LWLOCKNAMES_H here */\n\n"; |
| 28 |
|
| 29 |
|
| 30 |
# |
| 31 |
# First, record the predefined LWLocks listed in wait_event_names.txt. We'll |
| 32 |
# cross-check those with the ones in lwlocklist.h. |
| 33 |
# |
| 34 |
my @wait_event_lwlocks; |
| 35 |
my $record_lwlocks = 0; |
| 36 |
|
| 37 |
while (<$wait_event_names>) |
| 38 |
{ |
| 39 |
chomp; |
| 40 |
|
| 41 |
# Check for end marker. |
| 42 |
last if /^# END OF PREDEFINED LWLOCKS/; |
| 43 |
|
| 44 |
# Skip comments and empty lines. |
| 45 |
next if /^#/; |
| 46 |
next if /^\s*$/; |
| 47 |
|
| 48 |
# Start recording LWLocks when we find the WaitEventLWLock section. |
| 49 |
if (/^Section: ClassName - WaitEventLWLock$/) |
| 50 |
{ |
| 51 |
$record_lwlocks = 1; |
| 52 |
next; |
| 53 |
} |
| 54 |
|
| 55 |
# Go to the next line if we are not yet recording LWLocks. |
| 56 |
next if not $record_lwlocks; |
| 57 |
|
| 58 |
# Record the LWLock. |
| 59 |
(my $waiteventname, my $waitevendocsentence) = split(/\t/, $_); |
| 60 |
push(@wait_event_lwlocks, $waiteventname); |
| 61 |
} |
| 62 |
|
| 63 |
my $in_comment = 0; |
| 64 |
my $i = 0; |
| 65 |
while (<$lwlocklist>) |
| 66 |
{ |
| 67 |
chomp; |
| 68 |
|
| 69 |
# Skip single-line C comments and empty lines |
| 70 |
next if m{^\s*/\*.*\*/$}; |
| 71 |
next if /^\s*$/; |
| 72 |
|
| 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 |
} |
| 84 |
|
| 85 |
die "unable to parse lwlocklist.h line \"$_\"" |
| 86 |
unless /^PG_LWLOCK\((\d+),\s+(\w+)\)$/; |
| 87 |
|
| 88 |
(my $lockidx, my $lockname) = ($1, $2); |
| 89 |
|
| 90 |
die "lwlocklist.h not in order" if $lockidx < $lastlockidx; |
| 91 |
die "lwlocklist.h has duplicates" if $lockidx == $lastlockidx; |
| 92 |
|
| 93 |
die "$lockname defined in lwlocklist.h but missing from " |
| 94 |
. "wait_event_names.txt" |
| 95 |
if $i >= scalar @wait_event_lwlocks; |
| 96 |
die "lists of predefined LWLocks do not match (first mismatch at " |
| 97 |
. "$wait_event_lwlocks[$i] in wait_event_names.txt and $lockname in " |
| 98 |
. "lwlocklist.h)" |
| 99 |
if $wait_event_lwlocks[$i] ne $lockname; |
| 100 |
$i++; |
| 101 |
|
| 102 |
while ($lastlockidx < $lockidx - 1) |
| 103 |
{ |
| 104 |
++$lastlockidx; |
| 105 |
$continue = ",\n"; |
| 106 |
} |
| 107 |
$lastlockidx = $lockidx; |
| 108 |
$continue = ",\n"; |
| 109 |
|
| 110 |
# Add a "Lock" suffix to each lock name, as the C code depends on that |
| 111 |
printf $h "#define %-32s (&MainLWLockArray[$lockidx].lock)\n", |
| 112 |
$lockname . "Lock"; |
| 113 |
} |
| 114 |
|
| 115 |
die |
| 116 |
"$wait_event_lwlocks[$i] defined in wait_event_names.txt but missing from " |
| 117 |
. "lwlocklist.h" |
| 118 |
if $i < scalar @wait_event_lwlocks; |
| 119 |
|
| 120 |
print $h "\n"; |
| 121 |
printf $h "#define NUM_INDIVIDUAL_LWLOCKS %s\n", $lastlockidx + 1; |
| 122 |
|
| 123 |
close $h; |
| 124 |
|
| 125 |
rename($htmp, "$output_path/lwlocknames.h") |
| 126 |
|| die "rename: $htmp to $output_path/lwlocknames.h: $!"; |
| 127 |
|
| 128 |
close $lwlocklist; |