PostgreSQL Source Code git master
test_lwlock_tranches.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * test_lwlock_tranches.c
4 * Test code for LWLock tranches allocated by extensions.
5 *
6 * Copyright (c) 2025, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c
10 *
11 * -------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15
16#include "fmgr.h"
17#include "miscadmin.h"
18#include "storage/lwlock.h"
19#include "utils/builtins.h"
20#include "utils/wait_classes.h"
21
23
24#define STARTUP_TRANCHE_NAME "test_lwlock_tranches_startup"
25#define DYNAMIC_TRANCHE_NAME "test_lwlock_tranches_dynamic"
26
27#define NUM_STARTUP_TRANCHES (32)
28#define NUM_DYNAMIC_TRANCHES (256 - NUM_STARTUP_TRANCHES)
29
30#define GET_TRANCHE_NAME(a) GetLWLockIdentifier(PG_WAIT_LWLOCK, (a))
31
34
35void
37{
40}
41
42static void
44{
47
48 for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
50}
51
52/*
53 * Checks that GetLWLockIdentifier() returns the expected value for tranches
54 * registered via RequestNamedLWLockTranche() and LWLockNewTrancheId().
55 */
59{
60 int dynamic_tranches[NUM_DYNAMIC_TRANCHES];
61
62 for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
63 dynamic_tranches[i] = LWLockNewTrancheId(DYNAMIC_TRANCHE_NAME);
64
65 for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
66 {
69 elog(ERROR, "incorrect startup lock tranche name");
70 }
71
72 for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
73 {
74 if (strcmp(GET_TRANCHE_NAME(dynamic_tranches[i]),
76 elog(ERROR, "incorrect dynamic lock tranche name");
77 }
78
80}
81
82/*
83 * Wrapper for LWLockNewTrancheId().
84 */
88{
89 char *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0));
90
91 (void) LWLockNewTrancheId(tranche_name);
92
94}
95
96/*
97 * Wrapper for GetNamedLWLockTranche().
98 */
100Datum
102{
103 char *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0));
104
105 (void) GetNamedLWLockTranche(tranche_name);
106
108}
109
110/*
111 * Wrapper for LWLockInitialize().
112 */
114Datum
116{
117 int tranche_id = PG_GETARG_INT32(0);
118 LWLock lock;
119
120 LWLockInitialize(&lock, tranche_id);
121
123}
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
int i
Definition: isn.c:77
int LWLockNewTrancheId(const char *name)
Definition: lwlock.c:596
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
Definition: lwlock.c:566
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition: lwlock.c:649
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:698
@ LWTRANCHE_FIRST_USER_DEFINED
Definition: lwlock.h:186
void(* shmem_request_hook_type)(void)
Definition: miscadmin.h:533
shmem_request_hook_type shmem_request_hook
Definition: miscinit.c:1789
uint64_t Datum
Definition: postgres.h:70
Definition: lwlock.h:42
Datum test_lwlock_tranche_creation(PG_FUNCTION_ARGS)
void _PG_init(void)
#define STARTUP_TRANCHE_NAME
#define NUM_STARTUP_TRANCHES
PG_MODULE_MAGIC
PG_FUNCTION_INFO_V1(test_lwlock_tranches)
static shmem_request_hook_type prev_shmem_request_hook
Datum test_lwlock_tranche_lookup(PG_FUNCTION_ARGS)
#define GET_TRANCHE_NAME(a)
Datum test_lwlock_tranches(PG_FUNCTION_ARGS)
static void test_lwlock_tranches_shmem_request(void)
#define NUM_DYNAMIC_TRANCHES
#define DYNAMIC_TRANCHE_NAME
Datum test_lwlock_initialize(PG_FUNCTION_ARGS)