PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
aio_init.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "storage/aio.h"
#include "storage/aio_internal.h"
#include "storage/aio_subsys.h"
#include "storage/bufmgr.h"
#include "storage/io_worker.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "storage/shmem.h"
#include "utils/guc.h"
Include dependency graph for aio_init.c:

Go to the source code of this file.

Functions

static Size AioCtlShmemSize (void)
 
static uint32 AioProcs (void)
 
static Size AioBackendShmemSize (void)
 
static Size AioHandleShmemSize (void)
 
static Size AioHandleIOVShmemSize (void)
 
static Size AioHandleDataShmemSize (void)
 
static int AioChooseMaxConcurrency (void)
 
Size AioShmemSize (void)
 
void AioShmemInit (void)
 
void pgaio_init_backend (void)
 

Function Documentation

◆ AioBackendShmemSize()

static Size AioBackendShmemSize ( void  )
static

Definition at line 53 of file aio_init.c.

54{
55 return mul_size(AioProcs(), sizeof(PgAioBackend));
56}
static uint32 AioProcs(void)
Definition: aio_init.c:42
Size mul_size(Size s1, Size s2)
Definition: shmem.c:505

References AioProcs(), and mul_size().

Referenced by AioShmemInit(), and AioShmemSize().

◆ AioChooseMaxConcurrency()

static int AioChooseMaxConcurrency ( void  )
static

Definition at line 101 of file aio_init.c.

102{
103 uint32 max_backends;
104 int max_proportional_pins;
105
106 /* Similar logic to LimitAdditionalPins() */
107 max_backends = MaxBackends + NUM_AUXILIARY_PROCS;
108 max_proportional_pins = NBuffers / max_backends;
109
110 max_proportional_pins = Max(max_proportional_pins, 1);
111
112 /* apply upper limit */
113 return Min(max_proportional_pins, 64);
114}
#define Min(x, y)
Definition: c.h:975
#define Max(x, y)
Definition: c.h:969
uint32_t uint32
Definition: c.h:502
int NBuffers
Definition: globals.c:141
int MaxBackends
Definition: globals.c:145
#define NUM_AUXILIARY_PROCS
Definition: proc.h:447

References Max, MaxBackends, Min, NBuffers, and NUM_AUXILIARY_PROCS.

Referenced by AioShmemSize().

◆ AioCtlShmemSize()

static Size AioCtlShmemSize ( void  )
static

Definition at line 31 of file aio_init.c.

32{
33 Size sz;
34
35 /* pgaio_ctl itself */
36 sz = offsetof(PgAioCtl, io_handles);
37
38 return sz;
39}
size_t Size
Definition: c.h:576

Referenced by AioShmemInit(), and AioShmemSize().

◆ AioHandleDataShmemSize()

static Size AioHandleDataShmemSize ( void  )
static

Definition at line 83 of file aio_init.c.

