codekingpro/portable-devtools
115k
1/*-------------------------------------------------------------------------2 *3 * instrument.h4 * definitions for run-time statistics collection5 *6 *7 * Copyright (c) 2001-2023, PostgreSQL Global Development Group8 *9 * src/include/executor/instrument.h10 *11 *-------------------------------------------------------------------------12 */13#ifndef INSTRUMENT_H14#define INSTRUMENT_H15 16#include "portability/instr_time.h"17 18 19/*20 * BufferUsage and WalUsage counters keep being incremented infinitely,21 * i.e., must never be reset to zero, so that we can calculate how much22 * the counters are incremented in an arbitrary period.23 */24typedef struct BufferUsage25{26 int64 shared_blks_hit; /* # of shared buffer hits */27 int64 shared_blks_read; /* # of shared disk blocks read */28 int64 shared_blks_dirtied; /* # of shared blocks dirtied */29 int64 shared_blks_written; /* # of shared disk blocks written */30 int64 local_blks_hit; /* # of local buffer hits */31 int64 local_blks_read; /* # of local disk blocks read */32 int64 local_blks_dirtied; /* # of local blocks dirtied */33 int64 local_blks_written; /* # of local disk blocks written */34 int64 temp_blks_read; /* # of temp blocks read */35 int64 temp_blks_written; /* # of temp blocks written */36 instr_time blk_read_time; /* time spent reading blocks */37 instr_time blk_write_time; /* time spent writing blocks */38 instr_time temp_blk_read_time; /* time spent reading temp blocks */39 instr_time temp_blk_write_time; /* time spent writing temp blocks */40} BufferUsage;41 42/*43 * WalUsage tracks only WAL activity like WAL records generation that44 * can be measured per query and is displayed by EXPLAIN command,45 * pg_stat_statements extension, etc. It does not track other WAL activity46 * like WAL writes that it's not worth measuring per query. That's tracked47 * by WAL global statistics counters in WalStats, instead.48 */49typedef struct WalUsage50{51 int64 wal_records; /* # of WAL records produced */52 int64 wal_fpi; /* # of WAL full page images produced */53 uint64 wal_bytes; /* size of WAL records produced */54} WalUsage;55 56/* Flag bits included in InstrAlloc's instrument_options bitmask */57typedef enum InstrumentOption58{59 INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */60 INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */61 INSTRUMENT_ROWS = 1 << 2, /* needs row count */62 INSTRUMENT_WAL = 1 << 3, /* needs WAL usage */63 INSTRUMENT_ALL = PG_INT32_MAX64} InstrumentOption;65 66typedef struct Instrumentation67{68 /* Parameters set at node creation: */69 bool need_timer; /* true if we need timer data */70 bool need_bufusage; /* true if we need buffer usage data */71 bool need_walusage; /* true if we need WAL usage data */72 bool async_mode; /* true if node is in async mode */73 /* Info about current plan cycle: */74 bool running; /* true if we've completed first tuple */75 instr_time starttime; /* start time of current iteration of node */76 instr_time counter; /* accumulated runtime for this node */77 double firsttuple; /* time for first tuple of this cycle */78 double tuplecount; /* # of tuples emitted so far this cycle */79 BufferUsage bufusage_start; /* buffer usage at start */80 WalUsage walusage_start; /* WAL usage at start */81 /* Accumulated statistics across all completed cycles: */82 double startup; /* total startup time (in seconds) */83 double total; /* total time (in seconds) */84 double ntuples; /* total tuples produced */85 double ntuples2; /* secondary node-specific tuple counter */86 double nloops; /* # of run cycles for this node */87 double nfiltered1; /* # of tuples removed by scanqual or joinqual */88 double nfiltered2; /* # of tuples removed by "other" quals */89 BufferUsage bufusage; /* total buffer usage */90 WalUsage walusage; /* total WAL usage */91} Instrumentation;92 93typedef struct WorkerInstrumentation94{95 int num_workers; /* # of structures that follow */96 Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER];97} WorkerInstrumentation;98 99extern PGDLLIMPORT BufferUsage pgBufferUsage;100extern PGDLLIMPORT WalUsage pgWalUsage;101 102extern Instrumentation *InstrAlloc(int n, int instrument_options,103 bool async_mode);104extern void InstrInit(Instrumentation *instr, int instrument_options);105extern void InstrStartNode(Instrumentation *instr);106extern void InstrStopNode(Instrumentation *instr, double nTuples);107extern void InstrUpdateTupleCount(Instrumentation *instr, double nTuples);108extern void InstrEndLoop(Instrumentation *instr);109extern void InstrAggNode(Instrumentation *dst, Instrumentation *add);110extern void InstrStartParallelQuery(void);111extern void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage);112extern void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage);113extern void BufferUsageAccumDiff(BufferUsage *dst,114 const BufferUsage *add, const BufferUsage *sub);115extern void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add,116 const WalUsage *sub);117 118#endif /* INSTRUMENT_H */119 