Age Owner Branch data TLA Line data Source code
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 : :
40 nathan@postgresql.or 22 :GNC 1 : 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;
33 : : static void test_lwlock_tranches_shmem_request(void);
34 : :
35 : : void
36 : 1 : _PG_init(void)
37 : : {
38 : 1 : prev_shmem_request_hook = shmem_request_hook;
39 : 1 : shmem_request_hook = test_lwlock_tranches_shmem_request;
40 : 1 : }
41 : :
42 : : static void
43 : 1 : test_lwlock_tranches_shmem_request(void)
44 : : {
45 [ - + ]: 1 : if (prev_shmem_request_hook)
40 nathan@postgresql.or 46 :UNC 0 : prev_shmem_request_hook();
47 : :
40 nathan@postgresql.or 48 [ + + ]:GNC 33 : for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
49 : 32 : RequestNamedLWLockTranche(STARTUP_TRANCHE_NAME, 1);
50 : 1 : }
51 : :
52 : : /*
53 : : * Checks that GetLWLockIdentifier() returns the expected value for tranches
54 : : * registered via RequestNamedLWLockTranche() and LWLockNewTrancheId().
55 : : */
56 : 2 : PG_FUNCTION_INFO_V1(test_lwlock_tranches);
57 : : Datum
58 : 1 : test_lwlock_tranches(PG_FUNCTION_ARGS)
59 : : {
60 : : int dynamic_tranches[NUM_DYNAMIC_TRANCHES];
61 : :
62 [ + + ]: 225 : for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
63 : 224 : dynamic_tranches[i] = LWLockNewTrancheId(DYNAMIC_TRANCHE_NAME);
64 : :
65 [ + + ]: 33 : for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
66 : : {
67 [ - + ]: 32 : if (strcmp(GET_TRANCHE_NAME(LWTRANCHE_FIRST_USER_DEFINED + i),
68 : : STARTUP_TRANCHE_NAME) != 0)
40 nathan@postgresql.or 69 [ # # ]:UNC 0 : elog(ERROR, "incorrect startup lock tranche name");
70 : : }
71 : :
40 nathan@postgresql.or 72 [ + + ]:GNC 225 : for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
73 : : {
74 [ - + ]: 224 : if (strcmp(GET_TRANCHE_NAME(dynamic_tranches[i]),
75 : : DYNAMIC_TRANCHE_NAME) != 0)
40 nathan@postgresql.or 76 [ # # ]:UNC 0 : elog(ERROR, "incorrect dynamic lock tranche name");
77 : : }
78 : :
40 nathan@postgresql.or 79 :GNC 1 : PG_RETURN_VOID();
80 : : }
81 : :
82 : : /*
83 : : * Wrapper for LWLockNewTrancheId().
84 : : */
85 : 2 : PG_FUNCTION_INFO_V1(test_lwlock_tranche_creation);
86 : : Datum
87 : 3 : test_lwlock_tranche_creation(PG_FUNCTION_ARGS)
88 : : {
89 [ + + ]: 3 : char *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0));
90 : :
91 : 3 : (void) LWLockNewTrancheId(tranche_name);
92 : :
40 nathan@postgresql.or 93 :UNC 0 : PG_RETURN_VOID();
94 : : }
95 : :
96 : : /*
97 : : * Wrapper for GetNamedLWLockTranche().
98 : : */
40 nathan@postgresql.or 99 :GNC 2 : PG_FUNCTION_INFO_V1(test_lwlock_tranche_lookup);
100 : : Datum
101 : 2 : test_lwlock_tranche_lookup(PG_FUNCTION_ARGS)
102 : : {
103 : 2 : char *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0));
104 : :
105 : 2 : (void) GetNamedLWLockTranche(tranche_name);
106 : :
107 : 1 : PG_RETURN_VOID();
108 : : }
109 : :
110 : : /*
111 : : * Wrapper for LWLockInitialize().
112 : : */
113 : 2 : PG_FUNCTION_INFO_V1(test_lwlock_initialize);
114 : : Datum
115 : 1 : test_lwlock_initialize(PG_FUNCTION_ARGS)
116 : : {
117 : 1 : int tranche_id = PG_GETARG_INT32(0);
118 : : LWLock lock;
119 : :
120 : 1 : LWLockInitialize(&lock, tranche_id);
121 : :
40 nathan@postgresql.or 122 :UNC 0 : PG_RETURN_VOID();
123 : : }
|