84{
85 /* each buffer referenced by an iovec can have associated data */
86 return mul_size(sizeof(uint64),
89}
int io_max_concurrency
Definition: aio.c:73
int io_max_combine_limit
Definition: bufmgr.c:168
uint64_t uint64
Definition: c.h:503

References AioProcs(), io_max_combine_limit, io_max_concurrency, and mul_size().

Referenced by AioShmemInit(), and AioShmemSize().

◆ AioHandleIOVShmemSize()

static Size AioHandleIOVShmemSize ( void  )
static

Definition at line 74 of file aio_init.c.

75{
76 /* each IO handle can have up to io_max_combine_limit iovec objects */
77 return mul_size(sizeof(struct iovec),
80}

References AioProcs(), io_max_combine_limit, io_max_concurrency, and mul_size().

Referenced by AioShmemInit(), and AioShmemSize().

◆ AioHandleShmemSize()

static Size AioHandleShmemSize ( void  )
static

Definition at line 59 of file aio_init.c.

60{
61 Size sz;
62
63 /* verify AioChooseMaxConcurrency() did its thing */
65
66 /* io handles */
67 sz = mul_size(AioProcs(),
69
70 return sz;
71}
Assert(PointerIsAligned(start, uint64))

References AioProcs(), Assert(), io_max_concurrency, and mul_size().

Referenced by AioShmemInit(), and AioShmemSize().

◆ AioProcs()

static uint32 AioProcs ( void  )
static

Definition at line 42 of file aio_init.c.

43{
44 /*
45 * While AIO workers don't need their own AIO context, we can't currently
46 * guarantee nothing gets assigned to the a ProcNumber for an IO worker if
47 * we just subtracted MAX_IO_WORKERS.
48 */
50}

References MaxBackends, and NUM_AUXILIARY_PROCS.

Referenced by AioBackendShmemSize(), AioHandleDataShmemSize(), AioHandleIOVShmemSize(), AioHandleShmemSize(), AioShmemInit(), and pgaio_init_backend().

◆ AioShmemInit()

void AioShmemInit ( void  )

Definition at line 153 of file aio_init.c.

154{
155 bool found;
156 uint32 io_handle_off = 0;
157 uint32 iovec_off = 0;
158 uint32 per_backend_iovecs = io_max_concurrency * io_max_combine_limit;
159
160 pgaio_ctl = (PgAioCtl *)
161 ShmemInitStruct("AioCtl", AioCtlShmemSize(), &found);
162
163 if (found)
164 goto out;
165
166 memset(pgaio_ctl, 0, AioCtlShmemSize());
167
169 pgaio_ctl->iovec_count = AioProcs() * per_backend_iovecs;
170
172 ShmemInitStruct("AioBackend", AioBackendShmemSize(), &found);
173
175 ShmemInitStruct("AioHandle", AioHandleShmemSize(), &found);
176
177 pgaio_ctl->iovecs = (struct iovec *)
178 ShmemInitStruct("AioHandleIOV", AioHandleIOVShmemSize(), &found);
180 ShmemInitStruct("AioHandleData", AioHandleDataShmemSize(), &found);
181
182 for (int procno = 0; procno < AioProcs(); procno++)
183 {
184 PgAioBackend *bs = &pgaio_ctl->backend_state[procno];
185
186 bs->io_handle_off = io_handle_off;
187 io_handle_off += io_max_concurrency;
188
189 dclist_init(&bs->idle_ios);
190 memset(bs->staged_ios, 0, sizeof(PgAioHandle *) * PGAIO_SUBMIT_BATCH_SIZE);
192
193 /* initialize per-backend IOs */
194 for (int i = 0; i < io_max_concurrency; i++)
195 {
197
198 ioh->generation = 1;
199 ioh->owner_procno = procno;
200 ioh->iovec_off = iovec_off;
201 ioh->handle_data_len = 0;
202 ioh->report_return = NULL;
203 ioh->resowner = NULL;
204 ioh->num_callbacks = 0;
206 ioh->flags = 0;
207
209
210 dclist_push_tail(&bs->idle_ios, &ioh->node);
211 iovec_off += io_max_combine_limit;
212 }
213 }
214
215out:
216 /* Initialize IO method specific resources. */
219}
PgAioCtl * pgaio_ctl
Definition: aio.c:76
const IoMethodOps * pgaio_method_ops
Definition: aio.c:88
static Size AioHandleIOVShmemSize(void)
Definition: aio_init.c:74
static Size AioHandleDataShmemSize(void)
Definition: aio_init.c:83
static Size AioHandleShmemSize(void)
Definition: aio_init.c:59
static Size AioBackendShmemSize(void)
Definition: aio_init.c:53
static Size AioCtlShmemSize(void)
Definition: aio_init.c:31
#define PGAIO_SUBMIT_BATCH_SIZE
Definition: aio_internal.h:28
@ PGAIO_RS_UNKNOWN
Definition: aio_types.h:76
void ConditionVariableInit(ConditionVariable *cv)
static void dclist_push_tail(dclist_head *head, dlist_node *node)
Definition: ilist.h:709
static void dclist_init(dclist_head *head)
Definition: ilist.h:671
int i
Definition: isn.c:74
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382
void(* shmem_init)(bool first_time)
Definition: aio_internal.h:263
uint32 io_handle_off
Definition: aio_internal.h:183
dclist_head in_flight_ios
Definition: aio_internal.h:214
dclist_head idle_ios
Definition: aio_internal.h:186
PgAioHandle * staged_ios[PGAIO_SUBMIT_BATCH_SIZE]
Definition: aio_internal.h:204
uint32 iovec_count
Definition: aio_internal.h:228
struct iovec * iovecs
Definition: aio_internal.h:229
PgAioHandle * io_handles
Definition: aio_internal.h:241
uint32 io_handle_count
Definition: aio_internal.h:240
uint64 * handle_data
Definition: aio_internal.h:238
PgAioBackend * backend_state
Definition: aio_internal.h:221
struct ResourceOwnerData * resowner
Definition: aio_internal.h:137
int32 owner_procno
Definition: aio_internal.h:120
PgAioResult distilled_result
Definition: aio_internal.h:151
dlist_node node
Definition: aio_internal.h:135
uint8 handle_data_len
Definition: aio_internal.h:117
PgAioReturn * report_return
Definition: aio_internal.h:166
uint32 iovec_off
Definition: aio_internal.h:159
uint64 generation
Definition: aio_internal.h:141
uint8 num_callbacks
Definition: aio_internal.h:105
ConditionVariable cv
Definition: aio_internal.h:148
uint32 status
Definition: aio_types.h:95

References AioBackendShmemSize(), AioCtlShmemSize(), AioHandleDataShmemSize(), AioHandleIOVShmemSize(), AioHandleShmemSize(), AioProcs(), PgAioCtl::backend_state, ConditionVariableInit(), PgAioHandle::cv, dclist_init(), dclist_push_tail(), PgAioHandle::distilled_result, PgAioHandle::flags, PgAioHandle::generation, PgAioCtl::handle_data, PgAioHandle::handle_data_len, i, PgAioBackend::idle_ios, PgAioBackend::in_flight_ios, PgAioCtl::io_handle_count, PgAioBackend::io_handle_off, PgAioCtl::io_handles, io_max_combine_limit, io_max_concurrency, PgAioCtl::iovec_count, PgAioHandle::iovec_off, PgAioCtl::iovecs, PgAioHandle::node, PgAioHandle::num_callbacks, PgAioHandle::owner_procno, pgaio_ctl, pgaio_method_ops, PGAIO_RS_UNKNOWN, PGAIO_SUBMIT_BATCH_SIZE, PgAioHandle::report_return, PgAioHandle::resowner, IoMethodOps::shmem_init, ShmemInitStruct(), PgAioBackend::staged_ios, and PgAioResult::status.

Referenced by CreateOrAttachShmemStructs().

◆ AioShmemSize()

Size AioShmemSize ( void  )

Definition at line 117 of file aio_init.c.

118{
119 Size sz = 0;
120
121 /*
122 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
123 * However, if the DBA explicitly set io_max_concurrency = -1 in the
124 * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that and
125 * we must force the matter with PGC_S_OVERRIDE.
126 */
127 if (io_max_concurrency == -1)
128 {
129 char buf[32];
130
131 snprintf(buf, sizeof(buf), "%d", AioChooseMaxConcurrency());
132 SetConfigOption("io_max_concurrency", buf, PGC_POSTMASTER,
134 if (io_max_concurrency == -1) /* failed to apply it? */
135 SetConfigOption("io_max_concurrency", buf, PGC_POSTMASTER,
137 }
138
139 sz = add_size(sz, AioCtlShmemSize());
140 sz = add_size(sz, AioBackendShmemSize());
141 sz = add_size(sz, AioHandleShmemSize());
144
145 /* Reserve space for method specific resources. */
148
149 return sz;
150}
static int AioChooseMaxConcurrency(void)
Definition: aio_init.c:101
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:114
@ PGC_S_OVERRIDE
Definition: guc.h:123
@ PGC_POSTMASTER
Definition: guc.h:74
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:239
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
size_t(* shmem_size)(void)
Definition: aio_internal.h:257

References add_size(), AioBackendShmemSize(), AioChooseMaxConcurrency(), AioCtlShmemSize(), AioHandleDataShmemSize(), AioHandleIOVShmemSize(), AioHandleShmemSize(), buf, io_max_concurrency, pgaio_method_ops, PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT, PGC_S_OVERRIDE, SetConfigOption(), IoMethodOps::shmem_size, and snprintf.

Referenced by CalculateShmemSize().

◆ pgaio_init_backend()

void pgaio_init_backend ( void  )

Definition at line 222 of file aio_init.c.

223{
224 /* shouldn't be initialized twice */
226
228 return;
229
230 if (MyProc == NULL || MyProcNumber >= AioProcs())
231 elog(ERROR, "aio requires a normal PGPROC");
232
234
237
239}
PgAioBackend * pgaio_my_backend
Definition: aio.c:79
void pgaio_shutdown(int code, Datum arg)
Definition: aio.c:1127
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
ProcNumber MyProcNumber
Definition: globals.c:89
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
@ B_IO_WORKER
Definition: miscadmin.h:363
BackendType MyBackendType
Definition: miscinit.c:64
PGPROC * MyProc
Definition: proc.c:66
void(* init_backend)(void)
Definition: aio_internal.h:268

References AioProcs(), Assert(), B_IO_WORKER, PgAioCtl::backend_state, before_shmem_exit(), elog, ERROR, IoMethodOps::init_backend, MyBackendType, MyProc, MyProcNumber, pgaio_ctl, pgaio_method_ops, pgaio_my_backend, and pgaio_shutdown().

Referenced by BaseInit().