PostgreSQL Source Code: src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c Source File

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
22 PG_MODULE_MAGIC;
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
32 static shmem_request_hook_type prev_shmem_request_hook;
33static void test_lwlock_tranches_shmem_request(void);
34
35void
36 _PG_init(void)
37{
38 prev_shmem_request_hook = shmem_request_hook;
39 shmem_request_hook = test_lwlock_tranches_shmem_request;
40}
41
42static void
43 test_lwlock_tranches_shmem_request(void)
44{
45 if (prev_shmem_request_hook)
46 prev_shmem_request_hook();
47
48 for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
49 RequestNamedLWLockTranche(STARTUP_TRANCHE_NAME, 1);
50}
51
52/*
53 * Checks that GetLWLockIdentifier() returns the expected value for tranches
54 * registered via RequestNamedLWLockTranche() and LWLockNewTrancheId().
55 */
56 PG_FUNCTION_INFO_V1(test_lwlock_tranches);
57Datum
58 test_lwlock_tranches(PG_FUNCTION_ARGS)
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 {
67 if (strcmp(GET_TRANCHE_NAME(LWTRANCHE_FIRST_USER_DEFINED + i),
68 STARTUP_TRANCHE_NAME) != 0)
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]),
75 DYNAMIC_TRANCHE_NAME) != 0)
76 elog(ERROR, "incorrect dynamic lock tranche name");
77 }
78
79 PG_RETURN_VOID();
80}
81
82/*
83 * Wrapper for LWLockNewTrancheId().
84 */
85 PG_FUNCTION_INFO_V1(test_lwlock_tranche_creation);
86Datum
87 test_lwlock_tranche_creation(PG_FUNCTION_ARGS)
88{
89 char *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0));
90
91 (void) LWLockNewTrancheId(tranche_name);
92
93 PG_RETURN_VOID();
94}
95
96/*
97 * Wrapper for GetNamedLWLockTranche().
98 */
99 PG_FUNCTION_INFO_V1(test_lwlock_tranche_lookup);
100Datum
101 test_lwlock_tranche_lookup(PG_FUNCTION_ARGS)
102{
103 char *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0));
104
105 (void) GetNamedLWLockTranche(tranche_name);
106
107 PG_RETURN_VOID();
108}
109
110/*
111 * Wrapper for LWLockInitialize().
112 */
113 PG_FUNCTION_INFO_V1(test_lwlock_initialize);
114Datum
115 test_lwlock_initialize(PG_FUNCTION_ARGS)
116{
117 int tranche_id = PG_GETARG_INT32(0);
118 LWLock lock;
119
120 LWLockInitialize(&lock, tranche_id);
121
122 PG_RETURN_VOID();
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
i
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:532
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)

AltStyle によって変換されたページ (->オリジナル) /