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)
 
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 *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 43 of file instrument.h.

44 {
45  INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */
46  INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */
47  INSTRUMENT_ROWS = 1 << 2, /* needs row count */
48  INSTRUMENT_WAL = 1 << 3, /* needs WAL usage */
InstrumentOption
Definition: instrument.h:43
#define PG_INT32_MAX
Definition: c.h:457

Function Documentation

◆ BufferUsageAccumDiff()

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

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

228 {
229  dst->shared_blks_hit += add->shared_blks_hit - sub->shared_blks_hit;
233  dst->local_blks_hit += add->local_blks_hit - sub->local_blks_hit;
234  dst->local_blks_read += add->local_blks_read - sub->local_blks_read;
237  dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
240  add->blk_read_time, sub->blk_read_time);
242  add->blk_write_time, sub->blk_write_time);
243 }
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:182
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

◆ InstrAccumParallelQuery()

void InstrAccumParallelQuery ( BufferUsage bufusage,
WalUsage walusage 
)

Definition at line 199 of file instrument.c.

References BufferUsageAdd(), and WalUsageAdd().

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

200 {
201  BufferUsageAdd(&pgBufferUsage, bufusage);
202  WalUsageAdd(&pgWalUsage, walusage);
203 }
WalUsage pgWalUsage
Definition: instrument.c:22
static void WalUsageAdd(WalUsage *dst, WalUsage *add)
Definition: instrument.c:247
static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
Definition: instrument.c:207
BufferUsage pgBufferUsage
Definition: instrument.c:20

◆ InstrAggNode()

void InstrAggNode ( Instrumentation dst,
Instrumentation add 
)

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

151 {
152  if (!dst->running && add->running)
153  {
154  dst->running = true;
155  dst->firsttuple = add->firsttuple;
156  }
157  else if (dst->running && add->running && dst->firsttuple > add->firsttuple)
158  dst->firsttuple = add->firsttuple;
159 
160  INSTR_TIME_ADD(dst->counter, add->counter);
161 
162  dst->tuplecount += add->tuplecount;
163  dst->startup += add->startup;
164  dst->total += add->total;
165  dst->ntuples += add->ntuples;
166  dst->ntuples2 += add->ntuples2;
167  dst->nloops += add->nloops;
168  dst->nfiltered1 += add->nfiltered1;
169  dst->nfiltered2 += add->nfiltered2;
170 
171  /* Add delta of buffer usage since entry to node's totals */
172  if (dst->need_bufusage)
173  BufferUsageAdd(&dst->bufusage, &add->bufusage);
174 
175  if (dst->need_walusage)
176  WalUsageAdd(&dst->walusage, &add->walusage);
177 }
double nfiltered1
Definition: instrument.h:72
bool need_bufusage
Definition: instrument.h:56
WalUsage walusage
Definition: instrument.h:75
double startup
Definition: instrument.h:67
static void WalUsageAdd(WalUsage *dst, WalUsage *add)
Definition: instrument.c:247
double firsttuple
Definition: instrument.h:62
double nfiltered2
Definition: instrument.h:73
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:158
double ntuples
Definition: instrument.h:69
double tuplecount
Definition: instrument.h:63
BufferUsage bufusage
Definition: instrument.h:74
instr_time counter
Definition: instrument.h:61
double ntuples2
Definition: instrument.h:70
bool need_walusage
Definition: instrument.h:57
static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
Definition: instrument.c:207

◆ InstrAlloc()

Instrumentation* InstrAlloc ( int  n,
int  instrument_options 
)

Definition at line 31 of file instrument.c.

References 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  }
50  }
51 
52  return instr;
53 }
bool need_bufusage
Definition: instrument.h:56
void * palloc0(Size size)
Definition: mcxt.c:981
int i
bool need_walusage
Definition: instrument.h:57

◆ InstrEndLoop()

void InstrEndLoop ( Instrumentation instr)

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

122 {
123  double totaltime;
124 
125  /* Skip if nothing has happened, or already shut down */
126  if (!instr->running)
127  return;
128 
129  if (!INSTR_TIME_IS_ZERO(instr->starttime))
130  elog(ERROR, "InstrEndLoop called on running node");
131 
132  /* Accumulate per-cycle statistics into totals */
133  totaltime = INSTR_TIME_GET_DOUBLE(instr->counter);
134 
135  instr->startup += instr->firsttuple;
136  instr->total += totaltime;
137  instr->ntuples += instr->tuplecount;
138  instr->nloops += 1;
139 
140  /* Reset for next cycle (if any) */
141  instr->running = false;
144  instr->firsttuple = 0;
145  instr->tuplecount = 0;
146 }
double startup
Definition: instrument.h:67
#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:43
double firsttuple
Definition: instrument.h:62
instr_time starttime
Definition: instrument.h:60
double ntuples
Definition: instrument.h:69
double tuplecount
Definition: instrument.h:63
instr_time counter
Definition: instrument.h:61
#define elog(elevel,...)
Definition: elog.h:214

