PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
aio.c File Reference
#include "postgres.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "port/atomics.h"
#include "storage/aio.h"
#include "storage/aio_internal.h"
#include "storage/aio_subsys.h"
#include "utils/guc.h"
#include "utils/guc_hooks.h"
#include "utils/resowner.h"
#include "utils/wait_event_types.h"
Include dependency graph for aio.c:

Go to the source code of this file.

Macros

#define PGAIO_HS_TOSTR_CASE(sym)   case PGAIO_HS_##sym: return #sym
 

Functions

static void pgaio_io_update_state (PgAioHandle *ioh, PgAioHandleState new_state)
 
static void pgaio_io_reclaim (PgAioHandle *ioh)
 
static void pgaio_io_resowner_register (PgAioHandle *ioh)
 
static void pgaio_io_wait_for_free (void)
 
static PgAioHandlepgaio_io_from_wref (PgAioWaitRef *iow, uint64 *ref_generation)
 
static const char * pgaio_io_state_get_name (PgAioHandleState s)
 
static void pgaio_io_wait (PgAioHandle *ioh, uint64 ref_generation)
 
PgAioHandlepgaio_io_acquire (struct ResourceOwnerData *resowner, PgAioReturn *ret)
 
PgAioHandlepgaio_io_acquire_nb (struct ResourceOwnerData *resowner, PgAioReturn *ret)
 
void pgaio_io_release (PgAioHandle *ioh)
 
void pgaio_io_release_resowner (dlist_node *ioh_node, bool on_error)
 
void pgaio_io_set_flag (PgAioHandle *ioh, PgAioHandleFlags flag)
 
int pgaio_io_get_id (PgAioHandle *ioh)
 
ProcNumber pgaio_io_get_owner (PgAioHandle *ioh)
 
void pgaio_io_get_wref (PgAioHandle *ioh, PgAioWaitRef *iow)
 
void pgaio_io_stage (PgAioHandle *ioh, PgAioOp op)
 
bool pgaio_io_needs_synchronous_execution (PgAioHandle *ioh)
 
void pgaio_io_prepare_submit (PgAioHandle *ioh)
 
void pgaio_io_process_completion (PgAioHandle *ioh, int result)
 
bool pgaio_io_was_recycled (PgAioHandle *ioh, uint64 ref_generation, PgAioHandleState *state)
 
const char * pgaio_io_get_state_name (PgAioHandle *ioh)
 
const char * pgaio_result_status_string (PgAioResultStatus rs)
 
void pgaio_wref_clear (PgAioWaitRef *iow)
 
bool pgaio_wref_valid (PgAioWaitRef *iow)
 
int pgaio_wref_get_id (PgAioWaitRef *iow)
 
void pgaio_wref_wait (PgAioWaitRef *iow)
 
bool pgaio_wref_check_done (PgAioWaitRef *iow)
 
void pgaio_enter_batchmode (void)
 
void pgaio_exit_batchmode (void)
 
bool pgaio_have_staged (void)
 
void pgaio_submit_staged (void)
 
void pgaio_error_cleanup (void)
 
void AtEOXact_Aio (bool is_commit)
 
void pgaio_closing_fd (int fd)
 
void pgaio_shutdown (int code, Datum arg)
 
void assign_io_method (int newval, void *extra)
 
bool check_io_max_concurrency (int *newval, void **extra, GucSource source)
 

Variables

const struct config_enum_entry io_method_options []
 
int io_method = DEFAULT_IO_METHOD
 
int io_max_concurrency = -1
 
PgAioCtlpgaio_ctl
 
PgAioBackendpgaio_my_backend
 
static const IoMethodOps *const pgaio_method_ops_table []
 
const IoMethodOpspgaio_method_ops
 

Macro Definition Documentation

◆ PGAIO_HS_TOSTR_CASE

#define PGAIO_HS_TOSTR_CASE (   sym)    case PGAIO_HS_##sym: return #sym

Function Documentation

◆ assign_io_method()

void assign_io_method ( int  newval,
void *  extra 
)

Definition at line 1156 of file aio.c.

1157{
1160
1162}
const IoMethodOps * pgaio_method_ops
Definition: aio.c:88
static const IoMethodOps *const pgaio_method_ops_table[]
Definition: aio.c:82
const struct config_enum_entry io_method_options[]
Definition: aio.c:65
#define lengthof(array)
Definition: c.h:759
#define newval
Assert(PointerIsAligned(start, uint64))

References Assert(), io_method_options, lengthof, newval, pgaio_method_ops, and pgaio_method_ops_table.

◆ AtEOXact_Aio()

void AtEOXact_Aio ( bool  is_commit)

Definition at line 1080 of file aio.c.

1081{
1082 /*
1083 * We should never be in batch mode at transactional boundaries. In case
1084 * an error was thrown while in batch mode, pgaio_error_cleanup() should
1085 * have exited batchmode.
1086 *
1087 * In case we are in batchmode somehow, make sure to submit all staged
1088 * IOs, other backends may need them to complete to continue.
1089 */
1091 {
1093 elog(WARNING, "open AIO batch at end of (sub-)transaction");
1094 }
1095
1096 /*
1097 * As we aren't in batchmode, there shouldn't be any unsubmitted IOs.
1098 */
1100}
PgAioBackend * pgaio_my_backend
Definition: aio.c:79
void pgaio_error_cleanup(void)
Definition: aio.c:1052
#define WARNING
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:225
uint16 num_staged_ios
Definition: aio_internal.h:203

References Assert(), elog, PgAioBackend::in_batchmode, PgAioBackend::num_staged_ios, pgaio_error_cleanup(), pgaio_my_backend, and WARNING.

