PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
io_worker.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

pg_noreturn void IoWorkerMain (const void *startup_data, size_t startup_data_len)
 

Variables

PGDLLIMPORT int io_workers
 

Function Documentation

◆ IoWorkerMain()

pg_noreturn void IoWorkerMain ( const void *  startup_data,
size_t  startup_data_len 
)

Definition at line 383 of file method_worker.c.

384{
385 sigjmp_buf local_sigjmp_buf;
386 PgAioHandle *volatile error_ioh = NULL;
387 ErrorContextCallback errcallback = {0};
388 volatile int error_errno = 0;
389 char cmd[128];
390
393
395 pqsignal(SIGINT, die); /* to allow manually triggering worker restart */
396
397 /*
398 * Ignore SIGTERM, will get explicit shutdown via SIGUSR2 later in the
399 * shutdown sequence, similar to checkpointer.
400 */
401 pqsignal(SIGTERM, SIG_IGN);
402 /* SIGQUIT handler was already set up by InitPostmasterChild */
403 pqsignal(SIGALRM, SIG_IGN);
404 pqsignal(SIGPIPE, SIG_IGN);
407
408 /* also registers a shutdown callback to unregister */
410
411 sprintf(cmd, "%d", MyIoWorkerId);
412 set_ps_display(cmd);
413
415 errcallback.previous = error_context_stack;
416 error_context_stack = &errcallback;
417
418 /* see PostgresMain() */
419 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
420 {
421 error_context_stack = NULL;
423
425
426 /*
427 * In the - very unlikely - case that the IO failed in a way that
428 * raises an error we need to mark the IO as failed.
429 *
430 * Need to do just enough error recovery so that we can mark the IO as
431 * failed and then exit (postmaster will start a new worker).
432 */
434
435 if (error_ioh != NULL)
436 {
437 /* should never fail without setting error_errno */
438 Assert(error_errno != 0);
439
440 errno = error_errno;
441
443 pgaio_io_process_completion(error_ioh, -error_errno);
445 }
446
447 proc_exit(1);
448 }
449
450 /* We can now handle ereport(ERROR) */
451 PG_exception_stack = &local_sigjmp_buf;
452
453 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
454
456 {
457 uint32 io_index;
459 int nlatches = 0;
460 int nwakeups = 0;
461 int worker;
462
463 /* Try to get a job to do. */
464 LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
465 if ((io_index = pgaio_worker_submission_queue_consume()) == UINT32_MAX)
466 {
467 /*
468 * Nothing to do. Mark self idle.
469 *
470 * XXX: Invent some kind of back pressure to reduce useless
471 * wakeups?
472 */
474 }
475 else
476 {
477 /* Got one. Clear idle flag. */
478 io_worker_control->idle_worker_mask &= ~(UINT64_C(1) << MyIoWorkerId);
479
480 /* See if we can wake up some peers. */
483 for (int i = 0; i < nwakeups; ++i)
484 {
485 if ((worker = pgaio_choose_idle_worker()) < 0)
486 break;
487 latches[nlatches++] = io_worker_control->workers[worker].latch;
488 }
489 }
490 LWLockRelease(AioWorkerSubmissionQueueLock);
491
492 for (int i = 0; i < nlatches; ++i)
493 SetLatch(latches[i]);
494
495 if (io_index != UINT32_MAX)
496 {
497 PgAioHandle *ioh = NULL;
498
499 ioh = &pgaio_ctl->io_handles[io_index];
500 error_ioh = ioh;
501 errcallback.arg = ioh;
502
504 "worker %d processing IO",
506
507 /*
508 * Prevent interrupts between pgaio_io_reopen() and
509 * pgaio_io_perform_synchronously() that otherwise could lead to
510 * the FD getting closed in that window.
511 */
513
514 /*
515 * It's very unlikely, but possible, that reopen fails. E.g. due
516 * to memory allocations failing or file permissions changing or
517 * such. In that case we need to fail the IO.
518 *
519 * There's not really a good errno we can report here.
520 */
521 error_errno = ENOENT;
522 pgaio_io_reopen(ioh);
523
524 /*
525 * To be able to exercise the reopen-fails path, allow injection
526 * points to trigger a failure at this point.
527 */
528 pgaio_io_call_inj(ioh, "AIO_WORKER_AFTER_REOPEN");
529
530 error_errno = 0;
531 error_ioh = NULL;
532
533 /*
534 * As part of IO completion the buffer will be marked as NOACCESS,
535 * until the buffer is pinned again - which never happens in io
536 * workers. Therefore the next time there is IO for the same
537 * buffer, the memory will be considered inaccessible. To avoid
538 * that, explicitly allow access to the memory before reading data
539 * into it.
540 */
541#ifdef USE_VALGRIND
542 {
543 struct iovec *iov;
544 uint16 iov_length = pgaio_io_get_iovec_length(ioh, &iov);
545
546 for (int i = 0; i < iov_length; i++)
547 VALGRIND_MAKE_MEM_UNDEFINED(iov[i].iov_base, iov[i].iov_len);
548 }
549#endif
550
551 /*
552 * We don't expect this to ever fail with ERROR or FATAL, no need
553 * to keep error_ioh set to the IO.
554 * pgaio_io_perform_synchronously() contains a critical section to
555 * ensure we don't accidentally fail.
556 */
558
560 errcallback.arg = NULL;
561 }
562 else
563 {
565 WAIT_EVENT_IO_WORKER_MAIN);
567 }
568
570 }
571
572 error_context_stack = errcallback.previous;
573 proc_exit(0);
574}
void pgaio_io_process_completion(PgAioHandle *ioh, int result)
Definition: aio.c:500
PgAioCtl * pgaio_ctl
Definition: aio.c:81
#define pgaio_io_call_inj(ioh, injection_point)
Definition: aio_internal.h:407
#define pgaio_debug_io(elevel, ioh, msg,...)
Definition: aio_internal.h:389
void pgaio_io_perform_synchronously(PgAioHandle *ioh)
Definition: aio_io.c:116
int pgaio_io_get_iovec_length(PgAioHandle *ioh, struct iovec **iov)
Definition: aio_io.c:219
void pgaio_io_reopen(PgAioHandle *ioh)
Definition: aio_target.c:110
void AuxiliaryProcessMainCommon(void)
Definition: auxprocess.c:39
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define Min(x, y)
Definition: c.h:975
uint16_t uint16
Definition: c.h:501
uint32_t uint32
Definition: c.h:502
void EmitErrorReport(void)
Definition: elog.c:1709
ErrorContextCallback * error_context_stack
Definition: elog.c:95
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
#define DEBUG4
Definition: elog.h:27
struct Latch * MyLatch
Definition: globals.c:64
Assert(PointerIsAligned(start, uint64))
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:109
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:65
void proc_exit(int code)
Definition: ipc.c:104
int i
Definition: isn.c:77
void SetLatch(Latch *latch)
Definition: latch.c:288
void ResetLatch(Latch *latch)
Definition: latch.c:372
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:172
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
void LWLockReleaseAll(void)
Definition: lwlock.c:1953
@ LW_EXCLUSIVE
Definition: lwlock.h:114
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition: memdebug.h:28
static uint32 pgaio_worker_submission_queue_depth(void)
static void pgaio_worker_error_callback(void *arg)
static int pgaio_choose_idle_worker(void)
#define IO_WORKER_WAKEUP_FANOUT
Definition: method_worker.c:51
static void pgaio_worker_register(void)
static int MyIoWorkerId
Definition: method_worker.c:97
static uint32 pgaio_worker_submission_queue_consume(void)
static AioWorkerControl * io_worker_control
Definition: method_worker.c:99
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:136
#define START_CRIT_SECTION()
Definition: miscadmin.h:150
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:134
@ B_IO_WORKER
Definition: miscadmin.h:364
#define END_CRIT_SECTION()
Definition: miscadmin.h:152
BackendType MyBackendType
Definition: miscinit.c:64
#define die(msg)
#define pqsignal
Definition: port.h:531
#define sprintf
Definition: port.h:241
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:673
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
uint64 idle_worker_mask
Definition: method_worker.c:71
AioWorkerSlot workers[FLEXIBLE_ARRAY_MEMBER]
Definition: method_worker.c:72
struct ErrorContextCallback * previous
Definition: elog.h:297
void(* callback)(void *arg)
Definition: elog.h:298
Definition: latch.h:114
PgAioHandle * io_handles
Definition: aio_internal.h:246
#define WL_EXIT_ON_PM_DEATH
Definition: waiteventset.h:39
#define WL_LATCH_SET
Definition: waiteventset.h:34
#define SIGHUP
Definition: win32_port.h:158
#define SIGPIPE
Definition: win32_port.h:163
#define SIGUSR1
Definition: win32_port.h:170
#define SIGALRM
Definition: win32_port.h:164
#define SIGUSR2
Definition: win32_port.h:171

