PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 BufferUsageAccumDiff (BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
 
InstrumentationInstrAlloc (int n, int instrument_options)
 
void InstrInit (Instrumentation *instr, int instrument_options)
 
void InstrStartNode (Instrumentation *instr)
 
void InstrStopNode (Instrumentation *instr, double nTuples)
 
void InstrEndLoop (Instrumentation *instr)
 
void InstrAggNode (Instrumentation *dst, Instrumentation *add)
 
void InstrStartParallelQuery (void)
 
void InstrEndParallelQuery (BufferUsage *result)
 
void InstrAccumParallelQuery (BufferUsage *result)
 

Variables

BufferUsage pgBufferUsage
 
static BufferUsage save_pgBufferUsage
 

Function Documentation

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

Definition at line 210 of file instrument.c.

References BufferUsage::blk_read_time, BufferUsage::blk_write_time, INSTR_TIME_ACCUM_DIFF, BufferUsage::local_blks_dirtied, BufferUsage::local_blks_hit, BufferUsage::local_blks_read, BufferUsage::local_blks_written, BufferUsage::shared_blks_dirtied, BufferUsage::shared_blks_hit, BufferUsage::shared_blks_read, BufferUsage::shared_blks_written, BufferUsage::temp_blks_read, and BufferUsage::temp_blks_written.

Referenced by InstrEndParallelQuery(), and InstrStopNode().

213 {
214  dst->shared_blks_hit += add->shared_blks_hit - sub->shared_blks_hit;
218  dst->local_blks_hit += add->local_blks_hit - sub->local_blks_hit;
219  dst->local_blks_read += add->local_blks_read - sub->local_blks_read;
222  dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
225  add->blk_read_time, sub->blk_read_time);
227  add->blk_write_time, sub->blk_write_time);
228 }
long local_blks_hit
Definition: instrument.h:25
long local_blks_dirtied
Definition: instrument.h:27
long local_blks_read
Definition: instrument.h:26
instr_time blk_read_time
Definition: instrument.h:31
long shared_blks_read
Definition: instrument.h:22
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition: instr_time.h:179
long temp_blks_written
Definition: instrument.h:30
long shared_blks_written
Definition: instrument.h:24
long shared_blks_dirtied
Definition: instrument.h:23
long temp_blks_read
Definition: instrument.h:29
instr_time blk_write_time
Definition: instrument.h:32
long shared_blks_hit
Definition: instrument.h:21
long local_blks_written
Definition: instrument.h:28
static void BufferUsageAdd ( BufferUsage dst,
const BufferUsage add 
)
static

Definition at line 192 of file instrument.c.

References BufferUsage::blk_read_time, BufferUsage::blk_write_time, INSTR_TIME_ADD, BufferUsage::local_blks_dirtied, BufferUsage::local_blks_hit, BufferUsage::local_blks_read, BufferUsage::local_blks_written, BufferUsage::shared_blks_dirtied, BufferUsage::shared_blks_hit, BufferUsage::shared_blks_read, BufferUsage::shared_blks_written, BufferUsage::temp_blks_read, and BufferUsage::temp_blks_written.

Referenced by InstrAccumParallelQuery(), and InstrAggNode().

193 {
194  dst->shared_blks_hit += add->shared_blks_hit;
195  dst->shared_blks_read += add->shared_blks_read;
198  dst->local_blks_hit += add->local_blks_hit;
199  dst->local_blks_read += add->local_blks_read;
202  dst->temp_blks_read += add->temp_blks_read;
206 }
long local_blks_hit
Definition: instrument.h:25
long local_blks_dirtied
Definition: instrument.h:27
long local_blks_read
Definition: instrument.h:26
instr_time blk_read_time
Definition: instrument.h:31
long shared_blks_read
Definition: instrument.h:22
long temp_blks_written
Definition: instrument.h:30
long shared_blks_written
Definition: instrument.h:24
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:155
long shared_blks_dirtied
Definition: instrument.h:23
long temp_blks_read
Definition: instrument.h:29
instr_time blk_write_time
Definition: instrument.h:32
long shared_blks_hit
Definition: instrument.h:21
long local_blks_written
Definition: instrument.h:28
void InstrAccumParallelQuery ( BufferUsage result)

Definition at line 185 of file instrument.c.

References BufferUsageAdd().

Referenced by ExecParallelFinish().

186 {
187  BufferUsageAdd(&pgBufferUsage, result);
188 }
static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
Definition: instrument.c:192
BufferUsage pgBufferUsage
Definition: instrument.c:20
void InstrAggNode ( Instrumentation dst,
Instrumentation add 
)

Definition at line 143 of file instrument.c.