Referenced by AbortSubTransaction(), AbortTransaction(), CommitTransaction(), pgaio_shutdown(), and PrepareTransaction().

◆ check_io_max_concurrency()

bool check_io_max_concurrency ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 1165 of file aio.c.

1166{
1167 if (*newval == -1)
1168 {
1169 /*
1170 * Auto-tuning will be applied later during startup, as auto-tuning
1171 * depends on the value of various GUCs.
1172 */
1173 return true;
1174 }
1175 else if (*newval == 0)
1176 {
1177 GUC_check_errdetail("Only -1 or values bigger than 0 are valid.");
1178 return false;
1179 }
1180
1181 return true;
1182}
#define GUC_check_errdetail
Definition: guc.h:481

References GUC_check_errdetail, and newval.

◆ pgaio_closing_fd()

void pgaio_closing_fd ( int  fd)

Definition at line 1107 of file aio.c.

1108{
1109 /*
1110 * Might be called before AIO is initialized or in a subprocess that
1111 * doesn't use AIO.
1112 */
1113 if (!pgaio_my_backend)
1114 return;
1115
1116 /*
1117 * For now just submit all staged IOs - we could be more selective, but
1118 * it's probably not worth it.
1119 */
1121}
void pgaio_submit_staged(void)
Definition: aio.c:1010

References pgaio_my_backend, and pgaio_submit_staged().

◆ pgaio_enter_batchmode()

void pgaio_enter_batchmode ( void  )

Definition at line 968 of file aio.c.

969{
971 elog(ERROR, "starting batch while batch already in progress");
973}
#define ERROR
Definition: elog.h:39

References elog, ERROR, PgAioBackend::in_batchmode, and pgaio_my_backend.

◆ pgaio_error_cleanup()

void pgaio_error_cleanup ( void  )

Definition at line 1052 of file aio.c.

1053{
1054 /*
1055 * It is possible that code errored out after pgaio_enter_batchmode() but
1056 * before pgaio_exit_batchmode() was called. In that case we need to
1057 * submit the IO now.
1058 */
1060 {
1062
1064 }
1065
1066 /*
1067 * As we aren't in batchmode, there shouldn't be any unsubmitted IOs.
1068 */
1070}

References Assert(), PgAioBackend::in_batchmode, PgAioBackend::num_staged_ios, pgaio_my_backend, and pgaio_submit_staged().

Referenced by AbortSubTransaction(), AbortTransaction(), AtEOXact_Aio(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), pgarch_archiveXlog(), WalSndErrorCleanup(), WalSummarizerMain(), and WalWriterMain().

◆ pgaio_exit_batchmode()

void pgaio_exit_batchmode ( void  )

◆ pgaio_have_staged()

bool pgaio_have_staged ( void  )

◆ pgaio_io_acquire()

PgAioHandle * pgaio_io_acquire ( struct ResourceOwnerData resowner,
PgAioReturn ret 
)

Definition at line 165 of file aio.c.

166{
167 PgAioHandle *h;
168
169 while (true)
170 {
171 h = pgaio_io_acquire_nb(resowner, ret);
172
173 if (h != NULL)
174 return h;
175
176 /*
177 * Evidently all handles by this backend are in use. Just wait for
178 * some to complete.
179 */
181 }
182}
static void pgaio_io_wait_for_free(void)
Definition: aio.c:693
PgAioHandle * pgaio_io_acquire_nb(struct ResourceOwnerData *resowner, PgAioReturn *ret)
Definition: aio.c:191

References pgaio_io_acquire_nb(), and pgaio_io_wait_for_free().

◆ pgaio_io_acquire_nb()

PgAioHandle * pgaio_io_acquire_nb ( struct ResourceOwnerData resowner,
PgAioReturn ret 
)

Definition at line 191 of file aio.c.

192{
194 {
197 }
198
200 elog(ERROR, "API violation: Only one IO can be handed out");
201
203 {
205 PgAioHandle *ioh = dclist_container(PgAioHandle, node, ion);
206
207 Assert(ioh->state == PGAIO_HS_IDLE);
209
212
213 if (resowner)
215
216 if (ret)
217 {
218 ioh->report_return = ret;
220 }
221
222 return ioh;
223 }
224
225 return NULL;
226}
static void pgaio_io_update_state(PgAioHandle *ioh, PgAioHandleState new_state)
Definition: aio.c:366
static void pgaio_io_resowner_register(PgAioHandle *ioh)
Definition: aio.c:382
@ PGAIO_HS_IDLE
Definition: aio_internal.h:41
@ PGAIO_HS_HANDED_OUT
Definition: aio_internal.h:48
#define PGAIO_SUBMIT_BATCH_SIZE
Definition: aio_internal.h:28
@ PGAIO_RS_UNKNOWN
Definition: aio_types.h:76
ProcNumber MyProcNumber
Definition: globals.c:89
#define dclist_container(type, membername, ptr)
Definition: ilist.h:947
static bool dclist_is_empty(const dclist_head *head)
Definition: ilist.h:682
static dlist_node * dclist_pop_head_node(dclist_head *head)
Definition: ilist.h:789
dclist_head idle_ios
Definition: aio_internal.h:186
PgAioHandle * handed_out_io
Definition: aio_internal.h:195
int32 owner_procno
Definition: aio_internal.h:120
PgAioReturn * report_return
Definition: aio_internal.h:166
PgAioHandleState state
Definition: aio_internal.h:94
uint32 status
Definition: aio_types.h:95
PgAioResult result
Definition: aio_types.h:112

