2
0

instrument.h 4.9 KB

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