PostgreSQL Source Code git master
Loading...
Searching...
No Matches
aio_callback.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "storage/aio.h"
#include "storage/aio_internal.h"
#include "storage/bufmgr.h"
#include "storage/md.h"
Include dependency graph for aio_callback.c:

Go to the source code of this file.

Data Structures

struct  PgAioHandleCallbacksEntry
 

Macros

#define CALLBACK_ENTRY(id, callback)   [id] = {.cb = &callback, .name = #callback}
 

Typedefs

typedef struct PgAioHandleCallbacksEntry PgAioHandleCallbacksEntry
 

Functions

void pgaio_io_register_callbacks (PgAioHandle *ioh, PgAioHandleCallbackID cb_id, uint8 cb_data)
 
void pgaio_io_set_handle_data_64 (PgAioHandle *ioh, uint64 *data, uint8 len)
 
void pgaio_io_set_handle_data_32 (PgAioHandle *ioh, uint32 *data, uint8 len)
 
uint64pgaio_io_get_handle_data (PgAioHandle *ioh, uint8 *len)
 
void pgaio_result_report (PgAioResult result, const PgAioTargetData *target_data, int elevel)
 
void pgaio_io_call_stage (PgAioHandle *ioh)
 
void pgaio_io_call_complete_shared (PgAioHandle *ioh)
 
PgAioResult pgaio_io_call_complete_local (PgAioHandle *ioh)
 

Variables

static const PgAioHandleCallbacks aio_invalid_cb = {0}
 
static const PgAioHandleCallbacksEntry aio_handle_cbs []
 

Macro Definition Documentation

◆ CALLBACK_ENTRY

#define CALLBACK_ENTRY (   id,
  callback 
)    [id] = {.cb = &callback, .name = #callback}

Typedef Documentation

◆ PgAioHandleCallbacksEntry

Function Documentation

◆ pgaio_io_call_complete_local()

PgAioResult pgaio_io_call_complete_local ( PgAioHandle ioh)

Definition at line 285 of file aio_callback.c.

286{
287 PgAioResult result;
288
290
291 Assert(ioh->target > PGAIO_TID_INVALID && ioh->target < PGAIO_TID_COUNT);
293
294 /* start with distilled result from shared callback */
295 result = ioh->distilled_result;
296 Assert(result.status != PGAIO_RS_UNKNOWN);
297
298 for (int i = ioh->num_callbacks; i > 0; i--)
299 {
300 PgAioHandleCallbackID cb_id = ioh->callbacks[i - 1];
301 uint8 cb_data = ioh->callbacks_data[i - 1];
303
304 if (!ce->cb->complete_local)
305 continue;
306
308 "calling cb #%d, id %d/%s->complete_local(%u) with distilled result: status %s, id %u, error_data %d, result %d",
309 i, cb_id, ce->name, cb_data,
311 result.id, result.error_data, result.result);
312 result = ce->cb->complete_local(ioh, result, cb_data);
313
314 /* the callback should never transition to unknown */
315 Assert(result.status != PGAIO_RS_UNKNOWN);
316 }
317
318 /*
319 * Note that we don't save the result in ioh->distilled_result, the local
320 * callback's result should not ever matter to other waiters. However, the
321 * local backend does care, so we return the result as modified by local
322 * callbacks, which then can be passed to ioh->report_return->result.
323 */
325 "after local completion: result: (status %s, id %u, error_data %d, result %d), raw_result: %d",
327 result.id, result.error_data, result.result,
328 ioh->result);
329
331
332 return result;
333}
const char * pgaio_result_status_string(PgAioResultStatus rs)
Definition aio.c:934
PgAioHandleCallbackID
Definition aio.h:193
#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
static const PgAioHandleCallbacksEntry aio_handle_cbs[]
#define pgaio_debug_io(elevel, ioh, msg,...)
@ PGAIO_RS_UNKNOWN
Definition aio_types.h:80
uint8_t uint8
Definition c.h:544
#define Assert(condition)
Definition c.h:873
#define DEBUG3
Definition elog.h:28
#define DEBUG4
Definition elog.h:27
int i
Definition isn.c:77
#define START_CRIT_SECTION()
Definition miscadmin.h:150
#define END_CRIT_SECTION()
Definition miscadmin.h:152
static int fb(int x)
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