References Assert(), dclist_container, dclist_is_empty(), dclist_pop_head_node(), elog, ERROR, PgAioBackend::handed_out_io, PgAioBackend::idle_ios, MyProcNumber, PgAioBackend::num_staged_ios, PgAioHandle::owner_procno, PGAIO_HS_HANDED_OUT, PGAIO_HS_IDLE, pgaio_io_resowner_register(), pgaio_io_update_state(), pgaio_my_backend, PGAIO_RS_UNKNOWN, PGAIO_SUBMIT_BATCH_SIZE, pgaio_submit_staged(), PgAioHandle::report_return, PgAioReturn::result, PgAioHandle::state, and PgAioResult::status.

Referenced by pgaio_io_acquire().

◆ pgaio_io_from_wref()

static PgAioHandle * pgaio_io_from_wref ( PgAioWaitRef iow,
uint64 ref_generation 
)
static

Definition at line 785 of file aio.c.

786{
787 PgAioHandle *ioh;
788
790
791 ioh = &pgaio_ctl->io_handles[iow->aio_index];
792
793 *ref_generation = ((uint64) iow->generation_upper) << 32 |
794 iow->generation_lower;
795
796 Assert(*ref_generation != 0);
797
798 return ioh;
799}
PgAioCtl * pgaio_ctl
Definition: aio.c:76
uint64_t uint64
Definition: c.h:503
PgAioHandle * io_handles
Definition: aio_internal.h:241
uint32 io_handle_count
Definition: aio_internal.h:240
uint32 generation_upper
Definition: aio_types.h:45
uint32 aio_index
Definition: aio_types.h:35
uint32 generation_lower
Definition: aio_types.h:46

References PgAioWaitRef::aio_index, Assert(), PgAioWaitRef::generation_lower, PgAioWaitRef::generation_upper, PgAioCtl::io_handle_count, PgAioCtl::io_handles, and pgaio_ctl.

Referenced by pgaio_wref_check_done(), and pgaio_wref_wait().

◆ pgaio_io_get_id()

int pgaio_io_get_id ( PgAioHandle ioh)

Definition at line 322 of file aio.c.

