PostgreSQL Source Code  git master
instrument.h File Reference
Include dependency graph for instrument.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BufferUsage
 
struct  WalUsage
 
struct  Instrumentation
 
struct  WorkerInstrumentation
 

Typedefs

typedef struct BufferUsage BufferUsage
 
typedef struct WalUsage WalUsage
 
typedef enum InstrumentOption InstrumentOption
 
typedef struct Instrumentation Instrumentation
 
typedef struct WorkerInstrumentation WorkerInstrumentation
 

Enumerations

enum  InstrumentOption {
  INSTRUMENT_TIMER = 1 << 0, INSTRUMENT_BUFFERS = 1 << 1, INSTRUMENT_ROWS = 1 << 2, INSTRUMENT_WAL = 1 << 3,
  INSTRUMENT_ALL = PG_INT32_MAX
}
 

Functions

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

PGDLLIMPORT BufferUsage pgBufferUsage
 
PGDLLIMPORT WalUsage pgWalUsage
 

Typedef Documentation

◆ BufferUsage

typedef struct BufferUsage BufferUsage

◆ Instrumentation

◆ InstrumentOption

◆ WalUsage

typedef struct WalUsage WalUsage

◆ WorkerInstrumentation

Enumeration Type Documentation

◆ InstrumentOption

Enumerator
INSTRUMENT_TIMER 
INSTRUMENT_BUFFERS 
INSTRUMENT_ROWS 
INSTRUMENT_WAL 
INSTRUMENT_ALL 

Definition at line 55 of file instrument.h.

56 {
57  INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */
58  INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */
59  INSTRUMENT_ROWS = 1 << 2, /* needs row count */
60  INSTRUMENT_WAL = 1 << 3, /* needs WAL usage */
InstrumentOption
Definition: instrument.h:55
#define PG_INT32_MAX
Definition: c.h:524

Function Documentation

◆ BufferUsageAccumDiff()

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

Definition at line 244 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 ExplainExecuteQuery(), ExplainOneQuery(), InstrEndParallelQuery(), InstrStopNode(), pgss_planner(), and pgss_ProcessUtility().

247 {
248  dst->shared_blks_hit += add->shared_blks_hit - sub->shared_blks_hit;
252  dst->local_blks_hit += add->local_blks_hit - sub->local_blks_hit;
253  dst->local_blks_read += add->local_blks_read - sub->local_blks_read;
256  dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
259  add->blk_read_time, sub->blk_read_time);
261  add->blk_write_time, sub->blk_write_time);
262 }
instr_time blk_read_time
Definition: instrument.h:36
int64 shared_blks_read
Definition: instrument.h:27
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition: instr_time.h:182
int64 temp_blks_written
Definition: instrument.h:35
int64 shared_blks_dirtied
Definition: instrument.h:28
int64 local_blks_read
Definition: instrument.h:31
int64 local_blks_hit
Definition: instrument.h:30
int64 local_blks_dirtied
Definition: instrument.h:32
instr_time blk_write_time
Definition: instrument.h:37
int64 local_blks_written
Definition: instrument.h:33
int64 shared_blks_hit
Definition: instrument.h:26
int64 temp_blks_read
Definition: instrument.h:34
int64 shared_blks_written
Definition: instrument.h:29

◆ InstrAccumParallelQuery()

void InstrAccumParallelQuery ( BufferUsage bufusage,
WalUsage walusage 
)

Definition at line 218 of file instrument.c.

References BufferUsageAdd(), and WalUsageAdd().

Referenced by _bt_end_parallel(), do_parallel_vacuum_or_cleanup(), and ExecParallelFinish().

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

◆ InstrAggNode()

void InstrAggNode ( Instrumentation dst,
Instrumentation add 
)

Definition at line 169 of file instrument.c.

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

Referenced by ExecParallelReportInstrumentation(), and ExecParallelRetrieveInstrumentation().

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 && dst->firsttuple > add->firsttuple)
177  dst->firsttuple = add->firsttuple;
178 
179  INSTR_TIME_ADD(dst->counter, add->counter);
180 
181  dst->tuplecount += add->tuplecount;
182  dst->startup += add->startup;
183  dst->total += add->total;
184  dst->ntuples += add->ntuples;
185  dst->ntuples2 += add->ntuples2;
186  dst->nloops += add->nloops;
187  dst->nfiltered1 += add->nfiltered1;
188  dst->nfiltered2 += add->nfiltered2;
189 
190  /* Add delta of buffer usage since entry to node's totals */
191  if (dst->need_bufusage)
192  BufferUsageAdd(&dst->bufusage, &add->bufusage);
193 
194  if (dst->need_walusage)
195  WalUsageAdd(&dst->walusage, &add->walusage);
196 }
double nfiltered1
Definition: instrument.h:85
bool need_bufusage
Definition: instrument.h:68
WalUsage walusage
Definition: instrument.h:88
double startup
Definition: instrument.h:80
static void WalUsageAdd(WalUsage *dst, WalUsage *add)
Definition: instrument.c:266
double firsttuple
Definition: instrument.h:75
double nfiltered2
Definition: instrument.h:86
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:158
double ntuples
Definition: instrument.h:82
double tuplecount
Definition: instrument.h:76
BufferUsage bufusage
Definition: instrument.h:87
instr_time counter
Definition: instrument.h:74
double ntuples2
Definition: instrument.h:83
bool need_walusage
Definition: instrument.h:69
static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
Definition: instrument.c:226

