LCOV - differential code coverage report
Current view: top level - src/backend/storage/aio - method_worker.c (source / functions) Coverage Total Hit UBC CBC
Current: 806555e3000d0b0e0c536c1dc65548128d457d86 vs 1d325ad99cb2dec0e8b45ba36909ee0a497d2a57 Lines: 90.8 % 195 177 18 177
Current Date: 2025-12-17 08:58:58 +0900 Functions: 100.0 % 16 16 16
Baseline: lcov-20251217-005640-baseline Branches: 66.0 % 100 66 34 66
Baseline Date: 2025-12-16 12:57:12 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 90.8 % 195 177 18 177
Function coverage date bins:
(30,360] days: 100.0 % 16 16 16
Branch coverage date bins:
(30,360] days: 66.0 % 100 66 34 66

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * method_worker.c
                                  4                 :                :  *    AIO - perform AIO using worker processes
                                  5                 :                :  *
                                  6                 :                :  * IO workers consume IOs from a shared memory submission queue, run
                                  7                 :                :  * traditional synchronous system calls, and perform the shared completion
                                  8                 :                :  * handling immediately.  Client code submits most requests by pushing IOs
                                  9                 :                :  * into the submission queue, and waits (if necessary) using condition
                                 10                 :                :  * variables.  Some IOs cannot be performed in another process due to lack of
                                 11                 :                :  * infrastructure for reopening the file, and must processed synchronously by
                                 12                 :                :  * the client code when submitted.
                                 13                 :                :  *
                                 14                 :                :  * So that the submitter can make just one system call when submitting a batch
                                 15                 :                :  * of IOs, wakeups "fan out"; each woken IO worker can wake two more. XXX This
                                 16                 :                :  * could be improved by using futexes instead of latches to wake N waiters.
                                 17                 :                :  *
                                 18                 :                :  * This method of AIO is available in all builds on all operating systems, and
                                 19                 :                :  * is the default.
                                 20                 :                :  *
                                 21                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 22                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 23                 :                :  *
                                 24                 :                :  * IDENTIFICATION
                                 25                 :                :  *    src/backend/storage/aio/method_worker.c
                                 26                 :                :  *
                                 27                 :                :  *-------------------------------------------------------------------------
                                 28                 :                :  */
                                 29                 :                : 
                                 30                 :                : #include "postgres.h"
                                 31                 :                : 
                                 32                 :                : #include "libpq/pqsignal.h"
                                 33                 :                : #include "miscadmin.h"
                                 34                 :                : #include "port/pg_bitutils.h"
                                 35                 :                : #include "postmaster/auxprocess.h"
                                 36                 :                : #include "postmaster/interrupt.h"
                                 37                 :                : #include "storage/aio.h"
                                 38                 :                : #include "storage/aio_internal.h"
                                 39                 :                : #include "storage/aio_subsys.h"
                                 40                 :                : #include "storage/io_worker.h"
                                 41                 :                : #include "storage/ipc.h"
                                 42                 :                : #include "storage/latch.h"
                                 43                 :                : #include "storage/proc.h"
                                 44                 :                : #include "tcop/tcopprot.h"
                                 45                 :                : #include "utils/injection_point.h"
                                 46                 :                : #include "utils/memdebug.h"
                                 47                 :                : #include "utils/ps_status.h"
                                 48                 :                : #include "utils/wait_event.h"
                                 49                 :                : 
                                 50                 :                : 
                                 51                 :                : /* How many workers should each worker wake up if needed? */
                                 52                 :                : #define IO_WORKER_WAKEUP_FANOUT 2
                                 53                 :                : 
                                 54                 :                : 
                                 55                 :                : typedef struct PgAioWorkerSubmissionQueue
                                 56                 :                : {
                                 57                 :                :     uint32      size;
                                 58                 :                :     uint32      head;
                                 59                 :                :     uint32      tail;
                                 60                 :                :     int         sqes[FLEXIBLE_ARRAY_MEMBER];
                                 61                 :                : } PgAioWorkerSubmissionQueue;
                                 62                 :                : 
                                 63                 :                : typedef struct PgAioWorkerSlot
                                 64                 :                : {
                                 65                 :                :     Latch      *latch;
                                 66                 :                :     bool        in_use;
                                 67                 :                : } PgAioWorkerSlot;
                                 68                 :                : 
                                 69                 :                : typedef struct PgAioWorkerControl
                                 70                 :                : {
                                 71                 :                :     uint64      idle_worker_mask;
                                 72                 :                :     PgAioWorkerSlot workers[FLEXIBLE_ARRAY_MEMBER];
                                 73                 :                : } PgAioWorkerControl;
                                 74                 :                : 
                                 75                 :                : 
                                 76                 :                : static size_t pgaio_worker_shmem_size(void);
                                 77                 :                : static void pgaio_worker_shmem_init(bool first_time);
                                 78                 :                : 
                                 79                 :                : static bool pgaio_worker_needs_synchronous_execution(PgAioHandle *ioh);
                                 80                 :                : static int  pgaio_worker_submit(uint16 num_staged_ios, PgAioHandle **staged_ios);
                                 81                 :                : 
                                 82                 :                : 
                                 83                 :                : const IoMethodOps pgaio_worker_ops = {
                                 84                 :                :     .shmem_size = pgaio_worker_shmem_size,
                                 85                 :                :     .shmem_init = pgaio_worker_shmem_init,
                                 86                 :                : 
                                 87                 :                :     .needs_synchronous_execution = pgaio_worker_needs_synchronous_execution,
                                 88                 :                :     .submit = pgaio_worker_submit,
                                 89                 :                : };
                                 90                 :                : 
                                 91                 :                : 
                                 92                 :                : /* GUCs */
                                 93                 :                : int         io_workers = 3;
                                 94                 :                : 
                                 95                 :                : 
                                 96                 :                : static int  io_worker_queue_size = 64;
                                 97                 :                : static int  MyIoWorkerId;
                                 98                 :                : static PgAioWorkerSubmissionQueue *io_worker_submission_queue;
                                 99                 :                : static PgAioWorkerControl *io_worker_control;
                                100                 :                : 
                                101                 :                : 
                                102                 :                : static size_t
  274 andres@anarazel.de        103                 :CBC        3031 : pgaio_worker_queue_shmem_size(int *queue_size)
                                104                 :                : {
                                105                 :                :     /* Round size up to next power of two so we can make a mask. */
                                106                 :           3031 :     *queue_size = pg_nextpower2_32(io_worker_queue_size);
                                107                 :                : 
  158 tmunro@postgresql.or      108                 :           6062 :     return offsetof(PgAioWorkerSubmissionQueue, sqes) +
  118 peter@eisentraut.org      109                 :           3031 :         sizeof(int) * *queue_size;
                                110                 :                : }
                                111                 :                : 
                                112                 :                : static size_t
  274 andres@anarazel.de        113                 :           3031 : pgaio_worker_control_shmem_size(void)
                                114                 :                : {
  158 tmunro@postgresql.or      115                 :           3031 :     return offsetof(PgAioWorkerControl, workers) +
                                116                 :                :         sizeof(PgAioWorkerSlot) * MAX_IO_WORKERS;
                                117                 :                : }
                                118                 :                : 
                                119                 :                : static size_t
  274 andres@anarazel.de        120                 :           1972 : pgaio_worker_shmem_size(void)
                                121                 :                : {
                                122                 :                :     size_t      sz;
                                123                 :                :     int         queue_size;
                                124                 :                : 
                                125                 :           1972 :     sz = pgaio_worker_queue_shmem_size(&queue_size);
                                126                 :           1972 :     sz = add_size(sz, pgaio_worker_control_shmem_size());
                                127                 :                : 
                                128                 :           1972 :     return sz;
                                129                 :                : }
                                130                 :                : 
                                131                 :                : static void
                                132                 :           1059 : pgaio_worker_shmem_init(bool first_time)
                                133                 :                : {
                                134                 :                :     bool        found;
                                135                 :                :     int         queue_size;
                                136                 :                : 
                                137                 :           1059 :     io_worker_submission_queue =
                                138                 :           1059 :         ShmemInitStruct("AioWorkerSubmissionQueue",
                                139                 :                :                         pgaio_worker_queue_shmem_size(&queue_size),
                                140                 :                :                         &found);
                                141         [ +  - ]:           1059 :     if (!found)
                                142                 :                :     {
                                143                 :           1059 :         io_worker_submission_queue->size = queue_size;
                                144                 :           1059 :         io_worker_submission_queue->head = 0;
                                145                 :           1059 :         io_worker_submission_queue->tail = 0;
                                146                 :                :     }
                                147                 :                : 
                                148                 :           1059 :     io_worker_control =
                                149                 :           1059 :         ShmemInitStruct("AioWorkerControl",
                                150                 :                :                         pgaio_worker_control_shmem_size(),
                                151                 :                :                         &found);
                                152         [ +  - ]:           1059 :     if (!found)
                                153                 :                :     {
                                154                 :           1059 :         io_worker_control->idle_worker_mask = 0;
                                155         [ +  + ]:          34947 :         for (int i = 0; i < MAX_IO_WORKERS; ++i)
                                156                 :                :         {
                                157                 :          33888 :             io_worker_control->workers[i].latch = NULL;
                                158                 :          33888 :             io_worker_control->workers[i].in_use = false;
                                159                 :                :         }
                                160                 :                :     }
                                161                 :           1059 : }
                                162                 :                : 
                                163                 :                : static int
  158 tmunro@postgresql.or      164                 :         621398 : pgaio_worker_choose_idle(void)
                                165                 :                : {
                                166                 :                :     int         worker;
                                167                 :                : 
  274 andres@anarazel.de        168         [ +  + ]:         621398 :     if (io_worker_control->idle_worker_mask == 0)
                                169                 :          24715 :         return -1;
                                170                 :                : 
                                171                 :                :     /* Find the lowest bit position, and clear it. */
                                172                 :         596683 :     worker = pg_rightmost_one_pos64(io_worker_control->idle_worker_mask);
                                173                 :         596683 :     io_worker_control->idle_worker_mask &= ~(UINT64_C(1) << worker);
  158 tmunro@postgresql.or      174         [ -  + ]:         596683 :     Assert(io_worker_control->workers[worker].in_use);
                                175                 :                : 
  274 andres@anarazel.de        176                 :         596683 :     return worker;
                                177                 :                : }
                                178                 :                : 
                                179                 :                : static bool
                                180                 :         598872 : pgaio_worker_submission_queue_insert(PgAioHandle *ioh)
                                181                 :                : {
                                182                 :                :     PgAioWorkerSubmissionQueue *queue;
                                183                 :                :     uint32      new_head;
                                184                 :                : 
                                185                 :         598872 :     queue = io_worker_submission_queue;
                                186                 :         598872 :     new_head = (queue->head + 1) & (queue->size - 1);
                                187         [ -  + ]:         598872 :     if (new_head == queue->tail)
                                188                 :                :     {
  274 andres@anarazel.de        189         [ #  # ]:UBC           0 :         pgaio_debug(DEBUG3, "io queue is full, at %u elements",
                                190                 :                :                     io_worker_submission_queue->size);
                                191                 :              0 :         return false;           /* full */
                                192                 :                :     }
                                193                 :                : 
  158 tmunro@postgresql.or      194                 :CBC      598872 :     queue->sqes[queue->head] = pgaio_io_get_id(ioh);
  274 andres@anarazel.de        195                 :         598872 :     queue->head = new_head;
                                196                 :                : 
                                197                 :         598872 :     return true;
                                198                 :                : }
                                199                 :                : 
                                200                 :                : static int
                                201                 :         992227 : pgaio_worker_submission_queue_consume(void)
                                202                 :                : {
                                203                 :                :     PgAioWorkerSubmissionQueue *queue;
                                204                 :                :     int         result;
                                205                 :                : 
                                206                 :         992227 :     queue = io_worker_submission_queue;
                                207         [ +  + ]:         992227 :     if (queue->tail == queue->head)
  118 peter@eisentraut.org      208                 :         497029 :         return -1;              /* empty */
                                209                 :                : 
  158 tmunro@postgresql.or      210                 :         495198 :     result = queue->sqes[queue->tail];
  274 andres@anarazel.de        211                 :         495198 :     queue->tail = (queue->tail + 1) & (queue->size - 1);
                                212                 :                : 
                                213                 :         495198 :     return result;
                                214                 :                : }
                                215                 :                : 
                                216                 :                : static uint32
                                217                 :         988703 : pgaio_worker_submission_queue_depth(void)
                                218                 :                : {
                                219                 :                :     uint32      head;
                                220                 :                :     uint32      tail;
                                221                 :                : 
                                222                 :         988703 :     head = io_worker_submission_queue->head;
                                223                 :         988703 :     tail = io_worker_submission_queue->tail;
                                224                 :                : 
                                225         [ +  + ]:         988703 :     if (tail > head)
                                226                 :            673 :         head += io_worker_submission_queue->size;
                                227                 :                : 
                                228         [ -  + ]:         988703 :     Assert(head >= tail);
                                229                 :                : 
                                230                 :         988703 :     return head - tail;
                                231                 :                : }
                                232                 :                : 
                                233                 :                : static bool
                                234                 :        1202417 : pgaio_worker_needs_synchronous_execution(PgAioHandle *ioh)
                                235                 :                : {
                                236                 :                :     return
                                237                 :        1202417 :         !IsUnderPostmaster
                                238         [ +  + ]:        1198896 :         || ioh->flags & PGAIO_HF_REFERENCES_LOCAL
                                239   [ +  +  -  + ]:        2401313 :         || !pgaio_io_can_reopen(ioh);
                                240                 :                : }
                                241                 :                : 
                                242                 :                : static void
  158 tmunro@postgresql.or      243                 :         598287 : pgaio_worker_submit_internal(int num_staged_ios, PgAioHandle **staged_ios)
                                244                 :                : {
                                245                 :                :     PgAioHandle *synchronous_ios[PGAIO_SUBMIT_BATCH_SIZE];
  274 andres@anarazel.de        246                 :         598287 :     int         nsync = 0;
                                247                 :         598287 :     Latch      *wakeup = NULL;
                                248                 :                :     int         worker;
                                249                 :                : 
  158 tmunro@postgresql.or      250         [ -  + ]:         598287 :     Assert(num_staged_ios <= PGAIO_SUBMIT_BATCH_SIZE);
                                251                 :                : 
  274 andres@anarazel.de        252                 :         598287 :     LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
  158 tmunro@postgresql.or      253         [ +  + ]:        1197159 :     for (int i = 0; i < num_staged_ios; ++i)
                                254                 :                :     {
                                255         [ -  + ]:         598872 :         Assert(!pgaio_worker_needs_synchronous_execution(staged_ios[i]));
                                256         [ -  + ]:         598872 :         if (!pgaio_worker_submission_queue_insert(staged_ios[i]))
                                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                 :                :              */
  158 tmunro@postgresql.or      262                 :UBC           0 :             synchronous_ios[nsync++] = staged_ios[i];
  274 andres@anarazel.de        263                 :              0 :             continue;
                                264                 :                :         }
                                265                 :                : 
  274 andres@anarazel.de        266         [ +  + ]:CBC      598872 :         if (wakeup == NULL)
                                267                 :                :         {
                                268                 :                :             /* Choose an idle worker to wake up if we haven't already. */
  158 tmunro@postgresql.or      269                 :         598301 :             worker = pgaio_worker_choose_idle();
  274 andres@anarazel.de        270         [ +  + ]:         598301 :             if (worker >= 0)
                                271                 :         587149 :                 wakeup = io_worker_control->workers[worker].latch;
                                272                 :                : 
  158 tmunro@postgresql.or      273         [ -  + ]:         598301 :             pgaio_debug_io(DEBUG4, staged_ios[i],
                                274                 :                :                            "choosing worker %d",
                                275                 :                :                            worker);
                                276                 :                :         }
                                277                 :                :     }
  274 andres@anarazel.de        278                 :         598287 :     LWLockRelease(AioWorkerSubmissionQueueLock);
                                279                 :                : 
                                280         [ +  + ]:         598287 :     if (wakeup)
                                281                 :         587149 :         SetLatch(wakeup);
                                282                 :                : 
                                283                 :                :     /* Run whatever is left synchronously. */
                                284         [ -  + ]:         598287 :     if (nsync > 0)
                                285                 :                :     {
  274 andres@anarazel.de        286         [ #  # ]:UBC           0 :         for (int i = 0; i < nsync; ++i)
                                287                 :                :         {
                                288                 :              0 :             pgaio_io_perform_synchronously(synchronous_ios[i]);
                                289                 :                :         }
                                290                 :                :     }
  274 andres@anarazel.de        291                 :CBC      598287 : }
                                292                 :                : 
                                293                 :                : static int
                                294                 :         598287 : pgaio_worker_submit(uint16 num_staged_ios, PgAioHandle **staged_ios)
                                295                 :                : {
                                296         [ +  + ]:        1197159 :     for (int i = 0; i < num_staged_ios; i++)
                                297                 :                :     {
                                298                 :         598872 :         PgAioHandle *ioh = staged_ios[i];
                                299                 :                : 
                                300                 :         598872 :         pgaio_io_prepare_submit(ioh);
                                301                 :                :     }
                                302                 :                : 
                                303                 :         598287 :     pgaio_worker_submit_internal(num_staged_ios, staged_ios);
                                304                 :                : 
                                305                 :         598287 :     return num_staged_ios;
                                306                 :                : }
                                307                 :                : 
                                308                 :                : /*
                                309                 :                :  * on_shmem_exit() callback that releases the worker's slot in
                                310                 :                :  * io_worker_control.
                                311                 :                :  */
                                312                 :                : static void
                                313                 :           1593 : pgaio_worker_die(int code, Datum arg)
                                314                 :                : {
                                315                 :           1593 :     LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
                                316         [ -  + ]:           1593 :     Assert(io_worker_control->workers[MyIoWorkerId].in_use);
                                317         [ -  + ]:           1593 :     Assert(io_worker_control->workers[MyIoWorkerId].latch == MyLatch);
                                318                 :                : 
  158 tmunro@postgresql.or      319                 :           1593 :     io_worker_control->idle_worker_mask &= ~(UINT64_C(1) << MyIoWorkerId);
  274 andres@anarazel.de        320                 :           1593 :     io_worker_control->workers[MyIoWorkerId].in_use = false;
                                321                 :           1593 :     io_worker_control->workers[MyIoWorkerId].latch = NULL;
                                322                 :           1593 :     LWLockRelease(AioWorkerSubmissionQueueLock);
                                323                 :           1593 : }
                                324                 :                : 
                                325                 :                : /*
                                326                 :                :  * Register the worker in shared memory, assign MyIoWorkerId and register a
                                327                 :                :  * shutdown callback to release registration.
                                328                 :                :  */
                                329                 :                : static void
                                330                 :           1593 : pgaio_worker_register(void)
                                331                 :                : {
                                332                 :           1593 :     MyIoWorkerId = -1;
                                333                 :                : 
                                334                 :                :     /*
                                335                 :                :      * XXX: This could do with more fine-grained locking. But it's also not
                                336                 :                :      * very common for the number of workers to change at the moment...
                                337                 :                :      */
                                338                 :           1593 :     LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
                                339                 :                : 
                                340         [ +  - ]:           3689 :     for (int i = 0; i < MAX_IO_WORKERS; ++i)
                                341                 :                :     {
                                342         [ +  + ]:           3689 :         if (!io_worker_control->workers[i].in_use)
                                343                 :                :         {
                                344         [ -  + ]:           1593 :             Assert(io_worker_control->workers[i].latch == NULL);
                                345                 :           1593 :             io_worker_control->workers[i].in_use = true;
                                346                 :           1593 :             MyIoWorkerId = i;
                                347                 :           1593 :             break;
                                348                 :                :         }
                                349                 :                :         else
                                350         [ -  + ]:           2096 :             Assert(io_worker_control->workers[i].latch != NULL);
                                351                 :                :     }
                                352                 :                : 
                                353         [ -  + ]:           1593 :     if (MyIoWorkerId == -1)
  274 andres@anarazel.de        354         [ #  # ]:UBC           0 :         elog(ERROR, "couldn't find a free worker slot");
                                355                 :                : 
  274 andres@anarazel.de        356                 :CBC        1593 :     io_worker_control->idle_worker_mask |= (UINT64_C(1) << MyIoWorkerId);
                                357                 :           1593 :     io_worker_control->workers[MyIoWorkerId].latch = MyLatch;
                                358                 :           1593 :     LWLockRelease(AioWorkerSubmissionQueueLock);
                                359                 :                : 
                                360                 :           1593 :     on_shmem_exit(pgaio_worker_die, 0);
                                361                 :           1593 : }
                                362                 :                : 
                                363                 :                : static void
  260 melanieplageman@gmai      364                 :            403 : pgaio_worker_error_callback(void *arg)
                                365                 :                : {
                                366                 :                :     ProcNumber  owner;
                                367                 :                :     PGPROC     *owner_proc;
                                368                 :                :     int32       owner_pid;
                                369                 :            403 :     PgAioHandle *ioh = arg;
                                370                 :                : 
                                371         [ +  + ]:            403 :     if (!ioh)
                                372                 :              9 :         return;
                                373                 :                : 
                                374         [ -  + ]:            394 :     Assert(ioh->owner_procno != MyProcNumber);
                                375         [ -  + ]:            394 :     Assert(MyBackendType == B_IO_WORKER);
                                376                 :                : 
                                377                 :            394 :     owner = ioh->owner_procno;
                                378                 :            394 :     owner_proc = GetPGProcByNumber(owner);
                                379                 :            394 :     owner_pid = owner_proc->pid;
                                380                 :                : 
                                381                 :            394 :     errcontext("I/O worker executing I/O on behalf of process %d", owner_pid);
                                382                 :                : }
                                383                 :                : 
                                384                 :                : void
  274 andres@anarazel.de        385                 :           1593 : IoWorkerMain(const void *startup_data, size_t startup_data_len)
                                386                 :                : {
                                387                 :                :     sigjmp_buf  local_sigjmp_buf;
                                388                 :           1593 :     PgAioHandle *volatile error_ioh = NULL;
  260 melanieplageman@gmai      389                 :           1593 :     ErrorContextCallback errcallback = {0};
  274 andres@anarazel.de        390                 :           1593 :     volatile int error_errno = 0;
                                391                 :                :     char        cmd[128];
                                392                 :                : 
                                393                 :           1593 :     MyBackendType = B_IO_WORKER;
                                394                 :           1593 :     AuxiliaryProcessMainCommon();
                                395                 :                : 
                                396                 :           1593 :     pqsignal(SIGHUP, SignalHandlerForConfigReload);
                                397                 :           1593 :     pqsignal(SIGINT, die);      /* to allow manually triggering worker restart */
                                398                 :                : 
                                399                 :                :     /*
                                400                 :                :      * Ignore SIGTERM, will get explicit shutdown via SIGUSR2 later in the
                                401                 :                :      * shutdown sequence, similar to checkpointer.
                                402                 :                :      */
                                403                 :           1593 :     pqsignal(SIGTERM, SIG_IGN);
                                404                 :                :     /* SIGQUIT handler was already set up by InitPostmasterChild */
                                405                 :           1593 :     pqsignal(SIGALRM, SIG_IGN);
                                406                 :           1593 :     pqsignal(SIGPIPE, SIG_IGN);
                                407                 :           1593 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
                                408                 :           1593 :     pqsignal(SIGUSR2, SignalHandlerForShutdownRequest);
                                409                 :                : 
                                410                 :                :     /* also registers a shutdown callback to unregister */
                                411                 :           1593 :     pgaio_worker_register();
                                412                 :                : 
  270 tmunro@postgresql.or      413                 :           1593 :     sprintf(cmd, "%d", MyIoWorkerId);
  274 andres@anarazel.de        414                 :           1593 :     set_ps_display(cmd);
                                415                 :                : 
  260 melanieplageman@gmai      416                 :           1593 :     errcallback.callback = pgaio_worker_error_callback;
                                417                 :           1593 :     errcallback.previous = error_context_stack;
                                418                 :           1593 :     error_context_stack = &errcallback;
                                419                 :                : 
                                420                 :                :     /* see PostgresMain() */
  274 andres@anarazel.de        421         [ -  + ]:           1593 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
                                422                 :                :     {
  274 andres@anarazel.de        423                 :UBC           0 :         error_context_stack = NULL;
                                424                 :              0 :         HOLD_INTERRUPTS();
                                425                 :                : 
                                426                 :              0 :         EmitErrorReport();
                                427                 :                : 
                                428                 :                :         /*
                                429                 :                :          * In the - very unlikely - case that the IO failed in a way that
                                430                 :                :          * raises an error we need to mark the IO as failed.
                                431                 :                :          *
                                432                 :                :          * Need to do just enough error recovery so that we can mark the IO as
                                433                 :                :          * failed and then exit (postmaster will start a new worker).
                                434                 :                :          */
                                435                 :              0 :         LWLockReleaseAll();
                                436                 :                : 
                                437         [ #  # ]:              0 :         if (error_ioh != NULL)
                                438                 :                :         {
                                439                 :                :             /* should never fail without setting error_errno */
                                440         [ #  # ]:              0 :             Assert(error_errno != 0);
                                441                 :                : 
                                442                 :              0 :             errno = error_errno;
                                443                 :                : 
                                444                 :              0 :             START_CRIT_SECTION();
                                445                 :              0 :             pgaio_io_process_completion(error_ioh, -error_errno);
                                446         [ #  # ]:              0 :             END_CRIT_SECTION();
                                447                 :                :         }
                                448                 :                : 
                                449                 :              0 :         proc_exit(1);
                                450                 :                :     }
                                451                 :                : 
                                452                 :                :     /* We can now handle ereport(ERROR) */
  274 andres@anarazel.de        453                 :CBC        1593 :     PG_exception_stack = &local_sigjmp_buf;
                                454                 :                : 
                                455                 :           1593 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
                                456                 :                : 
                                457         [ +  + ]:         993807 :     while (!ShutdownRequestPending)
                                458                 :                :     {
                                459                 :                :         uint32      io_index;
                                460                 :                :         Latch      *latches[IO_WORKER_WAKEUP_FANOUT];
                                461                 :         992227 :         int         nlatches = 0;
                                462                 :         992227 :         int         nwakeups = 0;
                                463                 :                :         int         worker;
                                464                 :                : 
                                465                 :                :         /*
                                466                 :                :          * Try to get a job to do.
                                467                 :                :          *
                                468                 :                :          * The lwlock acquisition also provides the necessary memory barrier
                                469                 :                :          * to ensure that we don't see an outdated data in the handle.
                                470                 :                :          */
                                471                 :         992227 :         LWLockAcquire(AioWorkerSubmissionQueueLock, LW_EXCLUSIVE);
  118 peter@eisentraut.org      472         [ +  + ]:         992227 :         if ((io_index = pgaio_worker_submission_queue_consume()) == -1)
                                473                 :                :         {
                                474                 :                :             /*
                                475                 :                :              * Nothing to do.  Mark self idle.
                                476                 :                :              *
                                477                 :                :              * XXX: Invent some kind of back pressure to reduce useless
                                478                 :                :              * wakeups?
                                479                 :                :              */
  274 andres@anarazel.de        480                 :         497029 :             io_worker_control->idle_worker_mask |= (UINT64_C(1) << MyIoWorkerId);
                                481                 :                :         }
                                482                 :                :         else
                                483                 :                :         {
                                484                 :                :             /* Got one.  Clear idle flag. */
                                485                 :         495198 :             io_worker_control->idle_worker_mask &= ~(UINT64_C(1) << MyIoWorkerId);
                                486                 :                : 
                                487                 :                :             /* See if we can wake up some peers. */
                                488         [ +  + ]:         495198 :             nwakeups = Min(pgaio_worker_submission_queue_depth(),
                                489                 :                :                            IO_WORKER_WAKEUP_FANOUT);
                                490         [ +  + ]:         504732 :             for (int i = 0; i < nwakeups; ++i)
                                491                 :                :             {
  158 tmunro@postgresql.or      492         [ +  + ]:          23097 :                 if ((worker = pgaio_worker_choose_idle()) < 0)
  274 andres@anarazel.de        493                 :          13563 :                     break;
                                494                 :           9534 :                 latches[nlatches++] = io_worker_control->workers[worker].latch;
                                495                 :                :             }
                                496                 :                :         }
                                497                 :         992227 :         LWLockRelease(AioWorkerSubmissionQueueLock);
                                498                 :                : 
                                499         [ +  + ]:        1001761 :         for (int i = 0; i < nlatches; ++i)
                                500                 :           9534 :             SetLatch(latches[i]);
                                501                 :                : 
  118 peter@eisentraut.org      502         [ +  + ]:         992227 :         if (io_index != -1)
                                503                 :                :         {
  274 andres@anarazel.de        504                 :         495198 :             PgAioHandle *ioh = NULL;
                                505                 :                : 
                                506                 :         495198 :             ioh = &pgaio_ctl->io_handles[io_index];
                                507                 :         495198 :             error_ioh = ioh;
  260 melanieplageman@gmai      508                 :         495198 :             errcallback.arg = ioh;
                                509                 :                : 
  274 andres@anarazel.de        510         [ -  + ]:         495198 :             pgaio_debug_io(DEBUG4, ioh,
                                511                 :                :                            "worker %d processing IO",
                                512                 :                :                            MyIoWorkerId);
                                513                 :                : 
                                514                 :                :             /*
                                515                 :                :              * Prevent interrupts between pgaio_io_reopen() and
                                516                 :                :              * pgaio_io_perform_synchronously() that otherwise could lead to
                                517                 :                :              * the FD getting closed in that window.
                                518                 :                :              */
  266                           519                 :         495198 :             HOLD_INTERRUPTS();
                                520                 :                : 
                                521                 :                :             /*
                                522                 :                :              * It's very unlikely, but possible, that reopen fails. E.g. due
                                523                 :                :              * to memory allocations failing or file permissions changing or
                                524                 :                :              * such.  In that case we need to fail the IO.
                                525                 :                :              *
                                526                 :                :              * There's not really a good errno we can report here.
                                527                 :                :              */
  274                           528                 :         495198 :             error_errno = ENOENT;
                                529                 :         495198 :             pgaio_io_reopen(ioh);
                                530                 :                : 
                                531                 :                :             /*
                                532                 :                :              * To be able to exercise the reopen-fails path, allow injection
                                533                 :                :              * points to trigger a failure at this point.
                                534                 :                :              */
                                535                 :                :             INJECTION_POINT("aio-worker-after-reopen", ioh);
                                536                 :                : 
                                537                 :         495198 :             error_errno = 0;
                                538                 :         495198 :             error_ioh = NULL;
                                539                 :                : 
                                540                 :                :             /*
                                541                 :                :              * As part of IO completion the buffer will be marked as NOACCESS,
                                542                 :                :              * until the buffer is pinned again - which never happens in io
                                543                 :                :              * workers. Therefore the next time there is IO for the same
                                544                 :                :              * buffer, the memory will be considered inaccessible. To avoid
                                545                 :                :              * that, explicitly allow access to the memory before reading data
                                546                 :                :              * into it.
                                547                 :                :              */
                                548                 :                : #ifdef USE_VALGRIND
                                549                 :                :             {
                                550                 :                :                 struct iovec *iov;
                                551                 :                :                 uint16      iov_length = pgaio_io_get_iovec_length(ioh, &iov);
                                552                 :                : 
                                553                 :                :                 for (int i = 0; i < iov_length; i++)
                                554                 :                :                     VALGRIND_MAKE_MEM_UNDEFINED(iov[i].iov_base, iov[i].iov_len);
                                555                 :                :             }
                                556                 :                : #endif
                                557                 :                : 
                                558                 :                :             /*
                                559                 :                :              * We don't expect this to ever fail with ERROR or FATAL, no need
                                560                 :                :              * to keep error_ioh set to the IO.
                                561                 :                :              * pgaio_io_perform_synchronously() contains a critical section to
                                562                 :                :              * ensure we don't accidentally fail.
                                563                 :                :              */
                                564                 :         495198 :             pgaio_io_perform_synchronously(ioh);
                                565                 :                : 
  266                           566         [ -  + ]:         495198 :             RESUME_INTERRUPTS();
  260 melanieplageman@gmai      567                 :         495198 :             errcallback.arg = NULL;
                                568                 :                :         }
                                569                 :                :         else
                                570                 :                :         {
  274 andres@anarazel.de        571                 :         497029 :             WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, -1,
                                572                 :                :                       WAIT_EVENT_IO_WORKER_MAIN);
                                573                 :         497020 :             ResetLatch(MyLatch);
                                574                 :                :         }
                                575                 :                : 
                                576         [ +  + ]:         992218 :         CHECK_FOR_INTERRUPTS();
                                577                 :                : 
  158 tmunro@postgresql.or      578         [ +  + ]:         992214 :         if (ConfigReloadPending)
                                579                 :                :         {
                                580                 :            214 :             ConfigReloadPending = false;
                                581                 :            214 :             ProcessConfigFile(PGC_SIGHUP);
                                582                 :                :         }
                                583                 :                :     }
                                584                 :                : 
  260 melanieplageman@gmai      585                 :           1580 :     error_context_stack = errcallback.previous;
  274 andres@anarazel.de        586                 :           1580 :     proc_exit(0);
                                587                 :                : }
                                588                 :                : 
                                589                 :                : bool
                                590                 :          40849 : pgaio_workers_enabled(void)
                                591                 :                : {
                                592                 :          40849 :     return io_method == IOMETHOD_WORKER;
                                593                 :                : }
        

Generated by: LCOV version 2.4-beta