323{
324 Assert(ioh >= pgaio_ctl->io_handles &&
326 return ioh - pgaio_ctl->io_handles;
327}

References Assert(), PgAioCtl::io_handle_count, PgAioCtl::io_handles, and pgaio_ctl.

Referenced by pgaio_io_wait_for_free(), and pgaio_worker_submission_queue_insert().

◆ pgaio_io_get_owner()

ProcNumber pgaio_io_get_owner ( PgAioHandle ioh)

Definition at line 335 of file aio.c.

336{
337 return ioh->owner_procno;
338}

References PgAioHandle::owner_procno.

◆ pgaio_io_get_state_name()

const char * pgaio_io_get_state_name ( PgAioHandle ioh)

Definition at line 822 of file aio.c.

823{
824 return pgaio_io_state_get_name(ioh->state);
825}
static const char * pgaio_io_state_get_name(PgAioHandleState s)
Definition: aio.c:802

References pgaio_io_state_get_name(), and PgAioHandle::state.

◆ pgaio_io_get_wref()

void pgaio_io_get_wref ( PgAioHandle ioh,
PgAioWaitRef iow 
)

◆ pgaio_io_needs_synchronous_execution()

bool pgaio_io_needs_synchronous_execution ( PgAioHandle ioh)

Definition at line 447 of file aio.c.

448{
449 /*
450 * If the caller said to execute the IO synchronously, do so.
451 *
452 * XXX: We could optimize the logic when to execute synchronously by first
453 * checking if there are other IOs in flight and only synchronously
454 * executing if not. Unclear whether that'll be sufficiently common to be
455 * worth worrying about.
456 */
457 if (ioh->flags & PGAIO_HF_SYNCHRONOUS)
458 return true;
459
460 /* Check if the IO method requires synchronous execution of IO */
463
464 return false;
465}
@ PGAIO_HF_SYNCHRONOUS
Definition: aio.h:62
bool(* needs_synchronous_execution)(PgAioHandle *ioh)
Definition: aio_internal.h:274

References PgAioHandle::flags, IoMethodOps::needs_synchronous_execution, PGAIO_HF_SYNCHRONOUS, and pgaio_method_ops.

Referenced by pgaio_io_stage().

◆ pgaio_io_prepare_submit()

void pgaio_io_prepare_submit ( PgAioHandle ioh)

Definition at line 474 of file aio.c.

475{
477
479}
@ PGAIO_HS_SUBMITTED
Definition: aio_internal.h:64
static void dclist_push_tail(dclist_head *head, dlist_node *node)
Definition: ilist.h:709
dclist_head in_flight_ios
Definition: aio_internal.h:214
dlist_node node
Definition: aio_internal.h:135

References dclist_push_tail(), PgAioBackend::in_flight_ios, PgAioHandle::node, PGAIO_HS_SUBMITTED, pgaio_io_update_state(), and pgaio_my_backend.

Referenced by pgaio_io_stage(), and pgaio_worker_submit().

◆ pgaio_io_process_completion()

void pgaio_io_process_completion ( PgAioHandle ioh,
int  result 
)

Definition at line 492 of file aio.c.

493{
495
497
498 ioh->result = result;
499
501
502 pgaio_io_call_inj(ioh, "AIO_PROCESS_COMPLETION_BEFORE_SHARED");
503
505
507
508 /* condition variable broadcast ensures state is visible before wakeup */
510
511 /* contains call to pgaio_io_call_complete_local() */
512 if (ioh->owner_procno == MyProcNumber)
513 pgaio_io_reclaim(ioh);
514}
static void pgaio_io_reclaim(PgAioHandle *ioh)
Definition: aio.c:621
void pgaio_io_call_complete_shared(PgAioHandle *ioh)
Definition: aio_callback.c:214
@ PGAIO_HS_COMPLETED_SHARED
Definition: aio_internal.h:77
@ PGAIO_HS_COMPLETED_IO
Definition: aio_internal.h:67
#define pgaio_io_call_inj(ioh, injection_point)
Definition: aio_internal.h:376
void ConditionVariableBroadcast(ConditionVariable *cv)
volatile uint32 CritSectionCount
Definition: globals.c:44
ConditionVariable cv
Definition: aio_internal.h:148

References Assert(), ConditionVariableBroadcast(), CritSectionCount, PgAioHandle::cv, MyProcNumber, PgAioHandle::owner_procno, PGAIO_HS_COMPLETED_IO, PGAIO_HS_COMPLETED_SHARED, PGAIO_HS_SUBMITTED, pgaio_io_call_complete_shared(), pgaio_io_call_inj, pgaio_io_reclaim(), pgaio_io_update_state(), PgAioHandle::result, and PgAioHandle::state.

Referenced by IoWorkerMain(), and pgaio_io_perform_synchronously().

◆ pgaio_io_reclaim()

static void pgaio_io_reclaim ( PgAioHandle ioh)
static

Definition at line 621 of file aio.c.

622{
623 /* This is only ok if it's our IO */
625 Assert(ioh->state != PGAIO_HS_IDLE);
626
627 /*
628 * It's a bit ugly, but right now the easiest place to put the execution
629 * of shared completion callbacks is this function, as we need to execute
630 * local callbacks just before reclaiming at multiple callsites.
631 */
633 {
636 }
637
639 "reclaiming: distilled_result: (status %s, id %u, error_data %d), raw_result: %d",
641 ioh->distilled_result.id,
643 ioh->result);
644
645 /* if the IO has been defined, we might need to do more work */
646 if (ioh->state != PGAIO_HS_HANDED_OUT)
647 {
649
650 if (ioh->report_return)
651 {
654 }
655 }
656
657 if (ioh->resowner)
658 {
660 ioh->resowner = NULL;
661 }
662
663 Assert(!ioh->resowner);
664
665 ioh->op = PGAIO_OP_INVALID;
667 ioh->flags = 0;
668 ioh->num_callbacks = 0;
669 ioh->handle_data_len = 0;
670 ioh->report_return = NULL;
671 ioh->result = 0;
673
674 /* XXX: the barrier is probably superfluous */
676 ioh->generation++;
677
679
680 /*
681 * We push the IO to the head of the idle IO list, that seems more cache
682 * efficient in cases where only a few IOs are used.
683 */
685}
const char * pgaio_result_status_string(PgAioResultStatus rs)
Definition: aio.c:828
@ PGAIO_TID_INVALID
Definition: aio.h:111
@ PGAIO_OP_INVALID
Definition: aio.h:82
void pgaio_io_call_complete_local(PgAioHandle *ioh)
Definition: aio_callback.c:268
@ PGAIO_HS_COMPLETED_LOCAL
Definition: aio_internal.h:84
#define pgaio_debug_io(elevel, ioh, msg,...)
Definition: aio_internal.h:358
#define pg_write_barrier()
Definition: atomics.h:157
#define DEBUG4
Definition: elog.h:27
static void dclist_delete_from(dclist_head *head, dlist_node *node)
Definition: ilist.h:763
static void dclist_push_head(dclist_head *head, dlist_node *node)
Definition: ilist.h:693
void ResourceOwnerForgetAioHandle(ResourceOwner owner, struct dlist_node *ioh_node)
Definition: resowner.c:1110
PgAioTargetData target_data
Definition: aio_internal.h:176
struct ResourceOwnerData * resowner
Definition: aio_internal.h:137
PgAioResult distilled_result
Definition: aio_internal.h:151
uint8 handle_data_len
Definition: aio_internal.h:117
PgAioOp op
Definition: aio_internal.h:100
uint8 num_callbacks
Definition: aio_internal.h:105
dlist_node resowner_node
Definition: aio_internal.h:138
PgAioTargetID target
Definition: aio_internal.h:97
uint32 error_data
Definition: aio_types.h:98
uint32 id
Definition: aio_types.h:92
PgAioTargetData target_data
Definition: aio_types.h:113

References Assert(), dclist_delete_from(), dclist_push_head(), DEBUG4, PgAioHandle::distilled_result, PgAioResult::error_data, PgAioHandle::flags, PgAioHandle::generation, PgAioHandle::handle_data_len, PgAioResult::id, PgAioBackend::idle_ios, PgAioBackend::in_flight_ios, MyProcNumber, PgAioHandle::node, PgAioHandle::num_callbacks, PgAioHandle::op, PgAioHandle::owner_procno, pg_write_barrier, pgaio_debug_io, PGAIO_HS_COMPLETED_LOCAL, PGAIO_HS_COMPLETED_SHARED, PGAIO_HS_HANDED_OUT, PGAIO_HS_IDLE, pgaio_io_call_complete_local(), pgaio_io_update_state(), pgaio_my_backend, PGAIO_OP_INVALID, pgaio_result_status_string(), PGAIO_RS_UNKNOWN, PGAIO_TID_INVALID, PgAioHandle::report_return, ResourceOwnerForgetAioHandle(), PgAioHandle::resowner, PgAioHandle::resowner_node, PgAioHandle::result, PgAioReturn::result, PgAioHandle::state, PgAioResult::status, PgAioHandle::target, PgAioHandle::target_data, and PgAioReturn::target_data.

Referenced by pgaio_io_process_completion(), pgaio_io_release(), pgaio_io_release_resowner(), pgaio_io_wait(), pgaio_io_wait_for_free(), and pgaio_wref_check_done().

