PostgreSQL Source Code git master
method_worker.c File Reference
#include "postgres.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "postmaster/auxprocess.h"
#include "postmaster/interrupt.h"
#include "storage/aio.h"
#include "storage/aio_internal.h"
#include "storage/aio_subsys.h"
#include "storage/io_worker.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/injection_point.h"
#include "utils/memdebug.h"
#include "utils/ps_status.h"
#include "utils/wait_event.h"
Include dependency graph for method_worker.c:

Go to the source code of this file.

Data Structures

struct  AioWorkerSubmissionQueue
 
struct  AioWorkerSlot
 
struct  AioWorkerControl
 

Macros

#define IO_WORKER_WAKEUP_FANOUT   2
 

Typedefs

typedef struct AioWorkerSubmissionQueue AioWorkerSubmissionQueue
 
typedef struct AioWorkerSlot AioWorkerSlot
 
typedef struct AioWorkerControl AioWorkerControl
 

Functions

static size_t pgaio_worker_shmem_size (void)
 
static void pgaio_worker_shmem_init (bool first_time)
 
static bool pgaio_worker_needs_synchronous_execution (PgAioHandle *ioh)
 
static int pgaio_worker_submit (uint16 num_staged_ios, PgAioHandle **staged_ios)
 
static size_t pgaio_worker_queue_shmem_size (int *queue_size)
 
static size_t pgaio_worker_control_shmem_size (void)
 
static int pgaio_choose_idle_worker (void)
 
static bool pgaio_worker_submission_queue_insert (PgAioHandle *ioh)
 
static uint32 pgaio_worker_submission_queue_consume (void)
 
static uint32 pgaio_worker_submission_queue_depth (void)
 
static void pgaio_worker_submit_internal (int nios, PgAioHandle *ios[])
 
static void pgaio_worker_die (int code, Datum arg)
 
static void pgaio_worker_register (void)
 
static void pgaio_worker_error_callback (void *arg)
 
void IoWorkerMain (const void *startup_data, size_t startup_data_len)
 
bool pgaio_workers_enabled (void)
 

Variables

const IoMethodOps pgaio_worker_ops
 
int io_workers = 3
 
static int io_worker_queue_size = 64
 
static int MyIoWorkerId
 
static AioWorkerSubmissionQueueio_worker_submission_queue
 
static AioWorkerControlio_worker_control
 

Macro Definition Documentation

◆ IO_WORKER_WAKEUP_FANOUT

#define IO_WORKER_WAKEUP_FANOUT   2

Definition at line 52 of file method_worker.c.

Typedef Documentation

◆ AioWorkerControl

◆ AioWorkerSlot

typedef struct AioWorkerSlot AioWorkerSlot

◆ AioWorkerSubmissionQueue

Function Documentation

◆ IoWorkerMain()

void IoWorkerMain ( const void *  startup_data,
size_t  startup_data_len 
)

Definition at line 384 of file method_worker.c.

