PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
aio_init.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * aio_init.c
4 * AIO - Subsystem Initialization
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * IDENTIFICATION
10 * src/backend/storage/aio/aio_init.c
11 *
12 *-------------------------------------------------------------------------
13 */
14
15#include "postgres.h"
16
17#include "miscadmin.h"
18#include "storage/aio.h"
20#include "storage/aio_subsys.h"
21#include "storage/bufmgr.h"
22#include "storage/io_worker.h"
23#include "storage/ipc.h"
24#include "storage/proc.h"
25#include "storage/shmem.h"
26#include "utils/guc.h"
27
28
29
30static Size
32{
33 Size sz;
34
35 /* pgaio_ctl itself */
36 sz = offsetof(PgAioCtl, io_handles);
37
38 return sz;
39}
40
41static uint32
43{
44 /*
45 * While AIO workers don't need their own AIO context, we can't currently
46 * guarantee nothing gets assigned to the a ProcNumber for an IO worker if
47 * we just subtracted MAX_IO_WORKERS.
48 */
50}
51
52static Size
54{
55 return mul_size(AioProcs(), sizeof(PgAioBackend));
56}
57
58static Size
60{
61 Size sz;
62
63 /* verify AioChooseMaxConcurrency() did its thing */
65
66 /* io handles */
67 sz = mul_size(AioProcs(),
69
70 return sz;
71}
72
73static Size
75{
76 /* each IO handle can have up to io_max_combine_limit iovec objects */
77 return mul_size(sizeof(struct iovec),
80}
81
82static Size
84{
85 /* each buffer referenced by an iovec can have associated data */
86 return mul_size(sizeof(uint64),
89}
90
91/*
92 * Choose a suitable value for io_max_concurrency.
93 *
94 * It's unlikely that we could have more IOs in flight than buffers that we
95 * would be allowed to pin.
96 *
97 * On the upper end, apply a cap too - just because shared_buffers is large,
98 * it doesn't make sense have millions of buffers undergo IO concurrently.
99 */
100static int
102{
103 uint32 max_backends;
104 int max_proportional_pins;
105
106 /* Similar logic to LimitAdditionalPins() */
107 max_backends = MaxBackends + NUM_AUXILIARY_PROCS;
108 max_proportional_pins = NBuffers / max_backends;
109
110 max_proportional_pins = Max(max_proportional_pins, 1);
111
112 /* apply upper limit */
113 return Min(max_proportional_pins, 64);
114}
115
116Size
118{
119 Size sz = 0;
120
121 /*
122 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
123 * However, if the DBA explicitly set io_max_concurrency = -1 in the
124 * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that and
125 * we must force the matter with PGC_S_OVERRIDE.
126 */
127 if (io_max_concurrency == -1)
128 {
129 char buf[32];
130
131 snprintf(buf, sizeof(buf), "%d", AioChooseMaxConcurrency());
132 SetConfigOption("io_max_concurrency", buf, PGC_POSTMASTER,
134 if (io_max_concurrency == -1) /* failed to apply it? */
135 SetConfigOption("io_max_concurrency", buf, PGC_POSTMASTER,
137 }
138
139 sz = add_size(sz, AioCtlShmemSize());
140 sz = add_size(sz, AioBackendShmemSize());
141 sz = add_size(sz, AioHandleShmemSize());
144
145 /* Reserve space for method specific resources. */
148
149 return sz;
150}
151
152void
154{
155 bool found;
156 uint32 io_handle_off = 0;
157 uint32 iovec_off = 0;
158 uint32 per_backend_iovecs = io_max_concurrency * io_max_combine_limit;
159
160 pgaio_ctl = (PgAioCtl *)
161 ShmemInitStruct("AioCtl", AioCtlShmemSize(), &found);
162
163 if (found)
164 goto out;
165
166 memset(pgaio_ctl, 0, AioCtlShmemSize());
167
169 pgaio_ctl->iovec_count = AioProcs() * per_backend_iovecs;
170
172 ShmemInitStruct("AioBackend", AioBackendShmemSize(), &found);
173
175 ShmemInitStruct("AioHandle", AioHandleShmemSize(), &found);
176
177 pgaio_ctl->iovecs = (struct iovec *)
178 ShmemInitStruct("AioHandleIOV", AioHandleIOVShmemSize(), &found);
180 ShmemInitStruct("AioHandleData", AioHandleDataShmemSize(), &found);
181
182 for (int procno = 0; procno < AioProcs(); procno++)
183 {
184 PgAioBackend *bs = &pgaio_ctl->backend_state[procno];
185
186 bs->io_handle_off = io_handle_off;
187 io_handle_off += io_max_concurrency;
188
189 dclist_init(&bs->idle_ios);
190 memset(bs->staged_ios, 0, sizeof(PgAioHandle *) * PGAIO_SUBMIT_BATCH_SIZE);
192
193 /* initialize per-backend IOs */
194 for (int i = 0; i < io_max_concurrency; i++)
195 {
197
198 ioh->generation = 1;
199 ioh->owner_procno = procno;
200 ioh->iovec_off = iovec_off;
201 ioh->handle_data_len = 0;
202 ioh->report_return = NULL;
203 ioh->resowner = NULL;
204 ioh->num_callbacks = 0;
206 ioh->flags = 0;
207
209
210 dclist_push_tail(&bs->idle_ios, &ioh->node);
211 iovec_off += io_max_combine_limit;
212 }
213 }
214
215out:
216 /* Initialize IO method specific resources. */
219}
220
221void
223{
224 /* shouldn't be initialized twice */
226
228 return;
229
230 if (MyProc == NULL || MyProcNumber >= AioProcs())
231 elog(ERROR, "aio requires a normal PGPROC");
232
234
237
239}
PgAioBackend * pgaio_my_backend
Definition: aio.c:82
int io_max_concurrency
Definition: aio.c:76
PgAioCtl * pgaio_ctl
Definition: aio.c:79
const IoMethodOps * pgaio_method_ops
Definition: aio.c:94
void pgaio_shutdown(int code, Datum arg)
Definition: aio.c:1168
static Size AioHandleIOVShmemSize(void)
Definition: aio_init.c:74
void pgaio_init_backend(void)
Definition: aio_init.c:222
static Size AioHandleDataShmemSize(void)
Definition: aio_init.c:83
void AioShmemInit(void)
Definition: aio_init.c:153
static Size AioHandleShmemSize(void)
Definition: aio_init.c:59
Size AioShmemSize(void)
Definition: aio_init.c:117
static Size AioBackendShmemSize(void)
Definition: aio_init.c:53
static int AioChooseMaxConcurrency(void)
Definition: aio_init.c:101
static uint32 AioProcs(void)
Definition: aio_init.c:42
static Size AioCtlShmemSize(void)
Definition: aio_init.c:31
#define PGAIO_SUBMIT_BATCH_SIZE
Definition: aio_internal.h:28
@ PGAIO_RS_UNKNOWN
Definition: aio_types.h:76
int io_max_combine_limit
Definition: bufmgr.c:168
#define Min(x, y)
Definition: c.h:975
#define Max(x, y)
Definition: c.h:969
uint64_t uint64
Definition: c.h:503
uint32_t uint32
Definition: c.h:502
size_t Size
Definition: c.h:576
void ConditionVariableInit(ConditionVariable *cv)
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
int NBuffers
Definition: globals.c:141
ProcNumber MyProcNumber
Definition: globals.c:89
int MaxBackends
Definition: globals.c:145
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:114
@ PGC_S_OVERRIDE
Definition: guc.h:123
@ PGC_POSTMASTER
Definition: guc.h:74
Assert(PointerIsAligned(start, uint64))
static void dclist_push_tail(dclist_head *head, dlist_node *node)
Definition: ilist.h:709
static void dclist_init(dclist_head *head)
Definition: ilist.h:671
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
int i
Definition: isn.c:77
@ B_IO_WORKER
Definition: miscadmin.h:363
BackendType MyBackendType
Definition: miscinit.c:64
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:239
#define NUM_AUXILIARY_PROCS
Definition: proc.h:447
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
Size mul_size(Size s1, Size s2)
Definition: shmem.c:505
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382
PGPROC * MyProc
Definition: proc.c:66
size_t(* shmem_size)(void)
Definition: aio_internal.h:266
void(* shmem_init)(bool first_time)
Definition: aio_internal.h:272
void(* init_backend)(void)
Definition: aio_internal.h:277
uint32 io_handle_off
Definition: aio_internal.h:183
dclist_head in_flight_ios
Definition: aio_internal.h:214
dclist_head idle_ios
Definition: aio_internal.h:186
PgAioHandle * staged_ios[PGAIO_SUBMIT_BATCH_SIZE]
Definition: aio_internal.h:204
uint32 iovec_count
Definition: aio_internal.h:228
struct iovec * iovecs
Definition: aio_internal.h:229
PgAioHandle * io_handles
Definition: aio_internal.h:241
uint32 io_handle_count
Definition: aio_internal.h:240
uint64 * handle_data
Definition: aio_internal.h:238
PgAioBackend * backend_state
Definition: aio_internal.h:221
struct ResourceOwnerData * resowner
Definition: aio_internal.h:137
int32 owner_procno
Definition: aio_internal.h:120
PgAioResult distilled_result
Definition: aio_internal.h:151
dlist_node node
Definition: aio_internal.h:135
uint8 handle_data_len
Definition: aio_internal.h:117
PgAioReturn * report_return
Definition: aio_internal.h:166
uint32 iovec_off
Definition: aio_internal.h:159
uint64 generation
Definition: aio_internal.h:141
uint8 num_callbacks
Definition: aio_internal.h:105
ConditionVariable cv
Definition: aio_internal.h:148
uint32 status
Definition: aio_types.h:95