References ErrorContextCallback::arg, Assert(), AuxiliaryProcessMainCommon(), B_IO_WORKER, ErrorContextCallback::callback, CHECK_FOR_INTERRUPTS, DEBUG4, die, EmitErrorReport(), END_CRIT_SECTION, error_context_stack, HOLD_INTERRUPTS, i, AioWorkerControl::idle_worker_mask, PgAioCtl::io_handles, io_worker_control, IO_WORKER_WAKEUP_FANOUT, AioWorkerSlot::latch, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), Min, MyBackendType, MyIoWorkerId, MyLatch, PG_exception_stack, pgaio_choose_idle_worker(), pgaio_ctl, pgaio_debug_io, pgaio_io_call_inj, pgaio_io_get_iovec_length(), pgaio_io_perform_synchronously(), pgaio_io_process_completion(), pgaio_io_reopen(), pgaio_worker_error_callback(), pgaio_worker_register(), pgaio_worker_submission_queue_consume(), pgaio_worker_submission_queue_depth(), pqsignal, ErrorContextCallback::previous, proc_exit(), procsignal_sigusr1_handler(), ResetLatch(), RESUME_INTERRUPTS, set_ps_display(), SetLatch(), ShutdownRequestPending, SIGALRM, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, sprintf, START_CRIT_SECTION, UnBlockSig, VALGRIND_MAKE_MEM_UNDEFINED, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and AioWorkerControl::workers.

Variable Documentation

◆ io_workers

PGDLLIMPORT int io_workers
extern

Definition at line 93 of file method_worker.c.

Referenced by maybe_adjust_io_workers().