385{
386 sigjmp_buf local_sigjmp_buf;
387 PgAioHandle *volatile error_ioh = NULL;
388 ErrorContextCallback errcallback = {0};
389 volatile int error_errno = 0;
390 char cmd[128];
391
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 */
402 pqsignal(SIGTERM, SIG_IGN);
403 /* SIGQUIT handler was already set up by InitPostmasterChild */
404 pqsignal(SIGALRM, SIG_IGN);
405 pqsignal(SIGPIPE, SIG_IGN);
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 {
422 error_context_stack = NULL;
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
441 errno = error_errno;
442
444 pgaio_io_process_completion(error_ioh, -error_errno);
446 }
447
448 proc_exit(1);
449 }
450
451 /* We can now handle ereport(ERROR) */
452 PG_exception_stack = &local_sigjmp_buf;
453
454 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
455
457 {
458 uint32 io_index;
460 int nlatches = 0;
461 int nwakeups = 0;
462 int worker;
463
464 /* Try to get a job to do. */
465 LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
466 if ((io_index = pgaio_worker_submission_queue_consume()) == UINT32_MAX)
467 {
468 /*
469 * Nothing to do. Mark self idle.
470 *
471 * XXX: Invent some kind of back pressure to reduce useless
472 * wakeups?
473 */
475 }
476 else
477 {
478 /* Got one. Clear idle flag. */
479 io_worker_control->idle_worker_mask &= ~(UINT64_C(1) << MyIoWorkerId);
480
481 /* See if we can wake up some peers. */
484 for (int i = 0; i < nwakeups; ++i)
485 {
486 if ((worker = pgaio_choose_idle_worker()) < 0)
487 break;
488 latches[nlatches++] = io_worker_control->workers[worker].latch;
489 }
490 }
491 LWLockRelease(AioWorkerSubmissionQueueLock);
492
493 for (int i = 0; i < nlatches; ++i)
494 SetLatch(latches[i]);
495
496 if (io_index != UINT32_MAX)
497 {
498 PgAioHandle *ioh = NULL;
499
500 ioh = &pgaio_ctl->io_handles[io_index];
501 error_ioh = ioh;
502 errcallback.arg = ioh;
503
505 "worker %d processing IO",
507
508 /*
509 * Prevent interrupts between pgaio_io_reopen() and
510 * pgaio_io_perform_synchronously() that otherwise could lead to
511 * the FD getting closed in that window.
512 */
514
515 /*
516 * It's very unlikely, but possible, that reopen fails. E.g. due
517 * to memory allocations failing or file permissions changing or
518 * such. In that case we need to fail the IO.
519 *
520 * There's not really a good errno we can report here.
521 */
522 error_errno = ENOENT;
523 pgaio_io_reopen(ioh);
524
525 /*
526 * To be able to exercise the reopen-fails path, allow injection
527 * points to trigger a failure at this point.
528 */
529 INJECTION_POINT("aio-worker-after-reopen", ioh);
530
531 error_errno = 0;
532 error_ioh = NULL;
533
534 /*
535 * As part of IO completion the buffer will be marked as NOACCESS,
536 * until the buffer is pinned again - which never happens in io
537 * workers. Therefore the next time there is IO for the same
538 * buffer, the memory will be considered inaccessible. To avoid
539 * that, explicitly allow access to the memory before reading data
540 * into it.
541 */
542#ifdef USE_VALGRIND
543 {
544 struct iovec *iov;
545 uint16 iov_length = pgaio_io_get_iovec_length(ioh, &iov);
546
547 for (int i = 0; i < iov_length; i++)
548 VALGRIND_MAKE_MEM_UNDEFINED(iov[i].iov_base, iov[i].iov_len);
549 }
550#endif
551
552 /*
553 * We don't expect this to ever fail with ERROR or FATAL, no need
554 * to keep error_ioh set to the IO.
555 * pgaio_io_perform_synchronously() contains a critical section to
556 * ensure we don't accidentally fail.
557 */
559
561 errcallback.arg = NULL;
562 }
563 else
564 {
566 WAIT_EVENT_IO_WORKER_MAIN);
568 }
569
571 }
572
573 error_context_stack = errcallback.previous;
574 proc_exit(0);
575}
void pgaio_io_process_completion(PgAioHandle *ioh, int result)
Definition: aio.c:525
PgAioCtl * pgaio_ctl
Definition: aio.c:78
#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:116
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:1692
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))
#define INJECTION_POINT(name, arg)
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:52
static void pgaio_worker_register(void)
static int MyIoWorkerId
Definition: method_worker.c:98
static uint32 pgaio_worker_submission_queue_consume(void)
static AioWorkerControl * io_worker_control
#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:674
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
uint64 idle_worker_mask
Definition: method_worker.c:72
AioWorkerSlot workers[FLEXIBLE_ARRAY_MEMBER]
Definition: method_worker.c:73
struct ErrorContextCallback * previous
Definition: elog.h:296
void(* callback)(void *arg)
Definition: elog.h:297
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, INJECTION_POINT, 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_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.

◆ pgaio_choose_idle_worker()

static int pgaio_choose_idle_worker ( void  )
static

Definition at line 165 of file method_worker.c.

166{
167 int worker;
168
170 return -1;
171
172 /* Find the lowest bit position, and clear it. */
174 io_worker_control->idle_worker_mask &= ~(UINT64_C(1) << worker);
175
176 return worker;
177}
static int pg_rightmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:145

References AioWorkerControl::idle_worker_mask, io_worker_control, and pg_rightmost_one_pos64().

Referenced by IoWorkerMain(), and pgaio_worker_submit_internal().

◆ pgaio_worker_control_shmem_size()