References BufferUsageAdd(), Instrumentation::bufusage, Instrumentation::counter, Instrumentation::firsttuple, INSTR_TIME_ADD, Instrumentation::need_bufusage, Instrumentation::nfiltered1, Instrumentation::nfiltered2, Instrumentation::nloops, Instrumentation::ntuples, Instrumentation::running, Instrumentation::startup, Instrumentation::total, and Instrumentation::tuplecount.

Referenced by ExecParallelReportInstrumentation(), and ExecParallelRetrieveInstrumentation().

144 {
145  if (!dst->running && add->running)
146  {
147  dst->running = true;
148  dst->firsttuple = add->firsttuple;
149  }
150  else if (dst->running && add->running && dst->firsttuple > add->firsttuple)
151  dst->firsttuple = add->firsttuple;
152 
153  INSTR_TIME_ADD(dst->counter, add->counter);
154 
155  dst->tuplecount += add->tuplecount;
156  dst->startup += add->startup;
157  dst->total += add->total;
158  dst->ntuples += add->ntuples;
159  dst->nloops += add->nloops;
160  dst->nfiltered1 += add->nfiltered1;
161  dst->nfiltered2 += add->nfiltered2;
162 
163  /* Add delta of buffer usage since entry to node's totals */
164  if (dst->need_bufusage)
165  BufferUsageAdd(&dst->bufusage, &add->bufusage);
166 }
double nfiltered1
Definition: instrument.h:61
bool need_bufusage
Definition: instrument.h:48
double startup
Definition: instrument.h:57
double firsttuple
Definition: instrument.h:53
double nfiltered2
Definition: instrument.h:62
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:155
double ntuples
Definition: instrument.h:59
double tuplecount
Definition: instrument.h:54
BufferUsage bufusage
Definition: instrument.h:63
instr_time counter
Definition: instrument.h:52
static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
Definition: instrument.c:192
Instrumentation* InstrAlloc ( int  n,
int  instrument_options 
)

Definition at line 30 of file instrument.c.

References i, INSTRUMENT_BUFFERS, INSTRUMENT_TIMER, Instrumentation::need_bufusage, Instrumentation::need_timer, and palloc0().

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

31 {
32  Instrumentation *instr;
33 
34  /* initialize all fields to zeroes, then modify as needed */
35  instr = palloc0(n * sizeof(Instrumentation));
36  if (instrument_options & (INSTRUMENT_BUFFERS | INSTRUMENT_TIMER))
37  {
38  bool need_buffers = (instrument_options & INSTRUMENT_BUFFERS) != 0;
39  bool need_timer = (instrument_options & INSTRUMENT_TIMER) != 0;
40  int i;
41 
42  for (i = 0; i < n; i++)
43  {
44  instr[i].need_bufusage = need_buffers;
45  instr[i].need_timer = need_timer;
46  }
47  }
48 
49  return instr;
50 }
bool need_bufusage
Definition: instrument.h:48
void * palloc0(Size size)
Definition: mcxt.c:920
int i
void InstrEndLoop ( Instrumentation instr)

Definition at line 114 of file instrument.c.

References Instrumentation::counter, elog, ERROR, Instrumentation::firsttuple, INSTR_TIME_GET_DOUBLE, 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().

115 {
116  double totaltime;
117 
118  /* Skip if nothing has happened, or already shut down */
119  if (!instr->running)
120  return;
121 
122  if (!INSTR_TIME_IS_ZERO(instr->starttime))
123  elog(ERROR, "InstrEndLoop called on running node");
124 
125  /* Accumulate per-cycle statistics into totals */
126  totaltime = INSTR_TIME_GET_DOUBLE(instr->counter);
127 
128  instr->startup += instr->firsttuple;
129  instr->total += totaltime;
130  instr->ntuples += instr->tuplecount;
131  instr->nloops += 1;
132 
133  /* Reset for next cycle (if any) */
134  instr->running = false;
137  instr->firsttuple = 0;
138  instr->tuplecount = 0;
139 }
double startup
Definition: instrument.h:57
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:151
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:196
#define INSTR_TIME_IS_ZERO(t)
Definition: instr_time.h:149
#define ERROR
Definition: elog.h:43
double firsttuple
Definition: instrument.h:53
instr_time starttime
Definition: instrument.h:51
double ntuples
Definition: instrument.h:59
double tuplecount
Definition: instrument.h:54
instr_time counter
Definition: instrument.h:52
#define elog
Definition: elog.h:219
void InstrEndParallelQuery ( BufferUsage result)

Definition at line 177 of file instrument.c.

References BufferUsageAccumDiff().

Referenced by ParallelQueryMain().

178 {
179  memset(result, 0, sizeof(BufferUsage));
181 }
static BufferUsage save_pgBufferUsage
Definition: instrument.c:21
static void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:210
BufferUsage pgBufferUsage
Definition: instrument.c:20
void InstrInit ( Instrumentation instr,
int  instrument_options 
)

