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 385 of file method_worker.c.

386{
388 PgAioHandle *volatile error_ioh = NULL;
389 ErrorContextCallback errcallback = {0};
390 volatile int error_errno = 0;
391 char cmd[128];
392
394
396 pqsignal(SIGINT, die); /* to allow manually triggering worker restart */
397
398 /*
399 * Ignore SIGTERM, will get explicit shutdown via SIGUSR2 later in the
400 * shutdown sequence, similar to checkpointer.
401 */
403 /* SIGQUIT handler was already set up by InitPostmasterChild */
408
409 /* also registers a shutdown callback to unregister */
411
412 sprintf(cmd, "%d", MyIoWorkerId);
413 set_ps_display(cmd);
414
416 errcallback.previous = error_context_stack;
417 error_context_stack = &errcallback;
418
419 /* see PostgresMain() */
420 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
421 {
424
426
427 /*
428 * In the - very unlikely - case that the IO failed in a way that
429 * raises an error we need to mark the IO as failed.
430 *
431 * Need to do just enough error recovery so that we can mark the IO as
432 * failed and then exit (postmaster will start a new worker).
433 */
435
436 if (error_ioh != NULL)
437 {
438 /* should never fail without setting error_errno */
439 Assert(error_errno != 0);
440
442
446 }
447
448 proc_exit(1);
449 }
450
451 /* We can now handle ereport(ERROR) */
453
455
457 {
460 int nlatches = 0;
461 int nwakeups = 0;
462 int worker;
463
464 /*
465 * Try to get a job to do.
466 *
467 * The lwlock acquisition also provides the necessary memory barrier
468 * to ensure that we don't see an outdated data in the handle.
469 */
472 {
473 /*
474 * Nothing to do. Mark self idle.
475 *
476 * XXX: Invent some kind of back pressure to reduce useless
477 * wakeups?
478 */
480 }
481 else
482 {
483 /* Got one. Clear idle flag. */
485
486 /* See if we can wake up some peers. */
489 for (int i = 0; i < nwakeups; ++i)
490 {
491 if ((worker = pgaio_worker_choose_idle()) < 0)
492 break;
494 }
495 }
497
498 for (int i = 0; i < nlatches; ++i)
500
501 if (io_index != -1)
502 {
504
506 error_ioh = ioh;
507 errcallback.arg = ioh;
508
510 "worker %d processing IO",
512
513 /*
514 * Prevent interrupts between pgaio_io_reopen() and
515 * pgaio_io_perform_synchronously() that otherwise could lead to
516 * the FD getting closed in that window.
517 */
519
520 /*
521 * It's very unlikely, but possible, that reopen fails. E.g. due
522 * to memory allocations failing or file permissions changing or
523 * such. In that case we need to fail the IO.
524 *
525 * There's not really a good errno we can report here.
526 */
529
530 /*
531 * To be able to exercise the reopen-fails path, allow injection
532 * points to trigger a failure at this point.
533 */
534 INJECTION_POINT("aio-worker-after-reopen", ioh);
535
536 error_errno = 0;
537 error_ioh = NULL;
538
539 /*
540 * As part of IO completion the buffer will be marked as NOACCESS,
541 * until the buffer is pinned again - which never happens in io
542 * workers. Therefore the next time there is IO for the same
543 * buffer, the memory will be considered inaccessible. To avoid
544 * that, explicitly allow access to the memory before reading data
545 * into it.
546 */
547#ifdef USE_VALGRIND
548 {
549 struct iovec *iov;
550 uint16 iov_length = pgaio_io_get_iovec_length(ioh, &iov);
551
552 for (int i = 0; i < iov_length; i++)
554 }
555#endif
556
557 /*
558 * We don't expect this to ever fail with ERROR or FATAL, no need
559 * to keep error_ioh set to the IO.
560 * pgaio_io_perform_synchronously() contains a critical section to
561 * ensure we don't accidentally fail.
562 */
564
566 errcallback.arg = NULL;
567 }
568 else
569 {
573 }
574
576
578 {
579 ConfigReloadPending = false;
581 }
582 }
583
584 error_context_stack = errcallback.previous;
585 proc_exit(0);
586}
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:39
sigset_t UnBlockSig
Definition pqsignal.c:22
#define Min(x, y)
Definition c.h:997
#define Assert(condition)
Definition c.h:873
uint16_t uint16
Definition c.h:545
uint32_t uint32
Definition c.h:546
void EmitErrorReport(void)
Definition elog.c:1704
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: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:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
void LWLockReleaseAll(void)
Definition lwlock.c:1892
@ 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:677
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:114
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().