static size_t pgaio_worker_control_shmem_size ( void  )
static

Definition at line 114 of file method_worker.c.

115{
116 return offsetof(AioWorkerControl, workers) +
118}
struct AioWorkerSlot AioWorkerSlot
#define MAX_IO_WORKERS
Definition: proc.h:454

References MAX_IO_WORKERS.

Referenced by pgaio_worker_shmem_init(), and pgaio_worker_shmem_size().

◆ pgaio_worker_die()

static void pgaio_worker_die ( int  code,
Datum  arg 
)
static

◆ pgaio_worker_error_callback()

static void pgaio_worker_error_callback ( void *  arg)
static

Definition at line 363 of file method_worker.c.

364{
365 ProcNumber owner;
366 PGPROC *owner_proc;
367 int32 owner_pid;
368 PgAioHandle *ioh = arg;
369
370 if (!ioh)
371 return;
372
375
376 owner = ioh->owner_procno;
377 owner_proc = GetPGProcByNumber(owner);
378 owner_pid = owner_proc->pid;
379
380 errcontext("I/O worker executing I/O on behalf of process %d", owner_pid);
381}
int32_t int32
Definition: c.h:498
#define errcontext
Definition: elog.h:197
ProcNumber MyProcNumber
Definition: globals.c:91
void * arg
#define GetPGProcByNumber(n)
Definition: proc.h:432
int ProcNumber
Definition: procnumber.h:24
Definition: proc.h:171
int pid
Definition: proc.h:191
int32 owner_procno
Definition: aio_internal.h:125

References arg, Assert(), B_IO_WORKER, errcontext, GetPGProcByNumber, MyBackendType, MyProcNumber, PgAioHandle::owner_procno, and PGPROC::pid.

Referenced by IoWorkerMain().

◆ pgaio_worker_needs_synchronous_execution()

static bool pgaio_worker_needs_synchronous_execution ( PgAioHandle ioh)
static

Definition at line 234 of file method_worker.c.

235{
236 return
239 || !pgaio_io_can_reopen(ioh);
240}
@ PGAIO_HF_REFERENCES_LOCAL
Definition: aio.h:60
bool pgaio_io_can_reopen(PgAioHandle *ioh)
Definition: aio_target.c:103
bool IsUnderPostmaster
Definition: globals.c:121

References PgAioHandle::flags, IsUnderPostmaster, PGAIO_HF_REFERENCES_LOCAL, and pgaio_io_can_reopen().

Referenced by pgaio_worker_submit_internal().

◆ pgaio_worker_queue_shmem_size()

static size_t pgaio_worker_queue_shmem_size ( int *  queue_size)
static

Definition at line 104 of file method_worker.c.

105{
106 /* Round size up to next power of two so we can make a mask. */
108
109 return offsetof(AioWorkerSubmissionQueue, ios) +
110 sizeof(uint32) * *queue_size;
111}
static int io_worker_queue_size
Definition: method_worker.c:97
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:189

References io_worker_queue_size, and pg_nextpower2_32().

Referenced by pgaio_worker_shmem_init(), and pgaio_worker_shmem_size().

◆ pgaio_worker_register()

static void pgaio_worker_register ( void  )
static

Definition at line 329 of file method_worker.c.

330{
331 MyIoWorkerId = -1;
332
333 /*
334 * XXX: This could do with more fine-grained locking. But it's also not
335 * very common for the number of workers to change at the moment...
336 */
337 LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
338
339 for (int i = 0; i < MAX_IO_WORKERS; ++i)
340 {
342 {
345 MyIoWorkerId = i;
346 break;
347 }
348 else
350 }
351
352 if (MyIoWorkerId == -1)
353 elog(ERROR, "couldn't find a free worker slot");
354
357 LWLockRelease(AioWorkerSubmissionQueueLock);
358
360}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
static void pgaio_worker_die(int code, Datum arg)

References Assert(), elog, ERROR, i, AioWorkerControl::idle_worker_mask, AioWorkerSlot::in_use, io_worker_control, AioWorkerSlot::latch, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_IO_WORKERS, MyIoWorkerId, MyLatch, on_shmem_exit(), pgaio_worker_die(), and AioWorkerControl::workers.

Referenced by IoWorkerMain().

◆ pgaio_worker_shmem_init()

static void pgaio_worker_shmem_init ( bool  first_time)
static

