PostgreSQL Source Code git master
test_dsa.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * test_dsa.c
4 * Test dynamic shared memory areas (DSAs)
5 *
6 * Copyright (c) 2022-2025, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/test/modules/test_dsa/test_dsa.c
10 *
11 * -------------------------------------------------------------------------
12 */
13#include "postgres.h"
14
15#include "fmgr.h"
17#include "storage/lwlock.h"
18#include "utils/dsa.h"
19#include "utils/resowner.h"
20
22
23static void
24init_tranche(void *ptr)
25{
26 int *tranche_id = (int *) ptr;
27
28 *tranche_id = LWLockNewTrancheId("test_dsa");
29}
30
31/* Test basic DSA functionality */
35{
36 int *tranche_id;
37 bool found;
38 dsa_area *a;
39 dsa_pointer p[100];
40
41 tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
42 init_tranche, &found);
43
44 a = dsa_create(*tranche_id);
45 for (int i = 0; i < 100; i++)
46 {
47 p[i] = dsa_allocate(a, 1000);
48 snprintf(dsa_get_address(a, p[i]), 1000, "foobar%d", i);
49 }
50
51 for (int i = 0; i < 100; i++)
52 {
53 char buf[100];
54
55 snprintf(buf, 100, "foobar%d", i);
56 if (strcmp(dsa_get_address(a, p[i]), buf) != 0)
57 elog(ERROR, "no match");
58 }
59
60 for (int i = 0; i < 100; i++)
61 {
62 dsa_free(a, p[i]);
63 }
64
66
68}
69
70/* Test using DSA across different resource owners */
74{
75 int *tranche_id;
76 bool found;
77 dsa_area *a;
78 dsa_pointer p[10000];
79 ResourceOwner oldowner;
80 ResourceOwner childowner;
81
82 tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
83 init_tranche, &found);
84
85 /* Create DSA in parent resource owner */
86 a = dsa_create(*tranche_id);
87
88 /*
89 * Switch to child resource owner, and do a bunch of allocations in the
90 * DSA
91 */
92 oldowner = CurrentResourceOwner;
93 childowner = ResourceOwnerCreate(oldowner, "test_dsa temp owner");
94 CurrentResourceOwner = childowner;
95
96 for (int i = 0; i < 10000; i++)
97 {
98 p[i] = dsa_allocate(a, 1000);
99 snprintf(dsa_get_address(a, p[i]), 1000, "foobar%d", i);
100 }
101
102 /* Also test freeing, by freeing some of the allocations. */
103 for (int i = 0; i < 500; i++)
104 dsa_free(a, p[i]);
105
106 /* Release the child resource owner */
107 CurrentResourceOwner = oldowner;
108 ResourceOwnerRelease(childowner,
110 true, false);
111 ResourceOwnerRelease(childowner,
113 true, false);
114 ResourceOwnerRelease(childowner,
116 true, false);
117 ResourceOwnerDelete(childowner);
118
119 dsa_detach(a);
120
122}
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:957
void dsa_detach(dsa_area *area)
Definition: dsa.c:2002
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:841
uint64 dsa_pointer
Definition: dsa.h:62
#define dsa_create(tranche_id)
Definition: dsa.h:117
#define dsa_allocate(area, size)
Definition: dsa.h:109
void * GetNamedDSMSegment(const char *name, size_t size, void(*init_callback)(void *ptr), bool *found)
Definition: dsm_registry.c:186
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
int a
Definition: isn.c:73
int i
Definition: isn.c:77
int LWLockNewTrancheId(const char *name)
Definition: lwlock.c:596
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:260
uint64_t Datum
Definition: postgres.h:70
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:418
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:655
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:868
@ RESOURCE_RELEASE_LOCKS
Definition: resowner.h:55
@ RESOURCE_RELEASE_BEFORE_LOCKS
Definition: resowner.h:54
@ RESOURCE_RELEASE_AFTER_LOCKS
Definition: resowner.h:56
Definition: dsa.c:348
Datum test_dsa_basic(PG_FUNCTION_ARGS)
Definition: test_dsa.c:34
PG_MODULE_MAGIC
Definition: test_dsa.c:21
Datum test_dsa_resowners(PG_FUNCTION_ARGS)
Definition: test_dsa.c:73
PG_FUNCTION_INFO_V1(test_dsa_basic)
static void init_tranche(void *ptr)
Definition: test_dsa.c:24