PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 
)
extern

Definition at line 396 of file method_worker.c.

397{
399 PgAioHandle *volatile error_ioh = NULL;
400 ErrorContextCallback errcallback = {0};
401 volatile int error_errno = 0;
402 char cmd[128];
403
405
407 pqsignal(SIGINT, die); /* to allow manually triggering worker restart */
408
409 /*
410 * Ignore SIGTERM, will get explicit shutdown via SIGUSR2 later in the
411 * shutdown sequence, similar to checkpointer.
412 */
414 /* SIGQUIT handler was already set up by InitPostmasterChild */
419
420 /* also registers a shutdown callback to unregister */
422
423 sprintf(cmd, "%d", MyIoWorkerId);
424 set_ps_display(cmd);
425
427 errcallback.previous = error_context_stack;
428 error_context_stack = &errcallback;
429
430 /* see PostgresMain() */
431 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
432 {
435
437
438 /*
439 * In the - very unlikely - case that the IO failed in a way that
440 * raises an error we need to mark the IO as failed.
441 *
442 * Need to do just enough error recovery so that we can mark the IO as
443 * failed and then exit (postmaster will start a new worker).
444 */
446
447 if (error_ioh != NULL)
448 {
449 /* should never fail without setting error_errno */
450 Assert(error_errno != 0);
451
453
457 }
458
459 proc_exit(1);
460 }
461
462 /* We can now handle ereport(ERROR) */
464
466
468 {
471 int nlatches = 0;
472 int nwakeups = 0;
473 int worker;
474
475 /*
476 * Try to get a job to do.
477 *
478 * The lwlock acquisition also provides the necessary memory barrier
479 * to ensure that we don't see an outdated data in the handle.
480 */
483 {
484 /*
485 * Nothing to do. Mark self idle.
486 *
487 * XXX: Invent some kind of back pressure to reduce useless
488 * wakeups?
489 */
491 }
492 else
493 {
494 /* Got one. Clear idle flag. */
496
497 /* See if we can wake up some peers. */
500 for (int i = 0; i < nwakeups; ++i)
501 {
502 if ((worker = pgaio_worker_choose_idle()) < 0)
503 break;
505 }
506 }
508
509 for (int i = 0; i < nlatches; ++i)
511
512 if (io_index != -1)
513 {
515
517 error_ioh = ioh;
518 errcallback.arg = ioh;
519
521 "worker %d processing IO",
523
524 /*
525 * Prevent interrupts between pgaio_io_reopen() and
526 * pgaio_io_perform_synchronously() that otherwise could lead to
527 * the FD getting closed in that window.
528 */
530
531 /*
532 * It's very unlikely, but possible, that reopen fails. E.g. due
533 * to memory allocations failing or file permissions changing or
534 * such. In that case we need to fail the IO.
535 *
536 * There's not really a good errno we can report here.
537 */
540
541 /*
542 * To be able to exercise the reopen-fails path, allow injection
543 * points to trigger a failure at this point.
544 */
545 INJECTION_POINT("aio-worker-after-reopen", ioh);
546
547 error_errno = 0;
548 error_ioh = NULL;
549
550 /*
551 * As part of IO completion the buffer will be marked as NOACCESS,
552 * until the buffer is pinned again - which never happens in io
553 * workers. Therefore the next time there is IO for the same
554 * buffer, the memory will be considered inaccessible. To avoid
555 * that, explicitly allow access to the memory before reading data
556 * into it.
557 */
558#ifdef USE_VALGRIND
559 {
560 struct iovec *iov;
561 uint16 iov_length = pgaio_io_get_iovec_length(ioh, &iov);
562
563 for (int i = 0; i < iov_length; i++)
565 }
566#endif
567
568 /*
569 * We don't expect this to ever fail with ERROR or FATAL, no need
570 * to keep error_ioh set to the IO.
571 * pgaio_io_perform_synchronously() contains a critical section to
572 * ensure we don't accidentally fail.
573 */
575
577 errcallback.arg = NULL;
578 }
579 else
580 {
584 }
585
587
589 {
590 ConfigReloadPending = false;
592 }
593 }
594
595 error_context_stack = errcallback.previous;
596 proc_exit(0);
597}
void pgaio_io_process_completion(PgAioHandle *ioh, int result)
Definition aio.c:528
PgAioCtl * pgaio_ctl
Definition aio.c:78
#define pgaio_debug_io(elevel, ioh, msg,...)
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:116
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:40
sigset_t UnBlockSig
Definition pqsignal.c:22
#define Min(x, y)
Definition c.h:1093
#define Assert(condition)
Definition c.h:945
uint16_t uint16
Definition c.h:617
uint32_t uint32
Definition c.h:618
void EmitErrorReport(void)
Definition elog.c:1882
ErrorContextCallback * error_context_stack
Definition elog.c:99
sigjmp_buf * PG_exception_stack
Definition elog.c:101
#define DEBUG4
Definition elog.h:27
struct Latch * MyLatch
Definition globals.c:63
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
#define INJECTION_POINT(name, arg)
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition interrupt.c:104
volatile sig_atomic_t ShutdownRequestPending
Definition interrupt.c:28
volatile sig_atomic_t ConfigReloadPending
Definition interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void proc_exit(int code)
Definition ipc.c:105
int i
Definition isn.c:77
void SetLatch(Latch *latch)
Definition latch.c:290
void ResetLatch(Latch *latch)
Definition latch.c:374
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:1177
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
void LWLockReleaseAll(void)
Definition lwlock.c:1893
@ LW_EXCLUSIVE
Definition lwlock.h:112
#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)
#define IO_WORKER_WAKEUP_FANOUT
static void pgaio_worker_register(void)
static PgAioWorkerControl * io_worker_control
static int MyIoWorkerId
static int pgaio_worker_submission_queue_consume(void)
static int pgaio_worker_choose_idle(void)
#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
#define END_CRIT_SECTION()
Definition miscadmin.h:152
#define die(msg)
#define pqsignal
Definition port.h:547
#define sprintf
Definition port.h:262
static int fb(int x)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:680
static void set_ps_display(const char *activity)
Definition ps_status.h:40
struct ErrorContextCallback * previous
Definition elog.h:297
void(* callback)(void *arg)
Definition elog.h:298
Definition latch.h:116
PgAioHandle * io_handles
PgAioWorkerSlot workers[FLEXIBLE_ARRAY_MEMBER]
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#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(), ErrorContextCallback::callback, CHECK_FOR_INTERRUPTS, ConfigReloadPending, DEBUG4, die, EmitErrorReport(), END_CRIT_SECTION, error_context_stack, fb(), HOLD_INTERRUPTS, i, PgAioWorkerControl::idle_worker_mask, INJECTION_POINT, PgAioCtl::io_handles, io_worker_control, IO_WORKER_WAKEUP_FANOUT, PgAioWorkerSlot::latch, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), Min, MyIoWorkerId, MyLatch, PG_exception_stack, pgaio_ctl, pgaio_debug_io, pgaio_io_get_iovec_length(), pgaio_io_perform_synchronously(), pgaio_io_process_completion(), pgaio_io_reopen(), pgaio_worker_choose_idle(), pgaio_worker_error_callback(), pgaio_worker_register(), pgaio_worker_submission_queue_consume(), pgaio_worker_submission_queue_depth(), PGC_SIGHUP, pqsignal, ErrorContextCallback::previous, proc_exit(), ProcessConfigFile(), 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 PgAioWorkerControl::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().