◆ InstrAlloc()

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

Definition at line 31 of file instrument.c.

References Instrumentation::async_mode, 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().

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  {
46  instr[i].need_bufusage = need_buffers;
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 }
bool need_bufusage
Definition: instrument.h:68
void * palloc0(Size size)
Definition: mcxt.c:1093
int i
bool need_walusage
Definition: instrument.h:69

◆ InstrEndLoop()

void InstrEndLoop ( Instrumentation instr)

Definition at line 140 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().

141 {
142  double totaltime;
143 
144  /* Skip if nothing has happened, or already shut down */
145  if (!instr->running)
146  return;
147 
148  if (!INSTR_TIME_IS_ZERO(instr->starttime))
149  elog(ERROR, "InstrEndLoop called on running node");
150 
151  /* Accumulate per-cycle statistics into totals */
152  totaltime = INSTR_TIME_GET_DOUBLE(instr->counter);
153 
154  instr->startup += instr->firsttuple;
155  instr->total += totaltime;
156  instr->ntuples += instr->tuplecount;
157  instr->nloops += 1;
158 
159  /* Reset for next cycle (if any) */
160  instr->running = false;
163  instr->firsttuple = 0;
164  instr->tuplecount = 0;
165 }
double startup
Definition: instrument.h:80
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:154
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:199
#define INSTR_TIME_IS_ZERO(t)
Definition: instr_time.h:152
#define ERROR
Definition: elog.h:46
double firsttuple
Definition: instrument.h:75
instr_time starttime
Definition: instrument.h:73
double ntuples
Definition: instrument.h:82
double tuplecount
Definition: instrument.h:76
instr_time counter
Definition: instrument.h:74
#define elog(elevel,...)
Definition: elog.h:232

◆ InstrEndParallelQuery()

void InstrEndParallelQuery ( BufferUsage bufusage,
WalUsage walusage 
)

Definition at line 208 of file instrument.c.

References BufferUsageAccumDiff(), and WalUsageAccumDiff().

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

209 {
210  memset(bufusage, 0, sizeof(BufferUsage));
212  memset(walusage, 0, sizeof(WalUsage));
214 }
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:274
static BufferUsage save_pgBufferUsage
Definition: instrument.c:21
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:244
BufferUsage pgBufferUsage
Definition: instrument.c:20
static WalUsage save_pgWalUsage
Definition: instrument.c:23

◆ InstrInit()

void InstrInit ( Instrumentation instr,
int  instrument_options 
)

Definition at line 58 of file instrument.c.

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

Referenced by ExecInitParallelPlan().

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 }
bool need_bufusage
Definition: instrument.h:68
bool need_walusage
Definition: instrument.h:69

◆ InstrStartNode()

void InstrStartNode ( Instrumentation instr)

Definition at line 68 of file instrument.c.

References Instrumentation::bufusage_start, elog, ERROR, INSTR_TIME_SET_CURRENT_LAZY, Instrumentation::need_bufusage, Instrumentation::need_timer, Instrumentation::need_walusage, pgBufferUsage, pgWalUsage, Instrumentation::starttime, and Instrumentation::walusage_start.

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