Definition at line 54 of file instrument.c.

References INSTRUMENT_BUFFERS, INSTRUMENT_TIMER, Instrumentation::need_bufusage, and Instrumentation::need_timer.

Referenced by ExecInitParallelPlan().

55 {
56  memset(instr, 0, sizeof(Instrumentation));
57  instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0;
58  instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0;
59 }
bool need_bufusage
Definition: instrument.h:48
void InstrStartNode ( Instrumentation instr)

Definition at line 63 of file instrument.c.

References Instrumentation::bufusage_start, elog, ERROR, INSTR_TIME_IS_ZERO, INSTR_TIME_SET_CURRENT, Instrumentation::need_bufusage, Instrumentation::need_timer, pgBufferUsage, and Instrumentation::starttime.

Referenced by AfterTriggerExecute(), ExecCallTriggerFunc(), ExecProcNode(), MultiExecBitmapAnd(), MultiExecBitmapIndexScan(), MultiExecBitmapOr(), MultiExecHash(), standard_ExecutorFinish(), and standard_ExecutorRun().

64 {
65  if (instr->need_timer)
66  {
67  if (INSTR_TIME_IS_ZERO(instr->starttime))
69  else
70  elog(ERROR, "InstrStartNode called twice in a row");
71  }
72 
73  /* save buffer usage totals at node entry, if needed */
74  if (instr->need_bufusage)
76 }
bool need_bufusage
Definition: instrument.h:48
#define INSTR_TIME_IS_ZERO(t)
Definition: instr_time.h:149
BufferUsage bufusage_start
Definition: instrument.h:55
#define ERROR
Definition: elog.h:43
instr_time starttime
Definition: instrument.h:51
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
#define elog
Definition: elog.h:219
BufferUsage pgBufferUsage
Definition: instrument.c:20
void InstrStartParallelQuery ( void  )

Definition at line 170 of file instrument.c.

References pgBufferUsage.

Referenced by ParallelQueryMain().

171 {
173 }
static BufferUsage save_pgBufferUsage
Definition: instrument.c:21
BufferUsage pgBufferUsage
Definition: instrument.c:20
void InstrStopNode ( Instrumentation instr,
double  nTuples 
)

Definition at line 80 of file instrument.c.

References BufferUsageAccumDiff(), Instrumentation::bufusage, Instrumentation::bufusage_start, Instrumentation::counter, elog, ERROR, Instrumentation::firsttuple, INSTR_TIME_ACCUM_DIFF, INSTR_TIME_GET_DOUBLE, INSTR_TIME_IS_ZERO, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, Instrumentation::need_bufusage, Instrumentation::need_timer, Instrumentation::running, Instrumentation::starttime, and Instrumentation::tuplecount.

Referenced by AfterTriggerExecute(), ExecCallTriggerFunc(), ExecProcNode(), MultiExecBitmapAnd(), MultiExecBitmapIndexScan(), MultiExecBitmapOr(), MultiExecHash(), standard_ExecutorFinish(), and standard_ExecutorRun().

81 {
82  instr_time endtime;
83 
84  /* count the returned tuples */
85  instr->tuplecount += nTuples;
86 
87  /* let's update the time only if the timer was requested */
88  if (instr->need_timer)
89  {
90  if (INSTR_TIME_IS_ZERO(instr->starttime))
91  elog(ERROR, "InstrStopNode called without start");
92 
93  INSTR_TIME_SET_CURRENT(endtime);
94  INSTR_TIME_ACCUM_DIFF(instr->counter, endtime, instr->starttime);
95 
97  }
98 
99  /* Add delta of buffer usage since entry to node's totals */
100  if (instr->need_bufusage)
102  &pgBufferUsage, &instr->bufusage_start);
103 
104  /* Is this the first tuple of this cycle? */
105  if (!instr->running)
106  {
107  instr->running = true;
108  instr->firsttuple = INSTR_TIME_GET_DOUBLE(instr->counter);
109  }
110 }
bool need_bufusage
Definition: instrument.h:48
struct timeval instr_time
Definition: instr_time.h:147
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition: instr_time.h:179
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:151
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:196
#define INSTR_TIME_IS_ZERO(t)
Definition: instr_time.h:149
BufferUsage bufusage_start
Definition: instrument.h:55
#define ERROR
Definition: elog.h:43
double firsttuple
Definition: instrument.h:53
instr_time starttime
Definition: instrument.h:51
static void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:210
double tuplecount
Definition: instrument.h:54
BufferUsage bufusage
Definition: instrument.h:63
instr_time counter
Definition: instrument.h:52
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
#define elog
Definition: elog.h:219
BufferUsage pgBufferUsage
Definition: instrument.c:20

Variable Documentation

BufferUsage save_pgBufferUsage
static

Definition at line 21 of file instrument.c.