Definition at line 133 of file method_worker.c.

134{
135 bool found;
136 int queue_size;
137
139 ShmemInitStruct("AioWorkerSubmissionQueue",
141 &found);
142 if (!found)
143 {
144 io_worker_submission_queue->size = queue_size;
147 }
148
150 ShmemInitStruct("AioWorkerControl",
152 &found);
153 if (!found)
154 {
156 for (int i = 0; i < MAX_IO_WORKERS; ++i)
157 {
160 }
161 }
162}
static size_t pgaio_worker_control_shmem_size(void)
static size_t pgaio_worker_queue_shmem_size(int *queue_size)
static AioWorkerSubmissionQueue * io_worker_submission_queue
Definition: method_worker.c:99
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387

References AioWorkerSubmissionQueue::head, i, AioWorkerControl::idle_worker_mask, AioWorkerSlot::in_use, io_worker_control, io_worker_submission_queue, AioWorkerSlot::latch, MAX_IO_WORKERS, pgaio_worker_control_shmem_size(), pgaio_worker_queue_shmem_size(), ShmemInitStruct(), AioWorkerSubmissionQueue::size, AioWorkerSubmissionQueue::tail, and AioWorkerControl::workers.

◆ pgaio_worker_shmem_size()

static size_t pgaio_worker_shmem_size ( void  )
static

Definition at line 121 of file method_worker.c.

122{
123 size_t sz;
124 int queue_size;
125
126 sz = pgaio_worker_queue_shmem_size(&queue_size);
128
129 return sz;
130}
Size add_size(Size s1, Size s2)
Definition: shmem.c:493

References add_size(), pgaio_worker_control_shmem_size(), and pgaio_worker_queue_shmem_size().

◆ pgaio_worker_submission_queue_consume()

static uint32 pgaio_worker_submission_queue_consume ( void  )
static

Definition at line 201 of file method_worker.c.

202{
204 uint32 result;
205
207 if (queue->tail == queue->head)
208 return UINT32_MAX; /* empty */
209
210 result = queue->ios[queue->tail];
211 queue->tail = (queue->tail + 1) & (queue->size - 1);
212
213 return result;
214}
uint32 ios[FLEXIBLE_ARRAY_MEMBER]
Definition: method_worker.c:61

References AioWorkerSubmissionQueue::head, io_worker_submission_queue, AioWorkerSubmissionQueue::ios, AioWorkerSubmissionQueue::size, and AioWorkerSubmissionQueue::tail.

Referenced by IoWorkerMain().

◆ pgaio_worker_submission_queue_depth()

static uint32 pgaio_worker_submission_queue_depth ( void  )
static

Definition at line 217 of file method_worker.c.

218{
219 uint32 head;
220 uint32 tail;
221
224
225 if (tail > head)
227
228 Assert(head >= tail);
229
230 return head - tail;
231}

References Assert(), AioWorkerSubmissionQueue::head, io_worker_submission_queue, AioWorkerSubmissionQueue::size, and AioWorkerSubmissionQueue::tail.

Referenced by IoWorkerMain().

◆ pgaio_worker_submission_queue_insert()

static bool pgaio_worker_submission_queue_insert ( PgAioHandle ioh)
static

Definition at line 180 of file method_worker.c.

