PostgreSQL Source Code git master
Loading...
Searching...
No Matches
instrument.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "executor/instrument.h"
Include dependency graph for instrument.c:

Go to the source code of this file.

Functions

static void BufferUsageAdd (BufferUsage *dst, const BufferUsage *add)
 
static void WalUsageAdd (WalUsage *dst, WalUsage *add)
 
InstrumentationInstrAlloc (int n, int instrument_options, bool async_mode)
 
void InstrInit (Instrumentation *instr, int instrument_options)
 
void InstrStartNode (Instrumentation *instr)
 
void InstrStopNode (Instrumentation *instr, double nTuples)
 
void InstrUpdateTupleCount (Instrumentation *instr, double nTuples)
 
void InstrEndLoop (Instrumentation *instr)
 
void InstrAggNode (Instrumentation *dst, Instrumentation *add)
 
void InstrStartParallelQuery (void)
 
void InstrEndParallelQuery (BufferUsage *bufusage, WalUsage *walusage)
 
void InstrAccumParallelQuery (BufferUsage *bufusage, WalUsage *walusage)
 
void BufferUsageAccumDiff (BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
 
void WalUsageAccumDiff (WalUsage *dst, const WalUsage *add, const WalUsage *sub)
 

Variables

BufferUsage pgBufferUsage
 
static BufferUsage save_pgBufferUsage
 
WalUsage pgWalUsage
 
static WalUsage save_pgWalUsage
 

Function Documentation

◆ BufferUsageAccumDiff()

void BufferUsageAccumDiff ( BufferUsage dst,
const BufferUsage add,
const BufferUsage sub 
)

Definition at line 249 of file instrument.c.

252{
253 dst->shared_blks_hit += add->shared_blks_hit - sub->shared_blks_hit;
254 dst->shared_blks_read += add->shared_blks_read - sub->shared_blks_read;
255 dst->shared_blks_dirtied += add->shared_blks_dirtied - sub->shared_blks_dirtied;
256 dst->shared_blks_written += add->shared_blks_written - sub->shared_blks_written;
257 dst->local_blks_hit += add->local_blks_hit - sub->local_blks_hit;
258 dst->local_blks_read += add->local_blks_read - sub->local_blks_read;
259 dst->local_blks_dirtied += add->local_blks_dirtied - sub->local_blks_dirtied;
260 dst->local_blks_written += add->local_blks_written - sub->local_blks_written;
261 dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
262 dst->temp_blks_written += add->temp_blks_written - sub->temp_blks_written;
263 INSTR_TIME_ACCUM_DIFF(dst->shared_blk_read_time,
264 add->shared_blk_read_time, sub->shared_blk_read_time);
265 INSTR_TIME_ACCUM_DIFF(dst->shared_blk_write_time,
266 add->shared_blk_write_time, sub->shared_blk_write_time);
267 INSTR_TIME_ACCUM_DIFF(dst->local_blk_read_time,
268 add->local_blk_read_time, sub->local_blk_read_time);
269 INSTR_TIME_ACCUM_DIFF(dst->local_blk_write_time,
270 add->local_blk_write_time, sub->local_blk_write_time);
271 INSTR_TIME_ACCUM_DIFF(dst->temp_blk_read_time,
272 add->temp_blk_read_time, sub->temp_blk_read_time);
273 INSTR_TIME_ACCUM_DIFF(dst->temp_blk_write_time,
274 add->temp_blk_write_time, sub->temp_blk_write_time);
275}
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition instr_time.h:180
static int fb(int x)
instr_time local_blk_read_time
Definition instrument.h:38
int64 shared_blks_dirtied
Definition instrument.h:28
int64 local_blks_hit
Definition instrument.h:30
instr_time temp_blk_write_time
Definition instrument.h:41
instr_time shared_blk_read_time
Definition instrument.h:36
instr_time shared_blk_write_time
Definition instrument.h:37
int64 local_blks_written
Definition instrument.h:33
instr_time temp_blk_read_time
Definition instrument.h:40
instr_time local_blk_write_time
Definition instrument.h:39
int64 temp_blks_read
Definition instrument.h:34
int64 shared_blks_read
Definition instrument.h:27
int64 shared_blks_written
Definition instrument.h:29
int64 temp_blks_written
Definition instrument.h:35
int64 local_blks_read
Definition instrument.h:31
int64 local_blks_dirtied
Definition instrument.h:32
int64 shared_blks_hit
Definition instrument.h:26

References fb(), INSTR_TIME_ACCUM_DIFF, BufferUsage::local_blk_read_time, BufferUsage::local_blk_write_time, BufferUsage::local_blks_dirtied, BufferUsage::local_blks_hit, BufferUsage::local_blks_read, BufferUsage::local_blks_written, BufferUsage::shared_blk_read_time, BufferUsage::shared_blk_write_time, BufferUsage::shared_blks_dirtied, BufferUsage::shared_blks_hit, BufferUsage::shared_blks_read, BufferUsage::shared_blks_written, BufferUsage::temp_blk_read_time, BufferUsage::temp_blk_write_time, BufferUsage::temp_blks_read, and BufferUsage::temp_blks_written.

Referenced by do_analyze_rel(), ExplainExecuteQuery(), heap_vacuum_rel(), InstrEndParallelQuery(), InstrStopNode(), pgss_planner(), pgss_ProcessUtility(), serializeAnalyzeReceive(), and standard_ExplainOneQuery().

◆ BufferUsageAdd()

static void BufferUsageAdd ( BufferUsage dst,
const BufferUsage add 
)
static

Definition at line 227 of file instrument.c.

228{
229 dst->shared_blks_hit += add->shared_blks_hit;
230 dst->shared_blks_read += add->shared_blks_read;
231 dst->shared_blks_dirtied += add->shared_blks_dirtied;
232 dst->shared_blks_written += add->shared_blks_written;
233 dst->local_blks_hit += add->local_blks_hit;
234 dst->local_blks_read += add->local_blks_read;
235 dst->local_blks_dirtied += add->local_blks_dirtied;
236 dst->local_blks_written += add->local_blks_written;
237 dst->temp_blks_read += add->temp_blks_read;
238 dst->temp_blks_written += add->temp_blks_written;
239 INSTR_TIME_ADD(dst->shared_blk_read_time, add->shared_blk_read_time);
240 INSTR_TIME_ADD(dst->shared_blk_write_time, add->shared_blk_write_time);
241 INSTR_TIME_ADD(dst->local_blk_read_time, add->local_blk_read_time);
242 INSTR_TIME_ADD(dst->local_blk_write_time, add->local_blk_write_time);
243 INSTR_TIME_ADD(dst->temp_blk_read_time, add->temp_blk_read_time);
244 INSTR_TIME_ADD(dst->temp_blk_write_time, add->temp_blk_write_time);
245}
#define INSTR_TIME_ADD(x, y)
Definition instr_time.h:174

References fb(), and INSTR_TIME_ADD.

Referenced by InstrAccumParallelQuery(), and InstrAggNode().

◆ InstrAccumParallelQuery()

void InstrAccumParallelQuery ( BufferUsage bufusage,
WalUsage walusage 
)

Definition at line 219 of file instrument.c.

220{
221 BufferUsageAdd(&pgBufferUsage, bufusage);
222 WalUsageAdd(&pgWalUsage, walusage);
223}
static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
Definition instrument.c:227
static void WalUsageAdd(WalUsage *dst, WalUsage *add)
Definition instrument.c:279
WalUsage pgWalUsage
Definition instrument.c:22
BufferUsage pgBufferUsage
Definition instrument.c:20

References BufferUsageAdd(), pgBufferUsage, pgWalUsage, and WalUsageAdd().

Referenced by _brin_end_parallel(), _bt_end_parallel(), _gin_end_parallel(), ExecParallelFinish(), and parallel_vacuum_process_all_indexes().

◆ InstrAggNode()

void InstrAggNode ( Instrumentation dst,
Instrumentation add 
)

Definition at line 169 of file instrument.c.

170{
171 if (!dst->running && add->running)
172 {
173 dst->running = true;
174 dst->firsttuple = add->firsttuple;
175 }
176 else if (dst->running && add->running &&
177 INSTR_TIME_GT(dst->firsttuple, add->firsttuple))
178 dst->firsttuple = add->firsttuple;
179
180 INSTR_TIME_ADD(dst->counter, add->counter);
181
182 dst->tuplecount += add->tuplecount;
183 INSTR_TIME_ADD(dst->startup, add->startup);
184 INSTR_TIME_ADD(dst->total, add->total);
185 dst->ntuples += add->ntuples;
186 dst->ntuples2 += add->ntuples2;
187 dst->nloops += add->nloops;
188 dst->nfiltered1 += add->nfiltered1;
189 dst->nfiltered2 += add->nfiltered2;
190
191 /* Add delta of buffer usage since entry to node's totals */
192 if (dst->need_bufusage)
193 BufferUsageAdd(&dst->bufusage, &add->bufusage);
194
195 if (dst->need_walusage)
196 WalUsageAdd(&dst->walusage, &add->walusage);
197}
#define INSTR_TIME_GT(x, y)
Definition instr_time.h:183

References BufferUsageAdd(), fb(), INSTR_TIME_ADD, INSTR_TIME_GT, and WalUsageAdd().

Referenced by ExecParallelReportInstrumentation(), and ExecParallelRetrieveInstrumentation().

◆ InstrAlloc()

Instrumentation * InstrAlloc ( int  n,
int  instrument_options,
bool  async_mode 
)

Definition at line 31 of file instrument.c.

32{
33 Instrumentation *instr;
34
35 /* initialize all fields to zeroes, then modify as needed */
36 instr = palloc0(n * sizeof(Instrumentation));
37 if (instrument_options & (INSTRUMENT_BUFFERS | INSTRUMENT_TIMER | INSTRUMENT_WAL))
38 {
39 bool need_buffers = (instrument_options & INSTRUMENT_BUFFERS) != 0;
40 bool need_wal = (instrument_options & INSTRUMENT_WAL) != 0;
41 bool need_timer = (instrument_options & INSTRUMENT_TIMER) != 0;
42 int i;
43
44 for (i = 0; i < n; i++)
45 {
47 instr[i].need_walusage = need_wal;
48 instr[i].need_timer = need_timer;
49 instr[i].async_mode = async_mode;
50 }
51 }
52
53 return instr;
54}
@ INSTRUMENT_TIMER
Definition instrument.h:63
@ INSTRUMENT_BUFFERS
Definition instrument.h:64
@ INSTRUMENT_WAL
Definition instrument.h:66
int i
Definition isn.c:77
void * palloc0(Size size)
Definition mcxt.c:1417

References Instrumentation::async_mode, fb(), i, INSTRUMENT_BUFFERS, INSTRUMENT_TIMER, INSTRUMENT_WAL, Instrumentation::need_bufusage, Instrumentation::need_timer, Instrumentation::need_walusage, and palloc0().

Referenced by ExecInitNode(), explain_ExecutorStart(), InitResultRelInfo(), and pgss_ExecutorStart().

◆ InstrEndLoop()

void InstrEndLoop ( Instrumentation instr)

Definition at line 144 of file instrument.c.

145{
146 /* Skip if nothing has happened, or already shut down */
147 if (!instr->running)
148 return;
149
150 if (!INSTR_TIME_IS_ZERO(instr->starttime))
151 elog(ERROR, "InstrEndLoop called on running node");
152
153 /* Accumulate per-cycle statistics into totals */
154 INSTR_TIME_ADD(instr->startup, instr->firsttuple);
155 INSTR_TIME_ADD(instr->total, instr->counter);
156 instr->ntuples += instr->tuplecount;
157 instr->nloops += 1;
158
159 /* Reset for next cycle (if any) */
160 instr->running = false;
164 instr->tuplecount = 0;
165}
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define INSTR_TIME_IS_ZERO(t)
Definition instr_time.h:169
#define INSTR_TIME_SET_ZERO(t)
Definition instr_time.h:171
instr_time total
Definition instrument.h:87
instr_time starttime
Definition instrument.h:79
instr_time counter
Definition instrument.h:80
instr_time firsttuple
Definition instrument.h:81
double tuplecount
Definition instrument.h:82
instr_time startup
Definition instrument.h:86

References Instrumentation::counter, elog, ERROR, Instrumentation::firsttuple, INSTR_TIME_ADD, INSTR_TIME_IS_ZERO, INSTR_TIME_SET_ZERO, Instrumentation::nloops, Instrumentation::ntuples, Instrumentation::running, Instrumentation::starttime, Instrumentation::startup, Instrumentation::total, and Instrumentation::tuplecount.

Referenced by ExecParallelReportInstrumentation(), ExecReScan(), explain_ExecutorEnd(), ExplainNode(), pgss_ExecutorEnd(), report_triggers(), and show_modifytable_info().

◆ InstrEndParallelQuery()

void InstrEndParallelQuery ( BufferUsage bufusage,
WalUsage walusage 
)

Definition at line 209 of file instrument.c.

210{
211 memset(bufusage, 0, sizeof(BufferUsage));
213 memset(walusage, 0, sizeof(WalUsage));
215}
static BufferUsage save_pgBufferUsage
Definition instrument.c:21
static WalUsage save_pgWalUsage
Definition instrument.c:23
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition instrument.c:289
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition instrument.c:249

References BufferUsageAccumDiff(), fb(), pgBufferUsage, pgWalUsage, save_pgBufferUsage, save_pgWalUsage, and WalUsageAccumDiff().

Referenced by _brin_parallel_build_main(), _bt_parallel_build_main(), _gin_parallel_build_main(), parallel_vacuum_main(), and ParallelQueryMain().

◆ InstrInit()

void InstrInit ( Instrumentation instr,
int  instrument_options 
)

Definition at line 58 of file instrument.c.

59{
60 memset(instr, 0, sizeof(Instrumentation));
61 instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0;
62 instr->need_walusage = (instrument_options & INSTRUMENT_WAL) != 0;
63 instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0;
64}

References fb(), INSTRUMENT_BUFFERS, INSTRUMENT_TIMER, INSTRUMENT_WAL, Instrumentation::need_bufusage, Instrumentation::need_timer, and Instrumentation::need_walusage.

Referenced by ExecInitParallelPlan().

◆ InstrStartNode()

◆ InstrStartParallelQuery()

◆ InstrStopNode()

void InstrStopNode ( Instrumentation instr,
double  nTuples 
)

Definition at line 88 of file instrument.c.

89{
90 double save_tuplecount = instr->tuplecount;
92
93 /* count the returned tuples */
94 instr->tuplecount += nTuples;
95
96 /* let's update the time only if the timer was requested */
97 if (instr->need_timer)
98 {
99 if (INSTR_TIME_IS_ZERO(instr->starttime))
100 elog(ERROR, "InstrStopNode called without start");
101
104
106 }
107
108 /* Add delta of buffer usage since entry to node's totals */
109 if (instr->need_bufusage)
111 &pgBufferUsage, &instr->bufusage_start);
112
113 if (instr->need_walusage)
115 &pgWalUsage, &instr->walusage_start);
116
117 /* Is this the first tuple of this cycle? */
118 if (!instr->running)
119 {
120 instr->running = true;
121 instr->firsttuple = instr->counter;
122 }
123 else
124 {
125 /*
126 * In async mode, if the plan node hadn't emitted any tuples before,
127 * this might be the first tuple
128 */
129 if (instr->async_mode && save_tuplecount < 1.0)
130 instr->firsttuple = instr->counter;
131 }
132}
WalUsage walusage
Definition instrument.h:94
BufferUsage bufusage
Definition instrument.h:93

