1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*-------------------------------------------------------------------------
*
* aio_funcs.c
* AIO - SQL interface for AIO
*
*
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/storage/aio/aio_funcs.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "nodes/execnodes.h"
#include "port/atomics.h"
#include "storage/aio_internal.h"
#include "storage/lock.h"
#include "storage/proc.h"
#include "storage/procnumber.h"
#include "utils/builtins.h"
#include "utils/fmgrprotos.h"
#include "utils/tuplestore.h"
/*
* Byte length of an iovec.
*/
static size_t
iov_byte_length(const struct iovec *iov, int cnt)
{
size_t len = 0;
for (int i = 0; i < cnt; i++)
{
len += iov[i].iov_len;
}
return len;
}
Datum
pg_get_aios(PG_FUNCTION_ARGS)
{
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
InitMaterializedSRF(fcinfo, 0);
#define PG_GET_AIOS_COLS 15
for (uint64 i = 0; i < pgaio_ctl->io_handle_count; i++)
{
PgAioHandle *live_ioh = &pgaio_ctl->io_handles[i];
uint32 ioh_id = pgaio_io_get_id(live_ioh);
Datum values[PG_GET_AIOS_COLS] = {0};
bool nulls[PG_GET_AIOS_COLS] = {0};
ProcNumber owner;
PGPROC *owner_proc;
int32 owner_pid;
PgAioHandleState start_state;
uint64 start_generation;
PgAioHandle ioh_copy;
struct iovec iov_copy[PG_IOV_MAX];
/*
* There is no lock that could prevent the state of the IO to advance
* concurrently - and we don't want to introduce one, as that would
* introduce atomics into a very common path. Instead we
*
* 1) Determine the state + generation of the IO.
*
* 2) Copy the IO to local memory.
*
* 3) Check if state or generation of the IO changed. If the state
* changed, retry, if the generation changed don't display the IO.
*/
/* 1) from above */
start_generation = live_ioh->generation;
/*
* Retry at this point, so we can accept changing states, but not
* changing generations.
*/
retry:
pg_read_barrier();
start_state = live_ioh->state;
if (start_state == PGAIO_HS_IDLE)
continue;
/* 2) from above */
memcpy(&ioh_copy, live_ioh, sizeof(PgAioHandle));
/*
* Safe to copy even if no iovec is used - we always reserve the
* required space.
*/
memcpy(&iov_copy, &pgaio_ctl->iovecs[ioh_copy.iovec_off],
PG_IOV_MAX * sizeof(struct iovec));
/*
* Copy information about owner before 3) below, if the process exited
* it'd have to wait for the IO to finish first, which we would detect
* in 3).
*/
owner = ioh_copy.owner_procno;
owner_proc = GetPGProcByNumber(owner);
owner_pid = owner_proc->pid;
/* 3) from above */
pg_read_barrier();
/*
* The IO completed and a new one was started with the same ID. Don't
* display it - it really started after this function was called.
* There be a risk of a livelock if we just retried endlessly, if IOs
* complete very quickly.
*/
if (live_ioh->generation != start_generation)
continue;
/*
* The IO's state changed while we were "rendering" it. Just start
* from scratch. There's no risk of a livelock here, as an IO has a
* limited sets of states it can be in, and state changes go only in a
* single direction.
*/
if (live_ioh->state != start_state)
goto retry;
/*
* Now that we have copied the IO into local memory and checked that
* it's still in the same state, we are not allowed to access "live"
* memory anymore. To make it slightly easier to catch such cases, set
* the "live" pointers to NULL.
*/
live_ioh = NULL;
owner_proc = NULL;
/* column: owning pid */
if (owner_pid != 0)
values[0] = Int32GetDatum(owner_pid);
else
nulls[0] = false;
/* column: IO's id */
values[1] = ioh_id;
/* column: IO's generation */
values[2] = Int64GetDatum(start_generation);
/* column: IO's state */
values[3] = CStringGetTextDatum(pgaio_io_get_state_name(&ioh_copy));
/*
* If the IO is in PGAIO_HS_HANDED_OUT state, none of the following
* fields are valid yet (or are in the process of being set).
* Therefore we don't want to display any other columns.
*/
if (start_state == PGAIO_HS_HANDED_OUT)
{
memset(nulls + 4, 1, (lengthof(nulls) - 4) * sizeof(bool));
goto display;
}
/* column: IO's operation */
values[4] = CStringGetTextDatum(pgaio_io_get_op_name(&ioh_copy));
/* columns: details about the IO's operation (offset, length) */
switch (ioh_copy.op)
{
case PGAIO_OP_INVALID:
nulls[5] = true;
nulls[6] = true;
break;
case PGAIO_OP_READV:
values[5] = Int64GetDatum(ioh_copy.op_data.read.offset);
values[6] =
Int64GetDatum(iov_byte_length(iov_copy, ioh_copy.op_data.read.iov_length));
break;
case PGAIO_OP_WRITEV:
values[5] = Int64GetDatum(ioh_copy.op_data.write.offset);
values[6] =
Int64GetDatum(iov_byte_length(iov_copy, ioh_copy.op_data.write.iov_length));
break;
}
/* column: IO's target */
values[7] = CStringGetTextDatum(pgaio_io_get_target_name(&ioh_copy));
/* column: length of IO's data array */
values[8] = Int16GetDatum(ioh_copy.handle_data_len);
/* column: raw result (i.e. some form of syscall return value) */
if (start_state == PGAIO_HS_COMPLETED_IO
|| start_state == PGAIO_HS_COMPLETED_SHARED
|| start_state == PGAIO_HS_COMPLETED_LOCAL)
values[9] = Int32GetDatum(ioh_copy.result);
else
nulls[9] = true;
/*
* column: result in the higher level representation (unknown if not
* finished)
*/
values[10] =
CStringGetTextDatum(pgaio_result_status_string(ioh_copy.distilled_result.status));
/* column: target description */
values[11] = CStringGetTextDatum(pgaio_io_get_target_description(&ioh_copy));
/* columns: one for each flag */
values[12] = BoolGetDatum(ioh_copy.flags & PGAIO_HF_SYNCHRONOUS);
values[13] = BoolGetDatum(ioh_copy.flags & PGAIO_HF_REFERENCES_LOCAL);
values[14] = BoolGetDatum(ioh_copy.flags & PGAIO_HF_BUFFERED);
display:
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
}
return (Datum) 0;
}
|