181{
183 uint32 new_head;
184
186 new_head = (queue->head + 1) & (queue->size - 1);
187 if (new_head == queue->tail)
188 {
189 pgaio_debug(DEBUG3, "io queue is full, at %u elements",
191 return false; /* full */
192 }
193
194 queue->ios[queue->head] = pgaio_io_get_id(ioh);
195 queue->head = new_head;
196
197 return true;
198}
int pgaio_io_get_id(PgAioHandle *ioh)
Definition: aio.c:339
#define pgaio_debug(elevel, msg,...)
Definition: aio_internal.h:376
#define DEBUG3
Definition: elog.h:28

References DEBUG3, AioWorkerSubmissionQueue::head, io_worker_submission_queue, AioWorkerSubmissionQueue::ios, pgaio_debug, pgaio_io_get_id(), AioWorkerSubmissionQueue::size, and AioWorkerSubmissionQueue::tail.

Referenced by pgaio_worker_submit_internal().

◆ pgaio_worker_submit()

static int pgaio_worker_submit ( uint16  num_staged_ios,
PgAioHandle **  staged_ios 
)
static

Definition at line 294 of file method_worker.c.

295{
296 for (int i = 0; i < num_staged_ios; i++)
297 {
298 PgAioHandle *ioh = staged_ios[i];
299
301 }
302
303 pgaio_worker_submit_internal(num_staged_ios, staged_ios);
304
305 return num_staged_ios;
306}
void pgaio_io_prepare_submit(PgAioHandle *ioh)
Definition: aio.c:507
static void pgaio_worker_submit_internal(int nios, PgAioHandle *ios[])

References i, pgaio_io_prepare_submit(), and pgaio_worker_submit_internal().

◆ pgaio_worker_submit_internal()

static void pgaio_worker_submit_internal ( int  nios,
PgAioHandle ios[] 
)
static

Definition at line 243 of file method_worker.c.

244{
245 PgAioHandle *synchronous_ios[PGAIO_SUBMIT_BATCH_SIZE];
246 int nsync = 0;
247 Latch *wakeup = NULL;
248 int worker;
249
251
252 LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
253 for (int i = 0; i < nios; ++i)
254 {
257 {
258 /*
259 * We'll do it synchronously, but only after we've sent as many as
260 * we can to workers, to maximize concurrency.
261 */
262 synchronous_ios[nsync++] = ios[i];
263 continue;
264 }
265
266 if (wakeup == NULL)
267 {
268 /* Choose an idle worker to wake up if we haven't already. */
269 worker = pgaio_choose_idle_worker();
270 if (worker >= 0)
272
273 pgaio_debug_io(DEBUG4, ios[i],
274 "choosing worker %d",
275 worker);
276 }
277 }
278 LWLockRelease(AioWorkerSubmissionQueueLock);
279
280 if (wakeup)
282
283 /* Run whatever is left synchronously. */
284 if (nsync > 0)
285 {
286 for (int i = 0; i < nsync; ++i)
287 {
288 pgaio_io_perform_synchronously(synchronous_ios[i]);
289 }
290 }
291}
#define PGAIO_SUBMIT_BATCH_SIZE
Definition: aio_internal.h:28
static bool pgaio_worker_needs_synchronous_execution(PgAioHandle *ioh)
static bool pgaio_worker_submission_queue_insert(PgAioHandle *ioh)
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]
Definition: walreceiver.c:130

References Assert(), DEBUG4, i, io_worker_control, AioWorkerSlot::latch, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgaio_choose_idle_worker(), pgaio_debug_io, pgaio_io_perform_synchronously(), PGAIO_SUBMIT_BATCH_SIZE, pgaio_worker_needs_synchronous_execution(), pgaio_worker_submission_queue_insert(), SetLatch(), wakeup, and AioWorkerControl::workers.

Referenced by pgaio_worker_submit().

◆ pgaio_workers_enabled()

bool pgaio_workers_enabled ( void  )

Definition at line 578 of file method_worker.c.

579{
580 return io_method == IOMETHOD_WORKER;
581}
int io_method
Definition: aio.c:74
@ IOMETHOD_WORKER
Definition: aio.h:35

References io_method, and IOMETHOD_WORKER.

Referenced by maybe_adjust_io_workers().

Variable Documentation

◆ io_worker_control

◆ io_worker_queue_size

int io_worker_queue_size = 64
static

Definition at line 97 of file method_worker.c.

Referenced by pgaio_worker_queue_shmem_size().

◆ io_worker_submission_queue

◆ io_workers

int io_workers = 3

Definition at line 94 of file method_worker.c.

Referenced by maybe_adjust_io_workers().

◆ MyIoWorkerId

int MyIoWorkerId
static

Definition at line 98 of file method_worker.c.

Referenced by IoWorkerMain(), pgaio_worker_die(), and pgaio_worker_register().

◆ pgaio_worker_ops

const IoMethodOps pgaio_worker_ops
Initial value:
= {
.shmem_size = pgaio_worker_shmem_size,
.shmem_init = pgaio_worker_shmem_init,
.needs_synchronous_execution = pgaio_worker_needs_synchronous_execution,
}
static int pgaio_worker_submit(uint16 num_staged_ios, PgAioHandle **staged_ios)
static size_t pgaio_worker_shmem_size(void)
static void pgaio_worker_shmem_init(bool first_time)

Definition at line 84 of file method_worker.c.