References Instrumentation::async_mode, BufferUsageAccumDiff(), Instrumentation::bufusage, Instrumentation::bufusage_start, Instrumentation::counter, elog, ERROR, fb(), Instrumentation::firsttuple, INSTR_TIME_ACCUM_DIFF, INSTR_TIME_IS_ZERO, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, Instrumentation::need_bufusage, Instrumentation::need_timer, Instrumentation::need_walusage, pgBufferUsage, pgWalUsage, Instrumentation::running, Instrumentation::starttime, Instrumentation::tuplecount, Instrumentation::walusage, Instrumentation::walusage_start, and WalUsageAccumDiff().

Referenced by AfterTriggerExecute(), ExecAsyncConfigureWait(), ExecAsyncNotify(), ExecAsyncRequest(), ExecCallTriggerFunc(), ExecProcNodeInstr(), ExecShutdownNode_walker(), MultiExecBitmapAnd(), MultiExecBitmapIndexScan(), MultiExecBitmapOr(), MultiExecHash(), standard_ExecutorFinish(), and standard_ExecutorRun().

◆ InstrUpdateTupleCount()

void InstrUpdateTupleCount ( Instrumentation instr,
double  nTuples 
)

Definition at line 136 of file instrument.c.

137{
138 /* count the returned tuples */
139 instr->tuplecount += nTuples;
140}