References aio_handle_cbs, Assert, DEBUG3, DEBUG4, END_CRIT_SECTION, PgAioResult::error_data, fb(), i, PgAioResult::id, pgaio_debug_io, PGAIO_OP_COUNT, PGAIO_OP_INVALID, pgaio_result_status_string(), PGAIO_RS_UNKNOWN, PGAIO_TID_COUNT, PGAIO_TID_INVALID, PgAioResult::result, START_CRIT_SECTION, and PgAioResult::status.

Referenced by pgaio_io_reclaim().

◆ pgaio_io_call_complete_shared()

void pgaio_io_call_complete_shared ( PgAioHandle ioh)

Definition at line 225 of file aio_callback.c.

226{
227 PgAioResult result;
228
230
231 Assert(ioh->target > PGAIO_TID_INVALID && ioh->target < PGAIO_TID_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];
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 /* the callback should never transition to unknown */
261 Assert(result.status != PGAIO_RS_UNKNOWN);
262 }
263
264 ioh->distilled_result = result;
265
267 "after shared completion: distilled result: (status %s, id %u, error_data: %d, result %d), raw_result: %d",
269 result.id, result.error_data, result.result,
270 ioh->result);
271
273}
@ PGAIO_HCB_INVALID
Definition aio.h:194
@ PGAIO_RS_OK
Definition aio_types.h:81

References aio_handle_cbs, Assert, DEBUG3, DEBUG4, END_CRIT_SECTION, PgAioResult::error_data, fb(), i, PgAioResult::id, pgaio_debug_io, PGAIO_HCB_INVALID, PGAIO_OP_COUNT, PGAIO_OP_INVALID, pgaio_result_status_string(), PGAIO_RS_OK, PGAIO_RS_UNKNOWN, PGAIO_TID_COUNT, PGAIO_TID_INVALID, PgAioResult::result, START_CRIT_SECTION, and PgAioResult::status.

Referenced by pgaio_io_process_completion().

◆ pgaio_io_call_stage()

void pgaio_io_call_stage ( PgAioHandle ioh)

Definition at line 199 of file aio_callback.c.

200{
201 Assert(ioh->target > PGAIO_TID_INVALID && ioh->target < PGAIO_TID_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];
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}

References aio_handle_cbs, Assert, DEBUG3, fb(), i, pgaio_debug_io, PGAIO_OP_COUNT, PGAIO_OP_INVALID, PGAIO_TID_COUNT, and PGAIO_TID_INVALID.

Referenced by pgaio_io_stage().

◆ pgaio_io_get_handle_data()

uint64 * pgaio_io_get_handle_data ( PgAioHandle ioh,
uint8 len 
)

Definition at line 156 of file aio_callback.c.

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}
PgAioCtl * pgaio_ctl
Definition aio.c:78
const void size_t len
uint64 * handle_data

References Assert, fb(), PgAioCtl::handle_data, len, and pgaio_ctl.

Referenced by buffer_readv_complete(), and buffer_stage_common().

◆ pgaio_io_register_callbacks()

void pgaio_io_register_callbacks ( PgAioHandle ioh,
PgAioHandleCallbackID  cb_id,
uint8  cb_data 
)

Definition at line 86 of file aio_callback.c.