◆ pgaio_io_release()

void pgaio_io_release ( PgAioHandle ioh)

Definition at line 234 of file aio.c.

235{
237 {
239 Assert(ioh->resowner);
240
242 pgaio_io_reclaim(ioh);
243 }
244 else
245 {
246 elog(ERROR, "release in unexpected state");
247 }
248}

References Assert(), elog, ERROR, PgAioBackend::handed_out_io, PGAIO_HS_HANDED_OUT, pgaio_io_reclaim(), pgaio_my_backend, PgAioHandle::resowner, and PgAioHandle::state.

◆ pgaio_io_release_resowner()

void pgaio_io_release_resowner ( dlist_node ioh_node,
bool  on_error 
)

Definition at line 254 of file aio.c.

255{
256 PgAioHandle *ioh = dlist_container(PgAioHandle, resowner_node, ioh_node);
257
258 Assert(ioh->resowner);
259
261 ioh->resowner = NULL;
262
263 switch (ioh->state)
264 {
265 case PGAIO_HS_IDLE:
266 elog(ERROR, "unexpected");
267 break;
270
272 {
274 if (!on_error)
275 elog(WARNING, "leaked AIO handle");
276 }
277
278 pgaio_io_reclaim(ioh);
279 break;
280 case PGAIO_HS_DEFINED:
281 case PGAIO_HS_STAGED:
282 if (!on_error)
283 elog(WARNING, "AIO handle was not submitted");
285 break;
290 /* this is expected to happen */
291 break;
292 }
293
294 /*
295 * Need to unregister the reporting of the IO's result, the memory it's
296 * referencing likely has gone away.
297 */
298 if (ioh->report_return)
299 ioh->report_return = NULL;
300}
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593

References Assert(), dlist_container, elog, ERROR, PgAioBackend::handed_out_io, PGAIO_HS_COMPLETED_IO, PGAIO_HS_COMPLETED_LOCAL, PGAIO_HS_COMPLETED_SHARED, PGAIO_HS_DEFINED, PGAIO_HS_HANDED_OUT, PGAIO_HS_IDLE, PGAIO_HS_STAGED, PGAIO_HS_SUBMITTED, pgaio_io_reclaim(), pgaio_my_backend, pgaio_submit_staged(), PgAioHandle::report_return, ResourceOwnerForgetAioHandle(), PgAioHandle::resowner, PgAioHandle::resowner_node, PgAioHandle::state, and WARNING.

Referenced by ResourceOwnerReleaseInternal().

◆ pgaio_io_resowner_register()

static void pgaio_io_resowner_register ( PgAioHandle ioh)
static

Definition at line 382 of file aio.c.

383{
384 Assert(!ioh->resowner);
386
389}
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void ResourceOwnerRememberAioHandle(ResourceOwner owner, struct dlist_node *ioh_node)
Definition: resowner.c:1104

References Assert(), CurrentResourceOwner, ResourceOwnerRememberAioHandle(), PgAioHandle::resowner, and PgAioHandle::resowner_node.

Referenced by pgaio_io_acquire_nb().

◆ pgaio_io_set_flag()

void pgaio_io_set_flag ( PgAioHandle ioh,
PgAioHandleFlags  flag 
)

Definition at line 310 of file aio.c.

311{
313
314 ioh->flags |= flag;
315}
char * flag(int b)
Definition: test-ctype.c:33

References Assert(), flag(), PgAioHandle::flags, PGAIO_HS_HANDED_OUT, and PgAioHandle::state.

◆ pgaio_io_stage()

void pgaio_io_stage ( PgAioHandle ioh,
PgAioOp  op 
)

Definition at line 397 of file aio.c.

398{
399 bool needs_synchronous;
400
404
405 ioh->op = op;
406 ioh->result = 0;
407
409
410 /* allow a new IO to be staged */
412
414
416
417 /*
418 * Synchronous execution has to be executed, well, synchronously, so check
419 * that first.
420 */
421 needs_synchronous = pgaio_io_needs_synchronous_execution(ioh);
422
424 "prepared (synchronous: %d, in_batch: %d)",
425 needs_synchronous, pgaio_my_backend->in_batchmode);
426
427 if (!needs_synchronous)
428 {
431
432 /*
433 * Unless code explicitly opted into batching IOs, submit the IO
434 * immediately.
435 */
438 }
439 else
440 {
443 }
444}
bool pgaio_io_needs_synchronous_execution(PgAioHandle *ioh)
Definition: aio.c:447
void pgaio_io_prepare_submit(PgAioHandle *ioh)
Definition: aio.c:474
void pgaio_io_call_stage(PgAioHandle *ioh)
Definition: aio_callback.c:188
void pgaio_io_perform_synchronously(PgAioHandle *ioh)
Definition: aio_io.c:116
bool pgaio_io_has_target(PgAioHandle *ioh)
Definition: aio_target.c:38
#define DEBUG3
Definition: elog.h:28
PgAioHandle * staged_ios[PGAIO_SUBMIT_BATCH_SIZE]
Definition: aio_internal.h:204

References Assert(), DEBUG3, PgAioBackend::handed_out_io, PgAioBackend::in_batchmode, PgAioBackend::num_staged_ios, PgAioHandle::op, pgaio_debug_io, PGAIO_HS_DEFINED, PGAIO_HS_HANDED_OUT, PGAIO_HS_STAGED, pgaio_io_call_stage(), pgaio_io_has_target(), pgaio_io_needs_synchronous_execution(), pgaio_io_perform_synchronously(), pgaio_io_prepare_submit(), pgaio_io_update_state(), pgaio_my_backend, PGAIO_SUBMIT_BATCH_SIZE, pgaio_submit_staged(), PgAioHandle::result, PgAioBackend::staged_ios, and PgAioHandle::state.