69 {
70  if (instr->need_timer &&
72  elog(ERROR, "InstrStartNode called twice in a row");
73 
74  /* save buffer usage totals at node entry, if needed */
75  if (instr->need_bufusage)
77 
78  if (instr->need_walusage)
79  instr->walusage_start = pgWalUsage;
80 }
bool need_bufusage
Definition: instrument.h:68
WalUsage pgWalUsage
Definition: instrument.c:22
BufferUsage bufusage_start
Definition: instrument.h:77
#define ERROR
Definition: elog.h:46
instr_time starttime
Definition: instrument.h:73
WalUsage walusage_start
Definition: instrument.h:78
#define INSTR_TIME_SET_CURRENT_LAZY(t)
Definition: instr_time.h:253
#define elog(elevel,...)
Definition: elog.h:232
bool need_walusage
Definition: instrument.h:69
BufferUsage pgBufferUsage
Definition: instrument.c:20

◆ InstrStartParallelQuery()

void InstrStartParallelQuery ( void  )

Definition at line 200 of file instrument.c.

References pgBufferUsage, and pgWalUsage.

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

201 {
204 }
WalUsage pgWalUsage
Definition: instrument.c:22
static BufferUsage save_pgBufferUsage
Definition: instrument.c:21
BufferUsage pgBufferUsage
Definition: instrument.c:20
static WalUsage save_pgWalUsage
Definition: instrument.c:23

◆ InstrStopNode()

void InstrStopNode ( Instrumentation instr,
double  nTuples 
)

Definition at line 84 of file instrument.c.

References Instrumentation::async_mode, 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::need_walusage, Instrumentation::running, Instrumentation::starttime, Instrumentation::tuplecount, Instrumentation::walusage, Instrumentation::walusage_start, and WalUsageAccumDiff().

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

85 {
86  double save_tuplecount = instr->tuplecount;
87  instr_time endtime;
88 
89  /* count the returned tuples */
90  instr->tuplecount += nTuples;
91 
92  /* let's update the time only if the timer was requested */
93  if (instr->need_timer)
94  {
95  if (INSTR_TIME_IS_ZERO(instr->starttime))
96  elog(ERROR, "InstrStopNode called without start");
97 
98  INSTR_TIME_SET_CURRENT(endtime);
99  INSTR_TIME_ACCUM_DIFF(instr->counter, endtime, instr->starttime);
100 
102  }
103 
104  /* Add delta of buffer usage since entry to node's totals */
105  if (instr->need_bufusage)
107  &pgBufferUsage, &instr->bufusage_start);
108 
109  if (instr->need_walusage)
110  WalUsageAccumDiff(&instr->walusage,
111  &pgWalUsage, &instr->walusage_start);
112 
113  /* Is this the first tuple of this cycle? */
114  if (!instr->running)
115  {
116  instr->running = true;
117  instr->firsttuple = INSTR_TIME_GET_DOUBLE(instr->counter);
118  }
119  else
120  {
121  /*
122  * In async mode, if the plan node hadn't emitted any tuples before,
123  * this might be the first tuple
124  */
125  if (instr->async_mode && save_tuplecount < 1.0)
126  instr->firsttuple = INSTR_TIME_GET_DOUBLE(instr->counter);
127  }
128 }
bool need_bufusage
Definition: instrument.h:68
WalUsage walusage
Definition: instrument.h:88
WalUsage pgWalUsage
Definition: instrument.c:22
struct timeval instr_time
Definition: instr_time.h:150
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:274
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition: instr_time.h:182
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:154
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:199
#define INSTR_TIME_IS_ZERO(t)
Definition: instr_time.h:152
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:244
BufferUsage bufusage_start
Definition: instrument.h:77
#define ERROR
Definition: elog.h:46
double firsttuple
Definition: instrument.h:75
instr_time starttime
Definition: instrument.h:73
WalUsage walusage_start
Definition: instrument.h:78
double tuplecount
Definition: instrument.h:76
BufferUsage bufusage
Definition: instrument.h:87
instr_time counter
Definition: instrument.h:74
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
#define elog(elevel,...)
Definition: elog.h:232
bool need_walusage
Definition: instrument.h:69
BufferUsage pgBufferUsage
Definition: instrument.c:20

◆ InstrUpdateTupleCount()

void InstrUpdateTupleCount ( Instrumentation instr,
double  nTuples 
)

Definition at line 132 of file instrument.c.

References Instrumentation::tuplecount.

Referenced by complete_pending_request().

133 {
134  /* count the returned tuples */
135  instr->tuplecount += nTuples;
136 }
double tuplecount
Definition: instrument.h:76

◆ WalUsageAccumDiff()

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

Definition at line 274 of file instrument.c.

References WalUsage::wal_bytes, WalUsage::wal_fpi, and WalUsage::wal_records.

Referenced by heap_vacuum_rel(), InstrEndParallelQuery(), InstrStopNode(), pgss_planner(), pgss_ProcessUtility(), and pgstat_send_wal().

275 {
276  dst->wal_bytes += add->wal_bytes - sub->wal_bytes;
277  dst->wal_records += add->wal_records - sub->wal_records;
278  dst->wal_fpi += add->wal_fpi - sub->wal_fpi;
279 }
int64 wal_fpi
Definition: instrument.h:50
int64 wal_records
Definition: instrument.h:49
uint64 wal_bytes
Definition: instrument.h:51

Variable Documentation

◆ pgBufferUsage

◆ pgWalUsage