PostgreSQL Source Code git master
aio_callback.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * aio_callback.c
4 * AIO - Functionality related to callbacks that can be registered on IO
5 * Handles
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * IDENTIFICATION
11 * src/backend/storage/aio/aio_callback.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include "miscadmin.h"
19#include "storage/aio.h"
21#include "storage/bufmgr.h"
22#include "storage/md.h"
23
24
25/* just to have something to put into aio_handle_cbs */
27
29{
31 const char *const name;
33
34/*
35 * Callback definition for the callbacks that can be registered on an IO
36 * handle. See PgAioHandleCallbackID's definition for an explanation for why
37 * callbacks are not identified by a pointer.
38 */
40#define CALLBACK_ENTRY(id, callback) [id] = {.cb = &callback, .name = #callback}
42
44
46
48#undef CALLBACK_ENTRY
49};
50
51
52
53/* --------------------------------------------------------------------------------
54 * Public callback related functions operating on IO Handles
55 * --------------------------------------------------------------------------------
56 */
57
58/*
59 * Register callback for the IO handle.
60 *
61 * Only a limited number (PGAIO_HANDLE_MAX_CALLBACKS) of callbacks can be
62 * registered for each IO.
63 *
64 * Callbacks need to be registered before [indirectly] calling
65 * pgaio_io_start_*(), as the IO may be executed immediately.
66 *
67 * A callback can be passed a small bit of data, e.g. to indicate whether to
68 * zero a buffer if it is invalid.
69 *
70 *
71 * Note that callbacks are executed in critical sections. This is necessary
72 * to be able to execute IO in critical sections (consider e.g. WAL
73 * logging). To perform AIO we first need to acquire a handle, which, if there
74 * are no free handles, requires waiting for IOs to complete and to execute
75 * their completion callbacks.
76 *
77 * Callbacks may be executed in the issuing backend but also in another
78 * backend (because that backend is waiting for the IO) or in IO workers (if
79 * io_method=worker is used).
80 *
81 *
82 * See PgAioHandleCallbackID's definition for an explanation for why
83 * callbacks are not identified by a pointer.
84 */
85void
87 uint8 cb_data)
88{
89 const PgAioHandleCallbacksEntry *ce = &aio_handle_cbs[cb_id];
90
91 Assert(cb_id <= PGAIO_HCB_MAX);
92 if (cb_id >= lengthof(aio_handle_cbs))
93 elog(ERROR, "callback %d is out of range", cb_id);
94 if (aio_handle_cbs[cb_id].cb->complete_shared == NULL &&
95 aio_handle_cbs[cb_id].cb->complete_local == NULL)
96 elog(ERROR, "callback %d does not have a completion callback", cb_id);
98 elog(PANIC, "too many callbacks, the max is %d",
100 ioh->callbacks[ioh->num_callbacks] = cb_id;
101 ioh->callbacks_data[ioh->num_callbacks] = cb_data;
102
104 "adding cb #%d, id %d/%s",
105 ioh->num_callbacks + 1,
106 cb_id, ce->name);
107
108 ioh->num_callbacks++;
109}
110
111/*
112 * Associate an array of data with the Handle. This is e.g. useful to the
113 * transport knowledge about which buffers a multi-block IO affects to
114 * completion callbacks.
115 *
116 * Right now this can be done only once for each IO, even though multiple
117 * callbacks can be registered. There aren't any known usecases requiring more
118 * and the required amount of shared memory does add up, so it doesn't seem
119 * worth multiplying memory usage by PGAIO_HANDLE_MAX_CALLBACKS.
120 */
121void
123{
125 Assert(ioh->handle_data_len == 0);
128
129 for (int i = 0; i < len; i++)
131 ioh->handle_data_len = len;
132}
133
134/*
135 * Convenience version of pgaio_io_set_handle_data_64() that converts a 32bit
136 * array to a 64bit array. Without it callers would end up needing to
137 * open-code equivalent code.
138 */
139void
141{
143 Assert(ioh->handle_data_len == 0);
146
147 for (int i = 0; i < len; i++)
149 ioh->handle_data_len = len;
150}
151
152/*
153 * Return data set with pgaio_io_set_handle_data_*().
154 */
155uint64 *
157{
158 Assert(ioh->handle_data_len > 0);
159
160 *len = ioh->handle_data_len;
161
162 return &pgaio_ctl->handle_data[ioh->iovec_off];
163}
164
165
166
167/* --------------------------------------------------------------------------------
168 * Public IO Result related functions
169 * --------------------------------------------------------------------------------
170 */
171
172void
173pgaio_result_report(PgAioResult result, const PgAioTargetData *target_data, int elevel)
174{
175 PgAioHandleCallbackID cb_id = result.id;
176 const PgAioHandleCallbacksEntry *ce = &aio_handle_cbs[cb_id];
177
178 Assert(result.status != PGAIO_RS_UNKNOWN);
179 Assert(result.status != PGAIO_RS_OK);
180
181 if (ce->cb->report == NULL)
182 elog(ERROR, "callback %d/%s does not have report callback",
183 result.id, ce->name);
184
185 ce->cb->report(result, target_data, elevel);
186}
187
188
189
190/* --------------------------------------------------------------------------------
191 * Internal callback related functions operating on IO Handles
192 * --------------------------------------------------------------------------------
193 */
194
195/*
196 * Internal function which invokes ->stage for all the registered callbacks.
197 */
198void
200{
202 Assert(ioh->op > PGAIO_OP_INVALID && ioh->op < PGAIO_OP_COUNT);
203
204 for (int i = ioh->num_callbacks; i > 0; i--)
205 {
206 PgAioHandleCallbackID cb_id = ioh->callbacks[i - 1];
207 uint8 cb_data = ioh->callbacks_data[i - 1];
208 const PgAioHandleCallbacksEntry *ce = &aio_handle_cbs[cb_id];
209
210 if (!ce->cb->stage)
211 continue;
212
214 "calling cb #%d %d/%s->stage(%u)",
215 i, cb_id, ce->name, cb_data);
216 ce->cb->stage(ioh, cb_data);
217 }
218}
219
220/*
221 * Internal function which invokes ->complete_shared for all the registered
222 * callbacks.
223 */
224void
226{
227 PgAioResult result;
228
230
232 Assert(ioh->op > PGAIO_OP_INVALID && ioh->op < PGAIO_OP_COUNT);
233
234 result.status = PGAIO_RS_OK; /* low level IO is always considered OK */
235 result.result = ioh->result;
236 result.id = PGAIO_HCB_INVALID;
237 result.error_data = 0;
238
239 /*
240 * Call callbacks with the last registered (innermost) callback first.
241 * Each callback can modify the result forwarded to the next callback.
242 */
243 for (int i = ioh->num_callbacks; i > 0; i--)
244 {
245 PgAioHandleCallbackID cb_id = ioh->callbacks[i - 1];
246 uint8 cb_data = ioh->callbacks_data[i - 1];
247 const PgAioHandleCallbacksEntry *ce = &aio_handle_cbs[cb_id];
248
249 if (!ce->cb->complete_shared)
250 continue;
251
253 "calling cb #%d, id %d/%s->complete_shared(%u) with distilled result: (status %s, id %u, error_data %d, result %d)",
254 i, cb_id, ce->name,
255 cb_data,
257 result.id, result.error_data, result.result);
258 result = ce->cb->complete_shared(ioh, result, cb_data);
259 }
260
261 ioh->distilled_result = result;
262
264 "after shared completion: distilled result: (status %s, id %u, error_data: %d, result %d), raw_result: %d",
266 result.id, result.error_data, result.result,
267 ioh->result);
268
270}
271
272/*
273 * Internal function which invokes ->complete_local for all the registered
274 * callbacks.
275 *
276 * Returns ioh->distilled_result after, possibly, being modified by local
277 * callbacks.
278 *
279 * XXX: It'd be nice to deduplicate with pgaio_io_call_complete_shared().
280 */
283{
284 PgAioResult result;
285
287
289 Assert(ioh->op > PGAIO_OP_INVALID && ioh->op < PGAIO_OP_COUNT);
290
291 /* start with distilled result from shared callback */
292 result = ioh->distilled_result;
293
294 for (int i = ioh->num_callbacks; i > 0; i--)
295 {
296 PgAioHandleCallbackID cb_id = ioh->callbacks[i - 1];
297 uint8 cb_data = ioh->callbacks_data[i - 1];
298 const PgAioHandleCallbacksEntry *ce = &aio_handle_cbs[cb_id];
299
300 if (!ce->cb->complete_local)
301 continue;
302
304 "calling cb #%d, id %d/%s->complete_local(%u) with distilled result: status %s, id %u, error_data %d, result %d",
305 i, cb_id, ce->name, cb_data,
307 result.id, result.error_data, result.result);
308 result = ce->cb->complete_local(ioh, result, cb_data);
309 }
310
311 /*
312 * Note that we don't save the result in ioh->distilled_result, the local
313 * callback's result should not ever matter to other waiters. However, the
314 * local backend does care, so we return the result as modified by local
315 * callbacks, which then can be passed to ioh->report_return->result.
316 */
318 "after local completion: result: (status %s, id %u, error_data %d, result %d), raw_result: %d",
320 result.id, result.error_data, result.result,
321 ioh->result);
322
324
325 return result;
326}
const char * pgaio_result_status_string(PgAioResultStatus rs)
Definition: aio.c:852
PgAioCtl * pgaio_ctl
Definition: aio.c:81
PgAioHandleCallbackID
Definition: aio.h:193
@ PGAIO_HCB_MD_READV
Definition: aio.h:196
@ PGAIO_HCB_LOCAL_BUFFER_READV
Definition: aio.h:200
@ PGAIO_HCB_SHARED_BUFFER_READV
Definition: aio.h:198
@ PGAIO_HCB_INVALID
Definition: aio.h:194
#define PGAIO_HANDLE_MAX_CALLBACKS
Definition: aio.h:267
#define PGAIO_TID_COUNT
Definition: aio.h:123
#define PGAIO_OP_COUNT
Definition: aio.h:107
@ PGAIO_TID_INVALID
Definition: aio.h:119
@ PGAIO_OP_INVALID
Definition: aio.h:90
#define PGAIO_HCB_MAX
Definition: aio.h:203
void pgaio_io_call_stage(PgAioHandle *ioh)
Definition: aio_callback.c:199
static const PgAioHandleCallbacksEntry aio_handle_cbs[]
Definition: aio_callback.c:39
void pgaio_io_set_handle_data_32(PgAioHandle *ioh, uint32 *data, uint8 len)
Definition: aio_callback.c:140
PgAioResult pgaio_io_call_complete_local(PgAioHandle *ioh)
Definition: aio_callback.c:282
void pgaio_io_register_callbacks(PgAioHandle *ioh, PgAioHandleCallbackID cb_id, uint8 cb_data)
Definition: aio_callback.c:86
void pgaio_io_call_complete_shared(PgAioHandle *ioh)
Definition: aio_callback.c:225
uint64 * pgaio_io_get_handle_data(PgAioHandle *ioh, uint8 *len)
Definition: aio_callback.c:156
void pgaio_io_set_handle_data_64(PgAioHandle *ioh, uint64 *data, uint8 len)
Definition: aio_callback.c:122
static const PgAioHandleCallbacks aio_invalid_cb
Definition: aio_callback.c:26
struct PgAioHandleCallbacksEntry PgAioHandleCallbacksEntry
#define CALLBACK_ENTRY(id, callback)
void pgaio_result_report(PgAioResult result, const PgAioTargetData *target_data, int elevel)
Definition: aio_callback.c:173
@ PGAIO_HS_HANDED_OUT
Definition: aio_internal.h:53
#define pgaio_debug_io(elevel, ioh, msg,...)
Definition: aio_internal.h:389
@ PGAIO_RS_OK
Definition: aio_types.h:81
@ PGAIO_RS_UNKNOWN
Definition: aio_types.h:80
int io_max_combine_limit
Definition: bufmgr.c:172
const PgAioHandleCallbacks aio_shared_buffer_readv_cb
Definition: bufmgr.c:7437
const PgAioHandleCallbacks aio_local_buffer_readv_cb
Definition: bufmgr.c:7446
uint8_t uint8
Definition: c.h:500
uint64_t uint64
Definition: c.h:503
uint32_t uint32
Definition: c.h:502
#define lengthof(array)
Definition: c.h:759
#define DEBUG3
Definition: elog.h:28
#define PANIC
Definition: elog.h:42
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define DEBUG4
Definition: elog.h:27
Assert(PointerIsAligned(start, uint64))
int i
Definition: isn.c:77
const PgAioHandleCallbacks aio_md_readv_cb
Definition: md.c:159
#define START_CRIT_SECTION()
Definition: miscadmin.h:150
#define END_CRIT_SECTION()
Definition: miscadmin.h:152
const void size_t len
const void * data
#define PG_IOV_MAX
Definition: pg_iovec.h:41
uint64 * handle_data
Definition: aio_internal.h:243
const PgAioHandleCallbacks *const cb
Definition: aio_callback.c:30
const char *const name
Definition: aio_callback.c:31
PgAioHandleCallbackComplete complete_shared
Definition: aio.h:239
PgAioHandleCallbackStage stage
Definition: aio.h:219
PgAioHandleCallbackReport report
Definition: aio.h:258
PgAioHandleCallbackComplete complete_local
Definition: aio.h:251
PgAioResult distilled_result
Definition: aio_internal.h:156
uint8 callbacks[PGAIO_HANDLE_MAX_CALLBACKS]
Definition: aio_internal.h:113
uint8 handle_data_len
Definition: aio_internal.h:122
PgAioOp op
Definition: aio_internal.h:105
uint32 iovec_off
Definition: aio_internal.h:164
uint8 callbacks_data[PGAIO_HANDLE_MAX_CALLBACKS]
Definition: aio_internal.h:116
uint8 num_callbacks
Definition: aio_internal.h:110
PgAioHandleState state
Definition: aio_internal.h:99
PgAioTargetID target
Definition: aio_internal.h:102
uint32 status
Definition: aio_types.h:108
uint32 error_data
Definition: aio_types.h:111
int32 result
Definition: aio_types.h:113
uint32 id
Definition: aio_types.h:105