References Instrumentation::tuplecount.

Referenced by complete_pending_request(), and ExecMergeMatched().

◆ WalUsageAccumDiff()

void WalUsageAccumDiff ( WalUsage dst,
const WalUsage add,
const WalUsage sub 
)

Definition at line 289 of file instrument.c.

290{
291 dst->wal_bytes += add->wal_bytes - sub->wal_bytes;
292 dst->wal_records += add->wal_records - sub->wal_records;
293 dst->wal_fpi += add->wal_fpi - sub->wal_fpi;
294 dst->wal_fpi_bytes += add->wal_fpi_bytes - sub->wal_fpi_bytes;
295 dst->wal_buffers_full += add->wal_buffers_full - sub->wal_buffers_full;
296}
int64 wal_buffers_full
Definition instrument.h:57
uint64 wal_bytes
Definition instrument.h:55
int64 wal_fpi
Definition instrument.h:54
uint64 wal_fpi_bytes
Definition instrument.h:56
int64 wal_records
Definition instrument.h:53

References fb(), WalUsage::wal_buffers_full, WalUsage::wal_bytes, WalUsage::wal_fpi, WalUsage::wal_fpi_bytes, and WalUsage::wal_records.

Referenced by do_analyze_rel(), heap_vacuum_rel(), InstrEndParallelQuery(), InstrStopNode(), pgss_planner(), pgss_ProcessUtility(), pgstat_flush_backend_entry_wal(), and pgstat_wal_flush_cb().

◆ WalUsageAdd()

static void WalUsageAdd ( WalUsage dst,
WalUsage add 
)
static

Definition at line 279 of file instrument.c.

280{
281 dst->wal_bytes += add->wal_bytes;
282 dst->wal_records += add->wal_records;
283 dst->wal_fpi += add->wal_fpi;
284 dst->wal_fpi_bytes += add->wal_fpi_bytes;
285 dst->wal_buffers_full += add->wal_buffers_full;
286}

References fb().

Referenced by InstrAccumParallelQuery(), and InstrAggNode().

Variable Documentation

◆ pgBufferUsage

◆ pgWalUsage

◆ save_pgBufferUsage

BufferUsage save_pgBufferUsage
static

Definition at line 21 of file instrument.c.

Referenced by InstrEndParallelQuery(), and InstrStartParallelQuery().

◆ save_pgWalUsage

WalUsage save_pgWalUsage
static

Definition at line 23 of file instrument.c.

Referenced by InstrEndParallelQuery(), and InstrStartParallelQuery().