Referenced by pgaio_io_prep_readv(), and pgaio_io_prep_writev().

◆ pgaio_io_state_get_name()

static const char * pgaio_io_state_get_name ( PgAioHandleState  s)
static

Definition at line 802 of file aio.c.

803{
804#define PGAIO_HS_TOSTR_CASE(sym) case PGAIO_HS_##sym: return #sym
805 switch (s)
806 {
808 PGAIO_HS_TOSTR_CASE(HANDED_OUT);
809 PGAIO_HS_TOSTR_CASE(DEFINED);
810 PGAIO_HS_TOSTR_CASE(STAGED);
811 PGAIO_HS_TOSTR_CASE(SUBMITTED);
812 PGAIO_HS_TOSTR_CASE(COMPLETED_IO);
813 PGAIO_HS_TOSTR_CASE(COMPLETED_SHARED);
814 PGAIO_HS_TOSTR_CASE(COMPLETED_LOCAL);
815 }
816#undef PGAIO_HS_TOSTR_CASE
817
818 return NULL; /* silence compiler */
819}
#define PGAIO_HS_TOSTR_CASE(sym)

References PGAIO_HS_TOSTR_CASE.

Referenced by pgaio_io_get_state_name(), and pgaio_io_update_state().

◆ pgaio_io_update_state()

static void pgaio_io_update_state ( PgAioHandle ioh,
PgAioHandleState  new_state 
)
inlinestatic

Definition at line 366 of file aio.c.

367{
369 "updating state to %s",
370 pgaio_io_state_get_name(new_state));
371
372 /*
373 * Ensure the changes signified by the new state are visible before the
374 * new state becomes visible.
375 */
377
378 ioh->state = new_state;
379}
#define DEBUG5
Definition: elog.h:26

References DEBUG5, pg_write_barrier, pgaio_debug_io, pgaio_io_state_get_name(), and PgAioHandle::state.

Referenced by pgaio_io_acquire_nb(), pgaio_io_prepare_submit(), pgaio_io_process_completion(), pgaio_io_reclaim(), and pgaio_io_stage().

◆ pgaio_io_wait()

static void pgaio_io_wait ( PgAioHandle ioh,
uint64  ref_generation 
)
static

Definition at line 536 of file aio.c.

537{
539 bool am_owner;
540
541 am_owner = ioh->owner_procno == MyProcNumber;
542
543 if (pgaio_io_was_recycled(ioh, ref_generation, &state))
544 return;
545
546 if (am_owner)
547 {
552 {
553 elog(PANIC, "waiting for own IO in wrong state: %d",
554 state);
555 }
556 }
557
558 while (true)
559 {
560 if (pgaio_io_was_recycled(ioh, ref_generation, &state))
561 return;
562
563 switch (state)
564 {
565 case PGAIO_HS_IDLE:
567 elog(ERROR, "IO in wrong state: %d", state);
568 break;
569
571
572 /*
573 * If we need to wait via the IO method, do so now. Don't
574 * check via the IO method if the issuing backend is executing
575 * the IO synchronously.
576 */
578 {
579 pgaio_method_ops->wait_one(ioh, ref_generation);
580 continue;
581 }
582 /* fallthrough */
583
584 /* waiting for owner to submit */
585 case PGAIO_HS_DEFINED:
586 case PGAIO_HS_STAGED:
587 /* waiting for reaper to complete */
588 /* fallthrough */
590 /* shouldn't be able to hit this otherwise */
592 /* ensure we're going to get woken up */
594
595 while (!pgaio_io_was_recycled(ioh, ref_generation, &state))
596 {
599 break;
600 ConditionVariableSleep(&ioh->cv, WAIT_EVENT_AIO_IO_COMPLETION);
601 }
602
604 break;
605
608 /* see above */
609 if (am_owner)
610 pgaio_io_reclaim(ioh);
611 return;
612 }
613 }
614}
bool pgaio_io_was_recycled(PgAioHandle *ioh, uint64 ref_generation, PgAioHandleState *state)
Definition: aio.c:523
PgAioHandleState
Definition: aio_internal.h:39
bool ConditionVariableCancelSleep(void)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define PANIC
Definition: elog.h:42
bool IsUnderPostmaster
Definition: globals.c:119
void(* wait_one)(PgAioHandle *ioh, uint64 ref_generation)
Definition: aio_internal.h:294
Definition: regguts.h:323

References Assert(), ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), PgAioHandle::cv, elog, ERROR, PgAioHandle::flags, IsUnderPostmaster, MyProcNumber, PgAioHandle::owner_procno, PANIC, PGAIO_HF_SYNCHRONOUS, PGAIO_HS_COMPLETED_IO, PGAIO_HS_COMPLETED_LOCAL, PGAIO_HS_COMPLETED_SHARED, PGAIO_HS_DEFINED, PGAIO_HS_HANDED_OUT, PGAIO_HS_IDLE, PGAIO_HS_STAGED, PGAIO_HS_SUBMITTED, pgaio_io_reclaim(), pgaio_io_was_recycled(), pgaio_method_ops, and IoMethodOps::wait_one.

Referenced by pgaio_io_wait_for_free(), pgaio_shutdown(), and pgaio_wref_wait().

◆ pgaio_io_wait_for_free()

static void pgaio_io_wait_for_free ( void  )
static

Definition at line 693 of file aio.c.

