LCOV - differential code coverage report
Current view: top level - src/backend/storage/aio - aio_init.c (source / functions) Coverage Total Hit UBC CBC
Current: a2387c32f2f8a1643c7d71b951587e6bcb2d4744 vs 371a302eecdc82274b0ae2967d18fd726a0aa6a1 Lines: 96.7 % 90 87 3 87
Current Date: 2025-10-26 12:31:50 -0700 Functions: 100.0 % 10 10 10
Baseline: lcov-20251027-010456-baseline Branches: 71.4 % 28 20 8 20
Baseline Date: 2025-10-26 11:01:32 +1300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 96.7 % 90 87 3 87
Function coverage date bins:
(30,360] days: 100.0 % 10 10 10
Branch coverage date bins:
(30,360] days: 71.4 % 28 20 8 20

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * aio_init.c
                                  4                 :                :  *    AIO - Subsystem Initialization
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  * IDENTIFICATION
                                 10                 :                :  *    src/backend/storage/aio/aio_init.c
                                 11                 :                :  *
                                 12                 :                :  *-------------------------------------------------------------------------
                                 13                 :                :  */
                                 14                 :                : 
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "miscadmin.h"
                                 18                 :                : #include "storage/aio.h"
                                 19                 :                : #include "storage/aio_internal.h"
                                 20                 :                : #include "storage/aio_subsys.h"
                                 21                 :                : #include "storage/bufmgr.h"
                                 22                 :                : #include "storage/io_worker.h"
                                 23                 :                : #include "storage/ipc.h"
                                 24                 :                : #include "storage/proc.h"
                                 25                 :                : #include "storage/shmem.h"
                                 26                 :                : #include "utils/guc.h"
                                 27                 :                : 
                                 28                 :                : 
                                 29                 :                : 
                                 30                 :                : static Size
  224 andres@anarazel.de         31                 :CBC        4047 : AioCtlShmemSize(void)
                                 32                 :                : {
                                 33                 :                :     /* pgaio_ctl itself */
   40 michael@paquier.xyz        34                 :           4047 :     return sizeof(PgAioCtl);
                                 35                 :                : }
                                 36                 :                : 
                                 37                 :                : static uint32
  224 andres@anarazel.de         38                 :         169044 : AioProcs(void)
                                 39                 :                : {
                                 40                 :                :     /*
                                 41                 :                :      * While AIO workers don't need their own AIO context, we can't currently
                                 42                 :                :      * guarantee nothing gets assigned to the a ProcNumber for an IO worker if
                                 43                 :                :      * we just subtracted MAX_IO_WORKERS.
                                 44                 :                :      */
                                 45                 :         169044 :     return MaxBackends + NUM_AUXILIARY_PROCS;
                                 46                 :                : }
                                 47                 :                : 
                                 48                 :                : static Size
                                 49                 :           2998 : AioBackendShmemSize(void)
                                 50                 :                : {
                                 51                 :           2998 :     return mul_size(AioProcs(), sizeof(PgAioBackend));
                                 52                 :                : }
                                 53                 :                : 
                                 54                 :                : static Size
                                 55                 :           2998 : AioHandleShmemSize(void)
                                 56                 :                : {
                                 57                 :                :     Size        sz;
                                 58                 :                : 
                                 59                 :                :     /* verify AioChooseMaxConcurrency() did its thing */
                                 60         [ -  + ]:           2998 :     Assert(io_max_concurrency > 0);
                                 61                 :                : 
                                 62                 :                :     /* io handles */
                                 63                 :           2998 :     sz = mul_size(AioProcs(),
                                 64                 :                :                   mul_size(io_max_concurrency, sizeof(PgAioHandle)));
                                 65                 :                : 
                                 66                 :           2998 :     return sz;
                                 67                 :                : }
                                 68                 :                : 
                                 69                 :                : static Size
                                 70                 :           2998 : AioHandleIOVShmemSize(void)
                                 71                 :                : {
                                 72                 :                :     /* each IO handle can have up to io_max_combine_limit iovec objects */
                                 73                 :           2998 :     return mul_size(sizeof(struct iovec),
  222 tmunro@postgresql.or       74                 :           2998 :                     mul_size(mul_size(io_max_combine_limit, AioProcs()),
                                 75                 :                :                              io_max_concurrency));
                                 76                 :                : }
                                 77                 :                : 
                                 78                 :                : static Size
  224 andres@anarazel.de         79                 :           2998 : AioHandleDataShmemSize(void)
                                 80                 :                : {
                                 81                 :                :     /* each buffer referenced by an iovec can have associated data */
                                 82                 :           2998 :     return mul_size(sizeof(uint64),
  222 tmunro@postgresql.or       83                 :           2998 :                     mul_size(mul_size(io_max_combine_limit, AioProcs()),
                                 84                 :                :                              io_max_concurrency));
                                 85                 :                : }
                                 86                 :                : 
                                 87                 :                : /*
                                 88                 :                :  * Choose a suitable value for io_max_concurrency.
                                 89                 :                :  *
                                 90                 :                :  * It's unlikely that we could have more IOs in flight than buffers that we
                                 91                 :                :  * would be allowed to pin.
                                 92                 :                :  *
                                 93                 :                :  * On the upper end, apply a cap too - just because shared_buffers is large,
                                 94                 :                :  * it doesn't make sense have millions of buffers undergo IO concurrently.
                                 95                 :                :  */
                                 96                 :                : static int
  224 andres@anarazel.de         97                 :           1047 : AioChooseMaxConcurrency(void)
                                 98                 :                : {
                                 99                 :                :     uint32      max_backends;
                                100                 :                :     int         max_proportional_pins;
                                101                 :                : 
                                102                 :                :     /* Similar logic to LimitAdditionalPins() */
                                103                 :           1047 :     max_backends = MaxBackends + NUM_AUXILIARY_PROCS;
                                104                 :           1047 :     max_proportional_pins = NBuffers / max_backends;
                                105                 :                : 
                                106                 :           1047 :     max_proportional_pins = Max(max_proportional_pins, 1);
                                107                 :                : 
                                108                 :                :     /* apply upper limit */
                                109                 :           1047 :     return Min(max_proportional_pins, 64);
                                110                 :                : }
                                111                 :                : 
                                112                 :                : Size
                                113                 :           1949 : AioShmemSize(void)
                                114                 :                : {
                                115                 :           1949 :     Size        sz = 0;
                                116                 :                : 
                                117                 :                :     /*
                                118                 :                :      * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
                                119                 :                :      * However, if the DBA explicitly set io_max_concurrency = -1 in the
                                120                 :                :      * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that and
                                121                 :                :      * we must force the matter with PGC_S_OVERRIDE.
                                122                 :                :      */
                                123         [ +  + ]:           1949 :     if (io_max_concurrency == -1)
                                124                 :                :     {
                                125                 :                :         char        buf[32];
                                126                 :                : 
                                127                 :           1047 :         snprintf(buf, sizeof(buf), "%d", AioChooseMaxConcurrency());
                                128                 :           1047 :         SetConfigOption("io_max_concurrency", buf, PGC_POSTMASTER,
                                129                 :                :                         PGC_S_DYNAMIC_DEFAULT);
                                130         [ -  + ]:           1047 :         if (io_max_concurrency == -1)   /* failed to apply it? */
  224 andres@anarazel.de        131                 :UBC           0 :             SetConfigOption("io_max_concurrency", buf, PGC_POSTMASTER,
                                132                 :                :                             PGC_S_OVERRIDE);
                                133                 :                :     }
                                134                 :                : 
  224 andres@anarazel.de        135                 :CBC        1949 :     sz = add_size(sz, AioCtlShmemSize());
                                136                 :           1949 :     sz = add_size(sz, AioBackendShmemSize());
                                137                 :           1949 :     sz = add_size(sz, AioHandleShmemSize());
                                138                 :           1949 :     sz = add_size(sz, AioHandleIOVShmemSize());
                                139                 :           1949 :     sz = add_size(sz, AioHandleDataShmemSize());
                                140                 :                : 
                                141                 :                :     /* Reserve space for method specific resources. */
                                142         [ +  + ]:           1949 :     if (pgaio_method_ops->shmem_size)
                                143                 :           1942 :         sz = add_size(sz, pgaio_method_ops->shmem_size());
                                144                 :                : 
                                145                 :           1949 :     return sz;
                                146                 :                : }
                                147                 :                : 
                                148                 :                : void
                                149                 :           1049 : AioShmemInit(void)
                                150                 :                : {
                                151                 :                :     bool        found;
                                152                 :           1049 :     uint32      io_handle_off = 0;
                                153                 :           1049 :     uint32      iovec_off = 0;
  222 tmunro@postgresql.or      154                 :           1049 :     uint32      per_backend_iovecs = io_max_concurrency * io_max_combine_limit;
                                155                 :                : 
  224 andres@anarazel.de        156                 :           1049 :     pgaio_ctl = (PgAioCtl *)
                                157                 :           1049 :         ShmemInitStruct("AioCtl", AioCtlShmemSize(), &found);
                                158                 :                : 
                                159         [ -  + ]:           1049 :     if (found)
  224 andres@anarazel.de        160                 :UBC           0 :         goto out;
                                161                 :                : 
  224 andres@anarazel.de        162                 :CBC        1049 :     memset(pgaio_ctl, 0, AioCtlShmemSize());
                                163                 :                : 
                                164                 :           1049 :     pgaio_ctl->io_handle_count = AioProcs() * io_max_concurrency;
                                165                 :           1049 :     pgaio_ctl->iovec_count = AioProcs() * per_backend_iovecs;
                                166                 :                : 
                                167                 :           2098 :     pgaio_ctl->backend_state = (PgAioBackend *)
                                168                 :           1049 :         ShmemInitStruct("AioBackend", AioBackendShmemSize(), &found);
                                169                 :                : 
                                170                 :           2098 :     pgaio_ctl->io_handles = (PgAioHandle *)
                                171                 :           1049 :         ShmemInitStruct("AioHandle", AioHandleShmemSize(), &found);
                                172                 :                : 
                                173                 :           2098 :     pgaio_ctl->iovecs = (struct iovec *)
                                174                 :           1049 :         ShmemInitStruct("AioHandleIOV", AioHandleIOVShmemSize(), &found);
                                175                 :           2098 :     pgaio_ctl->handle_data = (uint64 *)
                                176                 :           1049 :         ShmemInitStruct("AioHandleData", AioHandleDataShmemSize(), &found);
                                177                 :                : 
                                178         [ +  + ]:         137440 :     for (int procno = 0; procno < AioProcs(); procno++)
                                179                 :                :     {
                                180                 :         136391 :         PgAioBackend *bs = &pgaio_ctl->backend_state[procno];
                                181                 :                : 
                                182                 :         136391 :         bs->io_handle_off = io_handle_off;
                                183                 :         136391 :         io_handle_off += io_max_concurrency;
                                184                 :                : 
                                185                 :         136391 :         dclist_init(&bs->idle_ios);
                                186                 :         136391 :         memset(bs->staged_ios, 0, sizeof(PgAioHandle *) * PGAIO_SUBMIT_BATCH_SIZE);
                                187                 :         136391 :         dclist_init(&bs->in_flight_ios);
                                188                 :                : 
                                189                 :                :         /* initialize per-backend IOs */
                                190         [ +  + ]:        6484266 :         for (int i = 0; i < io_max_concurrency; i++)
                                191                 :                :         {
                                192                 :        6347875 :             PgAioHandle *ioh = &pgaio_ctl->io_handles[bs->io_handle_off + i];
                                193                 :                : 
                                194                 :        6347875 :             ioh->generation = 1;
                                195                 :        6347875 :             ioh->owner_procno = procno;
                                196                 :        6347875 :             ioh->iovec_off = iovec_off;
                                197                 :        6347875 :             ioh->handle_data_len = 0;
                                198                 :        6347875 :             ioh->report_return = NULL;
                                199                 :        6347875 :             ioh->resowner = NULL;
                                200                 :        6347875 :             ioh->num_callbacks = 0;
  219                           201                 :        6347875 :             ioh->distilled_result.status = PGAIO_RS_UNKNOWN;
  224                           202                 :        6347875 :             ioh->flags = 0;
                                203                 :                : 
                                204                 :        6347875 :             ConditionVariableInit(&ioh->cv);
                                205                 :                : 
                                206                 :        6347875 :             dclist_push_tail(&bs->idle_ios, &ioh->node);
  222 tmunro@postgresql.or      207                 :        6347875 :             iovec_off += io_max_combine_limit;
                                208                 :                :         }
                                209                 :                :     }
                                210                 :                : 
  224 andres@anarazel.de        211                 :           1049 : out:
                                212                 :                :     /* Initialize IO method specific resources. */
                                213         [ +  + ]:           1049 :     if (pgaio_method_ops->shmem_init)
                                214                 :           1044 :         pgaio_method_ops->shmem_init(!found);
                                215                 :           1049 : }
                                216                 :                : 
                                217                 :                : void
                                218                 :          19090 : pgaio_init_backend(void)
                                219                 :                : {
                                220                 :                :     /* shouldn't be initialized twice */
                                221         [ -  + ]:          19090 :     Assert(!pgaio_my_backend);
                                222                 :                : 
  223                           223         [ +  + ]:          19090 :     if (MyBackendType == B_IO_WORKER)
                                224                 :           1576 :         return;
                                225                 :                : 
  224                           226   [ +  -  -  + ]:          17514 :     if (MyProc == NULL || MyProcNumber >= AioProcs())
  224 andres@anarazel.de        227         [ #  # ]:UBC           0 :         elog(ERROR, "aio requires a normal PGPROC");
                                228                 :                : 
  224 andres@anarazel.de        229                 :CBC       17514 :     pgaio_my_backend = &pgaio_ctl->backend_state[MyProcNumber];
                                230                 :                : 
                                231         [ +  + ]:          17514 :     if (pgaio_method_ops->init_backend)
                                232                 :             31 :         pgaio_method_ops->init_backend();
                                233                 :                : 
                                234                 :          17514 :     before_shmem_exit(pgaio_shutdown, 0);
                                235                 :                : }
        

Generated by: LCOV version 2.4-beta