88{
90
93 elog(ERROR, "callback %d is out of range", cb_id);
94 if (aio_handle_cbs[cb_id].cb->complete_shared == NULL &&
96 elog(ERROR, "callback %d does not have a completion callback", cb_id);
97 if (ioh->num_callbacks >= PGAIO_HANDLE_MAX_CALLBACKS)
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}
#define PGAIO_HANDLE_MAX_CALLBACKS
Definition aio.h:267
#define PGAIO_HCB_MAX
Definition aio.h:203
#define lengthof(array)
Definition c.h:803
#define PANIC
Definition elog.h:42
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
const PgAioHandleCallbacks *const cb
PgAioHandleCallbackComplete complete_local
Definition aio.h:251

References aio_handle_cbs, Assert, PgAioHandleCallbacksEntry::cb, PgAioHandleCallbacks::complete_local, DEBUG3, elog, ERROR, fb(), lengthof, PANIC, pgaio_debug_io, PGAIO_HANDLE_MAX_CALLBACKS, and PGAIO_HCB_MAX.

Referenced by AsyncReadBuffers(), mdstartreadv(), and read_rel_block_ll().

◆ pgaio_io_set_handle_data_32()

void pgaio_io_set_handle_data_32 ( PgAioHandle ioh,
uint32 data,
uint8  len 
)

Definition at line 140 of file aio_callback.c.

141{
142 Assert(ioh->state == PGAIO_HS_HANDED_OUT);
143 Assert(ioh->handle_data_len == 0);
146
147 for (int i = 0; i < len; i++)
148 pgaio_ctl->handle_data[ioh->iovec_off + i] = data[i];
149 ioh->handle_data_len = len;
150}
@ PGAIO_HS_HANDED_OUT
int io_max_combine_limit
Definition bufmgr.c:201
const void * data
#define PG_IOV_MAX
Definition pg_iovec.h:47

References Assert, data, fb(), PgAioCtl::handle_data, i, io_max_combine_limit, len, PG_IOV_MAX, pgaio_ctl, and PGAIO_HS_HANDED_OUT.

Referenced by AsyncReadBuffers(), and read_rel_block_ll().

◆ pgaio_io_set_handle_data_64()

void pgaio_io_set_handle_data_64 ( PgAioHandle ioh,
uint64 data,
uint8  len 
)

Definition at line 122 of file aio_callback.c.

123{
124 Assert(ioh->state == PGAIO_HS_HANDED_OUT);
125 Assert(ioh->handle_data_len == 0);
128
129 for (int i = 0; i < len; i++)
130 pgaio_ctl->handle_data[ioh->iovec_off + i] = data[i];
131 ioh->handle_data_len = len;
132}

References Assert, data, fb(), PgAioCtl::handle_data, i, io_max_combine_limit, len, PG_IOV_MAX, pgaio_ctl, and PGAIO_HS_HANDED_OUT.

◆ pgaio_result_report()

void pgaio_result_report ( PgAioResult  result,
const PgAioTargetData target_data,
int  elevel 
)

Definition at line 173 of file aio_callback.c.

174{
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}

References aio_handle_cbs, Assert, elog, ERROR, fb(), PgAioResult::id, PGAIO_RS_OK, PGAIO_RS_UNKNOWN, and PgAioResult::status.

Referenced by buffer_readv_complete(), buffer_readv_complete_one(), md_readv_complete(), ProcessReadBuffersResult(), and read_rel_block_ll().

Variable Documentation

◆ aio_handle_cbs

const PgAioHandleCallbacksEntry aio_handle_cbs[]
static
Initial value:
= {
#define CALLBACK_ENTRY(id, callback)
}
@ 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
static const PgAioHandleCallbacks aio_invalid_cb
#define CALLBACK_ENTRY(id, callback)
const PgAioHandleCallbacks aio_shared_buffer_readv_cb
Definition bufmgr.c:8510
const PgAioHandleCallbacks aio_local_buffer_readv_cb
Definition bufmgr.c:8519
const PgAioHandleCallbacks aio_md_readv_cb
Definition md.c:169

Definition at line 39 of file aio_callback.c.

39 {
40#define CALLBACK_ENTRY(id, callback) [id] = {.cb = &callback, .name = #callback}
42
44
46
48#undef CALLBACK_ENTRY
49};

Referenced by pgaio_io_call_complete_local(), pgaio_io_call_complete_shared(), pgaio_io_call_stage(), pgaio_io_register_callbacks(), and pgaio_result_report().

◆ aio_invalid_cb

const PgAioHandleCallbacks aio_invalid_cb = {0}
static

Definition at line 26 of file aio_callback.c.

26{0};