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)
 
bool pgaio_worker_pm_test_grow_signal_sent (void)
 
void pgaio_worker_pm_clear_grow_signal_sent (void)
 
bool pgaio_worker_pm_test_grow (void)
 

Variables

PGDLLIMPORT int io_min_workers
 
PGDLLIMPORT int io_max_workers
 
PGDLLIMPORT int io_worker_idle_timeout
 
PGDLLIMPORT int io_worker_launch_interval
 

Function Documentation

◆ IoWorkerMain()

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

Definition at line 687 of file method_worker.c.

688{
691 int timeout_guc_used = 0;
692 PgAioHandle *volatile error_ioh = NULL;
693 ErrorContextCallback errcallback = {0};
694 volatile int error_errno = 0;
695 char cmd[128];
696 int hist_ios = 0;
697 int hist_wakeups = 0;
698
700
702 pqsignal(SIGINT, die); /* to allow manually triggering worker restart */
703
704 /*
705 * Ignore SIGTERM, will get explicit shutdown via SIGUSR2 later in the
706 * shutdown sequence, similar to checkpointer.
707 */
709 /* SIGQUIT handler was already set up by InitPostmasterChild */
714
715 /* also registers a shutdown callback to unregister */
717
719
720 sprintf(cmd, "%d", MyIoWorkerId);
721 set_ps_display(cmd);
722
724 errcallback.previous = error_context_stack;
725 error_context_stack = &errcallback;
726
727 /* see PostgresMain() */
728 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
729 {
732
734
735 /*
736 * In the - very unlikely - case that the IO failed in a way that
737 * raises an error we need to mark the IO as failed.
738 *
739 * Need to do just enough error recovery so that we can mark the IO as
740 * failed and then exit (postmaster will start a new worker).
741 */
743
744 if (error_ioh != NULL)
745 {
746 /* should never fail without setting error_errno */
747 Assert(error_errno != 0);
748
750
754 }
755
756 proc_exit(1);
757 }
758
759 /* We can now handle ereport(ERROR) */
761
763
765 {
767 int worker = -1;
768 int queue_depth = 0;
769 bool maybe_grow = false;
770
771 /*
772 * Try to get a job to do.
773 *
774 * The lwlock acquisition also provides the necessary memory barrier
775 * to ensure that we don't see an outdated data in the handle.
776 */
779 {
780 /* Nothing to do. Mark self idle. */
783 }
784 else
785 {
786 /* Got one. Clear idle flag. */
789
790 /*
791 * See if we should wake up a higher numbered peer. Only do that
792 * if this worker is not receiving spurious wakeups itself. The
793 * intention is create a frontier beyond which idle workers stay
794 * asleep.
795 *
796 * This heuristic tries to discover the useful wakeup propagation
797 * chain length when IOs are very fast and workers wake up to find
798 * that all IOs have already been taken.
799 *
800 * If we chose not to wake a worker when we ideally should have,
801 * then the ratio will soon change to correct that.
802 */
803 if (hist_wakeups <= hist_ios)
804 {
806 if (queue_depth > 0)
807 {
808 /* Choose a worker higher than me to wake. */
810 if (worker == -1)
811 maybe_grow = true;
812 }
813 }
814 }
816
817 /* Propagate wakeups. */
818 if (worker != -1)
819 {
820 pgaio_worker_wake(worker);
821 }
822 else if (maybe_grow)
823 {
824 /*
825 * We know there was at least one more item in the queue, and we
826 * failed to find a higher-numbered idle worker to wake. Now we
827 * decide if we should try to start one more worker.
828 *
829 * We do this with a simple heuristic: is the queue depth greater
830 * than the current number of workers?
831 *
832 * Consider the following situations:
833 *
834 * 1. The queue depth is constantly increasing, because IOs are
835 * arriving faster than they can possibly be serviced. It doesn't
836 * matter much which threshold we choose, as we will surely hit
837 * it. Crossing the current worker count is a useful signal
838 * because it's clearly too deep to avoid queuing latency already,
839 * but still leaves a small window of opportunity to improve the
840 * situation before the queue overflows.
841 *
842 * 2. The worker pool is keeping up, no latency is being
843 * introduced and an extra worker would be a waste of resources.
844 * Queue depth distributions tend to be heavily skewed, with long
845 * tails of low probability spikes (due to submission clustering,
846 * scheduling, jitter, stalls, noisy neighbors, etc). We want a
847 * number that is very unlikely to be triggered by an outlier, and
848 * we bet that an exponential or similar distribution whose
849 * outliers never reach this threshold must be almost entirely
850 * concentrated at the low end. If we do see a spike as big as
851 * the worker count, we take it as a signal that the distribution
852 * is surely too wide.
853 *
854 * On its own, this is an extremely crude signal. When combined
855 * with the wakeup propagation test that precedes it (but on its
856 * own tends to overshoot) and io_worker_launch_interval, the
857 * result is that we gradually test each pool size until we find
858 * one that doesn't trigger further expansion, and then hold it
859 * for at least io_worker_idle_timeout.
860 *
861 * XXX Perhaps ideas from queueing theory or control theory could
862 * do a better job of this.
863 */
864
865 /* Read nworkers without lock for this heuristic purpose. */
868 }
869
870 if (io_index != -1)
871 {
873
874 /* Cancel timeout and update wakeup:work ratio. */
877 {
878 hist_wakeups /= 2;
879 hist_ios /= 2;
880 }
881
883 error_ioh = ioh;
884 errcallback.arg = ioh;
885
887 "worker %d processing IO",
889
890 /*
891 * Prevent interrupts between pgaio_io_reopen() and
892 * pgaio_io_perform_synchronously() that otherwise could lead to
893 * the FD getting closed in that window.
894 */
896
897 /*
898 * It's very unlikely, but possible, that reopen fails. E.g. due
899 * to memory allocations failing or file permissions changing or
900 * such. In that case we need to fail the IO.
901 *
902 * There's not really a good errno we can report here.
903 */
906
907 /*
908 * To be able to exercise the reopen-fails path, allow injection
909 * points to trigger a failure at this point.
910 */
911 INJECTION_POINT("aio-worker-after-reopen", ioh);
912
913 error_errno = 0;
914 error_ioh = NULL;
915
916 /*
917 * As part of IO completion the buffer will be marked as NOACCESS,
918 * until the buffer is pinned again - which never happens in io
919 * workers. Therefore the next time there is IO for the same
920 * buffer, the memory will be considered inaccessible. To avoid
921 * that, explicitly allow access to the memory before reading data
922 * into it.
923 */
924#ifdef USE_VALGRIND
925 {
926 struct iovec *iov;
927 uint16 iov_length = pgaio_io_get_iovec_length(ioh, &iov);
928
929 for (int i = 0; i < iov_length; i++)
931 }
932#endif
933
934#ifdef PGAIO_WORKER_SHOW_PS_INFO
935 {
937
938 sprintf(cmd, "%d: [%s] %s",
943 set_ps_display(cmd);
944 }
945#endif
946
947 /*
948 * We don't expect this to ever fail with ERROR or FATAL, no need
949 * to keep error_ioh set to the IO.
950 * pgaio_io_perform_synchronously() contains a critical section to
951 * ensure we don't accidentally fail.
952 */
954
956 errcallback.arg = NULL;
957 }
958 else
959 {
960 int timeout_ms;
961
962 /* Cancel new worker request if pending. */
964
965 /* Compute the remaining allowed idle time. */
966 if (io_worker_idle_timeout == -1)
967 {
968 /* Never time out. */
969 timeout_ms = -1;
970 }
971 else
972 {
974
975 /* If the GUC changes, reset timer. */
976 if (idle_timeout_abs != 0 &&
979
980 /* Only the highest-numbered worker can time out. */
982 {
983 if (idle_timeout_abs == 0)
984 {
985 /*
986 * I have just been promoted to the timeout worker, or
987 * the GUC changed. Compute new absolute time from
988 * now.
989 */
994 }
995 timeout_ms =
997 }
998 else
999 {
1000 /* No timeout for me. */
1001 idle_timeout_abs = 0;
1002 timeout_ms = -1;
1003 }
1004 }
1005
1006#ifdef PGAIO_WORKER_SHOW_PS_INFO
1007 sprintf(cmd, "%d: idle, wakeups:ios = %d:%d",
1009 set_ps_display(cmd);
1010#endif
1011
1013 timeout_ms,
1015 {
1016 /* WL_TIMEOUT */
1019 break;
1020 }
1021 else
1022 {
1023 /* WL_LATCH_SET */
1025 {
1026 hist_wakeups /= 2;
1027 hist_ios /= 2;
1028 }
1029 }
1031 }
1032
1034
1036 {
1039
1040 ConfigReloadPending = false;
1042
1043 /*
1044 * Emit a WARNING if io_min_workers > io_max_workers. If no bound
1045 * has changed, skip this to avoid too many log messages.
1046 */
1050
1051 /* If io_max_workers has been decreased, exit highest first. */
1053 break;
1054 }
1055 }
1056
1057 error_context_stack = errcallback.previous;
1058 proc_exit(0);
1059}
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
const char * pgaio_io_get_op_name(PgAioHandle *ioh)
Definition aio_io.c:180
int pgaio_io_get_iovec_length(PgAioHandle *ioh, struct iovec **iov)
Definition aio_io.c:224
void pgaio_io_reopen(PgAioHandle *ioh)
Definition aio_target.c:116
char * pgaio_io_get_target_description(PgAioHandle *ioh)
Definition aio_target.c:84
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:41
sigset_t UnBlockSig
Definition pqsignal.c:22
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.c:1765
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1649
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1613
#define Assert(condition)
Definition c.h:1002
uint16_t uint16
Definition c.h:682
uint32_t uint32
Definition c.h:683
int64 TimestampTz
Definition timestamp.h:39
void EmitErrorReport(void)
Definition elog.c:1883
ErrorContextCallback * error_context_stack
Definition elog.c:100
sigjmp_buf * PG_exception_stack
Definition elog.c:102
#define DEBUG4
Definition elog.h:28
struct Latch * MyLatch
Definition globals.c:65
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 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:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
void LWLockReleaseAll(void)
Definition lwlock.c:1866
@ LW_EXCLUSIVE
Definition lwlock.h:104
void pfree(void *pointer)
Definition mcxt.c:1619
#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size)
Definition memdebug.h:28
#define PGAIO_WORKER_WAKEUP_RATIO_SATURATE
static uint32 pgaio_worker_submission_queue_depth(void)
static void pgaio_worker_error_callback(void *arg)
static void pgaio_worker_request_grow(void)
static bool pgaio_worker_can_timeout(void)
static void pgaio_worker_cancel_grow(void)
static void pgaio_workerset_remove(PgAioWorkerSet *set, int worker)
static int pgaio_worker_choose_idle(int only_workers_above)
int io_min_workers
static void pgaio_worker_wake(int worker)
int io_max_workers
static void pgaio_worker_register(void)
static PgAioWorkerControl * io_worker_control
static void check_io_worker_gucs(void)
static int MyIoWorkerId
int io_worker_idle_timeout
static int pgaio_worker_submission_queue_consume(void)
static void pgaio_workerset_insert(PgAioWorkerSet *set, int worker)
#define RESUME_INTERRUPTS()
Definition miscadmin.h:138
#define START_CRIT_SECTION()
Definition miscadmin.h:152
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
#define HOLD_INTERRUPTS()
Definition miscadmin.h:136
#define END_CRIT_SECTION()
Definition miscadmin.h:154
#define die(msg)
#define pqsignal
Definition port.h:548
#define sprintf
Definition port.h:263
#define PG_SIG_IGN
Definition port.h:552
static int fb(int x)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:696
static void set_ps_display(const char *activity)
Definition ps_status.h:40
struct ErrorContextCallback * previous
Definition elog.h:299
void(* callback)(void *arg)
Definition elog.h:300
PgAioHandle * io_handles
PgAioWorkerSet idle_workerset
#define TimestampTzPlusMilliseconds(tz, ms)
Definition timestamp.h:85
const char * description
#define WL_TIMEOUT
#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, check_io_worker_gucs(), ConfigReloadPending, DEBUG4, description, die, EmitErrorReport(), END_CRIT_SECTION, error_context_stack, fb(), GetCurrentTimestamp(), HOLD_INTERRUPTS, i, PgAioWorkerControl::idle_workerset, INJECTION_POINT, PgAioCtl::io_handles, io_max_workers, io_min_workers, io_worker_control, io_worker_idle_timeout, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), MyIoWorkerId, MyLatch, now(), PgAioWorkerControl::nworkers, pfree(), PG_exception_stack, PG_SIG_IGN, pgaio_ctl, pgaio_debug_io, pgaio_io_get_iovec_length(), pgaio_io_get_op_name(), pgaio_io_get_target_description(), pgaio_io_perform_synchronously(), pgaio_io_process_completion(), pgaio_io_reopen(), pgaio_worker_can_timeout(), pgaio_worker_cancel_grow(), pgaio_worker_choose_idle(), pgaio_worker_error_callback(), pgaio_worker_register(), pgaio_worker_request_grow(), pgaio_worker_submission_queue_consume(), pgaio_worker_submission_queue_depth(), pgaio_worker_wake(), PGAIO_WORKER_WAKEUP_RATIO_SATURATE, pgaio_workerset_insert(), pgaio_workerset_remove(), PGC_SIGHUP, pqsignal, ErrorContextCallback::previous, proc_exit(), ProcessConfigFile(), procsignal_sigusr1_handler(), ResetLatch(), RESUME_INTERRUPTS, set_ps_display(), ShutdownRequestPending, SIGALRM, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, sprintf, START_CRIT_SECTION, TimestampDifferenceMilliseconds(), TimestampTzPlusMilliseconds, UnBlockSig, VALGRIND_MAKE_MEM_UNDEFINED, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

◆ pgaio_worker_pm_clear_grow_signal_sent()

void pgaio_worker_pm_clear_grow_signal_sent ( void  )
extern

Definition at line 349 of file method_worker.c.

350{
352 return;
353
354 io_worker_control->grow = false;
357}
#define pg_memory_barrier()
Definition atomics.h:141

References PgAioWorkerControl::grow, PgAioWorkerControl::grow_signal_sent, io_worker_control, and pg_memory_barrier.

Referenced by maybe_start_io_workers().

◆ pgaio_worker_pm_test_grow()

bool pgaio_worker_pm_test_grow ( void  )
extern

Definition at line 339 of file method_worker.c.

340{
343}

References PgAioWorkerControl::grow, io_worker_control, and pg_memory_barrier.

Referenced by maybe_start_io_workers().

◆ pgaio_worker_pm_test_grow_signal_sent()

bool pgaio_worker_pm_test_grow_signal_sent ( void  )
extern

Variable Documentation

◆ io_max_workers

◆ io_min_workers

◆ io_worker_idle_timeout

PGDLLIMPORT int io_worker_idle_timeout
extern

Definition at line 133 of file method_worker.c.

Referenced by IoWorkerMain().

◆ io_worker_launch_interval

PGDLLIMPORT int io_worker_launch_interval
extern

Definition at line 134 of file method_worker.c.

Referenced by maybe_start_io_workers().