◆ InstrEndParallelQuery()

void InstrEndParallelQuery ( BufferUsage bufusage,
WalUsage walusage 
)

Definition at line 189 of file instrument.c.

References BufferUsageAccumDiff(), and WalUsageAccumDiff().

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

190 {
191  memset(bufusage, 0, sizeof(BufferUsage));
193  memset(walusage, 0, sizeof(WalUsage));
195 }
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:255
static BufferUsage save_pgBufferUsage
Definition: instrument.c:21
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:225
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 57 of file instrument.c.

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

Referenced by ExecInitParallelPlan().

58 {
59  memset(instr, 0, sizeof(Instrumentation));
60  instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0;
61  instr->need_walusage = (instrument_options & INSTRUMENT_WAL) != 0;
62  instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0;
63 }
bool need_bufusage
Definition: instrument.h:56
bool need_walusage
Definition: instrument.h:57

◆ InstrStartNode()

void InstrStartNode ( Instrumentation instr)

Definition at line 67 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(), ExecCallTriggerFunc(), ExecProcNodeInstr(), ExecShutdownNode(), MultiExecBitmapAnd(), MultiExecBitmapIndexScan(), MultiExecBitmapOr(), MultiExecHash(), standard_ExecutorFinish(), and standard_ExecutorRun().

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

◆ InstrStartParallelQuery()

void InstrStartParallelQuery ( void  )

Definition at line 181 of file instrument.c.

References pgBufferUsage, and pgWalUsage.

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

182 {
185 }
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 83 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::need_walusage, Instrumentation::running, Instrumentation::starttime, Instrumentation::tuplecount, Instrumentation::walusage, Instrumentation::walusage_start, and WalUsageAccumDiff().

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

84 {
85  instr_time endtime;
86 
87  /* count the returned tuples */
88  instr->tuplecount += nTuples;
89 
90  /* let's update the time only if the timer was requested */
91  if (instr->need_timer)
92  {
93  if (INSTR_TIME_IS_ZERO(instr->starttime))
94  elog(ERROR, "InstrStopNode called without start");
95 
96  INSTR_TIME_SET_CURRENT(endtime);
97  INSTR_TIME_ACCUM_DIFF(instr->counter, endtime, instr->starttime);
98 
100  }
101 
102  /* Add delta of buffer usage since entry to node's totals */
103  if (instr->need_bufusage)
105  &pgBufferUsage, &instr->bufusage_start);
106 
107  if (instr->need_walusage)
108  WalUsageAccumDiff(&instr->walusage,
109  &pgWalUsage, &instr->walusage_start);
110 
111  /* Is this the first tuple of this cycle? */
112  if (!instr->running)
113  {
114  instr->running = true;
115  instr->firsttuple = INSTR_TIME_GET_DOUBLE(instr->counter);
116  }
117 }
bool need_bufusage
Definition: instrument.h:56
WalUsage walusage
Definition: instrument.h:75
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:255
#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:225
BufferUsage bufusage_start
Definition: instrument.h:64
#define ERROR
Definition: elog.h:43
double firsttuple
Definition: instrument.h:62
instr_time starttime
Definition: instrument.h:60
WalUsage walusage_start
Definition: instrument.h:65
double tuplecount
Definition: instrument.h:63
BufferUsage bufusage
Definition: instrument.h:74
instr_time counter
Definition: instrument.h:61
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
#define elog(elevel,...)
Definition: elog.h:214
bool need_walusage
Definition: instrument.h:57
BufferUsage pgBufferUsage
Definition: instrument.c:20

◆ WalUsageAccumDiff()

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

Definition at line 255 of file instrument.c.

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

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

256 {
257  dst->wal_bytes += add->wal_bytes - sub->wal_bytes;
258  dst->wal_records += add->wal_records - sub->wal_records;
259  dst->wal_fpi += add->wal_fpi - sub->wal_fpi;
260 }
long wal_records
Definition: instrument.h:37
long wal_fpi
Definition: instrument.h:38
uint64 wal_bytes
Definition: instrument.h:39

Variable Documentation

◆ pgBufferUsage

◆ pgWalUsage