694{
695 int reclaimed = 0;
696
697 pgaio_debug(DEBUG2, "waiting for self with %d pending",
699
700 /*
701 * First check if any of our IOs actually have completed - when using
702 * worker, that'll often be the case. We could do so as part of the loop
703 * below, but that'd potentially lead us to wait for some IO submitted
704 * before.
705 */
706 for (int i = 0; i < io_max_concurrency; i++)
707 {
709
711 {
712 pgaio_io_reclaim(ioh);
713 reclaimed++;
714 }
715 }
716
717 if (reclaimed > 0)
718 return;
719
720 /*
721 * If we have any unsubmitted IOs, submit them now. We'll start waiting in
722 * a second, so it's better they're in flight. This also addresses the
723 * edge-case that all IOs are unsubmitted.
724 */
727
729 elog(ERROR, "no free IOs despite no in-flight IOs");
730
731 /*
732 * Wait for the oldest in-flight IO to complete.
733 *
734 * XXX: Reusing the general IO wait is suboptimal, we don't need to wait
735 * for that specific IO to complete, we just need *any* IO to complete.
736 */
737 {
740
741 switch (ioh->state)
742 {
743 /* should not be in in-flight list */
744 case PGAIO_HS_IDLE:
745 case PGAIO_HS_DEFINED:
747 case PGAIO_HS_STAGED:
749 elog(ERROR, "shouldn't get here with io:%d in state %d",
750 pgaio_io_get_id(ioh), ioh->state);
751 break;
752
756 "waiting for free io with %d in flight",
758
759 /*
760 * In a more general case this would be racy, because the
761 * generation could increase after we read ioh->state above.
762 * But we are only looking at IOs by the current backend and
763 * the IO can only be recycled by this backend.
764 */
765 pgaio_io_wait(ioh, ioh->generation);
766 break;
767
769 /* it's possible that another backend just finished this IO */
770 pgaio_io_reclaim(ioh);
771 break;
772 }
773
775 elog(PANIC, "no idle IO after waiting for IO to terminate");
776 return;
777 }
778}
int pgaio_io_get_id(PgAioHandle *ioh)
Definition: aio.c:322
int io_max_concurrency
Definition: aio.c:73
static void pgaio_io_wait(PgAioHandle *ioh, uint64 ref_generation)
Definition: aio.c:536
#define pgaio_debug(elevel, msg,...)
Definition: aio_internal.h:345
#define DEBUG2
Definition: elog.h:29
#define dclist_head_element(type, membername, lhead)
Definition: ilist.h:955
static uint32 dclist_count(const dclist_head *head)
Definition: ilist.h:932
int i
Definition: isn.c:74
uint32 io_handle_off
Definition: aio_internal.h:183

References dclist_count(), dclist_head_element, DEBUG2, elog, ERROR, PgAioHandle::generation, i, PgAioBackend::idle_ios, PgAioBackend::in_flight_ios, PgAioBackend::io_handle_off, PgAioCtl::io_handles, io_max_concurrency, PgAioBackend::num_staged_ios, PANIC, pgaio_ctl, pgaio_debug, pgaio_debug_io, PGAIO_HS_COMPLETED_IO, PGAIO_HS_COMPLETED_LOCAL, PGAIO_HS_COMPLETED_SHARED, PGAIO_HS_DEFINED, PGAIO_HS_HANDED_OUT, PGAIO_HS_IDLE, PGAIO_HS_STAGED, PGAIO_HS_SUBMITTED, pgaio_io_get_id(), pgaio_io_reclaim(), pgaio_io_wait(), pgaio_my_backend, pgaio_submit_staged(), and PgAioHandle::state.

Referenced by pgaio_io_acquire().

◆ pgaio_io_was_recycled()

bool pgaio_io_was_recycled ( PgAioHandle ioh,
uint64  ref_generation,
PgAioHandleState state 
)

Definition at line 523 of file aio.c.

524{
525 *state = ioh->state;
527
528 return ioh->generation != ref_generation;
529}
#define pg_read_barrier()
Definition: atomics.h:156

References PgAioHandle::generation, pg_read_barrier, and PgAioHandle::state.

Referenced by pgaio_io_wait(), and pgaio_wref_check_done().

◆ pgaio_result_status_string()

const char * pgaio_result_status_string ( PgAioResultStatus  rs)

Definition at line 828 of file aio.c.

829{
830 switch (rs)
831 {
832 case PGAIO_RS_UNKNOWN:
833 return "UNKNOWN";
834 case PGAIO_RS_OK:
835 return "OK";
836 case PGAIO_RS_PARTIAL:
837 return "PARTIAL";
838 case PGAIO_RS_ERROR:
839 return "ERROR";
840 }
841
842 return NULL; /* silence compiler */
843}
@ PGAIO_RS_OK
Definition: aio_types.h:77
@ PGAIO_RS_PARTIAL
Definition: aio_types.h:78
@ PGAIO_RS_ERROR
Definition: aio_types.h:79

References PGAIO_RS_ERROR, PGAIO_RS_OK, PGAIO_RS_PARTIAL, and PGAIO_RS_UNKNOWN.

Referenced by pgaio_io_call_complete_local(), pgaio_io_call_complete_shared(), and pgaio_io_reclaim().

◆ pgaio_shutdown()

void pgaio_shutdown ( int  code,
Datum  arg 
)

Definition at line 1127 of file aio.c.

1128{
1131
1132 /* first clean up resources as we would at a transaction boundary */
1133 AtEOXact_Aio(code == 0);
1134
1135 /*
1136 * Before exiting, make sure that all IOs are finished. That has two main
1137 * purposes:
1138 *
1139 * - Some kernel-level AIO mechanisms don't deal well with the issuer of
1140 * an AIO exiting before IO completed
1141 *
1142 * - It'd be confusing to see partially finished IOs in stats views etc
1143 */
1145 {
1147
1148 /* see comment in pgaio_io_wait_for_free() about raciness */
1149 pgaio_io_wait(ioh, ioh->generation);
1150 }
1151
1152 pgaio_my_backend = NULL;
1153}
void AtEOXact_Aio(bool is_commit)
Definition: aio.c:1080

References Assert(), AtEOXact_Aio(), dclist_head_element, dclist_is_empty(), PgAioHandle::generation, PgAioBackend::handed_out_io, PgAioBackend::in_flight_ios, pgaio_io_wait(), and pgaio_my_backend.

Referenced by pgaio_init_backend().

◆ pgaio_submit_staged()

void pgaio_submit_staged ( void  )

Definition at line 1010 of file aio.c.

1011{
1012 int total_submitted = 0;
1013 int did_submit;
1014
1016 return;
1017
1018
1020
1023
1025
1026 total_submitted += did_submit;
1027
1028 Assert(total_submitted == did_submit);
1029
1031
1033 "aio: submitted %d IOs",
1034 total_submitted);
1035}
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
int(* submit)(uint16 num_staged_ios, PgAioHandle **staged_ios)
Definition: aio_internal.h:285

References Assert(), DEBUG4, END_CRIT_SECTION, PgAioBackend::num_staged_ios, pgaio_debug, pgaio_method_ops, pgaio_my_backend, PgAioBackend::staged_ios, START_CRIT_SECTION, and IoMethodOps::submit.

Referenced by pgaio_closing_fd(), pgaio_error_cleanup(), pgaio_exit_batchmode(), pgaio_io_acquire_nb(), pgaio_io_release_resowner(), pgaio_io_stage(), and pgaio_io_wait_for_free().

◆ pgaio_wref_check_done()

bool pgaio_wref_check_done ( PgAioWaitRef iow)

Definition at line 897 of file aio.c.

898{
899 uint64 ref_generation;
901 bool am_owner;
902 PgAioHandle *ioh;
903
904 ioh = pgaio_io_from_wref(iow, &ref_generation);
905
906 if (pgaio_io_was_recycled(ioh, ref_generation, &state))
907 return true;
908
909 if (state == PGAIO_HS_IDLE)
910 return true;
911
912 am_owner = ioh->owner_procno == MyProcNumber;
913
916 {
917 if (am_owner)
918 pgaio_io_reclaim(ioh);
919 return true;
920 }
921
922 /*
923 * XXX: It likely would be worth checking in with the io method, to give
924 * the IO method a chance to check if there are completion events queued.
925 */
926
927 return false;
928}
static PgAioHandle * pgaio_io_from_wref(PgAioWaitRef *iow, uint64 *ref_generation)
Definition: aio.c:785

References MyProcNumber, PgAioHandle::owner_procno, PGAIO_HS_COMPLETED_LOCAL, PGAIO_HS_COMPLETED_SHARED, PGAIO_HS_IDLE, pgaio_io_from_wref(), pgaio_io_reclaim(), and pgaio_io_was_recycled().

◆ pgaio_wref_clear()

void pgaio_wref_clear ( PgAioWaitRef iow)

Definition at line 856 of file aio.c.

857{
859}
#define PG_UINT32_MAX
Definition: c.h:561

References PgAioWaitRef::aio_index, and PG_UINT32_MAX.

◆ pgaio_wref_get_id()

int pgaio_wref_get_id ( PgAioWaitRef iow)

Definition at line 872 of file aio.c.

873{
875 return iow->aio_index;
876}
bool pgaio_wref_valid(PgAioWaitRef *iow)
Definition: aio.c:863

References PgAioWaitRef::aio_index, Assert(), and pgaio_wref_valid().

◆ pgaio_wref_valid()

bool pgaio_wref_valid ( PgAioWaitRef iow)

Definition at line 863 of file aio.c.

864{
865 return iow->aio_index != PG_UINT32_MAX;
866}

References PgAioWaitRef::aio_index, and PG_UINT32_MAX.

Referenced by pgaio_wref_get_id().

◆ pgaio_wref_wait()

void pgaio_wref_wait ( PgAioWaitRef iow)

Definition at line 883 of file aio.c.

884{
885 uint64 ref_generation;
886 PgAioHandle *ioh;
887
888 ioh = pgaio_io_from_wref(iow, &ref_generation);
889
890 pgaio_io_wait(ioh, ref_generation);
891}

References pgaio_io_from_wref(), and pgaio_io_wait().

Variable Documentation

◆ io_max_concurrency

int io_max_concurrency = -1

◆ io_method

int io_method = DEFAULT_IO_METHOD

Definition at line 72 of file aio.c.

Referenced by pgaio_workers_enabled().

◆ io_method_options

const struct config_enum_entry io_method_options[]
Initial value:
= {
{"sync", IOMETHOD_SYNC, false},
{"worker", IOMETHOD_WORKER, false},
{NULL, 0, false}
}
@ IOMETHOD_WORKER
Definition: aio.h:30
@ IOMETHOD_SYNC
Definition: aio.h:29

Definition at line 65 of file aio.c.

Referenced by assign_io_method().

◆ pgaio_ctl

◆ pgaio_method_ops

◆ pgaio_method_ops_table

const IoMethodOps* const pgaio_method_ops_table[]
static
Initial value:
= {
}
const IoMethodOps pgaio_sync_ops
Definition: method_sync.c:28
const IoMethodOps pgaio_worker_ops
Definition: method_worker.c:82

Definition at line 82 of file aio.c.

Referenced by assign_io_method().

◆ pgaio_my_backend